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

%% PURPOSE:
% Map SSH data for glider deployments and tracking of eddies/frontal
% features from gridded altimetry data in NRT

% NOTE: gridded SSH is not very reliable to identify eddies (lots of
% spurious data due to interpolation of very narrow swaths from altimeters)
% BUT once identified in SST, SSH can provide a means of locating/keeping
% track of eddy location (that is not subject to cloud cover)

%% Select path to save plots

DD='2022-06-17'; % DeploymentDate/MissionName
path_plots=['/Users/Mau/Documents/MBARIprojects/DielCruise/Deployment_conditions/',DD];
mission=['DielCruise-',DD];

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


%% Path to NRT SSH data from NOAA site

% if readung data from local folder
% path_data='/Users/Mau/Documents/MBARIprojects/ssh/NOAA_NRT';
% files=dir([path_data,'/*.nc']);
% f=8; % select which file in directory to plot
% filename=files(f).name;
% filename=files(end).name; % or uncomment to plot last file in directory


% if reading data from OPENDAP server
path_data= 'https://www.star.nesdis.noaa.gov/thredds/dodsC/AltimetryBlendedGlobal/2022/';
%url='https://www.star.nesdis.noaa.gov/thredds/dodsC/AltimetryBlendedGlobal/2022/rads_global_nrt_sla_20220615_20220616_001.nc';
filename='rads_global_nrt_sla_20220615_20220616_001.nc';
url=[path_data,filename];

% read date from filename
Date=filename(21:end-7);
Datei=Date(1:8);
Datef=Date(10:17);

% read lat lon data
latitude=ncread([path_data,'/',filename],'latitude');
longitude=ncread([path_data,'/',filename],'longitude'); %range is -180 to 180
res=0.25;

% Set LIMITS of REGION in geographic coordinates: % lon range [-180 180]

southl=34;
northl=38;

westl=-126;
eastl=-120;

lat1=searchclosest_m(latitude,southl);
lat2=searchclosest_m(latitude,northl);
lon1=searchclosest_m(longitude,westl);
lon2=searchclosest_m(longitude,eastl);
        
% Subset REGION
% startLoc=[nlon1 nlat1];
% count=

% Load SLA data (for the entire globe, low res dataset)
varname='sla';
long_name = "Sea Level Anomalies";
units = 'm';
sla=ncread([path_data,'/',filename],varname); %,startLoc,count); %lonxlat

%
u=ncread([path_data,'/',filename],'ugos'); %,startLoc,count); %lonxlat
v=ncread([path_data,'/',filename],'vgos'); %,startLoc,count); %lonxlat

% Select REGION
[Y,X]=meshgrid(latitude(lat1:lat2),longitude(lon1:lon2));
X=flipud(X');
Y=flipud(Y');
Z=flipud(sla(lon1:lon2,lat1:lat2)');

U=flipud(u(lon1:lon2,lat1:lat2)');
V=flipud(v(lon1:lon2,lat1:lat2)');

%% Map SSH
close all

% fig = figure;
% fig.Units = 'centimeters';% 'normalized'
% fig.PaperPosition=[.25 .25 20 16]; %[left bottom width height].
% % need this for m_vec, but can't get the arrow to appear in the plot

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

caxis([-0.2 0.2])
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,' ',Datei,'-',Datef])

print_setup(40,30)
print([path_plots,'/SeaLevelAnomaly_',filename(13:end-3),'_',mission,'.jpg'],'-djpeg');


%% Map geostrophic currents from SSH
close all

W=sqrt(U.^2+V.^2);
% fig = figure;
% fig.Units = 'centimeters';% 'normalized'
% fig.PaperPosition=[.25 .25 20 16]; %[left bottom width height].
% % need this for m_vec, but can't get the arrow to appear in the plot

mapMB_3(X,Y,W,2,eastl,westl,southl,northl)

% Colorbar
cmap = flipud(cbrewer('div','Spectral',22));
colormap(cmap)

caxis([0 0.5])
h=colorbar('vert','fontsize',18);%40); 
set(get(h,'Title'),'String',['[m/s]'],'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,' ',Datei,'-',Datef])


print_setup(40,30)

print([path_plots,'/GeostrophicCurrents_',filename(13:end-3),'_',mission,'.eps'],'-depsc2');
print([path_plots,'/GeostrophicCurrents_',filename(13:end-3),'_',mission,'.jpg'],'-djpeg');


