Analyse Time Series Data based on Seasonality

6 views (last 30 days)
Hello everyone,
I'm trying to analyse seasonal dataset from 2015 to 2022. I have total 88 'nc' file. file name as "H08_20190301_0000_1MSST200_FLDK.06001_06001.ncI". I aims to seperate data into four seasons (e.g., Spring from March to May). I used coding below to store all data into cell structure.
My problem is I don't know how to scanning the file name to seperate and integrate data into season. For example, this data name ""H08_20190301_0000_1MSST200_FLDK.06001_06001.ncI" provides time in 20190301 belonging to Spring. I need to read specific date time to store all data with same season into one strucuture. Please help me to deal with this. Thank in advance!
pth = 'C:\Users\LongHoang\OneDrive\Desktop\Spring_2023\Ocean_Atmosphere_Mathlab\Mid_Term\H8_SST\H8_SST\';
ncvars = {'lat', 'lon', 'sea_surface_temperature'};
dinfo = dir( fullfile(pth, '*.nc') );
num_files = length(dinfo);
filenames = fullfile( pth, {dinfo.name} );
name = fullfile({dinfo.name}); %I have cell file to store all file name but don't know how to scan the file to get
% date time
lats = cell(num_files, 1);
lons = cell(num_files, 1);
ssts = cell(num_files, 1);
for k = 1: num_files
this_file = filenames{k};
Name_file = name{k};
lats{k} = ncread(this_file, ncvars{1});
lons{k} = ncread(this_file, ncvars{2});
ssts{k} = ncread(this_file, ncvars{3});
end
  1 Comment
Peter Perkins
Peter Perkins on 6 Apr 2023
Edited: Peter Perkins on 6 Apr 2023
Long, it's not clear what are in your files. Does each file contain one lat-by-lon SST array for each season in each year? That sounds wrong, because that would be 8*4=32 files, not 88. Does each file contain daily SST values, i.e. lat-by-lon-by-day, over some range, like monthly files?

Sign in to comment.

Accepted Answer

Mathieu NOE
Mathieu NOE on 28 Mar 2023
Edited: Mathieu NOE on 28 Mar 2023
hello
I wished I had some files to test the code , but let's go directly to the task
I simply extracted from the filename the number corresponding to the month , then you make a simple test to store the data in the correct array depending of the month number
I was lazy so I created explicitely one array per season like lats_spring , lats_summer, lats_fall , lats_winter
but you could change that to a structure or cell array if you prefer
also for each season instead or storing in separate cells you might want simply to concatenate the data and have one unique array at the end (your preference ?)
could be something like this :
pth = 'C:\Users\LongHoang\OneDrive\Desktop\Spring_2023\Ocean_Atmosphere_Mathlab\Mid_Term\H8_SST\H8_SST\';
ncvars = {'lat', 'lon', 'sea_surface_temperature'};
dinfo = dir( fullfile(pth, '*.nc') );
num_files = length(dinfo);
filenames = fullfile( pth, {dinfo.name} );
name = fullfile({dinfo.name}); %I have cell file to store all file name but don't know how to scan the file to get
% date time
lats = cell(num_files, 1);
lons = cell(num_files, 1);
ssts = cell(num_files, 1);
k1 = 0; % "winter" counter init
k2 = 0; % "spring" counter init
k3 = 0; % "summer" counter init
k4 = 0; % "fall" counter init
for k = 1: num_files
this_file = filenames{k};
Name_file = name{k};
% new code
fulldate = split(Name_file,'_');
fulldate = fulldate{2}; % assuming the date appears always in second position in the filename (between '_')
monthsnumber = str2num(fulldate(5:6)); % to have the month corresponding to the file
if monthsnumber >= 12 && monthsnumber <= 2 % winter
k1 = k1 + 1; % counter
lats_winter{k1} = ncread(this_file, ncvars{1});
lons_winter{k1} = ncread(this_file, ncvars{2});
ssts_winter{k1} = ncread(this_file, ncvars{3});
end
if monthsnumber >= 3 && monthsnumber <= 5 % spring
k2 = k2 + 1; % counter
lats_spring{k2} = ncread(this_file, ncvars{1});
lons_spring{k2} = ncread(this_file, ncvars{2});
ssts_spring{k2} = ncread(this_file, ncvars{3});
end
if monthsnumber >= 6 && monthsnumber <= 8 % summer
k3 = k3 + 1; % counter
lats_summer{k3} = ncread(this_file, ncvars{1});
lons_summer{k3} = ncread(this_file, ncvars{2});
ssts_summer{k3} = ncread(this_file, ncvars{3});
end
if monthsnumber >= 9 && monthsnumber <= 11 % fall
k4 = k4 + 1; % counter
lats_fall{k4} = ncread(this_file, ncvars{1});
lons_fall{k4} = ncread(this_file, ncvars{2});
ssts_fall{k4} = ncread(this_file, ncvars{3});
end
end
  6 Comments
Mathieu NOE
Mathieu NOE on 28 Jun 2023
Hello
Problem solved ?
would you mind accepting my answer ? thanks !
Long
Long on 18 Aug 2023
Edited: Long on 18 Aug 2023
I had accepted your answer. Sorry for some inconvience stuff.

Sign in to comment.

More Answers (0)

Categories

Find more on Data Type Identification in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!