- 积分
- 2005
- 贡献
-
- 精华
- 在线时间
- 小时
- 注册时间
- 2016-4-7
- 最后登录
- 1970-1-1
|
登录后查看更多精彩内容~
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
要把三个时间连续的nc文件,(190001-194912,195001-199912,200001-201414),写入一个新创建的nc文件中,但循环写入的时候时间怎么连续起来?急急急!
错误显示:
错误使用 netcdflib
输入元素的数目与变量大小不匹配。
出错 netcdf.putVar (line 84)
netcdflib(funcstr,ncid,varid,varargin{:});
出错 humidity (line 34)
netcdf.putVar(outid,timeid,time);
create.m
- function varargout = create(filename, mode, varargin)
- %netcdf.create Create new netCDF file.
- % ncid = netcdf.create(filename, mode) creates a new netCDF file
- % according to the file creation mode. The return value is a file
- % ID.
- %
- % The type of access is described by the mode parameter, which could
- % be one of the following string values or a bitwise-or of numeric mode
- % values:
- %
- % 'CLOBBER' - overwrite existing files
- % 'NOCLOBBER' - do not overwrite existing files
- % 'SHARE' - allow for synchronous file updates
- % '64BIT_OFFSET' - allow the creation of 64-bit files instead of
- % the classic format
- % 'NETCDF4' - create a netCDF-4/HDF5 file
- % 'CLASSIC_MODEL' - enforce classic model, has no effect unless used
- % in a bitwise-or with 'NETCDF4'
- %
- % [chunksize_out, ncid]=netcdf.create(filename,mode,initsz,chunksize)
- % creates a new netCDF file with additional performance tuning
- % parameters. initsz sets the initial size of the file.
- % chunksize can affect I/O performance. The actual value chosen by
- % the netCDF library may not correspond to the input value.
- %
- % This function corresponds to the "nc_create" and "nc__create" functions
- % in the netCDF library C API.
- %
- % Example: create a netCDF file that overwrites any existing file by the
- % same name.
- % ncid = netcdf.create('myfile.nc','CLOBBER');
- % netcdf.close(ncid);
- %
- % Example: create a netCDF-4 file that uses the classic model.
- % mode = netcdf.getConstant('NETCDF4');
- % mode = bitor(mode,netcdf.getConstant('CLASSIC_MODEL'));
- % ncid = netcdf.create('myfile.nc',mode);
- % netcdf.close(ncid);
- %
- % Please read the files netcdfcopyright.txt and mexnccopyright.txt for
- % more information.
- %
- % See also netcdf, netcdf.getConstant, BITOR.
- %
- % Copyright 2008-2013 The MathWorks, Inc.
- validateattributes(mode,{'char','numeric'},{'row'},'netcdf.create','mode');
- varargout = cell(1,nargout);
- switch nargin
- case 2
- ncid = netcdflib('create', filename, mode);
- varargout{1} = ncid;
- case 4
- [czout,ncid] = netcdflib('pCreate', filename, mode, varargin{:});
- varargout{1} = czout;
- varargout{2} = ncid;
- otherwise
- error (message('MATLAB:imagesci:validate:wrongNumberOfInputs'));
- end
复制代码
code
- clear all
- clc
- %定义输入目录
- inputdir = 'D:/matlabht/pdsi0505';
- %定义输出目录
- outputdir = 'D:/matlabht/pdsi';
- %获取目录下所有nc文件的信息
- inputfiles = dir([inputdir , '/', '*.nc']);
- outfile = ['pdsi1991-2014.nc'];%定义输出的nc文件的文件名
- outdirfile = [outputdir '/' outfile];%输出路径
- outid = netcdf.create(outdirfile,'NC_NOCLOBBER');%创建nc文件
- latdimID = netcdf.defDim(outid,'lat',360);%创建维度及维数
- londimID = netcdf.defDim(outid,'lon',720);%同理
- timedimID = netcdf.defDim(outid,'time',1380);%同
- latid = netcdf.defVar(outid,'lat','double',latdimID);%定义变量,变量名等。
- lonid = netcdf.defVar(outid,'lon','double',londimID);
- timeid = netcdf.defVar(outid,'time','int',timedimID);
- pdsiid = netcdf.defVar(outid,'pdsi','double',[londimID,latdimID,timedimID]);
- netcdf.endDef(outid);%定义结束
- for ifiles = 1:length(inputfiles) %length(inputfiles)是获取inputfiles的长度,就是nc文件的数目
- infile = inputfiles(ifiles).name;%获取nc文件名
- varName = infile(11:14);%把文件名的pdsi赋值给varname
- indirfile = [inputdir '/' inputfiles(ifiles).name];%获取nc文件的完整路径
- time = ncread(indirfile,'time');%读取时间变量
- Ti=size(time);%获取时间变量的尺度
- lon = ncread(indirfile,'lon');%读取经度
- lat = ncread(indirfile,'lat');%读取纬度
- pdsi = ncread(indirfile,'pdsi');
- pdsi = double(pdsi);
- netcdf.putVar(outid,latid,lat);%将值赋给变量
- netcdf.putVar(outid,lonid,lon);
- netcdf.putVar(outid,timeid,time);
- netcdf.putVar(outid,pdsiid,pdsi);
- netcdf.reDef(outid);%赋值结束
- netcdf.putAtt(outid,latid,'long_name','latitude');%对变量的说明
- netcdf.putAtt(outid,latid,'units','degrees_north');
- netcdf.putAtt(outid,latid,'standard_name','latitude');
- netcdf.putAtt(outid,lonid,'long_name','longitude');
- netcdf.putAtt(outid,lonid,'units','degrees_east');
- netcdf.putAtt(outid,lonid,'standard_name','longitude');
- netcdf.putAtt(outid,timeid,'long_name','time');
- netcdf.putAtt(outid,timeid,'units','hours since 1900-01-01 00:00:00');
- netcdf.putAtt(outid,timeid,'standard_name','time');
-
- netcdf.putAtt(outid,pdsiid,'long_name','Palmer Drought Severity Index');
- netcdf.putAtt(outid,pdsiid,'units','mm');
- netcdf.putAtt(outid,pdsiid,'_FillValue','-9.99e+33');
- netcdf.putAtt(outid,pdsiid,'missing_value','-9.99e+33');
- netcdf.close(outid);
- end
复制代码
|
|