How to extract time series averaged over a region?

5 visualizaciones (últimos 30 días)
Keegan Carvalho
Keegan Carvalho el 1 de Mayo de 2022
Comentada: Keegan Carvalho el 18 de Mayo de 2022
Hi All,
I've been working on this monthly chlorophyll dataset where the "chl variable" has the dimensions 489x221x1X24 (lon x lat x depth x time). So basically the 4th dimensions has 24 chlorophyll values corresponding to 24 months from Jan'20 to Dec'21. Now I wanted to extract the chlorophyll values for 24 months of the entire region into a table; in that way I have a time series for further analysis.
lon=ncread("chl2020.nc","longitude");lat=ncread("chl2020.nc","latitude");
chl=ncread("chl2020.nc","chl");
this=squeeze(chl);
% removes the 3rd dimension
latrange= -25:0.25:30;
lonrange= 32:0.25:120;
% Indexing the region
for i=1:length(latrange)
latindex(i)= find(lat==latrange(i));
end
for i=1:length(lonrange)
longindex(i)= find(lon==lonrange(i));
end
%Finding the values for January - 2020
newchl = this(longindex,latindex,1);
%Finding the values for February - 2020
newchl2 = this(longindex,latindex,2);
Now, the last two lines basically extract for each month. However, this is time consuming and I wanted to know how to code a more efficient and quicker way to get all the monthly values averaged over the entire region from Jan '20 - Dec '21. The dataset is below:
My end goal is a 2-D result with Months and Chlorophyll values. I'd be grateful for any assistance in this. Thanks!

Respuesta aceptada

Peter Perkins
Peter Perkins el 18 de Mayo de 2022
Keegan, there's a bunch of ways to do this, here's one that uses a timetable:
>> lat = ncread("chl2020.nc","latitude");
>> lon = ncread("chl2020.nc","longitude");
>> chlor = squeeze(ncread("chl2020.nc","chl"));
>> whos
Name Size Bytes Class Attributes
chlor 489x241x24 22627008 double
lat 241x1 1928 double
lon 489x1 3912 double
>> j = (-25 <= lat) & (lat <= 30);
>> i = (32 <= lon) & (lon <= 120);
>> lat = lat(j);
>> lon = lon(i);
>> chlor = chlor(i,j,:);
>> whos
Name Size Bytes Class Attributes
chlor 353x221x24 14978496 double
i 489x1 489 logical
j 241x1 241 logical
lat 221x1 1768 double
lon 353x1 2824 double
>> Date = datetime(2020,(1:24)',1,Format="MMMyy");
>> Chlor = permute(chlor,[3 2 1]);
>> tt = timetable(Date,Chlor)
tt =
24×1 timetable
Date Chlor
_____ ________________
Jan20 1×221×353 double
Feb20 1×221×353 double
Mar20 1×221×353 double
Apr20 1×221×353 double
May20 1×221×353 double
Jun20 1×221×353 double
Jul20 1×221×353 double
Aug20 1×221×353 double
Sep20 1×221×353 double
Oct20 1×221×353 double
Nov20 1×221×353 double
Dec20 1×221×353 double
Jan21 1×221×353 double
Feb21 1×221×353 double
Mar21 1×221×353 double
Apr21 1×221×353 double
May21 1×221×353 double
Jun21 1×221×353 double
Jul21 1×221×353 double
Aug21 1×221×353 double
Sep21 1×221×353 double
Oct21 1×221×353 double
Nov21 1×221×353 double
Dec21 1×221×353 double
>> tt.MeanChlor = mean(tt.Chlor,2:3,"omitnan")
tt =
24×2 timetable
Date Chlor MeanChlor
_____ ________________ _________
Jan20 1×221×353 double 0.21169
Feb20 1×221×353 double 0.21149
Mar20 1×221×353 double 0.15771
Apr20 1×221×353 double 0.13574
May20 1×221×353 double 0.1589
Jun20 1×221×353 double 0.22037
Jul20 1×221×353 double 0.29464
Aug20 1×221×353 double 0.32263
Sep20 1×221×353 double 0.26623
Oct20 1×221×353 double 0.21402
Nov20 1×221×353 double 0.19245
Dec20 1×221×353 double 0.22103
Jan21 1×221×353 double 0.26072
Feb21 1×221×353 double 0.20912
Mar21 1×221×353 double 0.15732
Apr21 1×221×353 double 0.13859
May21 1×221×353 double 0.16133
Jun21 1×221×353 double 0.23466
Jul21 1×221×353 double 0.30558
Aug21 1×221×353 double 0.32634
Sep21 1×221×353 double 0.27826
Oct21 1×221×353 double 0.22286
Nov21 1×221×353 double 0.19408
Dec21 1×221×353 double 0.21508
  1 comentario
Keegan Carvalho
Keegan Carvalho el 18 de Mayo de 2022
Hi Peter, thanks a ton for this. I've understood where I've missed out; seems the indexing is key here to get things right.
Much appreciated :)
Cheers!

Iniciar sesión para comentar.

Más respuestas (0)

Community Treasure Hunt

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

Start Hunting!

Translated by