clear all
close all

% PURPOSE: generate images for SST, All Surface Temperature (no atm
% correction), and Chla from NOAA ERDDAP site

% Select directory where to save plots/data
path_plots=['/Users/Mau/Documents/MBARIprojects/DielCruise/Deployment_conditions/'];
mission=['DielCruise'];

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

%%  All Surface Temperature
% load(urlwrite('https://coastwatch.pfeg.noaa.gov/erddap/griddap/jplMURSST41.mat?analysed_sst[(2002-06-01T09:00:00Z)][(-89.99):1000:(89.99)][(-179.99):1000:(180.0)]', 'test.mat'));

% Define map boundaries
westl=-126;
eastl=-121.225';

southl=35.5;
northl=37.675;

% Selet variable from coastwatch
varname='sst'; units='C';
dataset='erdATsstdhday_LonPM180';

% URL=['https://coastwatch.pfeg.noaa.gov/erddap/griddap/',dataset,'.mat?',...
%     var,'%5B(2021-11-03T18:17:00Z)%5D%5B(0.0)%5D%5B(',...
%     southl,'):(',northl,')%5D%5B(',westl,'):(',eastl,')%5D&.draw=surface&.vars=longitude%7Clatitude%7Csst&.colorBar=%7C%7C%7C14%7C18%7C&.bgColor=0xffccccff'];

% Working URLs
% % all sst (in case it's too cloudy)
% URL='https://coastwatch.pfeg.noaa.gov/erddap/griddap/erdATastnhday_LonPM180.graph?temperature%5B(2021-10-19T04:28:00Z)%5D%5B(0.0)%5D%5B(35.5):(37.675)%5D%5B(-125):(-121.225)%5D&.draw=surface&.vars=longitude%7Clatitude%7Ctemperature&.colorBar=%7C%7C%7C12%7C17%7C&.bgColor=0xffccccff';
% 
% % all temperature (no cloud correction)

URL='https://coastwatch.pfeg.noaa.gov/erddap/griddap/erdATastnhday_LonPM180.mat?temperature%5B(2022-06-17T04:41:00Z)%5D%5B(0.0)%5D%5B(35.5):(37.675)%5D%5B(-125.0):(-121.225)%5D&.draw=surface&.vars=longitude%7Clatitude%7Ctemperature&.colorBar=%7C%7C%7C12%7C17%7C&.bgColor=0xffccccff';
load(urlwrite(URL,'all_temp.mat'));
varname='all_temp';

% % sst daytime
URL='https://coastwatch.pfeg.noaa.gov/erddap/griddap/erdATsstdhday_LonPM180.mat?sst%5B(2022-06-17T04:41:00Z)%5D%5B(0.0)%5D%5B(35.5):(37.675)%5D%5B(-126.0):(-121.225)%5D&.draw=surface&.vars=longitude%7Clatitude%7Csst&.colorBar=%7C%7C%7C12%7C17%7C&.bgColor=0xffccccff';
load(urlwrite(URL,'sst_daytime.mat'));
varname='sst_day';

% % sst nightime
URL='https://coastwatch.pfeg.noaa.gov/erddap/griddap/erdATsstnhday_LonPM180.mat?sst%5B(2022-06-17T04:41:00Z)%5D%5B(0.0)%5D%5B(35.5):(37.675)%5D%5B(-126.0):(-121.225)%5D&.draw=surface&.vars=longitude%7Clatitude%7Csst&.colorBar=%7C%7C%7C12%7C17%7C&.bgColor=0xffccccff';
load(urlwrite(URL,'sst_nigthtime.mat'));
varname='sst_night';

% % CHLA from VIRS
% URL='https://coastwatch.pfeg.noaa.gov/erddap/griddap/erdVHNchla1day.mat?chla%5B(2021-09-28T12:00:00Z)%5D%5B(0.0)%5D%5B(38.32125):(34.90125)%5D%5B(-124.14375):(-120.72375)%5D&.draw=surface&.vars=longitude%7Clatitude%7Cchla&.colorBar=%7C%7CLog%7C%7C10%7C&.bgColor=0xffccccff';
% load(urlwrite(URL,'chla.mat'));
% varname='chla';
% 

%% Specify date to save plot

Date='2022-06-17';

load(urlwrite(URL,'sst.mat'));

% Plot data
    
LON=erdATsstnhday_LonPM180.longitude;
LAT=erdATsstnhday_LonPM180.latitude;
VAR=erdATsstnhday_LonPM180.sst;

% LON=erdVHNchla1day.longitude;
% LAT=erdVHNchla1day.latitude;
% VAR=erdVHNchla1day.chla;

imagesc(LON, LAT, squeeze(VAR), [0 32])
set(gca, 'YDir', 'normal')


%% Map SST
close all

% Grid for REGION
[Y,X]=meshgrid(LAT,LON);
X=flipud(X');
Y=flipud(Y');
Z=squeeze(VAR);

mapMB_3(X,Y,Z,2,eastl,westl,southl,northl)
hold on
colormap(flipud(cbrewer('div','RdBu',21)))

colormap(flipud(cbrewer('div','Spectral',21)))
caxis([11 17])

cmin=nanmin(Z)
h=colorbar('vert','fontsize',18);%40); 
set(get(h,'Title'),'String',['[',units,']'],'fontsize',20);

% Plot vector velocities:
% % m_quiver(X,Y,U,V,'k')
% m_vec(1,X,Y,U,V)

% % Plot glider track (for reference)
% glider_track='/Users/Mau/Documents/MBARIprojects/Yui/glider_deployment_Feb2021/Mar21/glider_lat_lon.txt';
% G=load(glider_track);
% glat=G(:,1);
% glon=G(:,2);
% 
% m_plot(glon,glat,'color','k','MarkerFaceColor','w',...
%       'Marker','o','markersize',10); %'linewidth',2)%3) %'-.','color','w','markersize',4);
% % m_text(M1(:,1),M1(:,2),' M1','vertical','top'); 

title([varname,' ',Date])

print_setup(40,30,16)
print([path_plots,'/',varname,'_',mission,'_',Date,'.jpg'],'-djpeg');

