loop to read netcdf files
Mostrar comentarios más antiguos
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
Más respuestas (1)
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.
Categorías
Más información sobre Startup and Shutdown en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!