• Windows下Python读取GRIB数据


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

    一、在Windows下Python为什么无法读取GRIB

    大家在windows系统不能读取GRIB数据的主要原因是,GRIB_API在Windows下无法编译安装,从而导致pygrib安装失败。我曾经也为这个问题苦恼了很久,也到ECMWF论坛里找了很久,也给ECMWF发了邮件,回应我没有做Windows版本的打算,所以在Windows下直接用pygrib读取GRIB数据是基本不可能实现了。

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

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

    1.wgrib2

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

    • 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('.')+'wgribwgrib.exe '+gribfilename+' -d 1 -text -nh -o '+outfilename)
    

    这样就可以在指定目录找到输出的txt文件

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

    原数据是37*37的数组,而输出的数据是每个数据占一行,所以需要进行一下转换

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

    以上就是Windows下Python读取GRIB数据的完整方法,希望可以帮到需要的朋友

  • 相关阅读:
    HelpersRainCaptcha
    HelpersPHPMailer
    HelpersPassword
    HelpersPagination
    HelpersNumber
    HelpersHooks
    HelpersGeoCode
    HelpersFastCache
    HelpersDocument
    eclipse 设置jsp页面为HTML5
  • 原文地址:https://www.cnblogs.com/kallan/p/6346719.html
Copyright © 2020-2023  润新知