Extracting Data from Over 600 .nc Files...

1 view (last 30 days)
Hi all!
Here's a tough one I'm hoping to receive some guidance on: I'm trying to average a specific variable over six hundred days. That is, I have 600 .nc files from different dates which I have to look into, extract data from, and ultimately work with.
The variable itself is specific humidity (qv), and it is of size 1 x 4, where each column represents: longitude, latitude, pressure level, and time of day (timestep). I have figured out how to read qv for all pressure levels at a specific location and a specific timestep using ONE .nc file, but I would like to automate the process to do this (i.e. read the variable) for all 600 days using all 600 .nc files. Any pointers or suggestions? Thanks in advance!!!
Here's what I have so far:
merra = ('MERRA2_100.inst6_3d_ana_Np.19800115.SUB.nc')
ncdisp(merra)
qv = []
latitude = ncread(merra, 'lat')
longitude = ncread(merra, 'lon')
time = ncread(merra, 'time')
level = ncread(merra, 'lev')
for i = 1:size(level)
i
z = ncread(merra, 'QV', [130, 38, i, 1], [1, 1, 1, 1]) %the 130 is longitude, the 38 is latitude...while 1 is timestep
qv = [qv; z]
end
qv
%the variable 'qv' is an array of size 21 x 1, representing values of
%specific humidity at 21 different pressure levels.

Accepted Answer

Jan
Jan on 11 Apr 2021
Folder = 'C:\Your\Data\Folder';
FileList = dir(fullfile(Folder, '*.nc'));
for iFile = 1:numel(FileList)
merra = fullfile(FileList(iFile).folder, FileList(iFile).name);
... your code comes here
end
Do not reset qv to the empty matrix, but pre-allocate the output and use 2 indices:
Folder = 'C:\Your\Data\Folder';
FileList = dir(fullfile(Folder, '*.nc'));
qv = zeros(21, numel(FileList));
for iFile = 1:numel(FileList)
...
for i = ...
qv(i, iFile) = ncread(merra, 'QV', [130, 38, i, 1], [1, 1, 1, 1]);
end
end
This is tricky:
for i = 1:size(level)
size() replies a vector. Then 1:size(level) uses the 1st element of the size vector and ignores the rest. If this is really wanted, prefer to do this explicitly:
for i = 1:size(level, 1)
If you mean the number of elements, use
for i = 1:numel(level)
  3 Comments
Jan
Jan on 11 Apr 2021
Then expand qv:
qv = zeros(21, numel(fileList), 21);
...
qv(i, iFile, :) = ncread(merra, 'QV', [130, 38, i, 1], [1, 21, 1, 1])
Michelle De Luna
Michelle De Luna on 13 Apr 2021
@Jan: Thank you! You've been very helpful. Wishing you a great week!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!