请选择 进入手机版 | 继续访问电脑版
爱气象,爱气象家园! 

气象家园

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博登陆

只需一步, 快速开始

搜索
查看: 5985|回复: 0

[脚本编辑] matlab读取三个nc文件,写入一个新创建的nc文件中,时间怎么连续起来?

[复制链接]

新浪微博达人勋

发表于 2018-4-22 10:41:07 | 显示全部楼层 |阅读模式

登录后查看更多精彩内容~

您需要 登录 才可以下载或查看,没有帐号?立即注册 新浪微博登陆

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
  1. function varargout = create(filename, mode, varargin)
  2. %netcdf.create Create new netCDF file.
  3. %   ncid = netcdf.create(filename, mode) creates a new netCDF file
  4. %   according to the file creation mode.  The return value is a file
  5. %   ID.  
  6. %   
  7. %   The type of access is described by the mode parameter, which could
  8. %   be one of the following string values or a bitwise-or of numeric mode
  9. %   values:
  10. %
  11. %       'CLOBBER'       - overwrite existing files
  12. %       'NOCLOBBER'     - do not overwrite existing files
  13. %       'SHARE'         - allow for synchronous file updates
  14. %       '64BIT_OFFSET'  - allow the creation of 64-bit files instead of
  15. %                         the classic format
  16. %       'NETCDF4'       - create a netCDF-4/HDF5 file
  17. %       'CLASSIC_MODEL' - enforce classic model, has no effect unless used
  18. %                         in a bitwise-or with 'NETCDF4'
  19. %
  20. %   [chunksize_out, ncid]=netcdf.create(filename,mode,initsz,chunksize)
  21. %   creates a new netCDF file with additional performance tuning
  22. %   parameters.  initsz sets the initial size of the file.  
  23. %   chunksize can affect I/O performance.  The actual value chosen by
  24. %   the netCDF library may not correspond to the input value.
  25. %
  26. %   This function corresponds to the "nc_create" and "nc__create" functions
  27. %   in the netCDF library C API.
  28. %
  29. %   Example:  create a netCDF file that overwrites any existing file by the
  30. %   same name.
  31. %       ncid = netcdf.create('myfile.nc','CLOBBER');
  32. %       netcdf.close(ncid);
  33. %
  34. %   Example:  create a netCDF-4 file that uses the classic model.
  35. %       mode = netcdf.getConstant('NETCDF4');
  36. %       mode = bitor(mode,netcdf.getConstant('CLASSIC_MODEL'));
  37. %       ncid = netcdf.create('myfile.nc',mode);
  38. %       netcdf.close(ncid);
  39. %
  40. %   Please read the files netcdfcopyright.txt and mexnccopyright.txt for
  41. %   more information.
  42. %
  43. %   See also netcdf, netcdf.getConstant, BITOR.
  44. %

  45. %   Copyright 2008-2013 The MathWorks, Inc.

  46. validateattributes(mode,{'char','numeric'},{'row'},'netcdf.create','mode');

  47. varargout = cell(1,nargout);
  48. switch nargin
  49.     case 2
  50.         ncid = netcdflib('create', filename, mode);
  51.         varargout{1} = ncid;
  52.     case 4
  53.         [czout,ncid] = netcdflib('pCreate', filename, mode, varargin{:});
  54.         varargout{1} = czout;
  55.         varargout{2} = ncid;
  56.     otherwise
  57.         error (message('MATLAB:imagesci:validate:wrongNumberOfInputs'));
  58. end




复制代码


code
  1. clear all
  2. clc
  3. %定义输入目录
  4. inputdir = 'D:/matlabht/pdsi0505';
  5. %定义输出目录
  6. outputdir = 'D:/matlabht/pdsi';
  7. %获取目录下所有nc文件的信息
  8. inputfiles = dir([inputdir , '/', '*.nc']);
  9. outfile = ['pdsi1991-2014.nc'];%定义输出的nc文件的文件名
  10. outdirfile = [outputdir '/' outfile];%输出路径
  11. outid = netcdf.create(outdirfile,'NC_NOCLOBBER');%创建nc文件
  12. latdimID = netcdf.defDim(outid,'lat',360);%创建维度及维数
  13. londimID = netcdf.defDim(outid,'lon',720);%同理
  14. timedimID = netcdf.defDim(outid,'time',1380);%同

  15. latid = netcdf.defVar(outid,'lat','double',latdimID);%定义变量,变量名等。
  16. lonid = netcdf.defVar(outid,'lon','double',londimID);
  17. timeid = netcdf.defVar(outid,'time','int',timedimID);
  18. pdsiid = netcdf.defVar(outid,'pdsi','double',[londimID,latdimID,timedimID]);                 
  19. netcdf.endDef(outid);%定义结束
  20. for ifiles = 1:length(inputfiles) %length(inputfiles)是获取inputfiles的长度,就是nc文件的数目
  21.     infile = inputfiles(ifiles).name;%获取nc文件名
  22.     varName = infile(11:14);%把文件名的pdsi赋值给varname
  23.     indirfile = [inputdir '/' inputfiles(ifiles).name];%获取nc文件的完整路径
  24.     time = ncread(indirfile,'time');%读取时间变量
  25.     Ti=size(time);%获取时间变量的尺度
  26.     lon = ncread(indirfile,'lon');%读取经度
  27.     lat = ncread(indirfile,'lat');%读取纬度
  28.     pdsi = ncread(indirfile,'pdsi');
  29.     pdsi = double(pdsi);

  30.     netcdf.putVar(outid,latid,lat);%将值赋给变量
  31.     netcdf.putVar(outid,lonid,lon);
  32.     netcdf.putVar(outid,timeid,time);
  33.     netcdf.putVar(outid,pdsiid,pdsi);
  34.     netcdf.reDef(outid);%赋值结束

  35.     netcdf.putAtt(outid,latid,'long_name','latitude');%对变量的说明
  36.     netcdf.putAtt(outid,latid,'units','degrees_north');
  37.     netcdf.putAtt(outid,latid,'standard_name','latitude');

  38.     netcdf.putAtt(outid,lonid,'long_name','longitude');
  39.     netcdf.putAtt(outid,lonid,'units','degrees_east');
  40.     netcdf.putAtt(outid,lonid,'standard_name','longitude');

  41.     netcdf.putAtt(outid,timeid,'long_name','time');
  42.     netcdf.putAtt(outid,timeid,'units','hours since 1900-01-01 00:00:00');
  43.     netcdf.putAtt(outid,timeid,'standard_name','time');
  44.    
  45.     netcdf.putAtt(outid,pdsiid,'long_name','Palmer Drought Severity Index');
  46.     netcdf.putAtt(outid,pdsiid,'units','mm');
  47.     netcdf.putAtt(outid,pdsiid,'_FillValue','-9.99e+33');
  48.     netcdf.putAtt(outid,pdsiid,'missing_value','-9.99e+33');
  49.     netcdf.close(outid);
  50. end
复制代码


密码修改失败请联系微信:mofangbao
您需要登录后才可以回帖 登录 | 立即注册 新浪微博登陆

本版积分规则

Copyright ©2011-2014 bbs.06climate.com All Rights Reserved.  Powered by Discuz! (京ICP-10201084)

本站信息均由会员发表,不代表气象家园立场,禁止在本站发表与国家法律相抵触言论

快速回复 返回顶部 返回列表