how can I read multiple netcdf file using ncread in matlab?
Mostrar comentarios más antiguos
I have 6000 files netcdf files.In each file there are four variables like latitude, longitude,precipitation and time.
I made a variable
filename=dir(location of folder in which files are)
now I want to read data from each 6000 files for every variable I write
for j=3:length(filename)
a=ncread('\folder',filename(j).name,variable);
end
but files are not being read. can u tell me how can I read these files?
the format of file is 3B42_Daily.20020315.7.nc4
Respuestas (2)
Walter Roberson
el 2 de Sept. de 2017
ncvars = {'latitude', 'longitude', 'precipitation', 'time'};
projectdir = 'C:\some\location';
dinfo = dir( fullfile(projectdir, '*.nc4') );
num_files = length(dinfo);
filenames = fullfile( projectdir, {dinfo.name} );
lats = cell(num_files, 1);
lons = cell{num_files, 1);
precips = cell(num_files, 1);
times = cell(num_files, 1);
for K = 1 : num_files
this_file = filenames{K};
lats{K} = ncread(this_file, ncvars{1});
lons{K} = ncread(this_file, ncvars{2});
precips{K} = ncread(this_file, ncvars{3});
times{K} = ncread(this_file, ncvars{4});
end
10 comentarios
Farshid Daryabor
el 8 de Abr. de 2020
thanks
Michelle De Luna
el 1 de Ag. de 2020
Editada: Michelle De Luna
el 1 de Ag. de 2020
Hi @Walter Roberson! I know this is a long shot, but I was hoping to see if you could help me adapt this code to a situation where each of my .nc files has a 4x1 column vector. That is, there are four timesteps in each of the .nc files, and I am trying to combine all of the corresponding data values from all of my .nc files into one large file that I can work with in the future.
Walter Roberson
el 5 de Ag. de 2020
Your .nc file has only one variable of interest, and it is always a 4 x 1 result in each file?
How are your files named? In particular if they are numbered files, then we need to know if the numbers have leading zeros -- are the files data1.nc data2.nc ... data10.nc data11.nc ..., etc., or are they data001.nc, data002.nc, data003.nc, ... data010.nc, data011.nc, and so on ? If there are no leading zeros but they are numbered, then we have to compensate a bit to arrange them in the correct order.
Benjamin Osborne
el 23 de Dic. de 2021
Hi Walter, how would one adapt this code to enable it to plot the 'lats' 'lons' and 'precips' data as individual subplots in a figure using the pcolor function?
Walter Roberson
el 23 de Dic. de 2021
ntimes = max(cellfun(@length, times));
for K = 1 : num_files
these_times = times{K};
for T = 1 : length(these_times)
%plots are numbered along rows, reverse of usual way!!
subplot(num_files, ntimes, sub2ind([num_files, ntimes], T, K));
pcolor(lons{K}, lats{K}, precip{K}(:,:,T));
title( filenames{K} + " " + string(these_times(T)) );
xtitle('long'); ytitle('lat');
end
end
James Wyatt
el 4 de En. de 2022
@Walter Roberson this has been a great help, thank you!
Do you know of a way to have this program go through multiple folders to search for and combine the .nc files? I have similar data spread over multiple folders that I would like joined.
Walter Roberson
el 4 de En. de 2022
The modification to the code I posted several years ago would be
projectdir = 'C:\some\location';
dinfo = dir( fullfile(projectdir, '**', '*.nc4') );
num_files = length(dinfo);
filenames = fullfile( {dinfo.folder}, {dinfo.name} );
The '**' I used means "all sub-folders at any depths". If you want to use all the folders that are immediate children of the parent directory then use '*' instead of '**'
And how do I modify this in a way that I only substract the precips with timeseries for a location (thus one value for lat and lon) for all different .nc files?
Tsehaye Negash
el 14 de Jun. de 2023
This program is not working.
Walter Roberson
el 14 de Jun. de 2023
What error message are you encountering?
Dushantha Sandaruwan WIJENDRA NAIDHELAGE
el 21 de Feb. de 2023
Editada: Walter Roberson
el 14 de Jun. de 2023
This code can be used to substract the specific region from the set of netcdf files
clear all;clc
av_file=dir('*.nc');
ncdisp(av_file(1).name);
lon1=ncread(av_file(1).name,'lon');
lat1=ncread(av_file(1).name,'lat');
% temp1=zeros(141,121,365);
b=[1982:2021];
count=0;
for i=1:40
a=['sst.day.mean.',num2str(b(i)),'.nc'];
sst0=double(ncread(a,'sst'));
a1=find(lon1>=40&lon1<=110);
b1=find(lat1>=-10&lat1<=30);
lon_ind=lon1(a1);lat_ind=lat1(b1);
arb=sst0(a1,b1,:);
[~,~,nt]=size(lhflx1);
sst_final(:,:,count+1:count+nt)=arb(:,:,:);
count=count+nt;
end
1 comentario
DGM
el 21 de Feb. de 2023
How is this an answer to the question?
Categorías
Más información sobre NetCDF 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!