读取多个netcdf文件-matlab

读取多个netcdf文件-matlab,matlab,file-io,netcdf,Matlab,File Io,Netcdf,我有40个名为“lffd1961_V10M_Iberia.nc”、“lffd1962_V10M_Iberia”的netcdf文件lffd2000_V10M_Iberia' 我想在同一个程序中打开所有这些文件,在所有文件中获得相同的变量,在该变量中应用一些等式,并创建40个新的netcdf文件,修改该变量 为了打开我开发的40个文件,这段代码很有效 for yr=1961:2000 inFile=strcat('lffd',num2str(yr),'_V10M_Iberia.nc'); disp(

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

我想在同一个程序中打开所有这些文件,在所有文件中获得相同的变量,在该变量中应用一些等式,并创建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);

我很难用你们所有人都能理解的方式来解释这一点,所以请随便问你想要什么。

这里有一些“航空代码”让你开始

% 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

您必须修改此项以适应您的目标。试一试,然后在你被卡住的地方发帖。

你被卡住了,需要帮助的地方是哪里?。我不清楚你的方程式是否需要一个变量中所有文件的所有数据。如果没有,最好一次处理一个文件(读取、应用公式、写入)。使用更高级别的函数,如ncread、ncinfo、ncwriteschema和ncwrite可能会更容易。您能按部分帮助我吗?我的第一个循环打开了所有的.nc文件,运行良好。现在我需要某种循环来获取第四个位置“netcdf.getVar(nc,4);”中的所有变量在每个文件中。我的下一步是在我的工作区中有40个变量(每个文件一个)。你的版本有ncread函数吗?再说一遍,你真的需要全部40个变量来应用这个方程吗?或者你能一次将方程应用于一个变量吗?是的,我的版本有这个函数。我不需要40个变量来应用这个方程。我需要在这40个变量上应用这个方程。所以,是的,我可以一次对一个变量应用这个方程。你的“航空代码”完全符合我的所有需要。非常感谢你。我非常感谢你。