Extracting Data from Over 600 .nc Files...

조회 수: 1 (최근 30일)
Michelle De Luna
Michelle De Luna 2021년 4월 10일
댓글: Michelle De Luna 2021년 4월 13일
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.

채택된 답변

Jan
Jan 2021년 4월 11일
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
Jan
Jan 2021년 4월 11일
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 2021년 4월 13일
@Jan: Thank you! You've been very helpful. Wishing you a great week!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Elements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by