您的位置:首页 > 其它

欧洲数值天气预报文件拆分方法

2011-08-30 12:25 405 查看
欧洲天气数值天气预报可以在 http://data-portal.ecmwf.int/上下载或购买。 欧洲数值天气预报文件也可用工具wgrib和wgrib2来拆分。

下面以欧洲天气预报(grib1格式)为例子:这里提取北京地区的网格N41E117N39E115.

0、首先在Linux机器上安装wgrib。红帽子可在http://download.opensuse.org/repositories/home:/gbvalor/RHEL_5/i386/ 下载。

1、先看grib1文件中包含的网格。

# wgrib /路径/文件名称 -V ;注意:这里是大写的V

rec 1:0:date 2011083000 V kpds5=132 kpds6=100 kpds7=950 levels=(3,182) grid=255 950 mb 3hr fcst:

V=V velocity [m s**-1]

timerange 0 P1 3 P2 0 TimeU 1 nx 17 ny 25 GDS grid 0 num_in_ave 0 missing 0

center 98 subcenter 0 process 140 Table 128 scan: WE:NS winds(N/S)

latlon: lat 41.000000 to 39.000000 by 0.125000 nxny 425

long 117.000000 to 115.000000 by 0.125000, (17 x 25) scan 0 mode 128 bdsgrid 1

min/max data 1.32359 9.41734 num bits 12 BDS_Ref 1.32359 DecScale 0 BinScale -8

rec 2:722:date 2011083000 U kpds5=131 kpds6=100 kpds7=950 levels=(3,182) grid=255 950 mb 3hr fcst:

U=U velocity [m s**-1]

timerange 0 P1 3 P2 0 TimeU 1 nx 17 ny 25 GDS grid 0 num_in_ave 0 missing 0

center 98 subcenter 0 process 140 Table 128 scan: WE:NS winds(N/S)

latlon: lat 41.000000 to 39.000000 by 0.125000 nxny 425

long 117.000000 to 115.000000 by 0.125000, (17 x 25) scan 0 mode 128 bdsgrid 1

min/max data -0.197081 5.21894 num bits 12 BDS_Ref -0.197081 DecScale 0 BinScale -9

rec 3:1444:date 2011083000 V kpds5=132 kpds6=100 kpds7=950 levels=(3,182) grid=255 950 mb 3hr fcst:

V=V velocity [m s**-1]

timerange 0 P1 3 P2 0 TimeU 1 nx 43 ny 21 GDS grid 0 num_in_ave 0 missing 0

center 98 subcenter 0 process 140 Table 128 scan: WE:NS winds(N/S)

latlon: lat 38.250000 to 35.750000 by 0.125000 nxny 903

long 117.750000 to 123.000000 by 0.125000, (43 x 21) scan 0 mode 128 bdsgrid 1

min/max data 0.940542 10.503 num bits 12 BDS_Ref 0.940542 DecScale 0 BinScale -8

............................................................................................................................

2、 提取grib消息 rec后面的行号。

如:获取 1和2。

3、执行提取命令, 生成提取文件。

# wgrib /路径/grib1源文件名称 -v | egrep "(^ 1:|^ 2:)" | wgrib -i /路径/grib1源文件名称 -grib -o /路径/grib1输出文件名称

以下编写一个小程序实现网格数据的提取,它包含两个JAVA文件:cut_grib.java 和RunCommand.java.

package tools;

import java.io.File;

import java.util.*;

public class cut_grib{

private String split_flag ="41.000000 to 39.000000";//过滤条件

String list = "";

/**

* 获得记录号

* @param file

* @return

*/

public boolean pro_(String input_file){

list = "";

Vector runlog= null;

Vector errlog=null;

if (runlog ==null){

runlog = new Vector();

}

if (errlog ==null){

errlog = new Vector();

}

RunCommand rc = new RunCommand();

rc.out_Command("wgrib "+ input_file + " -V ", runlog, errlog);

int len = runlog.size();

if (len>0){

System.out.println(runlog.toString());

}

len = errlog.size();

if (len>0){

System.out.println(errlog.toString());

return false;

}

int len_log =runlog.size();

Iterator it = runlog.iterator();

String int_no = "-1";

String tmp_no = "-1";

while (it.hasNext()){

String line = (String)it.next();

if (line.indexOf("rec")>=0){

int_no = "-1";//行号缓存清除

//行首

int beg_ind = 3;

int end_ind = line.indexOf(":");

String no_=line.substring(beg_ind, end_ind).trim();

System.out.println("获得行号:"+ no_);

int_no = no_;

}else{

if (line.indexOf(split_flag)>=0){

//处理上此的行

if (!int_no.equals("-1")){

if (list.equals("") ){

list = list + "^" + int_no + ":";

}else{

list = list + "|^" + int_no + ":";

}

}

}

}

}

return true;

}

public boolean out_put(String input_file,String output_file){

Vector runlog= null;

Vector errlog=null;

if (runlog ==null){

runlog = new Vector();

}

if (errlog ==null){

errlog = new Vector();

}

RunCommand rc = new RunCommand();

rc.out_Command("wgrib "+input_file+" -v | egrep \"(" + list + ")\" | wgrib -i "+input_file+" -grib -o "+ output_file, runlog, errlog);

int len = runlog.size();

if (len>0){

System.out.println(runlog.toString());

}

len = errlog.size();

if (len>0){

System.out.println(errlog.toString());

return false;

}

return true;

}

private Vector <String> getGribFile(String path){

File dir=new File(path);

File []files=dir.listFiles();

Vector file_list=new Vector();

for(int i=0;i<files.length;i++){

//添加列表

String tmp = files[i].getPath();

file_list.add(tmp);

}

return file_list;

}

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

String input_file = "";

String output_file ="";

Vector file_list = new Vector();

//执行命令

cut_grib cf = new cut_grib();

file_list = cf.getGribFile("/ECM_DATA");//grib1文件所在目录

Iterator it = file_list.iterator();

while (it.hasNext()){

input_file = (String)it.next();

output_file = input_file+ "N41E117N39E115.grib1";

cf.pro_(input_file);

cf.out_put(input_file, output_file);

}

}

}

package tools;

import java.io.*;

import java.util.*;

//执行Linux命令

public class RunCommand {

/**

* 命令执行

* @param command_params 命令和参数

* @param runlog 执行后的结果

* @param errlog 错误信息

* @return 0:成功;其他有错误

*/

public int out_Command(String command_params, Vector runlog, Vector errlog){

int rtn = 0;

int file_count = 0;

if (runlog ==null){

return -100;

}

if (errlog ==null){

return -100;

}

try

{

Runtime rt = Runtime.getRuntime();

Process proc;

// /bin/csh -f

// /bin/sh -c

String[] cmd = {

//"/bin/csh",

//"-f",

"/bin/sh",

"-c",

command_params

};

System.out.println("命令执行线程:" + command_params);

proc = rt.exec(cmd);

InputStream stderr = proc.getErrorStream();

InputStreamReader isr_1 = new InputStreamReader(stderr);

BufferedReader br_1 = new BufferedReader(isr_1);

InputStream is = proc.getInputStream();

InputStreamReader isr_2 = new InputStreamReader(is,"GBK");

BufferedReader br_2 = new BufferedReader(isr_2);

String line = null;

//文件落地前,删除一下旧的

while ( (line = br_2.readLine()) != null) {

System.out.println("命令执行线程:" + line);

runlog.add(line+ "\n");

}

while ( (line = br_1.readLine()) != null){

System.out.println("命令执行线程:" + line);

errlog.add(line+ "\n");

}

int exitVal = proc.waitFor();

if (exitVal==0){

rtn = 0;

}else

{

rtn =-1;

}

br_1.close();

isr_1.close();

stderr.close();

br_2.close();

isr_2.close();

is.close();

proc.destroy();

} catch (Exception t){

t.printStackTrace();

rtn =-1;

}

return rtn;

}

public int out_Command(String[] cmd, StringBuffer runlog, StringBuffer errlog){

int rtn = 0;

int file_count = 0;

if (runlog ==null){

return -100;

}

if (errlog ==null){

return -100;

}

try

{

Runtime rt = Runtime.getRuntime();

Process proc;

//System.out.println("命令执行线程:" + command_params);

proc = rt.exec(cmd);

InputStream stderr = proc.getErrorStream();

InputStreamReader isr_1 = new InputStreamReader(stderr);

BufferedReader br_1 = new BufferedReader(isr_1);

InputStream is = proc.getInputStream();

InputStreamReader isr_2 = new InputStreamReader(is,"GBK");

BufferedReader br_2 = new BufferedReader(isr_2);

String line = null;

//文件落地前,删除一下旧的

while ( (line = br_2.readLine()) != null) {

System.out.println("命令执行线程:" + line);

runlog.append(line+ "\n");

}

while ( (line = br_1.readLine()) != null){

System.out.println("命令执行线程:" + line);

errlog.append(line+ "\n");

}

int exitVal = proc.waitFor();

if (exitVal==0){

rtn = 0;

}else

{

rtn =-1;

}

br_1.close();

isr_1.close();

stderr.close();

br_2.close();

isr_2.close();

is.close();

proc.destroy();

} catch (Exception t){

t.printStackTrace();

rtn =-1;

}

return rtn;

}

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

Vector runlog= null;

Vector errlog=null;

if (runlog ==null){

runlog = new Vector();

}

if (errlog ==null){

errlog = new Vector();

}

RunCommand rc = new RunCommand();

rc.out_Command("ls / ", runlog, errlog);

int len = runlog.size();

if (len>0){

System.out.println(runlog.toString());

}

len = errlog.size();

if (len>0){

System.out.println(errlog.toString());

}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: