clear all
close all
addpath(genpath('/Users/Mau/Documents/MATLABhelp_new'));

%% PURPOSE: Plot Anchor BGC data from CPF .mat files (PARK structure)
%  NOTE: .mat files saved from process_CPFprofiles.m and process_CPFanchor.m
%  by Magdalena Carranza

%% Path to data (CPF mission)

mission='ParagonTests/2022-3-14T16-30-13/'; % 6h cycle mission, failed to profile every 6h
% MC, profiles that have Anchor data (~6h), 001,002,,003,005,013

% mission='ParagonTests/2022-2-23T16-59-6/'; % Paragon Test
% mission='TankTests/2022-2-17T21-11-1/'; % TankTest week of Feb14/2022
% mission='ParagonTests/MICRORIDER/2021-11-16T19-16-49/'; % Overnight Mission, south Bay, 19 profiles.
% mission='ParagonTests/MICRORIDER/2021-11-12T18-28-7/'; % Park@Depth Tests
% mission='ParagonTests/MICRORIDER/2021-11-2T17-7-28/'; % Overnight Mission, attempt n2
% mission='TankTests/2021-10-29T0-1-23/'; % to fix SBDs, changed to 5' at Anchor
% mission='ParagonTests/MICRORIDER/OvernightMission/2021-10-21T18-34-19/';
% mission='ParagonTests/MICRORIDER/2021-10-19T16-3-9/'; % 6h mission CPF_topo1
% mission='ParagonTests/MICRORIDER/2021-10-15T16-10-6/'; %CPF-MR PV 0.15m/s
% mission='ParagonTests/MICRORIDER/2021-10-13/'; % ParagonTest, 3 profiles (in different folders).
% mission='ParagonTests/MICRORIDER/2021-10-8T16-19-7/'; % 1st Test on the Paragon of CPF+MR, T1 and SH1 probes in. 4 profiles
% mission='TankTests/MICRORIDERTESTS/2021-10-5T22-38-16/'; % TankTest CPF+MR, SH1 & T1 probes
% mission='TankTests/MICRORIDERTESTS/2021-10-4T22-43-27/'; % TankTest CPF+MR, Test Probes
% mission='ParagonTests/2021-10-1T16-3-9/'; % Deep Dive, 100m 
% mission='ParagonTests/2021Sep27/2021-9-27T18-12-43/'; % w test, turning buoyancy control off at 40m. PV set to 0.25 m/s (2 profiles, untethered)
% mission='ParagonTests/2021Sep27/2021-9-27T18-12-43/'; % w test, turning buoyancy control off at 40m. PV set to 0.25 m/s. first profile (tethered to a small buoy)
% mission='TankTests/2021-9-23T21-25-36/'
% mission='ParagonTests/2021-9-16T16-33-30/';
% mission='TankTests/2021-9-9T21-6-13/';
% mission='TankTests/2021-9-9T17-33-2/';
% mission=['TankTests/2021-9-3T18-48-26/'];
% mission='TankTests/2021-8-25T21-57-38/';
% % mission='TankTests/2021-8-31T16-23-33/';

data_root='/Volumes/CPF/CPF003/Data/matfiles/';
dir_plots=['/Volumes/CPF/CPF003/Data/data_processing/plots/'];
dirs.plots=dir_plots;

path_data=[data_root,mission]; % to mission folder
dirs.mat=path_data;

%extract date from fi=older name:
ddate=regexp(dirs.mat,['(?<year>\d{4})-(?<month>\d+)-(?<day>\d+)'],'names');
dirs.date=[ddate.year,'-',ddate.month,'-',ddate.day]

%% Create directory to save plots (if it doesn't exist already)
subdir=mission;
path_plots=[dir_plots,subdir];

if isfolder(path_plots)==0 && numel(dir(path_plots))<=2 % will create directory only if it is empty!
    mkdir(path_plots)
end

%% List .mat files (output from process_CPF_profiles)
mat_list=dir([dirs.mat,'*.mat']);

%% Loop through files
nf=length(mat_list); % number of files to process

% Preallocate memory
PRES=nan(250,nf); TEMP=PRES; PSAL=PRES;
DOXY=PRES;
CHLA=PRES;
BBP=PRES;
NO3=PRES;
PH=PRES;
OCR1=PRES;
OCR2=PRES;
OCR3=PRES;
OCR4=PRES;
TIME=PRES;
TEMP_DOXY=PRES;

for f=1:nf-1
    mat_file=mat_list(f).name;
    load([path_data,mat_file])
    
    % arrange in matrix for plotting
    nt=length(PARK.PRES);
                                           
    DOXY(1:nt,f)=PARK.DOXY; 
    NO3(1:nt,f)=PARK.NITRATE;
    CHLA(1:nt,f)=PARK.CHLA;
    BBP(1:nt,f)=PARK.BBP700;
    PH(1:nt,f)=PARK.PH_IN_SITU_TOTAL;
    
    OCR1(1:nt,f)=PARK.OCR1;  
    OCR2(1:nt,f)=PARK.OCR2;
    OCR3(1:nt,f)=PARK.OCR3;
    OCR4(1:nt,f)=PARK.OCR4;
    
    PRES(1:nt,f)=PARK.PRES;
    TEMP(1:nt,f)=PARK.TEMP;
    PSAL(1:nt,f)=PARK.PSAL;

    TEMP_DOXY(1:nt,f)=PARK.TEMP_DOXY;

    TIME(1:nt,f)=PARK.TIME;
end
%% ------------  PLOT PARK/ANCHOR BGC DATA ------------------------------------
tname=dirs.date; % mission name for plot labels

TIME(TIME==0)=NaN;

% Convert TIME to local time
LTIME=TIME-7*(1/24);

% Blanking out the first Anchor data point (bug printing first Anchor Pt in
% .msg files). Will eventually get fixed and will ommit the code below
DOXY(1,:)=NaN;
NO3(1,:)=NaN;
CHLA(1,:)=NaN;
BBP(1,:)=NaN;
PH(1,:)=NaN;

OCR1(1,:)=NaN;
OCR2(1,:)=NaN;
OCR3(1,:)=NaN;
OCR4(1,:)=NaN;

PRES(1,:)=NaN;
TEMP(1,:)=NaN;
PSAL(1,:)=NaN;

LTIME(1,:)=NaN;


%% T & S

figure(1)
% Sal
subplot(211)
plot(LTIME,PSAL,'-o')
datetick('x','HH:MM')
% axis ij
% ylim([0 maxp])
ylabel('Sal [psu]')

title(tname);

% Temp
subplot(212)
plot(LTIME,TEMP,'-o')
datetick('x','HH:MM')
% axis ij
% ylim([0 maxp])
ylabel('Temp [{\circ}C]')

print_setup(60,30)
print([path_plots,'TS_Anchor_allfiles.jpg'],'-djpeg'); % '-depsc2');

%% Oxygen Concentration

figure(2)

plot(LTIME,DOXY,'-o')
% plot(LTIME(1:end-1,:),DOXY(2:end,:),'-o') % corrected/aligned
datetick('x','HH:MM')
xlabel('Time')
ylabel('O_2 [\mu mol/kg]')

title(tname);

print_setup(60,20)
print([path_plots,'DOXY_Anchor_allfiles_aligned.jpg'],'-djpeg'); % '-depsc2');

%% NO3

figure(3)

plot(LTIME,NO3,'-o')
datetick('x','HH:MM')
xlabel('Time')
ylabel('NO_3 [\mumol/kg]')

title(tname);

print_setup(60,20)
print([path_plots,'NITRATE_Anchor_allfiles.jpg'],'-djpeg');

%% PH

figure(4)

plot(LTIME,PH,'-o')
datetick('x','HH:MM')
xlabel('Time')
ylabel('PH')

title(tname);

print_setup(60,20)
print([path_plots,'PH_Anchor_allfiles.jpg'],'-djpeg'); % '-depsc2');

%% FLBB

figure(5)

subplot(211)
plot(LTIME,CHLA,'-o')
datetick('x','HH:MM')
xlabel('Time')
ylabel('Chla [mg/m^3]')

title(tname);

subplot(212)
plot(LTIME,BBP,'-o')
datetick('x','HH:MM')
ylabel('Time')
xlabel('Bbp_{700} [m^{-1}]')

print_setup(60,30)
print([path_plots,'FLBB_Anchor_allfiles.jpg'],'-djpeg'); % '-depsc2');

%% OCR

figure(6)

subplot(411)
semilogy(LTIME,OCR1,'-o')
datetick('x','HH:MM')
ylim([0 100])
ylim([0.001 1]) %for tank data
ylabel('OCR_{1} [uW/cm^2/nm]')

title(tname);

subplot(412)
semilogy(LTIME,OCR2,'-o')
datetick('x','HH:MM')
ylim([0.001 1])
ylabel('OCR_{2} [uW/cm^2/nm]')


subplot(413)
semilogy(LTIME,OCR3,'-o')
datetick('x','HH:MM')
ylim([0.001 1])
ylabel('OCR_{3} [uW/cm^2/nm]')

subplot(414)
semilogy(LTIME,OCR4,'-o')
datetick('x','HH:MM')
ylim([0.001 10]) % for in situ data
% xlim([0 10]) %for tank data
xlabel('Time')
ylabel('OCR_{4} [\mumol/m^2/s]') %  Correct units for channel 4 are umol/m^2/sec (moles of photons).

print_setup(100,40)
print([path_plots,'OCR_Anchor_allfiles.jpg'],'-djpeg'); % '-depsc2');


%% %% Oxygen Concentration

figure(7)

subplot(211)
plot(LTIME,TEMP,'-k'); 
datetick('x','HH:MM')
hold on
plot(LTIME,TEMP_DOXY,'--')

xlabel('Time')
ylabel('TEMP [C]')

title(tname);

subplot(212)
plot(LTIME,TEMP-TEMP_DOXY,'--')
datetick('x','HH:MM')
hold on
xlabel('Time')
ylabel('CTD - Optode TEMP [C]')

print_setup(60,30)
print([path_plots,'TEMP_DOXY_CTD_Anchor_allfiles.jpg'],'-djpeg'); % '-depsc2');

%% close all

maxlags=30;

p=5;
nnan=find(~isnan(TEMP(:,p)) & ~isnan(TEMP_DOXY(:,p)))
[r,lags]=xcorr(TEMP(nnan,p),TEMP_DOXY(nnan,p),maxlags,'normalized');

close all
stem(lags,r)

xlabel('Lag (2 min)')
ylabel('CrossCorr CTD T vs Optode T')

print_setup(60,30)
print([path_plots,'CrossCorr_TEMP_CTD_DOXY_Anchor_p',num2str(p),'.jpg'],'-djpeg'); % '-depsc2');
