failure Reading Variables from multiple Netcdf files

조회 수: 2 (최근 30일)
Tianchu Lu
Tianchu Lu 2022년 7월 11일
댓글: Tianchu Lu 2022년 8월 2일
I am trying to read multiple different variables that is within a netcdf file, and almost extract the number that is associated with the variable that is listed. so what i have done is for it to loop for each variable, but i get an error displaying that one of the variable is not found even though it is on the nc file if i do ncdisp. could anyone help or optimise the code a little?
many thanks
ncvars={'duration','tecmin','tecmax','elevmin','elevmax','elev_tecmax','alt_elevmax','lat_elevmax','lon_elevmax','alt_tecmax','lat_tecmax','lon_tecmax','alt_start','lat_start','lon_start','alt_stop','lat_stop','lon_stop'};
projectdir= 'F:\cosmic_2_data_file';
dinfo=dir(fullfile(projectdir,'*.0001_nc'));
num_files=length(dinfo);
filenames=fullfile(projectdir,{dinfo.name});
durations=cell(num_files,1);
tecmins=cell(num_files,1);
tecmaxs=cell(num_files,1);
elevmins=cell(num_files,1);
elevmaxs=cell(num_files,1);
elevtecmaxs=cell(num_files,1);
altelevmaxs=cell(num_files,1);
latelevmaxs=cell(num_files,1);
lonelevmaxs=cell(num_files,1);
alttecmaxs=cell(num_files,1);
lattecmaxs=cell(num_files,1);
lontecmaxs=cell(num_files,1);
altstarts=cell(num_files,1);
latstarts=cell(num_files,1);
lonstarts=cell(num_files,1);
altstops=cell(num_files,1);
latstops=cell(num_files,1);
lonstops=cell(num_files,1);
for i=1 : num_files
this_file=filenames{i};
durations{i}=ncread(this_file,ncvars{1});
tecmins{i}=ncread(this_file,ncvars{2});
tecmaxs{i}=ncread(this_file,ncvars{3});
elevmins{i}=ncread(this_file,ncvars{4});
elevmaxs{i}=ncread(this_file,ncvars{5});
elevtecmaxs{i}=ncread(this_file,ncvars{6});
altelevmaxs{i}=ncread(this_file,ncvars{7});
latelevmaxs{i}=ncread(this_file,ncvars{8});
lonelevmaxs{i}=ncread(this_file,ncvars{9});
alttecmaxs{i}=ncread(this_file,ncvars{10});
lattecmaxs{i}=ncread(this_file,ncvars{11});
lontecmaxs{i}=ncread(this_file,ncvars{12});
altstarts{i}=ncread(this_file,ncvars{13});
latstarts{i}=ncread(this_file,ncvars{14});
lonstarts{i}=ncread(this_file,ncvars{15});
altstops{i}=ncread(this_file,ncvars{16});
latstops{i}=ncread(this_file,ncvars{17});
lonstop{i}=ncread(this_file,ncvars{18});
end
there are about 9400 .0001_nc files so i am hoping it can loop through and read the variables that i want and almost record that number and save it as a new cell.
  댓글 수: 4
Walter Roberson
Walter Roberson 2022년 7월 25일
Please show the output of
ncinfo(this_file)
for the first file.
Tianchu Lu
Tianchu Lu 2022년 8월 2일
ncvars={'TEC','elevation','time','x_LEO','y_LEO','z_LEO','x_GPS','y_GPS','z_GPS'};
projectdir= "F:\podtc2_data\podTc2_nrt_2022_004";
dinfo=dir(fullfile(projectdir,'*.0001_nc'));
num_files=length(dinfo);
filenames=fullfile(projectdir,{dinfo.name});
TEC_podtc2s=cell(num_files,1);
Elevation_podtc2s=cell(num_files,1);
times=cell(num_files,1);
x_LEOs=cell(num_files,1);
y_LEOs=cell(num_files,1);
z_LEOs=cell(num_files,1);
x_GPSs=cell(num_files,1);
y_GPSs=cell(num_files,1);
z_GPSs=cell(num_files,1);
for i=1 : num_files
this_file=filenames{i};
TEC_podtc2s{i}=ncread(this_file,ncvars{1});
Elevation_podtc2s{i}=ncread(this_file,ncvars{2});
times{i}=ncread(this_file,ncvars{3});
x_LEOs{i}=ncread(this_file,ncvars{4});
y_LEOs{i}=ncread(this_file,ncvars{5});
z_LEOs{i}=ncread(this_file,ncvars{6});
x_GPSs{i}=ncread(this_file,ncvars{7});
y_GPSs{i}=ncread(this_file,ncvars{8});
z_GPSs{i}=ncread(this_file,ncvars{9});
end

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

채택된 답변

Tianchu Lu
Tianchu Lu 2022년 8월 2일
ncvars={'TEC','elevation','time','x_LEO','y_LEO','z_LEO','x_GPS','y_GPS','z_GPS'};
projectdir= "F:\podtc2_data\podTc2_nrt_2022_004";
dinfo=dir(fullfile(projectdir,'*.0001_nc'));
num_files=length(dinfo);
filenames=fullfile(projectdir,{dinfo.name});
TEC_podtc2s=cell(num_files,1);
Elevation_podtc2s=cell(num_files,1);
times=cell(num_files,1);
x_LEOs=cell(num_files,1);
y_LEOs=cell(num_files,1);
z_LEOs=cell(num_files,1);
x_GPSs=cell(num_files,1);
y_GPSs=cell(num_files,1);
z_GPSs=cell(num_files,1);
for i=1 : num_files
this_file=filenames{i};
TEC_podtc2s{i}=ncread(this_file,ncvars{1});
Elevation_podtc2s{i}=ncread(this_file,ncvars{2});
times{i}=ncread(this_file,ncvars{3});
x_LEOs{i}=ncread(this_file,ncvars{4});
y_LEOs{i}=ncread(this_file,ncvars{5});
z_LEOs{i}=ncread(this_file,ncvars{6});
x_GPSs{i}=ncread(this_file,ncvars{7});
y_GPSs{i}=ncread(this_file,ncvars{8});
z_GPSs{i}=ncread(this_file,ncvars{9});
end
  댓글 수: 2
Walter Roberson
Walter Roberson 2022년 8월 2일
is the problem solved?
Tianchu Lu
Tianchu Lu 2022년 8월 2일
yes thank you so much for your time. could you help me on the other question I just posted with the ongoing project?
thank you for your time

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by