%animateNY.m
%
%Program to create the frames for a movie of data from New York. Most of the 
%data is in a file called movieData.mat, created with the program 
%createMovieFile; also requires the bathymetry data file model_grid_ll.
%Runs M_Map.
%
%Soupy Alexander, 5/14/02
%Motivated by vel_arrows.m from Courtney Harris

%Pull in the data
load C:\NewYork\NewYorkCD\Data_Files\Tri_Moor_Data\matlab\movieData.mat

wind_scale = 0.0007;
tide_scale = 0.00007;
current_scale = 0.00007;

for ti = 5:length(time_base);
    
figure(ti)
hold on

%Set up the mercator projection with M_Map
%Problem with the coastline data?
m_proj('mercator','longitudes',[-74.4 -73],'latitudes',[39.8 40.8]);
m_grid('tickdir','out','box','on','gridtoggle','off');

%Put in the wave base data
mud_color = [(217/255) (191/255) (0/255)];
mud=[mud_color; 1 1 1];
colormap(mud);
if (isfinite(wbase(ti)) & wbase(ti) > 0)
 [c,h]=m_contourf(xbathyg, ybathyg, -1*zbathygo,[0 wbase(ti)],'k');
end

m_usercoast('NewYorkCoast','patch',...
    [(0/255) (190/255) (0/255)],'edgecolor','k');

%Put in the bathymetry
[c,h]=m_contour(xbathyg, ybathyg, -1*zbathygo,[10:10:100],'k');
c_lbl =[
  -74.0700   39.8599    10
  -73.6300   40.5780    10
  -73.9568   39.8699    20
  -73.5661   40.4900    20
  -73.7374   39.9360    30
  -73.4177   40.3402    30
  -73.4532   39.8980    40
  -73.3300   40.1499    40
  -73.1100   39.9869    50
];
htxt=m_text(c_lbl(:,1), c_lbl(:,2), num2str(c_lbl(:,3)));
for ii=1:length(htxt)
  set(htxt(ii),'erasemode','normal');
  set(htxt(ii),'color',[0.4 0.4 0.4])
   set(htxt(ii),'fontweight','bold');
end
set(h,'color',[0.4 0.4 0.4])

%%Put in a legend
%Stations
m_plot(-74.36,39.85,'k^','Markersize',8,'MarkerFaceColor','b');
m_text(-74.32,39.85,'Tripod','horizontalalignment','left','fontsize',10)
%Buoys
m_plot(-74.36,39.90,'ko','Markersize',8,'MarkerFaceColor','r');
m_text(-74.32,39.90,'Buoy','horizontalalignment','left','fontsize',10)

%Put in the buoys and stations
dl=0.02;
label=['A';'B';'C';'D';'E';'F'];
m_plot(lon_ambrose,lat_ambrose,'ko','markersize',8,'markerfacecolor','r');
m_plot(lon_buoy,lat_buoy,'ko','markersize',8,'markerfacecolor','r');
m_plot(stat_lon,stat_lat,'k^','markersize',8,'markerfacecolor','b');
for si = 1:6;
    m_text(stat_lon(si)+dl,stat_lat(si)+dl,label(si),...
        'fontweight','bold','fontsize',10);
end

% Put in a scale for wind stress
[leg_x,leg_y] = m_ll2xy(-74.38,39.98);
wind_arr = arrowsafe(leg_x,leg_y,2*wind_scale,90,0.4*wind_scale);
set(wind_arr,'linewidth',2,'color','r');
m_text(-74.39,40.07,'Wind Stress','fontweight','bold');
m_text(-74.36,40.02,'2 N/m^2','fontsize',9);

%Put in the wind stress
[amb_x,amb_y] = m_ll2xy(lon_ambrose,lat_ambrose);
ws_ambrose = arrowsafe(amb_x,amb_y,...
    tau_ambrose_sp(ti)*wind_scale,tau_ambrose_dir(ti),0.4*wind_scale);
set(ws_ambrose,'color','r','linewidth',2);
[buoy_x,buoy_y] = m_ll2xy(lon_buoy,lat_buoy);
ws_buoy=arrowsafe(buoy_x,buoy_y,...
    tau_buoy_sp(ti)*wind_scale,tau_buoy_dir(ti),0.4*wind_scale);
set(ws_buoy,'color','r','linewidth',2);

%Put in a scale for the current velocities
[leg_x,leg_y] = m_ll2xy(-74.38,40.19);
leg = arrowsafe(leg_x,leg_y,20*current_scale,90,4*current_scale);
set(leg,'color','b','linewidth',2);
[leg_x,leg_y] = m_ll2xy(-74.38,40.12);
leg = arrowsafe(leg_x,leg_y,20*current_scale,90,4*current_scale);
set(leg,'color','g','linewidth',2);
m_text(-74.39,40.27,'Current','fontweight','bold');
m_text(-74.36,40.215,'Shallow: 20 cm/s','fontsize',9);
m_text(-74.36,40.15,'Deep: 20 cm/s','fontsize',9);

%Put in the current velocities
for si = 1:6;
    [stat_x,stat_y] = m_ll2xy(stat_lon(si),stat_lat(si));
    stat_surf = arrowsafe(stat_x,stat_y,...
        stat_surf_sp(ti,si)*current_scale,stat_surf_dir(ti,si),...
        4*current_scale);
    set(stat_surf,'linewidth',2)
    stat_deep = arrowsafe(stat_x,stat_y,...
        stat_deep_sp(ti,si)*current_scale,stat_deep_dir(ti,si),...
        4*current_scale);
    set(stat_deep,'linewidth',2,'color','g')
end

%Put in a scale for the tide data
[leg_x,leg_y] = m_ll2xy(-74.38,40.33);
h=sticksafe(leg_x,leg_y,20*tide_scale,90);
set(h,'linewidth',2,'color','m')
yd=get(h,'ydata');
xd=get(h,'xdata');
set(h,'ydata',yd(1:2));
set(h,'xdata',xd(1:2));
set(h,'marker','+');
m_text(-74.39,40.42,'Sea Level','fontweight','bold');
m_text(-74.36,40.36,'20 mbar','fontsize',9);

%Put in the Sandy Hook tide data
if isnan(tlev_SH(ti)) == 0;
[SH_x,SH_y] = m_ll2xy(-1*sand_lon,sand_lat);    
tideSH = sticksafe(SH_x,SH_y,tlev_SH(ti)*tide_scale*100,90);
set(tideSH,'linewidth',2,'color','m');
yd=get(tideSH,'ydata');
xd=get(tideSH,'xdata');
set(tideSH,'ydata',yd(1:2));
set(tideSH,'xdata',xd(1:2));
set(tideSH,'marker','+');
end

%Put in the tide data at station C
if isnan(pres_C(ti))==0;
[stat_x,stat_y] = m_ll2xy(stat_lon(3),stat_lat(3));
tideC = sticksafe(stat_x,stat_y,pres_C(ti)*tide_scale,90);
set(tideC,'linewidth',2,'color','m');
yd=get(tideC,'ydata');
xd=get(tideC,'xdata');
set(tideC,'ydata',yd(1:2));
set(tideC,'xdata',xd(1:2));
set(tideC,'marker','+');
end

%%---------------------------------------------------------------------%%
%Put in the tide data in the inset

%Put in the inset for the tidal data
[corn_x,corn_y] = m_ll2xy(-73.4,40.78);
[corn2_x,corn2_y] = m_ll2xy(-73.02,40.4);
tbox = area([corn_x corn2_x],repmat(corn_y,1,2),corn2_y);
set(tbox,'facecolor','w');
m_text(-73.33,40.75,'Sea Level','fontweight','bold',...
    'fontsize',10);

%Put in a scale for the tide data
[leg_x,leg_y] = m_ll2xy(-73.38,40.5);
h=sticksafe(leg_x,leg_y,20*tide_scale,90);
set(h,'linewidth',2,'color','m')
ydx=get(h,'ydata');
xdx=get(h,'xdata');
set(h,'ydata',ydx(1:2));
set(h,'xdata',xdx(1:2));
set(h,'marker','+');
m_text(-73.36,40.545,'40','fontsize',8);
m_text(-73.36,40.515,'mbar','fontsize',8);

m_line([-73.22 -73.10],[40.6 40.6],'linestyle','-.','color','k');
%Put in the tide data for station C
if isnan(pres_C(ti))==0;
[C_x,C_y] = m_ll2xy(-73.13,40.6);
tideC = sticksafe(C_x,C_y,pres_C(ti)*0.5*tide_scale,90);
set(tideC,'linewidth',2,'color','m');
ydc=get(tideC,'ydata');
xdc=get(tideC,'xdata');
set(tideC,'ydata',ydc(1:2));
set(tideC,'xdata',xdc(1:2));
set(tideC,'marker','+');
end
m_text(-73.07,40.65,'C','fontsize',8);

%Sandy Hook tide data
if isnan(tlev_SH(ti)) == 0;
[SH_x,SH_y] = m_ll2xy(-73.22,40.6);    
tideSH = sticksafe(SH_x,SH_y,tlev_SH(ti)*0.5*100*tide_scale,90);
set(tideSH,'linewidth',2,'color','m');
yd=get(tideSH,'ydata');
xd=get(tideSH,'xdata');
set(tideSH,'ydata',yd(1:2));
set(tideSH,'xdata',xd(1:2));
set(tideSH,'marker','+');
end
m_text(-73.376,40.66,'Sandy','fontsize',8);
m_text(-73.376,40.63,'Hook','fontsize',8);

if isnan(pres_C(ti))==0;
l2 = plot([xd(2) xdc(2)],[yd(2) ydc(2)]);
set(l2,'color','k')
end
%%-----------------------------------------------------------------------%%

%Put in the title of the plot
dtvec=gregorian(time_base(ti));
title(sprintf('New York Bight Experiment: %g / %7.2f / %g %g:%g', ...
      dtvec(2),dtvec(3)+(dtvec(4)+dtvec(5)/60)/24,...
      dtvec(1)), ...
    'fontweight','bold','fontsize',14);

%Define frame name and save as ppm file
frame_name = ['frame' num2str(ti) '.ppm'];
ppmwrite(frame_name);
close

end
