How to loop and extract particular variable from multiple nc files in a folder and merge them together in a sinlgle one.nc file?

2 views (last 30 days)
Hi Folks,
I have chlor_a 8 day data in .nc format from 2015 to 2021. How can I extract chlor_a variable from each file and make a one single .nc file
I need chlor_a data for particular lat[0 30] long[30 100] then how can I analyse and calculate climatalogy for 8 day data for 6 years
file ='A20190012019008.L3m_8D_CHL_chlor_a_4km.nc'
following figure shows the 'chlor_a' variable.
I hope to have final .mat file/ .nc file as total (at least) = '720*720*264'
I have used the following codes but finding error
filesdir = '/media/nerci/New Volume/karthik/chla globe/2019';
dir_info = dir( fullfile(filesdir, '*.nc') );
numberoffiles = length(dir_info);
filenames = fullfile( filesdir, {dir_info.name} );
ncvars = {'lat','lon','chlor_a'};
lat = cell(numberoffiles, 1);
lon = cell(numberoffiles, 1);
chlor_a = cell(numberoffiles, 1);
for K = 1 : numberoffiles
this_file = filenames{K};
lat= ncread(this_file, ncvars{1});
lon= ncread(this_file, ncvars{2});
chlor_a= ncread(this_file, ncvars{3});
[lat, lon, chlor_a] = meshgrid(lat,lon,chlor_a);
latlonchlor_a_grid=[lat(:) lon(:) chlor_a(:)]
end
[lat, lon] = meshgrid(lat,lon);
latlon_grid=[lat(:) lon(:)]
end
ncfiles = dir('*.nc') ;
N = length(ncfiles) ;
% loop for each file
figure
hold on
for i = 1:N
ncfile = ncfiles(i).name ;
lon = ncread(ncfile,'lon') ;
lat = ncread(ncfile,'lat') ;
chlor_a = ncread(ncfile,'chlor_a') ;
scatter(lon,lat,1,chlor_a,'filled')
end
[X,Y] = meshgrid(lon,lat) ;
xi = linspace(30,100,1000) ;
yi = linspace(0,30,1000) ;
[Xi,Yi] = meshgrid(xi,yi) ;
iwant = interp2(X,Y,chlor_a',Xi,Yi)' ;
scatter(lon,lat,1,chlor_a,'filled')
pcolor(Xi,Yi,iwant') ; shading interp ;
colorbar
kindly help
regards

Answers (0)

Categories

Find more on Conway's Game of Life in Help Center and File Exchange

Products


Release

R2016b

Community Treasure Hunt

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

Start Hunting!