1、netcdf文件格式说明
netCDF数据格式在气象中有广泛的应用,这种格式有一定的复杂性。作为数据的使用者可以不用对数据格式了解得很详细,不过大致的了解还是有必要的。netCDF是自描述的二进制数据格式,也就是说数据本身包含了相关的数据信息。经典的netCDF格式是由维(dimensions)、全局属性(global attribute)和变量(variations)组成的,netCDF4.0以后开始向HDF格式靠拢。下面是一个简单的netCDF数据的自描述信息。格点气象数据通常是4维,包括空间3维和时间维(x, y, z, t),如果把变量也算作一维也可以认为是5维。netCDF可以有一个无限长度的维(unlimited dimension),通常是时间维。
File Name: E:\Temp\nc\output.nc
Dimensions: 3
longitude = 240;
latitude = 121;
time = 12;
Global Attributes: 2
: Conventions = "CF-1.0";
: history = "2011-07-22 15:15:21 GMT by mars2netcdf-0.92";
Variations: 4
NC_FLOAT longitude(longitude);
longitude: units = "degrees_east";
longitude: long_name = "longitude";
NC_FLOAT latitude(latitude);
latitude: units = "degrees_north";
latitude: long_name = "latitude";
NC_INT time(time);
time: units = "hours since 1900-01-01 00:00:0.0";
time: long_name = "time";
time: avg_period = "0000-01-00 00:00:00";
NC_SHORT q(time,latitude,longitude);
q: scale_factor = 3.07609393740706E-07;
q: add_offset = 0.0101016578590996;
q: _FillValue = -32767;
q: missing_value = -32767;
q: units = "kg kg**-1";
q: long_name = "Specific humidity";
Unlimited dimension: 2
netCDF格式非常灵活,用程序自动判断维和变量等信息的前提条件是数据必须遵循某种约定(convensions)。气象上最常用的约定是CF(COARDS可以看作CF约定的子集),对于维、变量、属性有详细的规定,这样以来软件才能通过约定对数据进行正确的判读。
GrADS中netCDF数据可以通过三种方式打开:
(1)sdfopen,只限于符合COARDS约定(http://ferret.wrc.noaa.gov/noaa_coop/coop_cdf_profile.html)的nc数据。GrADS不支持CF约定(COARDS约定过于简单,无法描述复杂的数据),因此sdfopen能打开的数据十分有限。
(2)xdfopen,对于非COARDS约定的nc数据可以尝试xdfopen来打开,需要一个简单的ctl文件,具体可以看官方说明http://cola.gmu.edu/grads/gadoc/gradcomdxdfopen.html
(3)open,如同打开GrADS二进制文件,需要一个完整的ctl文件,详见http://cola.gmu.edu/grads/gadoc/descriptorfile.html
第2和3种打开nc文件的方法都需要control文件,是为了软件能够正确读取维和变量等信息。
MeteoInfo打开nc文件的方式类似sdfopen,无需control文件,不过MeteoInfo支持CF约定(包含了COARDS约定)、WRF Out约定(WRF模式输出)和IOAPI约定(用于CMAQ模式)。因此可以在不需要control文件的情况下打开更多的nc数据文件。
案例分析
使用 ncdump 命令查看 nc 文件:
C:\Documents and Settings\Administrator>ncdump -h D:\evapr_oaflux_2011-original.nc
netcdf D:\evapr_oaflux_2011-original { dimensions: time = UNLIMITED ; // (7 currently) lat = 180 ; lon = 360 ; variables: float lon(lon) ; lon:long_name = "longitude" ; lon:units = "degrees" ; lon:range = "0.5 to 359.5" ; float lat(lat) ; lat:long_name = "latitude" ; lat:units = "degrees" ; lat:range = "89.5S to 89.5N" ; int time(time) ; time:units = "month" ; time:comment = "Year 2011" ; short evapr(time, lat, lon) ; evapr:comments = "monthly mean evaporation rate" ; evapr:units = "cm/yr" ; evapr:scale_factor = 0.1 ; evapr:missing_value = 32766. ; short err(time, lat, lon) ; err:comments = "monthly mean estimated error of evaporation rate " ; err:units = "cm/yr" ; err:scale_factor = 0.1 ; 关于用 GrADS 命令”sdfopen” 打开 .nc 文件出现的“SDF file has no discernable X coordinate” 问题详解 兰溪之水 3 / 6 err:missing_value = 32766. ; // global attributes: :creation_date = "Oct 2011" ; :description = "WHOI Objectively Analyzed air-sea Fluxes (OAFlux ) Project" ; :project_PIs = "Lisan Yu, Bob Weller" ; :website = "http://oaflux.whoi.edu" ; }
我们从上面的变量单位可以看出这个 evapr_oaflux_2011-original.nc 文件并不符合 COARDS约定
所以我们用”sdfopen”命令打开时,会出现
解决方法1、写一个简单的ctl文件,用xdfopen打开nc文件
DSET ^evapr_oaflux_2011-original.nc TITLE monthly mean evapr_oaflux_2011 UNDEF 32766 XDEF lon 360 LINEAR 0.5 1.0 YDEF lat 180 LINEAR -89.5 1.0 TDEF time 7 LINEAR 00Z01JAN2011 1mo VARS 2 evapr=>evapr 0 99 monthly mean evaporation rate err=>err 0 99 monthly mean estimated error of evaporation rate ENDVARS
保存为:evapr_oaflux_2011.ctl,然后用xdfopen d:/evapr_oaflux_2011.ctl即可打开nc文件
解决方法2:写一个详细的ctl文件,用open打开nc文件
注意:这里的ctl 文件则需很详细,比如要加 ZDEF 之类的,
这里就不详细做了,有兴趣的可以自己试试!
解决方法3:直接修改 nc 文件里面的描述,使其符合 COARDS约定
使用ncl:
begin f = addfile("./evapr_oaflux_2011.nc","w") f->lon@units = "degrees_east" f->lat@units = "degrees_north" f->time@units = "months since 2011-01-01 00:00:0.0" end
执行之后,nc文件描述如下:
netcdf evapr_oaflux_2011 { dimensions: time = UNLIMITED ; // (7 currently) lat = 180 ; lon = 360 ; variables: float lon(lon) ; lon:long_name = "longitude" ; lon:units = "degrees_east" ; lon:range = "0.5 to 359.5" ; float lat(lat) ; lat:long_name = "latitude" ; lat:units = "degrees_north" ; lat:range = "89.5S to 89.5N" ; int time(time) ; time:units = "months since 2011-01-01 00:00:0.0" ; time:comment = "Year 2011" ; time:delta_t = "0000-01-00 00:00:00" ; time:actual_range = "0., 6." ; short evapr(time, lat, lon) ; evapr:comments = "monthly mean evaporation rate" ; evapr:units = "cm/yr" ; evapr:scale_factor = 0.1 ; evapr:missing_value = 32766. ; evapr:_FillValue = -32767s ; short err(time, lat, lon) ; err:comments = "monthly mean estimated error of evaporation rate" ; err:units = "cm/yr" ; err:scale_factor = 0.1 ; err:missing_value = 32766. ; // global attributes: :creation_date = "Oct 2011" ; :description = "WHOI Objectively Analyzed air-sea Fluxes (OAFlux) Project" ; :project_PIs = "Lisan Yu, Bob Weller" ; :website = "http://oaflux.whoi.edu" ;
之后就能用sdfopen打开nc文件了
文章内容参考气象家园的文章,如下:
http://bbs.06climate.com/forum.php?mod=viewthread&tid=1267
http://bbs.06climate.com/forum.php?mod=viewthread&tid=6008
grads读取netcdf文件,报错SDF file has no discernable X coordinate解决办法