function  timeplt ( jd, y, pltype, grid )
%function  timeplt ( jd, y, pltype, grid )
% Plots y vs month and day on x axis.
% Year      2 digit year of data start
% Yearday   vector of data times in Rocky units
% Y         vector of data values to plot
% Pltype    1 for big, 0 for small
%           3 for subplot, upper panel, 4 for subplot, lower panel
% 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
tab(3,:) = [.0832 .6236   .8767    .319];  % subplot 211
tab(4,:) = [.0832 .1223   .8767    .319];  % subplot 211

monstr = ['JAN';'FEB';'MAR';'APR';'MAY';'JUN'];
monstr = [monstr;'JUL';'AUG';'SEP';'OCT';'NOV';'DEC'];
%%%RPS
if(nargin==2),
pltype=1;
end
%  Calculate year and year day 
start=gregorian(jd(1));
year=start(1);
jd0=julian([year 1 1 0 0 0]);
yearday=jd-jd0;
%%%RPS 
if pltype == 0
   trow = 2; 
else; trow=pltype;
end

if pltype<4; clg; end

if pltype==3;  subplot(211); elseif pltype==4; subplot(212);end
xmin = floor(yearday(1)); xmax = ceil(yearday(length(yearday)));

if length(y)>1; 
    if pltype<3 
       [yadd,ymin,ymax] = deltax(real(y),8);     % get min, max, big tic diff
    else
       [yadd,ymin,ymax] = deltax(real(y),4);     % get min, max, big tic diff
    end
else [yadd,ymin,ymax]=deltax([-1 1],1);
end;
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 week
   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
if length(y)>1
  plot([xmin;xmin+lytic]*ones(ytic), [ytic;ytic],'w-') % left  y axis
  plot([xmax;xmax-lytic]*ones(ytic), [ytic;ytic],'w-') % right y axis
end
% 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
if length(y)>1
  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
end
%%RPS
jdi=jd0+(xtic+1);
greg=gregorian(jdi);
monthday = greg(:,[2 3]);
%%RPS
% 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    % modified by rocky to center
   day = int2str(monthday(i,2));
   if length(day) == 1
      xd = xday(i);
   else
      xd = xday(i) -0.006;
   end
   if ndays<30 xd=xd+daylen/2; end;
   if xd<1
   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 |  xadd >  20)
            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  %if xd
end

% Label large y-axis tics

xd = tab(trow,1)-.055;  yd = tab(trow,2)-.015:dely:1;
if length(y)>1;

for i=1:nytic
   if ytic(nytic)<1
     ytex = sprintf('%5.2f',ytic(i));        % format y value
   elseif ytic(nytic)<1000
     ytex = sprintf('%5.1f',ytic(i));        % format y value
   else 
     ytex = sprintf('%5.0f',ytic(i));        % format y value
	end
  text(xd, yd(i), ytex, 'sc')             % draw yaxis value
end

plot(yearday,y); 

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

