NetCDF文件概要与读写.doc_第1页
NetCDF文件概要与读写.doc_第2页
NetCDF文件概要与读写.doc_第3页
NetCDF文件概要与读写.doc_第4页
NetCDF文件概要与读写.doc_第5页
已阅读5页,还剩4页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

NetCDF文件概要与读写NetCDF简介NetCDF(network Common Data Format),即网络通用数据格式。最早是由美国国家科学委员会资助之计划-Unidata -所发展,其用意是在Unidata计划中不同的应用项目下,提供一种可以通用的数据存取方式,数据的形状包括单点的观测值、时间序列、规则排列的网格、以及人造卫星或雷达之影像档案。NetCDF 可简单的视为一种存取接口,任何使用NetCDF 存取格式的档案就可称为 NetCDF 档案;至于 NetCDF 这套软件的功能,在于提供C、Fortran、C+、Perl、或其它语言I/O的链接库,以让程序发展者可以读写数据文件,其本身具有说明的能力、并且可以跨越平台和机器的限制。每一个NetCDF档案可以含括多维度的、具有名称的变量,包括长短的整数、单倍与双倍精度的实数、字符等,且每一个变量都有其自我介绍的数据,包括量度的单位、全名及意义等文字说明,在此摘要性的檔头之后,才是真正的数据本身。NetCDF接口是一种多维的数据分布系统,由这个接口所产生的档案,具有多维的数据格式,当你需要其中的某一笔数据时,程序将不会从第一笔数据读到你所需要的数据处,而是由 NetCDF 软件直接存取那一个数据。如此一来将会大量的降低模式运算时数据存取的时间。但也就是因为这样, NetCDF 所需要的空间是很大的,因为他多了很多的自解释的申明。NetCDF文件结构对NETCDF文件的操作主要有读和写两个方面,在了解这两个方面内容之前,首先需要了解NETCDF文件的结构,NETCDF文件主要是Dimensions, Variables, Attributes, Data 四个部分组成的:Dimensions主要是多维资料的结构,如经度、纬度、时间等Variables各种变量,像温度等Attributes一些辅助记忆的说明,如变量的单位等Data主要资料部分下面是NETCDF文件基本结构图(箭头指向为可操作的对象)NETCDF文件基本结构图根据NETCDF文件的这种特殊的结构,所使用的NcFile类中包含了NcDim, NcVar, NcAtt几个类的对象作为成员,分别对应了上面的Dimensions, Variables, Attributes部分。NetCDF文件的读写1.1建工程文件用vc6建立一个工程,使用动态链接库netcdf,然后把文件netcdfcpp.hnetcdfcpp.cpp ,ncvalues.h ncvalues.cpp,ncconfig.h包含到工程中。1.2NETCDF文件的读取首先,定义一个NcFile类的对象,用NcFile类的构造函数直接对其初始化NcFile(const char * path, FileMode = ReadOnly , );path为文件的存储路径,FileMode为文件的打开方式,除了ReadOnly还有Write, New, Replace 等方式我们只是读文件可以选择ReadOnly,其他方式后面会介绍到,其他参数可以使用默认值,例如:NcFile nc(G:/File.nc, NcFile:ReadOnly);使用nc.is_valid()来判断文件打开是否成功,以便进行下一步对NETCDF文件数据进行读取。NETCDF文件主要是Dimensions, Variables, Attributes, Data 四个部分组成的,下面读出文件各个部分的内容。Dimensions:可以使用NETCDF 的成员函数num_dims()获得文件中的Dimensions的个数,然后用NcFile类的另一个成员函数get_dim(int id),或get_dim(NcToken name)-参数可以是id号(int),也可以是dim名字(NcToken),获得每一个文件的Dimensions,用NcDim类的成员函数id(),name(),size()可以依次读出每一个Dimensions的id号,名称,和size。例如:for (int i=0;iid(),nc.get_dim(i)-name(),nc.get_dim(i)-size();利用一个for循环,依据dim的id号可以一次读出所有dim的信息Variables:同理用NETCDF 的成员函数num_vars()可以获得文件中Variables数量,用NcFile类get_var(int id)或 get_var(NcToken name)可以读出每一个文件中的Variables,也可以读出id,name这些信息,所不同的是,Variables还可以用num_dims()读出其所包含的Dimensions的个数,Variables还可以用get_dim(int id)读出其包含的Dimensions的相关信息,同时文件的Data信息也是通过Variables来操作的。Data的读出:首先,我们要根据Variables包含的每一个Dimensions的size计算出数据的大小,定义一个相应大小的数组,用来存放数据。然后,用Variables的get(TYPE *array,long *num)第一个参数是刚才定义的存放数据的数组,第二个参数是自定义的一维数组,数组的元素用来存放每一维的size.一般用于多维数据。例如: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第一个参数是存放数据的数组,后面的参数分别记录第1,2,3,4,5维的size,默认值为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可分为两种一种是整个文件的说明,即global attributes可以通过NcFile类的get_att(int id)函数来获得,参数为id号,例如:String.Format(String+/n%s:%s,nc.get_att(0)-name(),nc.get_att(0)-as_string(0);还有一种是的说明,可以通过在Variables说明的方法获得,从读出的信息主要包括名称和相应的内容,可以通过name()和as_Type(long id),Type根据Attributes数据的类型来确定,如as_string(long id),as_int(long id)等,as_Type(long id)函数主要是用来取出每个Variables的内容。例子在Variables后面。Data: 通过Variables我们已经把数据读出。1.3NETCDF文件的写入和读文件时一样,首先要定义一个NcFile类的对象,如:NcFile nc(G:/File.nc, NcFile: Replace);用nc.is_valid()来判断文件打开是否成功,文件的打开方式我们可以选择New建立一个新的文件,用这种方式如果文件已经存在会返回错误,也可以用Write和Replace。选择Replace,如果文件已存在原文件就会被覆盖掉。用Write时文件必须已经存在,写入文件的数据会加在文件已存在数据的后面。首先,建立一个NETCDF文件,需要写入的数据和刚才读出的数据是一样的,有Dimensions, Variables, Attributes, Data;Dimensions:用NcFile类的add_dim(NcTokenname,long dimsize)成员函数加入Dimensions,如果size 为unlimited用NcFile类的add_dim(NcToken name)函数,NcFile类会自动把其size处理为unlimited。例如:nc.add_dim(lat,5);nc.add_dim(time);/size 为unlimited时Variables:用NcFile类的add_var(NcToken name,NcType type,type dim1,type dim2, )成员函数加入Variables,dim1,dimn,为Variables中包含的Dimensions。例如:nc.add_var(lat,ncInt,nc.get_dim(0);/一个dim时nc.add_var(rh,ncFloat,nc.get_dim(time),nc.get_dim(lat),nc.get_dim(lon);/多个dim时Attributes:整个文件的Attributes用NcFile的add_att(NcToken attname,Type value)函数Type根据加入Attributes的类型来判断;Variables可以用其成员函数add_att(attname, Type value)加入。例如:/写入文件的主要参数nc.add_att(sourse,Fictional Model Output);/写入单个变量的参数nc.get_var(0)-add_att(long_name,Temperature);Data:先定义一个数组,把要写入文件的数据写入数组,用 Variables的函数put(TYPE *arr,const long *count)TYPE 为数据类型或Variable的函数put( TYPE* vals,long edge0=0,long edge1=0,long edge2=0,long edge3=0,long edge4=0) const,第一个参数是存放数据的数组,后面的参数分别记录第1,2,3,4,5维的size,默认值为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等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论