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