2014-01-20 42 views
0

我有40个netcdf文件命名为'lffd1961_V10M_Iberia.nc','lffd1962_V10M_Iberia'...'lffd2000_V10M_Iberia'。阅读多个netcdf文件 - matlab

我想在同一个程序中打开所有的程序,在它们中获得相同的变量,在该变量中应用一些公式,并创建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。这个变量是一个三维矩阵(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); 

这是我很难解释这一点,所有的你明白所以请舒适问任何你想要的方式。

+0

您在哪一部分卡住并需要帮助?我不清楚你的方程是否需要来自一个变量的所有文件的所有数据。如果不是,你最好一次处理一个文件(读取,应用等式,写入)。使用ncread,ncinfo,ncwriteschema和ncwrite等更高级别的函数可能会更容易。 –

+0

你能帮我分零件吗? 我的第一个循环,打开我所有的.nc文件都能正常工作。 现在我需要一些循环来获得第四个位置的所有变量'netcdf.getVar(nc,4);'在每个文件。 我的下一步是在我的工作区中有40个变量(每个文件之一)。 – rochinha44

+0

您的版本是否具有ncread功能?再次,你是否真的需要全部40个变量来应用这个方程?或者你可以一次将方程应用于一个变量吗? –

回答

2

下面是一些让你开始使用的“空中密码”。

% 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 

您将不得不对此进行修改以适应您的目标。尝试一下,然后回到你卡住的地方。

+0

你的'空气密码'完全符合我的所有需求。 非常感谢。我非常感谢你。 – rochinha44