Windows下Python读取GRIB数据

以前写了一篇《基于Python的GRIB数据可视化》的文章,好多博友在评论里问我Windows系统下如何读取GRIB数据,在这里我作一下说明。html

1、在Windows下Python为何没法读取GRIB

你们在windows系统不能读取GRIB数据的主要缘由是,GRIB_API在Windows下没法编译安装,从而致使pygrib安装失败。我曾经也为这个问题苦恼了好久,也到ECMWF论坛里找了好久,也给ECMWF发了邮件,回应我没有作Windows版本的打算,因此在Windows下直接用pygrib读取GRIB数据是基本不可能实现了。python

2、Windows下间接读取GRIB数据方法

后来经过大量的百度,仍是找到了在Windows下读取GRIB数据的方法:一种是在Cygwin中安装pygrib,将pygrib的方法编译成读取GRIB的exe;另外一种是经过一个第三方的程序wgrib2,先用wgrib2把数据读存到txt,而后再用python读取txt文件。
首先尝试了Cygwin,pygrib成功编译出了exe,可是没法运行,感受Cygwin稍微复杂,因此我没有继续研究这一种方法,直接转向第二种方法。git

1.wgrib2

wgrib2是由NCEP开发的一个功能强大的命令行工具,用于读取、建立和修改GRIB2文件。它是原有支持GRIB1编码的wgrib程序的延续,能够完成GRIB2的编码、解码,插值、修改投影方式、修改经纬度范围和要素提取等功能。wgrib2做为GrADS软件包中的一个工具,用户能够经过安装GrADS得到该软件,也能够经过访问它的官网得到最新的源码 进行编译。因为新版的wgrib输出数据之间没有分隔符,因此我找了一个以前的版本,输出后每一个数据占一行。本文中使用的wgribwindows

  • wgrib2命令参数
    在cmd中直接运行wgrib.exe可获得如何使用的帮助信息。
Portable Grib decoder for NCEP/NCAR Reanalysis etc.
   it slices, dices    v1.7.3.1 (8-5-99) Wesley Ebisuzaki
   usage: /cygdrive/d/wgrib/wgrib [grib file] [options]

Inventory/diagnostic-output selections ;输出目录或诊断结果
   -s/-v                   short/verbose inventory ;简短/详细目录                                   
   -V                      diagnostic output (not inventory ;输入诊断           
   (none)                  regular inventory ;默认目录                   
 Options ;选项                                  
   -PDS/-PDS10             print PDS in hex/decimal ;输出16/10进制PDS                            
   -GDS/-GDS10             print GDS in hex/decimal ;输出16/10进制GDS                                    
   -verf                   print forecast verification time ;输出预测验证时间                            
   -ncep_opn/-ncep_rean    default T62 NCEP grib table ;默认为T62_NCEP GRIB数据表                     
   -4yr                    print year using 4 digits ;输出4位数字的年份                                      
Decoding GRIB selection ;GRIB解码选项                                  
   -d [record number|all]  decode record number  ;解码指定编号数据                                
   -p [byte position]      decode record at byte position ;解码所指定的二进制位置数据                       
   -i                      decode controlled by stdin (inventory list) ;按目录列表解码                      
   (none)                  no decoding ;不解码                      
 Options ;选项                                   
   -text/-ieee/-grib/-bin  convert to text/ieee/grib/bin (default) ;将解码数据转换成text/ieee/grib/bin格式的数据                  
   -nh/-h                  output will have no headers/headers (default) ;是否包含标题头  
   -H                      output will include PDS and GDS (-bin/-ieee only) ;输出是否包含PDS和GDS     
   -append                 append to output file ;在输出文件上添加而不是替换
   -o [file]               output file name, 'dump' is default ;输出文件名

2.使用os.system在Python中执行Windows命令行程序wgrib

import os
os.system(os.path.abspath('.')+'\wgrib\wgrib.exe '+gribfilename+' -d 1 -text -nh -o '+outfilename)

这样就能够在指定目录找到输出的txt文件数组

3.将txt文件中的数据转换成数组

原数据是37*37的数组,而输出的数据是每一个数据占一行,因此须要进行一下转换app

f=open(outfilename,'r')
grds=f.read().strip()
grds=grds.split('\n')
data= np.array(grds)
data.resize(37,37)
data= data.astype(float)

以上就是Windows下Python读取GRIB数据的完整方法,但愿能够帮到须要的朋友工具

相关文章
相关标签/搜索