function  timeplt ( jd, y, pltype, grid )
%function  timeplt ( jd,  y, pltype, grid )
% Plots y vs month and day on x axis.
% jd        decimal Julian days
% Y         vector of data values to plot
% Pltype    1 for big, 0 for small
% Grid      if there is an entry,  overlays a grid on the plot

%   Maxine Jones - 14 May 1991 - for Rocky Geyer

%            X0    Y0    Lxaxis   Lyaxis
tab(1,:) = [.074  .100    .896     .845];  % PLOTB
tab(2,:) = [.090  .140    .870     .790];  % PLOTL

if nargin==2,
  pltype=1;
end
gtime=gregorian(jd(1));
year=gtime(:,1);m=gtime(:,2);d=gtime(:,3);
h=hms2h(gtime(:,4),gtime(:,5),gtime(:,6));
jd0=julian(year,1,1);
yearday=jd-jd0;
%
monstr = ['JAN';'FEB';'MAR';'APR';'MAY';'JUN'];
monstr = [monstr;'JUL';'AUG';'SEP';'OCT';'NOV';'DEC'];
clg; 
trow = 1;
if pltype == 0
   trow = 2; 
end

xmin = floor(yearday(1)); xmax = ceil(yearday(length(yearday)));

[yadd,ymin,ymax] = deltax(y,5);     % get min, max, big tic diff
ytic = ymin:yadd/10:ymax;  nytic = length(ytic); 
dely = tab(trow,4)/(nytic-1)*10;    % 'sc' distance between big y tics

xx = [xmin xmax xmax xmin; xmax xmax xmin xmin];  % x coords of box
yy = [ymin ymin ymax ymax; ymin ymax ymax ymin];  % y coords of box

axis([xmin xmax ymin ymax])          % set axis limits
hold on                              % hold to allow overplotting
plot(xx, yy, 'w-')                    % draw the box

ndays = xmax - xmin;
daylen = tab(trow,3)/ndays;          % x length of 1 day
if ndays<14
    xtic =xmin:3/24:xmax;
    xadd = 1;
elseif ndays <= 180
   xtic = [xmin:xmax];               % x axis tic values every day
   xadd = 7;                         % days between big x tics
elseif ndays <= 360
   xtic = [xmin:7:xmax];             % x axis tic values every week
   xadd = 14;                        % days between big x tics 
else 
   xtic = [xmin:30:xmax];             % x axis tic values every month
   xadd = 60;                        % days between big x tics 
end
delx = daylen * xadd;                % distance between big x tics

% Draw small tics on x and y axes     
% x tic length = .008 in 'sc', y tic length = .007 in 'sc'

[dx,dy] = sc2dc([.500 .507],[.5000 .508]);
lxtic = diff(dy);  lytic = diff(dx);  
plot([xtic;xtic], [ymin;ymin+lxtic]*ones(xtic),'w-') % lower x axis
plot([xtic;xtic], [ymax;ymax-lxtic]*ones(xtic),'w-') % upper x axis
plot([xmin;xmin+lytic]*ones(ytic), [ytic;ytic],'w-') % left  y axis
plot([xmax;xmax-lytic]*ones(ytic), [ytic;ytic],'w-') % right y axis

% Make up vectors of big tics and draw them

xtic = xmin:xadd:xmax; nxtic = length(xtic);
ytic = ymin:yadd:ymax; nytic = length(ytic);
plot([xtic;xtic], [ymin;ymin+2*lxtic]*ones(xtic/xtic(1)),'w-') % lower x axis
plot([xtic;xtic], [ymax;ymax-2*lxtic]*ones(xtic/xtic(1)),'w-') % upper x axis
plot([xmin;xmin+2*lytic]*ones(ytic), [ytic;ytic],'w-') % left  y axis
plot([xmax;xmax-2*lytic]*ones(ytic), [ytic;ytic],'w-') % right y axis
gtime=gregorian(jd0+xtic);
yr=gtime(:,1);m=gtime(:,2);d=gtime(:,3);
h=hms2h(gtime(:,4),gtime(:,5),gtime(:,6));
monthday=[m(:) d(:)]; 
%
% Label large x-axis tics

mon = 0;
xday = tab(trow,1)-.006:delx:1; 
yday = tab(trow,2)-.04; ymon = tab(trow,2)-.08;
for i=1:nxtic
   day = int2str(monthday(i,2));
   if length(day) == 1
      xd = xday(i);
   else
      xd = xday(i) -0.006;
   end
   text(xd, yday, day, 'sc')
   if monthday(i,1) ~= mon              % check for month change
      if ndays < 180                    % align month name with day
         text(xd, ymon, monstr(monthday(i,1),:), 'sc')
      else                              % center month name
%        don't draw 1st month name if day is in last 1/2 of month
         if monthday(i,2) < 15
            xmon = (15 - monthday(i,2)) * daylen;   % dist to 15th
            text(xd+xmon-.012, ymon, monstr(monthday(i,1),:), 'sc')
         end
      end
      mon = monthday(i,1);               % save current month #
   end
end

% Label large y-axis tics

xd = tab(trow,1)-.055;  yd = tab(trow,2)-.015:dely:1;
for i=1:nytic
   ytex = sprintf('%5.1f',ytic(i));        % format y value
   text(xd, yd(i), ytex, 'sc')             % draw yaxis value
end

plot(yearday,y)
if nargin==5
   plot([xtic;xtic], [ymin;ymax]*ones(xtic), ':')
   plot([xmin;xmax]*ones(ytic), [ytic;ytic], ':')
end
hold off

