【问题标题】:read multiple netcdf files - matlab读取多个 netcdf 文件 - matlab
【发布时间】:2016-06-28 17:08:09
【问题描述】:

我有 40 个名为 'lffd1961_V10M_Iberia.nc'、'lffd1962_V10M_Iberia' ... 'lffd2000_V10M_Iberia' 的 netcdf 文件。

我想在同一个程序中打开所有这些,在所有这些中获取相同的变量,在该变量中应用一些等式并创建 40 个新的 netcdf 文件并修改该变量。

为了打开 40 个文件,我开发了这段代码,它可以工作。

for yr=1961:2000
inFile=strcat('lffd',num2str(yr),'_V10M_Iberia.nc'); disp(inFile)
nc=netcdf.open(inFile,'NC_NOWRITE');
netcdf.close(nc)
end

现在我想在所有变量中获取变量号 4。这个变量是一个 3D 矩阵(70x51x(8760 或 8784)) 像下一行这样的东西,但有 40 年的循环:

ws_1961(1962, etc, etc) = netcdf.getVar(nc,4);

它应该在我的工作区中有 40 个新变量。 在此之后,我想在所有 40 个变量中应用这个方程:

ws_80 = ws.*(log(z/alpha)/log(exp(1))/(log(10/alpha)/log(exp(1))));

最后在每个文件中使用我的新变量创建 40 个新的 netcdf 文件。

类似的东西:

outFile=strcat('year',num2str(yr),'_80m.nc')
ncid = netcdf.create(outFile,'CLOBBER');

dimid0 = netcdf.defDim(ncid,'longitude',nlon); % getvar number 1
dimid1 = netcdf.defDim(ncid,'latitude',nlat); %getvar number 2
dimid2 = netcdf.defDim(ncid,'time',nt); %getvar number 3

varid0 = netcdf.defVar(ncid,'longitude','double', dimid0);
varid1 = netcdf.defVar(ncid,'latitude','double', dimid1);
varid2 = netcdf.defVar(ncid,'time','double', dimid2);
varid3 = netcdf.defVar(ncid,'ws_80','double', [dimid0 dimid1 dimid2]);

netcdf.putAtt(ncid,varid0,'units','degrees_east');
netcdf.putAtt(ncid,varid0,'long_name','longitude');

netcdf.putAtt(ncid,varid1,'units','degrees_north');
netcdf.putAtt(ncid,varid1,'degrees_north','latitude');

netcdf.endDef(ncid);

netcdf.putVar(ncid,varid0,longitude);
netcdf.putVar(ncid,varid1,latitude);
netcdf.putVar(ncid,varid2,time);
netcdf.putVar(ncid,varid3,ws80);

我很难用你们所有人都理解的方式来解释这一点,所以请放心地问任何你想问的问题。

【问题讨论】:

  • 您在哪一部分卡住了需要帮助?我不清楚你的方程是否需要一个变量中所有文件的所有数据。如果没有,您最好一次处理一个文件(读取、应用方程式、写入)。使用更高级别的函数,如 ncread、ncinfo、ncwriteschema 和 ncwrite 可能会更容易。
  • 你能帮我分部分吗?我的第一个循环,打开我所有的 .nc 文件工作正常。现在我需要某种循环来获取第四个位置 'netcdf.getVar(nc,4);' 中的所有变量在每个文件。我的下一步是在我的工作区中有 40 个变量(每个文件一个)。
  • 你的版本有ncread功能吗?再一次,你真的需要所有 40 个变量来应用这个方程吗?或者您可以一次将方程应用于一个变量吗?
  • 是的,我的版本有这个功能。我不需要 40 个变量来应用方程。我需要将方程应用于这 40 个变量。所以,是的,我可以一次将方程应用于一个变量。

标签: matlab file-io netcdf


【解决方案1】:

这里有一些“空气代码”可以帮助您入门。

% The variable we want to process
myVarName = 'ws_80';

for yr=1961:2000
  inFile  = strcat('lffd',num2str(yr),'_V10M_Iberia.nc'); 
  disp(inFile);
  outFile = strcat('lffd',num2str(yr),'_V10M_Iberia_processed.nc'); 
  disp(outFile);

  % copy input file to create a template for the output
  copyDone = copyfile(inFile, outFile,'f');
  if(~copyDone)
      error('Could not copy file');
  end

  % Read variable
  inVar = ncread(inFile,myVarName);

  % Replace this line with your equation
  outVar = myProcessFunction(inVar);

  % Write variable
  ncwrite(outFile, myVarName, outVar);

end

您必须对其进行修改以适应您的目标。试一试,然后回复您遇到的问题。

【讨论】:

  • 您的“航空代码”完全符合我的所有需求。非常感谢。我非常感谢你。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-05-23
  • 1970-01-01
  • 1970-01-01
  • 2020-03-13
  • 2014-04-12
  • 2023-04-02
  • 1970-01-01
相关资源
最近更新 更多