




已閱讀5頁,還剩4頁未讀, 繼續(xù)免費(fèi)閱讀
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
NetCDF文件概要與讀寫NetCDF簡介NetCDF(network Common Data Format),即網(wǎng)絡(luò)通用數(shù)據(jù)格式。最早是由美國國家科學(xué)委員會(huì)資助之計(jì)劃-Unidata -所發(fā)展,其用意是在Unidata計(jì)劃中不同的應(yīng)用項(xiàng)目下,提供一種可以通用的數(shù)據(jù)存取方式,數(shù)據(jù)的形狀包括單點(diǎn)的觀測值、時(shí)間序列、規(guī)則排列的網(wǎng)格、以及人造衛(wèi)星或雷達(dá)之影像檔案。NetCDF 可簡單的視為一種存取接口,任何使用NetCDF 存取格式的檔案就可稱為 NetCDF 檔案;至于 NetCDF 這套軟件的功能,在于提供C、Fortran、C+、Perl、或其它語言I/O的鏈接庫,以讓程序發(fā)展者可以讀寫數(shù)據(jù)文件,其本身具有說明的能力、并且可以跨越平臺(tái)和機(jī)器的限制。每一個(gè)NetCDF檔案可以含括多維度的、具有名稱的變量,包括長短的整數(shù)、單倍與雙倍精度的實(shí)數(shù)、字符等,且每一個(gè)變量都有其自我介紹的數(shù)據(jù),包括量度的單位、全名及意義等文字說明,在此摘要性的檔頭之后,才是真正的數(shù)據(jù)本身。NetCDF接口是一種多維的數(shù)據(jù)分布系統(tǒng),由這個(gè)接口所產(chǎn)生的檔案,具有多維的數(shù)據(jù)格式,當(dāng)你需要其中的某一筆數(shù)據(jù)時(shí),程序?qū)⒉粫?huì)從第一筆數(shù)據(jù)讀到你所需要的數(shù)據(jù)處,而是由 NetCDF 軟件直接存取那一個(gè)數(shù)據(jù)。如此一來將會(huì)大量的降低模式運(yùn)算時(shí)數(shù)據(jù)存取的時(shí)間。但也就是因?yàn)檫@樣, NetCDF 所需要的空間是很大的,因?yàn)樗嗔撕芏嗟淖越忉尩纳昝?。NetCDF文件結(jié)構(gòu)對(duì)NETCDF文件的操作主要有讀和寫兩個(gè)方面,在了解這兩個(gè)方面內(nèi)容之前,首先需要了解NETCDF文件的結(jié)構(gòu),NETCDF文件主要是Dimensions, Variables, Attributes, Data 四個(gè)部分組成的:Dimensions主要是多維資料的結(jié)構(gòu),如經(jīng)度、緯度、時(shí)間等Variables各種變量,像溫度等Attributes一些輔助記憶的說明,如變量的單位等Data主要資料部分下面是NETCDF文件基本結(jié)構(gòu)圖(箭頭指向?yàn)榭刹僮鞯膶?duì)象)NETCDF文件基本結(jié)構(gòu)圖根據(jù)NETCDF文件的這種特殊的結(jié)構(gòu),所使用的NcFile類中包含了NcDim, NcVar, NcAtt幾個(gè)類的對(duì)象作為成員,分別對(duì)應(yīng)了上面的Dimensions, Variables, Attributes部分。NetCDF文件的讀寫1.1建工程文件用vc6建立一個(gè)工程,使用動(dòng)態(tài)鏈接庫netcdf,然后把文件netcdfcpp.hnetcdfcpp.cpp ,ncvalues.h ncvalues.cpp,ncconfig.h包含到工程中。1.2NETCDF文件的讀取首先,定義一個(gè)NcFile類的對(duì)象,用NcFile類的構(gòu)造函數(shù)直接對(duì)其初始化NcFile(const char * path, FileMode = ReadOnly , );path為文件的存儲(chǔ)路徑,F(xiàn)ileMode為文件的打開方式,除了ReadOnly還有Write, New, Replace 等方式我們只是讀文件可以選擇ReadOnly,其他方式后面會(huì)介紹到,其他參數(shù)可以使用默認(rèn)值,例如:NcFile nc(G:/File.nc, NcFile:ReadOnly);使用nc.is_valid()來判斷文件打開是否成功,以便進(jìn)行下一步對(duì)NETCDF文件數(shù)據(jù)進(jìn)行讀取。NETCDF文件主要是Dimensions, Variables, Attributes, Data 四個(gè)部分組成的,下面讀出文件各個(gè)部分的內(nèi)容。Dimensions:可以使用NETCDF 的成員函數(shù)num_dims()獲得文件中的Dimensions的個(gè)數(shù),然后用NcFile類的另一個(gè)成員函數(shù)get_dim(int id),或get_dim(NcToken name)-參數(shù)可以是id號(hào)(int),也可以是dim名字(NcToken),獲得每一個(gè)文件的Dimensions,用NcDim類的成員函數(shù)id(),name(),size()可以依次讀出每一個(gè)Dimensions的id號(hào),名稱,和size。例如:for (int i=0;iid(),nc.get_dim(i)-name(),nc.get_dim(i)-size();利用一個(gè)for循環(huán),依據(jù)dim的id號(hào)可以一次讀出所有dim的信息Variables:同理用NETCDF 的成員函數(shù)num_vars()可以獲得文件中Variables數(shù)量,用NcFile類get_var(int id)或 get_var(NcToken name)可以讀出每一個(gè)文件中的Variables,也可以讀出id,name這些信息,所不同的是,Variables還可以用num_dims()讀出其所包含的Dimensions的個(gè)數(shù),Variables還可以用get_dim(int id)讀出其包含的Dimensions的相關(guān)信息,同時(shí)文件的Data信息也是通過Variables來操作的。Data的讀出:首先,我們要根據(jù)Variables包含的每一個(gè)Dimensions的size計(jì)算出數(shù)據(jù)的大小,定義一個(gè)相應(yīng)大小的數(shù)組,用來存放數(shù)據(jù)。然后,用Variables的get(TYPE *array,long *num)第一個(gè)參數(shù)是剛才定義的存放數(shù)據(jù)的數(shù)組,第二個(gè)參數(shù)是自定義的一維數(shù)組,數(shù)組的元素用來存放每一維的size.一般用于多維數(shù)據(jù)。例如:float rhs50;long array3;array0=1;array1=5;array2=10;nc.get_var(rh)-get(rhs,array);還可以用Variables的get( TYPE* vals,long edge0=0,long edge1=0,long edge2=0,long edge3=0,long edge4=0) const第一個(gè)參數(shù)是存放數(shù)據(jù)的數(shù)組,后面的參數(shù)分別記錄第1,2,3,4,5維的size,默認(rèn)值為0,例如:int n=nc.get_dim(lat)-size();int lats5;nc.get_var(lat)-get(lats,n);for (i=0;iget_att(0)-name(),nc.get_var(0)-get_att(0)-as_string(0);Attributes:Attributes可分為兩種一種是整個(gè)文件的說明,即global attributes可以通過NcFile類的get_att(int id)函數(shù)來獲得,參數(shù)為id號(hào),例如:String.Format(String+/n%s:%s,nc.get_att(0)-name(),nc.get_att(0)-as_string(0);還有一種是的說明,可以通過在Variables說明的方法獲得,從讀出的信息主要包括名稱和相應(yīng)的內(nèi)容,可以通過name()和as_Type(long id),Type根據(jù)Attributes數(shù)據(jù)的類型來確定,如as_string(long id),as_int(long id)等,as_Type(long id)函數(shù)主要是用來取出每個(gè)Variables的內(nèi)容。例子在Variables后面。Data: 通過Variables我們已經(jīng)把數(shù)據(jù)讀出。1.3NETCDF文件的寫入和讀文件時(shí)一樣,首先要定義一個(gè)NcFile類的對(duì)象,如:NcFile nc(G:/File.nc, NcFile: Replace);用nc.is_valid()來判斷文件打開是否成功,文件的打開方式我們可以選擇New建立一個(gè)新的文件,用這種方式如果文件已經(jīng)存在會(huì)返回錯(cuò)誤,也可以用Write和Replace。選擇Replace,如果文件已存在原文件就會(huì)被覆蓋掉。用Write時(shí)文件必須已經(jīng)存在,寫入文件的數(shù)據(jù)會(huì)加在文件已存在數(shù)據(jù)的后面。首先,建立一個(gè)NETCDF文件,需要寫入的數(shù)據(jù)和剛才讀出的數(shù)據(jù)是一樣的,有Dimensions, Variables, Attributes, Data;Dimensions:用NcFile類的add_dim(NcTokenname,long dimsize)成員函數(shù)加入Dimensions,如果size 為unlimited用NcFile類的add_dim(NcToken name)函數(shù),NcFile類會(huì)自動(dòng)把其size處理為unlimited。例如:nc.add_dim(lat,5);nc.add_dim(time);/size 為unlimited時(shí)Variables:用NcFile類的add_var(NcToken name,NcType type,type dim1,type dim2, )成員函數(shù)加入Variables,dim1,dimn,為Variables中包含的Dimensions。例如:nc.add_var(lat,ncInt,nc.get_dim(0);/一個(gè)dim時(shí)nc.add_var(rh,ncFloat,nc.get_dim(time),nc.get_dim(lat),nc.get_dim(lon);/多個(gè)dim時(shí)Attributes:整個(gè)文件的Attributes用NcFile的add_att(NcToken attname,Type value)函數(shù)Type根據(jù)加入Attributes的類型來判斷;Variables可以用其成員函數(shù)add_att(attname, Type value)加入。例如:/寫入文件的主要參數(shù)nc.add_att(sourse,Fictional Model Output);/寫入單個(gè)變量的參數(shù)nc.get_var(0)-add_att(long_name,Temperature);Data:先定義一個(gè)數(shù)組,把要寫入文件的數(shù)據(jù)寫入數(shù)組,用 Variables的函數(shù)put(TYPE *arr,const long *count)TYPE 為數(shù)據(jù)類型或Variable的函數(shù)put( TYPE* vals,long edge0=0,long edge1=0,long edge2=0,long edge3=0,long edge4=0) const,第一個(gè)參數(shù)是存放數(shù)據(jù)的數(shù)組,后面的參數(shù)分別記錄第1,2,3,4,5維的size,默認(rèn)值為0,例如:int lats5=20,30,40,50,60; /lat nc.get_var(lat)-put(lats,5); float rhss50; for (int i=0;i1;i+) for (int j=0;j5;j+) for (int k=0;kput(rhss,count);netcdfreadnetcdfreadfunction(file,lonname=longitude,latname=latitude,timename=time,dataname,unpack=F) # Extract longitude vector, latitude vector, time vector and data array from a netcdf file.# Description:# Returns a vector of longitudes, a vector of latitudes, a vector of times and an array of data from a netcdf file.# Usage:# netcdfread(file,lonname,latname,timename,dataname,unpack=F)# Arguments:# file: String containing the full path and netcdf file name# lonname: String containing the name of the longitude variable. # Default name is longitude.# latname: String containing the name of the latitude variable# Default name is latitude.# timename: String containing the name of the time variable.# Default name is time# dataname: String containing the name of the data variable# unpack: Logical. If TRUE unpacks data contained in variable dataname by performing the following transformation: unpacked_data_value=packed_data_value*scale_factor+add_offset, where, scale_factor and add_offset are constant netcdf attributes of variable dataname. Default is FALSE.# Output:# $lonncfile: Vector of longitudes# $latncfile: Vector of latitudes# $timencfile: Vector of times# $datancfile: Array of data# Author:# Chris Ferro 9 June 2005# Dag Johan Steinskog # Caio Coelho # Examples:# First run the line below to extract the data structure/information so that you are able to know the names of variables netcdfinfo(netcdf_file_name.nc)# netcdfread(netcdf_file_name.nc,lon_name,lat_name,time_name,data_name)# netcdfread(/home/username/netcdf_file_name.nc,lon_name,lat_name,time_name,data_name)# Note: The user should use a netcdf file to be able to run these examples# open a netcdf filencfile - open.nc(file)# get latitude and longitudelonncfileaux - var.get.nc(ncfile, lonname)latncfileaux - var.get.nc(ncfile, latname)# get data and timedatancfile - var.get.nc(ncfile, dataname)timencfile - var.get.nc(ncfile, timename)if(unpack)datancfile-datancfile*att.get.nc(ncfile,dataname,scale_factor)+att.get.nc(ncfile,dataname,add_offset)# sort latitude and longitude in ascending orderlonncfile - sort(lonncfileaux)latncfile - sort(latncfileaux)# rearange data datancfile-datancfileorder(lonncfileaux),order(latncfileaux),invisible(list(lonncfile=lonncfile,latncfile=latncfile,timencfile=timencfile,datancfile=datancfile)netcdfwritenetcdfwrite - function(lon,lat,data,filename=data.nc,time=1,mv=-999) # Writes two-dimensional map or three-dimensional array of data, longitude vector,# latitude vector and time vector into a netcdf file.# Description:# Returns a netcdf file that contains a two-dimensional map or a three-dimensional array of data, a longitude vector, a latitude vector and a time vector# Usage:# netcdfwrite(lon,lat,data,filename,time,mv)# Arguments:# data: two-dimensional map with first dimension longitude and second dimension latitude or three-dimensional array with first dimension longitude, second dimension latitude and third dimension time, containing the data to be written to the netcdf file# lon: a vector containing the longitudes of data# lat: a vector containing the latitudes of data# time: a vector containing the times of data in hours since 1900-1-1 00:00:0.0 . Default is 1, which is appropriate for writing a two-dimensional map #filename: string with the name of the netcdf file to be created.# Default is data.nc# mv: Value attributed to missing data. Default is -999# Output:# netcdf-file containing the data # Author:# Dag Johan Steinskog 5 January 2006# Caio Coelho # Example:# x - seq(-20, 20, 5)# y - seq(30, 60, 5)# dims - c(length(x), length(y), 100)# data - array(rnorm(prod(dims), dims)# time - 1:100# # write a two-dimensional map of data to the netcdf file# netcdfwrite(x,y,data,3,filename=data.nc)# # write a three-dimensional array of data to the netcdf file# netcdfwrite(x,y,data,filename=data.nc,time)if(length(time)=1)data-array(data,c(dim(data)1,dim(data)2,1)#create netcdf file#ncfile - create.nc(filename,clobber)ncfile - create.nc(filename)#give dimensions to variablesdim.def.nc(ncfile,lon,length(lon)dim.def.nc(ncfile,lat,length(lat)dim.def.nc(ncfile,time,length(time)# create variablesvar.def.nc(ncfile,lon,NC_FLOAT,lon)var.def.nc(ncfile,lat,NC_FLOAT,lat)var.def.nc(ncfile,time,NC_DOUBLE,time)var.def.nc(ncfile,data,NC_FLOAT,c(lon,lat,time)#var.def.nc(ncfile,data,NC_DOUBLE,c(lon,lat)# add data to variablesvar.put.nc(ncfile,lat,lat)var.put.nc(ncfile,lon,lon)var.put.nc(ncfile,time,time)var.put.nc(ncfile,data,data)# add description information to variablesatt.put.nc(ncfile,data,missing_value,NC_FLOAT,mv)att.put.nc(ncfile,lon,long_name,NC_FLOAT,Longitude)att.put.nc(ncfile,lon,units,NC_FLOAT,degrees_east)att.put.nc(ncfile,lon,axis,NC_FLOAT,X)att.put.nc(ncfile,lat,long_name,NC_FLOAT,Latitude)att.put.nc(ncfile,lat,units,NC_FLOAT,degrees_north)att.put.nc(ncfile,lat,axis,NC_FLOAT,Y)att.put.nc(ncfile,time,long_name,NC_DOUBLE,Time)att.put.nc(ncfile,time,units,NC_DOUBLE,hours since 1900-1-1 00:00:0.0)att.put.nc(ncfile,time,delta_t,NC_DOUBLE,0000-01-00 00:00:00)# syncronize and close the netcdf file sync.nc(ncfile)close.nc(ncfile)Netcdfinfronetcdfinfo - function(file) # Extract data structure, names of variables and dimension information form netcdf file# Description:#
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 安徽省安慶市四中學(xué)2025屆八年級(jí)英語第二學(xué)期期末聯(lián)考試題含答案
- 2025屆浙江省紹興市名校七下英語期中達(dá)標(biāo)檢測模擬試題含答案
- 2025年醫(yī)院信息化升級(jí)攻略:電子病歷系統(tǒng)優(yōu)化與醫(yī)療質(zhì)量提升報(bào)告
- 2025年醫(yī)院信息化建設(shè)初步設(shè)計(jì)評(píng)估:系統(tǒng)安全與性能分析報(bào)告
- 2025年醫(yī)院電子病歷系統(tǒng)優(yōu)化與醫(yī)療信息化投資策略分析報(bào)告
- 2025年醫(yī)藥市場醫(yī)藥電商競爭格局:仿制藥一致性評(píng)價(jià)下的市場競爭力分析報(bào)告
- 2025年醫(yī)藥企業(yè)研發(fā)外包(CRO)與臨床試驗(yàn)質(zhì)量控制體系構(gòu)建報(bào)告
- 江蘇省蘇州市星港學(xué)校2025屆八下英語期中聯(lián)考試題含答案
- 聚焦2025年工業(yè)互聯(lián)網(wǎng)平臺(tái)漏洞掃描技術(shù)深度解析與應(yīng)用策略報(bào)告
- 安全轉(zhuǎn)崗培訓(xùn)試題及答案
- 某公司常用公文寫作規(guī)范與范例
- “五步一練”六環(huán)節(jié)在高中化學(xué)課堂教學(xué)中的實(shí)踐研究
- 建筑工程典型安全事故案例
- 抖音來客本地生活服務(wù)休閑娛樂購物行業(yè)商家運(yùn)營策劃方案
- 頤高集團(tuán)簡介數(shù)字園區(qū)投資運(yùn)營商
- 士官留隊(duì)申請(qǐng)書格式
- 2025年國學(xué)知識(shí)競賽中國古代文學(xué)知識(shí)競賽題庫及答案(共101題)
- 2025年上半年社區(qū)居委會(huì)工作總結(jié)(3篇)
- 《中國聯(lián)通IPv6培訓(xùn)》課件
- 部編版2025春六年級(jí)下冊(cè)語文15《真理誕生于一百個(gè)問號(hào)之后》 課件
- 小班安全課件幼兒園
評(píng)論
0/150
提交評(píng)論