GFS预报数据下载

#更新#2019年6月12日之后,gfs预报场存放的目录变了,需要修改。get_gfs.pl第51行改为

$URL='https://nomads.ncep.noaa.gov/pub/data/nccf/com/gfs/prod/gfs.$YYYY$MM$DD/$HH/gfs.t${HH}z.pgrb2.1p00.f${FHR3}';

 

 

 

快速下载GRIB文件,http传输部分 

译自https://www.cpc.ncep.noaa.gov/products/wesley/fast_downloading_grib.html

Introduction

新闻:1/2019 nomoads.ncep.noaa.gov正在将其网址从http:更改为https:。快速下载技术适用于http和https类型的URL。更改通常很简单,将脚本从http更改为https。只要将URL从http:修改为https:,使用grib_filter的脚本也可以正常工作。如果您使用的是旧版本,则可能需要新的cURL。顺便说一句,我已经及时修改了网页上的文档,但是变化非常微妙,我决定写下这个红色文本。

 

NOMADS, NOAA Operational Model Archive and Distribution System, NOAA业务模式储存和分发系统

 

 

如果你很幸运,那很简单 

一些数据集有可以通过写好的脚本下载。详见第2部分。

细节

http协议允许“随机访问”读取;然而,这意味着我们需要一个索引文件和一个http程序支持随机访问。对于索引文件,我们可以修改wgrib清单。对于随机访问的http程序,我们可以使用cURL。它们都是自由获取的,广泛使用的,在很多平台下面被使用,并且可以轻松地被脚本化/自动化/加入 到一个cronjob任务中。

快速下载的基本格式是,

get_inv.pl INV_URL | grep (options) FIELDS | get_grib.pl GRIB_URL OUTPUT

INV_URL是wgrib清单的URL。
   如 https://nomad3.ncep.noaa.gov/pub/gfs/rotating/gblav.t00z.pgrbf12.inv 

grep (options) FIELDS  选取要获取的场(与wgrib兼容)
   如 grep -F ":HGT:500 mb:" selects ":HGT:500 mb"
   如 grep -E ":(HGT|TMP):500 mb:" selects ":HGT:500 mb:" and ":TMP:500 mb:" 

GRIB_URL是grib file的URL
   如 https://nomad3.ncep.noaa.gov/pub/gfs/rotating/gblav.t00z.pgrbf12 

OUTPUT是为下载的grib file起的名字

“get_inv.pl INV_URL”从网上下载wgrib清单并添加一个范围变量场。

“grep FIELDS”使用grep命令从清单中选择所需的变量场。使用“grep FIELDS”类似于使用wgrib提取变量场的过程。

“get_grib.pl GRIB_URL OUTPUT”使用筛选的清单从GRIB_URL中选择要下载的变量场。选定的变量场保存在OUTPUT中。

 

例子

 

get_inv.pl https://nomad3.ncep.noaa.gov/pub/gfs/rotating/gblav.t00z.pgrbf12.inv | \ 
grep ":HGT:500 mb:" | \ 
get_grib.pl https://nomad3.ncep.noaa.gov/pub/gfs/rotating/gblav.t00z.pgrbf12 out.grb

 

上面的例子可以在没有反斜杠的情况下写在一行上。(反斜杠是unix约定,表示该行在下一行继续。)该示例从NCEP NOMAD2服务器的00Z(t00z)GFS fcst下载12小时(f12)的500 mb高度场。

 

 

get_inv.pl https://nomad2.ncep.noaa.gov/pub/gfs/rotating/gblav.t00z.pgrbf12.inv | \ 
egrep "(:HGT:500 mb:|:TMP:1000 mb:)" | \ 
get_grib.pl https://nomad2.ncep.noaa.gov/pub/gfs/rotating/gblav.t00z.pgrbf12 out.grb

 

上面的示例与前面的示例类似,只是它下载500 mb高度和1000 mb温度。

警告:元字符

在开始时,您可以使用类似字符串过滤库存

  egrep ":(UGRD|VGRD|TMP|HGT):(1000|500|200) mb:"
  egrep "(:UGRD:200 mb:|:TMP:2 m above ground:)"

 第一个egrep被弃用,并被“grep -E”取代。没什么大不了。然后有人决定将egrep通配符放入官方级别的信息中。想象一下尝试做

grep -E "(:UGRD:200 mb:|:HGT:PV=2e-06 (Km^2/kg/s) surface:)"

你看到了问题。HGT级别字段包含"(" 和 ")"。为了摆脱"(" 和 ")"的特殊含义,它们应该用 \( 和 \)引用。插入符号"^"也具有特殊含义,也应引用。修改后的命令行
  grep -E "(:UGRD:200 mb:|:HGT:PV=2e-06 \(Km\^2/kg/s\) surface:)"
您应该将所有正则表达式元字符用反斜杠引用,包括
\,^,$,.,|,?,*,+,(,),[,],{,}

示例脚本

以下是下载一年R2数据的示例。
#!/bin/sh
# simple script to download 4x daily V winds at 10mb
# from the R2 archive

set -x
date=197901
enddate=197912
while [ $date -le $enddate ]
do
     url="https://nomad3.ncep.noaa.gov/pub/reanalysis-2/6hr/pgb/pgb.$date"
     get_inv.pl "${url}.inv" | grep ":VGRD:" | grep ":10 mb" | \
     get_grib.pl "${url}" pgb.$date
     date=$(($date + 1))
     if [ $(($date % 100)) -eq 13 ] ; then
         date=$(($date - 12 + 100));
     fi
done

 

依赖

  1. perl
  2. grep
  3. cURL
  4. grib files and their wgrib inventory on an http server
  5. get_inv.pl
  6. get_grib.pl

配置(UNIX/LINUX)

需要修改get_inv.pl和get_grib.pl的前两行。第一行应该指向你的perl解释器。第二行需要指向curl的位置,如果它不在你的路径上。

 

HTTPS servers

要访问https服务器,您需要将get_inv.pl和get_grib.pl更新为当前版本(4/2017)。某些站点具有self-signed/invalid/dodgy 的证书,除非您进入不安全模式,否则curl将不会从这些站点下载。(肯定有一些政府政策问题,因为许多NOAA网站都有证书问题。)如果您愿意承担从这些网站下载的风险,您可以在不安全模式下运行curl。

 

在get_inv.pl
将这一行:   open (In, "$curl -f -s $file |");
    改为:   open (In, "$curl -k -f -s $file |");

在get_grib.pl
将这一行:   $err=system("$curl -f -v -s -r \"$range\" $url -o $file.tmp");
    改为:   $err=system("$curl -k -f -v -s -r \"$range\" $url -o $file.tmp");

Usage: Windows
有一些报告称perl脚本在Windows机器上不起作用。Alexander Ryan解决了这个问题。
Hi Wesley,

thought this might be of some use to your win32 users.

I had the following problem when running the get_grib.pl file as per your instructions.

run this
grep ":UGRD:" < my_inv | get_grib.pl $URL ugrd.grb
and I would get the error No download! No matching grib fields. on further 
investigation I found that it was just skipping the while STDIN part of the 
code. a few google searches later and I found that for some strange reason in 
the pipe I needed to specify the path or command for perl even though the file 
associations for .pl are set up. (don't fiqure)

this works for me

grep ":UGRD:" < my_inv | PERL get_grib.pl $URL ugrd.grb

Regards and thanks for the fine service
Alexander Ryan

Alexander的另一封邮件

Hi Wesley,
Further to my last email here are some details regarding the enviorment I run this all on for your referance. 

My computer is P4 1.7GHz with 1Gb Ram running Windows 2000 service pack 4
Perl version :V5.6.1 provided by https://www.activestate.com
cUrl Version: 7.15.4 from https://curl.haxx.se/
grep & egrep: win32 versions of grep and egrep, I found both athttps://unxutils.sourceforge.net who provide some useful ports of common GNU utilities to native Win32. (no cygwin required) 

so far this is working fine

Regards Alexander

 

显然,
    get_inv.pl INV_URL | grep FIELDS | perl get_grib.pl URL OUTPUT
 
应该管用。Linux用户可能会倾向于使用cygwin系统,因为它包括bash,X服务器,编译器和通常的unix工具。

Tips

如果要下载多个场,例如降水量和2米温度,可以输入,
URL="https://www.ftp.ncep.noaa.gov/data/nccf/com/gfs/prod/gfs.2006070312/gfs.t12z.pgrb2f00"
get_inv.pl $URL.idx | egrep ':(PRATE|TMP:2 m above gnd):' | get_grib.pl $URL out

 

 上面的代码将析出和2米的温度放在文件中。当然,egrep理解正则表达式,这是一个非常强大的功能。

如果从同一文件进行多次下载,则可以通过保留库存的本地副本来节省时间。例如,

URL="https://www.ftp.ncep.noaa.gov/data/nccf/com/gfs/prod/gfs.2006070312/gfs.t12z.pgrb2f00"
get_inv.pl $URL.idx > my_inv
grep ":UGRD:" < my_inv | get_grib.pl $URL ugrd.grb
grep ":VGRD:" < my_inv | get_grib.pl $URL vgrd.grb
grep ":TMP:" < my_inv | get_grib.pl $URL tmp.grb

上面的代码可以节省两次额外的库存下载。

 

数据提供者注意事项

grib数据需要在http服务器上访问。这通常是httpd配置中的一个小改动。

用户需要wgrib清单(grib-1)或wgrib2清单(grib-2)。如果清单与数据文件位于同一目录中并使用'.inv'后缀约定,则很方便。可以创建清单,通过,

GRIB-1:wgrib -s grib_file> grib_file.inv

GRIB-2:wgrib2 -s grib_file> grib_file.inv

 

GRIB-2

自2006年夏天以来,Grib-2一直得到支持。

 

注意

从理论上讲,curl允许随机访问FTP服务器,但实际上我们发现这很慢(每次随机访问都是自己的FTP会话)。由于我们希望数据提供者使用更快的http协议,因此支持FTP访问。

区域截取

随着网格变得越来越精细,对区域截取的需求也在增长。使用grib2,可以在客户端进行区域子集化,但如果可能的话,这将是一些棘手的编码。现在,我很高兴在nomads服务器上运行的g2subset软件。即使在jpeg2000解压缩的开销下,该服务器软件也比grib1软件(ftp2u / ftp4u)更快。 

 
创建时间:1/21/2005
最后修改日期:6/2017
评论:Wesley.Ebisuzaki@noaa.gov

 

快速下载Grib,第2部分

译自https://www.cpc.ncep.noaa.gov/products/wesley/get_gfs.html

新闻

2019年1月2日:nomads.ncep.noaa.gov正在将URL从http://更改为https://。2014年12月31日发布了带有新网址的get_gfs.pl版本。如果您遇到问题,可能需要获得更新版本的cURL。

Wrappers @ NCDC

虽然第1部分详述的程序是直截了当的,但它可能更容易。我不喜欢寻找和输入网址。写循环也需要时间。经验不足的人更喜欢它。Dan Swank为北美区域再分析(NARR)下载了一个很好的接口。他编写了get-httpsubset.pl,它工作得很好。2006年5月,95%的NCDC-NOMADS下载都是使用cURL完成的。

Wrappers @ NCEP (NOMADS): get_gfs.pl

在NCEP,我们希望人们(1)使用partial-http而不是ftp2u传输来获得预报场,以及(2)将nomads服务器移至更可靠的NCO服务器。所以get_gfs.pl诞生了。希望脚本易于使用,易于重新配置,更易于安装和在Windows下使用。

Requirements

  1. get_gfs.pl.

  2. perl

  3. cURL

配置

  1. 需要下载cURL可执行文件并将其放在$ PATH的目录中。
  2. get_gfs.pl的第一行应该指向本地perl解释器的位置。
  3. 非Windows用户可以在get_gfs.pl中将$windows flag设置为”thankfully no“,以提高效率。

简单用法:

get_gfs.pl data DATE HR0 HR1 DHR VARS LEVS DIRECTORY


注意:某些Windows设置需要输入:
      perl get_gfs.pl data DATE HR0 HR1 DHR DIRECTORY

DATE = 预报YYYYMMDDHH的开始时间。注意:HH应为00 06 12或18

HR0 = 想要的第一个预测的小时数

HR1 = 想要的最后预测的小时数

DHR = 预测小时增量(每3,6,12或24小时预测)

VARS = 变量列表或"all"
    例如 HGT:TMP:OZONE
    例如 all

LEVS = 层次列表,空格替换为下划线,或"all"
    例如 500_mb:200_mb:surface
    例如 all

DIRECTORY = 放置输出的目录

example: perl get_gfs.pl data 2006101800 0 12 6 UGRD:VGRD 200_mb .

example: perl get_gfs.pl data 2006101800 0 12 6 UGRD:VGRD 200_mb:500_mb:1000_mb .

example: perl get_gfs.pl data 2006101800 0 12 12 all surface .

正则元字符: ( ) . ^ * [ ] $ +

get_gfs.pl脚本使用perl正则表达式(regex)作为字符串匹配。因此,应该引用正则表达式元字符它们是搜索字符串的一部分。例如,试图找到以下层

       "entire atmosphere (considered as a single_layer)"

       "entire_atmosphere_(considered_as_a_single_layer)"

因为括号是元字符,所以不起作用。下列技术会起作用。

引用 "(”和 ")"字符

 get_gfs.pl data 2012053000 0 6 3 TCDC "entire atmosphere \(considered as a single layer\)" .
 get_gfs.pl data 2012053000 0 6 3 TCDC entire_atmosphere_\\\(considered_as_a_single_layer\\\) .

使用 句点(匹配所有字符) 来匹配 "(”和 ")"字符  get_gfs.pl data 2012053000 0 6 3 TCDC "entire atmosphere .considered as a single layer." . get_gfs.pl data 2012053000 0 6 3 TCDC entire_atmosphere_.considered_as_a_single_layer. .

How get_gfs.pl works 

get_gfs.pl基于get_inv.pl和get_grib.pl脚本。get_gfs.pl的优点是URL内置预报时间循环。 

元语言  get_gfs.pl data DATE HR0 HR1 DHR VARS LEVS DIRECTORY

 

# convert LEVS and VARS into REGEX
  if (VARS == "all") {
    VARS=".";
  }
  else {
    VARS = substitute(VARS,':','|')
    VARS = substitute(VARS,'_',' ')
    VARS = ":(VARS):";
  }

  if (LEVS == "all") {
    LEVS=".";
  }
    LEVS = substitute(LEVS,':','|')
    LEVS = substitute(LEVS,'_',' ')
    LEVS = ":(LEVS)";
  }

# loop over all forecaset hours

  for fhour = HR0, HR1, DHR
     URL= URL_name(DATE,fhour)
     URLinv= URL_name(DATE,fhour).idx

     inventory_array[] = get_inv(URLinv);
     for i = inventory..array[0] .. inventory_array[last]
        if (regex_match(LEVS,inventory_array[i]) and regex_match(VARS,inventory_array[i]) {
	   add_to_curl_fetch_request(invetory_array[i]);
        }
     }
     curl_request(URL,curl_fetch_request,DIRECTORY);
  endfor

Advanced Users

有一个用户询问是否可以混合变量和层次。例如,TMP @ 500 mb,HGT @(250和700 mb)。当然你可以运行两次get-gfs.pl但这样效率不高。

这是可能的,因为get-gfs.pl使用正则表达式,而正则表达式非常强大。您需要记住的是,get-gfs.pl分别将冒号和下划线转换为垂直条和空格,分别用于VAR / LEV参数。

Unix/Linux:

       get-gfs.pl data 2006111500 0 12 12 all 'TMP.500 mb|HGT.(200 mb|700 mb)'  data_dir

Windows:

       get-gfs.pl data 2006111500 0 12 12 all "TMP.500 mb|HGT.(200 mb|700 mb)"  C:\unix\

 Other GRIB Data sets

get_gfs.pl的一个目的是提供一个简单的脚本,用于使用部分httpd下载协议下载grib数据。编写代码使其易于适应其他grib + inv数据集。

Wrappers @ NCEP (NCO): get_data.sh

NCO(NCEP Centeral Operations)也有一个接口get_data.sh

 

创建时间:10/2006,

更新日期:2012年5月

评论:Wesley.Ebisuzaki@noaa.gov

posted @ 2019-06-04 14:44  chinagod  阅读(8220)  评论(0编辑  收藏  举报