Using a wildcard in a string with ncread for complex file names

4 vues (au cours des 30 derniers jours)
Carrie Merritt
Carrie Merritt le 25 Mar 2020
Hi all:
I'm working with MERRA2 monthly files from 199801 to 201412. A set of files with heat variables ('tdt') with pressure levels, and a set of files with T, omega, height, etc ('asm') that is used to regrid the heat variables (hence the following interpolation/regridding function). The goal is to regrid the variables and write to a single netCDF file for each month (204 total).
I tested it out for the first couple of years/months and then ctrl+C’ed it to check that the newly-produced netCDFs looked good. It all worked out great.
So now, my only issue is, where the string of the filename has MERRA2_200.instM_3d_asm_Np. – the ‘200’ part of the file name changes after 36 files with ‘200’ then to
120 files with ‘300’ and then the last 120 files with ‘400’...i.e., the filename is changing also before the year/month is listed. So this works for the first set of files named MERRA2_200... but will not be able to process the remaining files with the names MERRA2_300 or MERRA2_400.
I was thinking this could be resolved with a wildcard (*) by simply doing ‘fname1 = strcat('MERRA2_*00.instM_3d_asm_Np.',f_yr,f_mo,'.nc4.nc4');
however, this doesn’t work and it won’t open for reading (with the following error message):
Error using internal.matlab.imagesci.nc/openToRead (line 1272)
Could not open MERRA2_*00.instM_3d_asm_Np.199801.nc4.nc4 for reading.
Error in internal.matlab.imagesci.nc (line 121)
this.openToRead();
Error in ncread (line 61)
ncObj = internal.matlab.imagesci.nc(ncFile);
Error in MERRA2_regrid_interpv2 (line 11)
LON=ncread(fname1,'lon');
Can anyone think of another way around this? Here's my script:
%Regrid monthly ASM and TDT files, then write to netCDF
for t_yr = 1998:2014 %Loop through and create strings for each year
f_yr = sprintf('%04d',t_yr);
for t_mo = 1:12 %Loop through and create strings for each month
f_mo = sprintf('%02d',t_mo);
%Read in ASM variables %vvvv originally strcat('MERRA2_200.instM....')
fname1 = strcat('MERRA2_*00.instM_3d_asm_Np.',f_yr,f_mo,'.nc4.nc4'); %concatenated string for filename
%^ filename changes from MERRA2_100 to MERRA2_200 etc..just use *?
LON=ncread(fname1,'lon');
LAT=ncread(fname1,'lat');
LEV=ncread(fname1,'lev');
TIME=ncread(fname1,'time');
temp=ncread(fname1,'T');
omega=ncread(fname1,'OMEGA');
qv=ncread(fname1,'QV');
Z=ncread(fname1,'H'); %Do NOT write as a variable
%Read in TDT variables
fname2 = strcat('MERRA2_*00.tavgM_3d_tdt_Np.',f_yr,f_mo,'.nc4.nc');
dtdtmst=ncread(fname2,'DTDTMST');
dtdttrb=ncread(fname2,'DTDTTRB');
dtdtrad=ncread(fname2,'DTDTRAD');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%INTERPOLATE TO HEIGHT LEVELS%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%select whatever height levels for interp
%SLH_lev=(0.125:.25:19.875);
zlvls=125:250:19875;
zlvls=zlvls.'; % to turn into 80x1 double
%pre-allocate space to speed up
hght_zlvls=single(nan(size(Z,1),size(Z,2),length(zlvls),size(Z,4))); %4D even though single files?
temp_zlvls=hght_zlvls;
omega_zlvls=hght_zlvls;
qv_zlvls=hght_zlvls;
dtdtmst_zlvls=hght_zlvls;
dtdttrb_zlvls=hght_zlvls;
dtdtrad_zlvls=hght_zlvls;
%linear interp
for i=1:size(Z,1) %loop through lon
for j=1:size(Z,2) %loop through lat
for t=1:size(Z,4) %loop through time
%%%%%
model_z=squeeze(Z(i,j,:,t));
for k=1:length(zlvls) %loop through each height level (80)
z_lower=find(model_z<zlvls(k),1,'last'); %find where last index of Z is < zlvls
p1=model_z(z_lower); %p1 is corresponding pressure level
z_upper=z_lower+1; %assign upper boundary of each Z level
p2=model_z(z_upper); %p2 is corresponding pressure level
p_range=p2-p1;
w2=(p2-zlvls(k))./p_range;
w1=(1-w2);
if ~isempty(w2)
hght_zlvls(i,j,k,t) = w2.*Z(i,j,z_lower,t) + w1.*Z(i,j,z_upper,t);
temp_zlvls(i,j,k,t) = w2.*temp(i,j,z_lower,t) + w1.*temp(i,j,z_upper,t);
omega_zlvls(i,j,k,t) = w2.*omega(i,j,z_lower,t) + w1.*omega(i,j,z_upper,t);
qv_zlvls(i,j,k,t) = w2.*qv(i,j,z_lower,t) + w1.*qv(i,j,z_upper,t);
dtdtmst_zlvls(i,j,k,t) = w2.*dtdtmst(i,j,z_lower,t) + w1.*dtdtmst(i,j,z_upper,t);
dtdttrb_zlvls(i,j,k,t) = w2.*dtdttrb(i,j,z_lower,t) + w1.*dtdttrb(i,j,z_upper,t);
dtdtrad_zlvls(i,j,k,t) = w2.*dtdtrad(i,j,z_lower,t) + w1.*dtdtrad(i,j,z_upper,t);
end
end
%%%%
end
end
%%%DISPLAY PROGRESS
frac_now =5*(ceil(i/size(Z,1)*100/5));
frac_next=5*(ceil((i+1)/size(Z,1)*100/5));
if frac_next>frac_now; disp([num2str(frac_now) '% completed']); end
end
MERRA2_write_netCDFv2 %executes write script
end
end

Réponses (0)

Catégories

En savoir plus sur Text Data Preparation dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by