宇筱博客

  • 解决办法
  • 学无止境
  • 记录时光
  • 百宝箱
宇筱博客
点滴记忆,汇聚成海。
  1. 首页
  2. 学无止境
  3. 正文

采集怀俄明大学探空数据-Java代码

2019年1月6日 506点热度 0人点赞 0条评论

代码:

Calendar c = Calendar.getInstance();//可以对每个时间域单独修改

Integer year = c.get(Calendar.YEAR);
Integer month = c.get(Calendar.MONTH)+1;
Integer date = c.get(Calendar.DATE);
Integer hour = c.get(Calendar.HOUR_OF_DAY);
String dateCode = String.format("%02d", date);
if(hour <= 12){
	dateCode += "00";
}else {
	dateCode += "12";
}
//区县地图编号
String stnm = "51644";
//数据日期拼接
StringBuffer sb = new StringBuffer();
sb.append(year);
sb.append(month);
sb.append(dateCode);

//查询当前数据时间段类,是否已经有数据,如果有数据直接返回
List<DynaBean> dataList = this.queryUrlData(sb.toString(), stnm);
if(dataList != null && dataList.size() > 0) {
	return;
}
String urlStr = "http://weather.uwyo.edu/cgi-bin/sounding?region=seasia&TYPE=TEXT%3ALIST&YEAR="+year+"&MONTH="+String.format("%02d", month)+"&FROM="+dateCode+"&TO="+dateCode+"&STNM="+stnm; // 网址

//创建一个url对象来指向要采集信息的网址
URL url = new URL(urlStr);
//将读取到的字节转化为字符
InputStreamReader inStrRead = new InputStreamReader(url.openStream(),"utf-8");
//读取InputStreamReader转化成的字符
BufferedReader bufRead = new BufferedReader(inStrRead); 
//读到的内容不为空
String line = null;
Integer iLine = 1;
//转换数据
List<List<String>> dataAllList = new ArrayList<List<String>>();
//数据最大间隔数
List<String> dataBreakList = Arrays.asList("1,1,1,2,3,1,3,4,0,0,0".split(","));

try {
	while (((line = bufRead.readLine())!= null)) {
		//System.out.println(line);
		//如果是最后一行就结束
		if("</PRE><H3>Station information and sounding indices</H3><PRE>".equals(line) || "<HR SIZE=\"1\">".equals(line)) {
			break;
		}
		//从数据第11行数据起始行开始
	    if (iLine >= 11){
	    	List<String> iLineList = new ArrayList<String>();
	    	
	    	String[] split = line.split(" ");
	    	List<String> dataList = Arrays.asList(split);
	    	//起始行数
	    	Integer startIndex = 0;
	    	//结束行数
	    	Integer endIndex = 0;
	    	//当前行数
	    	Integer nowIndex = 0;
	    	//循环数据最大间隔
	    	for (Integer i = 0;i < dataBreakList.size(); i++) {
	    		//默认数据位数
	    		endIndex = startIndex + 3;
	    		String dataNum = dataBreakList.get(i);
	    		//是否拿到数据
	    		Boolean dataD = false;
	    		//是否达到最大值
	    		Boolean dataNumD = false;
	    		//是否补充空白
	    		Boolean dataNullD = false;
	    		//循环数据
	    		for(Integer j = startIndex;j < endIndex; j++) {
	    			//获取当前预留行数,为数据为空时做准备
	    			nowIndex = j+1;
	    			
	        		String dataStr = dataList.get(j);
					if(StringUtils.isNotEmpty(dataStr)){
						dataD = true;
						iLineList.add(dataStr);
						//System.out.print(dataStr+"_");
						break;
					}
					if(dataD == false && !dataNumD && j == endIndex-1){
						dataNumD = true;
						endIndex = endIndex+Integer.valueOf(dataNum);
					}
					if(j == endIndex-1 && dataNumD && !dataNullD) {
						dataNullD = true;
						switch (i) {
						case 3:
							endIndex = endIndex+3;
							break;
						case 4:
							endIndex = endIndex+2;
							break;
						case 5:
							endIndex = endIndex+4;
							break;
						case 9:
							endIndex = endIndex+5;
							break;
						default:
							break;
						}
						
					}
					
				}
	    		if(dataD == false){
	    			iLineList.add("0");
	    			//System.out.print("!_");
	    		}
	    		
	    		startIndex = nowIndex;
			}
	    	dataAllList.add(iLineList);
	    	//System.out.println();
	    }
	    iLine++;
	}
} catch (NumberFormatException e) {
	new RuntimeException(e.getMessage());
} catch (IOException e) {
	new RuntimeException(e.getMessage());
}finally {
	bufRead.close();
}
//返回数据
return dataAllList;

原文链接:url = this.url = (还没发布没有链接)

标签: 暂无
最后更新:2019年1月6日

小渔民

这个人很懒,什么都没留下

点赞
< 上一篇
下一篇 >

COPYRIGHT © 2025 宇筱博客. ALL RIGHTS RESERVED.

Theme Kratos Made By Seaton Jiang

豫ICP备15017825号-2