loop to read netcdf files

15 visualizaciones (últimos 30 días)
Maite M.
Maite M. el 3 de Ag. de 2016
Editada: Maite M. el 4 de Ag. de 2016
hi, I have hundreds of netcdf files to work and I want to generate a loop to read all of them and save the result in other folder. The result it is a cut (a smaller region of interest) and then I will calculate the mean value. I have done the code for a single file and it works:
ncid1=netcdf.open('ESACCI-SOILMOISTURE-L3S-SSMV-COMBINED-20100101000000-fv02.2.nc', 'NC_NOWRITE');
varname = netcdf.inqVar(ncid1,4);
varid = netcdf.inqVarID(ncid1,varname);
data = netcdf.getVar(ncid1,varid);
lon1=netcdf.getVar(ncid1,0,0,1440);
lat1=netcdf.getVar(ncid1,1,0,720);
[longrid,latgrid]=meshgrid(lon1,lat1);
data2=data((longrid>-1)&(longrid<0)&(latgrid>39)&(latgrid<40));
(the mean value I will calculate it when I have all the files cut). I have found some information on previous answers but I am still not able to solve it. It works until sentence "%Now do whatever you want with this file name" because it is copied from internet and I don't know how to continue:
% Specify the folder where the files are.
myFolder = 'G:\DATA_validation\ESA\data\daily_files\COMBINED\v02.2\2010\files';
% Check to make sure that folder actually exists. Warn user if it doesn't.
if ~isdir(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s', myFolder);
uiwait(warndlg(errorMessage));
return;
end
% Get a list of all files in the folder with the desired file name pattern.
filePattern = fullfile(myFolder, '*.nc');
theFiles = dir(filePattern);
for k = 1 : length(theFiles)
baseFileName = theFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
% Now do whatever you want with this file name
ncid=netcdf.open(theFiles(k), 'NC_NOWRITE');
varname = netcdf.inqVar(ncid,4);
varid = netcdf.inqVarID(ncid,varname);
data(k) = netcdf.getVar(ncid,varid);
end
Thanks in advanced, I'm not good coding... if you don't understand me just ask!

Respuesta aceptada

Maite M.
Maite M. el 4 de Ag. de 2016
Editada: Maite M. el 4 de Ag. de 2016
just in case someone need it, I could solve it and I share the code, I haven't finished yet, but my doubt is solved as:
% Specify the folder where the files are.
myFolder = 'G:\SWICCA\DATA_validation\ESA\data\daily_files\COMBINED\v02.2\2010\files';
% Check to make sure that folder actually exists. Warn user if it doesn't.
if ~isdir(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s', myFolder);
uiwait(warndlg(errorMessage));
return;
end
filePattern = fullfile(myFolder, '*.nc');
theFiles = dir(filePattern);
% Loop for each nc-file
for i = 1:length(theFiles)
baseFileName = theFiles(i).name;
fullFileName = fullfile(myFolder, baseFileName);
% fprintf(1, 'Now reading %s\n', fullFileName);
% ncdisp(ncfiles(i).name) ;
ncid=netcdf.open(fullFileName, 'NC_NOWRITE');
varname= netcdf.inqVar(ncid,4);
varid = netcdf.inqVarID(ncid,varname);
% data = netcdf.getVar(ncid,varid);
netcdf.close(ncid);
end

Más respuestas (1)

KSSV
KSSV el 3 de Ag. de 2016
% Read the names of nc-files in the folder
ncfiles = dir('*.nc') ;
Nfiles = length(ncfiles) ; % Total number of nc files
% Loop for each nc-file
for i = 1:Nfiles
% display the nc file
ncdisp(ncfiles(i).name) ; % call the nc file by ncfiles(i).name
% do what you want %
end
In the above I am displaying nc-files in the loop; you can do what ever you want instead. hope the above hint helps you.
  2 comentarios
Maite M.
Maite M. el 3 de Ag. de 2016
thank you a lot for answering, it's much simpler now but still I can't read the files...
for i = 1:Nfiles
ncid=netcdf.open('ncfiles(i)', 'NC_NOWRITE');
varname = netcdf.inqVar(ncid,4);
varid = netcdf.inqVarID(ncid,varname);
data(i) = netcdf.getVar(ncid,varid);
end
How should I write the inside part of the loop? I want to read every file and save each one as a matrix "data" in other folder. This matrix should contain only "varname" data.
KSSV
KSSV el 3 de Ag. de 2016
You make your code to read single variable into a function and call it inside the loop.

Iniciar sesión para comentar.

Categorías

Más información sobre Startup and Shutdown en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by