ncwrite issue + loop through many netcdf files

5 Ansichten (letzte 30 Tage)
Julia
Julia am 30 Okt. 2014
Kommentiert: Ashish Uthama am 12 Nov. 2014
I've looked through some of the questions already answered regarding nccreate and ncwrite, but I still find myself stumped. Would be very appreciative if someone could explain some components of the documentation in layman's terms. (I'm still relatively new to matlab and it goes over my head).
Ultimately, I am trying to go through many netcdf files, modify a variable and rewrite the the variable onto new netcdfs. I am more unsure about the ncwrite part than the loop, but I haven't gotten to checking on my loop part yet. The netcdf files differ only by the year in the name....
Some of the code was written here with the help from a previously answered question on stackoverflow.
myVarName = 'uwnd';
for yr=1948:1950
inFile = strcat('uwnd.',num2str(yr),'.nc');
disp(inFile);
outFile = strcat('uwnd.',num2str(yr),'_.nc');
disp(outFile);
% Read variable
inVar = ncread(inFile,myVarName); %read in the uwnd variable
lev=ncread(inFile,'level'); %read in level
lev=find(lev==250); %look for the level that i'll be using
lat=ncread(inFile,'lat'); %read in the latitude
lat = find(lat>=-90 & lat<=0); %look for lat that i'll be using
% Replace this line with your equation
outVar = squeeze(inVar(:,lat,lev,:)); %make final variable "inVar" with correct lat, level, all lon and all time
uwnd_=outVar;
% Write variable
nccreate('Outfile','uwnd_');
ncwrite('Outfile', 'uwnd_', 'outVar');
end
I believe that I am missing something for the start,stride in ncwrite, but I don't really understand the documentation for that yet.
Any words of advice would be helpful. Thank you!

Akzeptierte Antwort

Kelly Kearney
Kelly Kearney am 30 Okt. 2014
When you create a new variable in the file, you'll probably need to specify the dimensions for the new variable. For example, to save a 5 x 10 array to a netcdf file:
nccreate('myfile.nc', 'myvar', 'dimensions', {'rows', 5, 'cols', 10});
ncwrite('myfile.nc', 'myvar', rand(5,10));
The start and stride options will only be applicable if you're writing to a specific hyperslab, rather than writing the full variable all at once. For example, to write one row at a time, you need to specify where in the file to start writing:
for ii = 1:5
ncwrite('myfile.nc', 'myvar', ones(1,10)*ii, [ii 1]);
end
The stride is necessary if you're writing non-contiguous data; otherwise it defaults to 1.
  1 Kommentar
Ashish Uthama
Ashish Uthama am 12 Nov. 2014
If you have additional attributes you need from the original file, look at ncinfo and ncwriteschema combo. It will help you replicate structure (not data though).

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by