% Explore CMEMS data - GlobColour
% Sources:
% https://resources.marine.copernicus.eu/product-detail/OCEANCOLOUR_GLO_CHL_L4_REP_OBSERVATIONS_009_082/DATA-ACCESS
% https://resources.marine.copernicus.eu/product-detail/OCEANCOLOUR_GLO_OPTICS_L4_REP_OBSERVATIONS_009_081/DATA-ACCESS
%
%
% Jessie Turner 
% Original code January 2022
% Updated to plot a time series of L4 chl from daily interpolated product
% Updated September 2022 to add 2021-2022 (one year) to analysis.
% New data go from 9-1997 to 8-2022.
% Important: Renamed newest downloaded file to match others.
% Original name:
% cmems_obs-oc_glo_bgc-plankton_my_l4-gapfree-multi-4km_P1D_1662413113718.nc
% Renamed to:
% dataset-oc-glo-bio-multi-l4-chl_interpolated_4km_daily-rep_1650000000001.nc
%
% Updated code October 2023
% Updated to adjust Chl-a concentration to account for "factor of 2"
% underestimation by satellites in WAP region (Dierssen & Smith 2000).
% Uses function "chl_4thorderpolynomial.m"
% Saves dataset as "daily_chl_CMEMS_WAP_v3.mat"
%
% Version 4 updated January 2024
% To include 2022-2023 appended to end of time series
% Downloaded using python API "copernicus_marine_client" subset tool
% Renamed resulting .nc file to match existing files in this directory.
% Saves dataset as "daily_chl_CMEMS_WAP_v4.mat"


folder = '/Users/jsturner/satellite_data/WAP_CMEMS/';

% List files
dir1 = dir(folder);
lf=length(dir1);
filelist=[];
for i=1:lf
    nf=dir1(i).name; tf=contains(nf,'.nc');  % is it an nc file
    yrf = contains(nf,'chl_interpolated_4km_daily'); % does it contain the correct day-of-year for month start days
    if tf==1 && yrf==1 % if this IS an nc file and IS one of those three months
        filelist =[filelist;nf];
    end
end; clearvars lf nf tf yrf

% Load files and concatenate Chl-a data by time
disp('  ');
chl = [];
date_mat = [];
for i = 1:size(filelist,1)
    thisfile = [folder filelist(i,:)];
    chl0 = ncread(thisfile,'CHL');
    if size(chl0,1)==601 % if .nc file has different dimension, should be 600 x 241
        chl0(end,:,:) = [];
    elseif size(chl0,2)==240 % if .nc file has different dimension, should be 600 x 241
        addon = nan(size(chl0,1),1,size(chl0,3));
        chl0 = cat(2,chl0,addon);
    end
    time_days = ncread(thisfile,'time');
    if i==1 % for first file in list, get lat lon
        lon0 = ncread(thisfile,'lon'); % MAKE THIS INTO A 2D ARRAY
        lat0 = ncread(thisfile,'lat'); % MAKE THIS INTO A 2D ARRAY
        lon_chl = repmat(lon0,1,size(chl,2));
        lat_chl = repmat(lat0,1,size(chl,1))';
    end
    % code for coverting days since 1900-01-01 to datenum 
    % function: timesince1900.m
    [~,date_mat0]=timesince1900(time_days);
    
    % NEW v3: adjust chl using 4th order polynomial (Dierssen and Smith
    % 2000):
    chl1 = chl_4thorderpolynomial(chl0);
    
    % save to arrays, concatenate
    chl = cat(3,chl,chl1);
    date_mat = [date_mat; date_mat0];
    disp(['Loaded daily chl file ' num2str(i) ' of ' num2str(size(filelist,1))]);
end; clearvars thisfile *0
whos date_mat chl
% 
% Write a metadata string and save to .mat file
metadata =['This file is concatenated daily interpolated (cloud-free) '...
    'chl-a concentration for the western Antarctic Peninsula region for '...
    '1997-09 to 2023-12. This file was created by Jessie Turner '...
    '(jturner@uconn.edu on ' datestr(now,'yyyy-mm-dd')];
disp('Saving...');
save('daily_chl_CMEMS_WAP_v4.mat','date_mat','chl','metadata','-v7.3');

