Question

I have 40 netcdf files named 'lffd1961_V10M_Iberia.nc', 'lffd1962_V10M_Iberia' ... 'lffd2000_V10M_Iberia'.

I want open all of them in same program, get the same variable in all of them, apply some equation in that variable and create 40 new netcdf files with that variable modified.

To open the 40 files I've developed this code and it works.

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

Now I want get the variable number 4 in all of them. This variable is a 3D matrix (70x51x(8760 or 8784)) Something like this next line but with a 40 years loop:

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

and it's supposed to have 40 new variables in my workspace. After this I want to apply this equation in all of that 40 variables:

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

and finally create 40 new netcdf files with my new variable in each file.

something like that:

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);

It's hard for me to explain this in a way that all of you understand so please be confortable to ask whatever you want.

Was it helpful?

Solution

Here is some 'air code' to get you started.

% 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

You will have to modify this to suite your goal. Give it a try and post back on where you get stuck.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top