爱气象,爱气象家园! 

气象家园

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博登陆

只需一步, 快速开始

搜索
楼主: 022

[源程序] MATLAB读nc文件及创建写入nc

  [复制链接]

新浪微博达人勋

发表于 2018-4-18 23:11:32 | 显示全部楼层

嗯嗯,初学者,分享了
密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2018-4-18 23:11:36 | 显示全部楼层

嗯嗯,初学者,分享了
密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2018-4-18 23:11:39 | 显示全部楼层

嗯嗯,初学者,分享了
密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2018-4-20 20:49:59 | 显示全部楼层
希望对我的毕设有用
密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2018-4-21 16:10:13 | 显示全部楼层
楼主你好,我按照你的分享结合自己的修改了一下,出现了错误,但我不会改,求指点。我是菜鸟,新新手。麻烦了。
代码如下,出错显示随后:
  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 = (['pdsi199101_201412.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,shumid,'long_name','Air specific humidity');
  46.     netcdf.putAtt(outid,shumid,'units','kg kg-1');
  47.     netcdf.putAtt(outid,shumid,'_FillValue','-9.99e+33');
  48.     netcdf.putAtt(outid,shumid,'missing_value','-9.99e+33');
  49.     netcdf.close(outid);
  50. end
复制代码

错误命令行窗口
错误使用 netcdflib
无法使用指定的文件名创建文件。文件名可能包含不受支持的字符。

出错 netcdf.create (line 53)
        ncid = netcdflib('create', filename, mode);

出错 humidity (line 11)
outid = netcdf.create(outdirfile,'NC_NOCLOBBER');%创建nc文件
密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2018-4-21 16:33:41 | 显示全部楼层
之前的问的问题不够详细和准确问题,我自己发了帖子求助,求指教!
详见http://bbs.06climate.com/forum.p ... id=60398&extra=
密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2018-4-30 22:41:38 来自手机 | 显示全部楼层
实用性比较强大的
密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2018-5-17 21:58:02 | 显示全部楼层
谢谢分享,果断下载啦~
密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2018-5-18 07:56:21 来自手机 | 显示全部楼层
6“““好::不”””
密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2018-5-30 22:22:35 | 显示全部楼层
谢谢楼主  十分感谢分享  666   雪中送炭
密码修改失败请联系微信:mofangbao
您需要登录后才可以回帖 登录 | 立即注册 新浪微博登陆

本版积分规则

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

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

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