function [times, Pass_Times] = monthly(Time, ranges)
%
%  MONTHLY --- This function will generate a time (year and fraction of a year) at
%              the middle of every month in an interval. Arguments passed in are:
%
%  Time       - The time range of the data set.
%
%  The returned arguments are:
%
%  times      - The time in years (and fractions, see note below) for all hits.
%  Pass_Times - This is the vector of time with the year, day, hour and minute
%               of each hit. Note that the day here is the calendar day; i.e.,
%               the value that would be obtained by taking the fraction of the
%               year, multiplying by 365 (or 366 for a leap year) AND adding 1.
%
% Note Jan 1, year day 001 is decimal day 0, so we need to be careful.
% January 1 starts at midnight on the 365/366th day of the previous year.
% Yearday 0 starts at midnight on the 365/366th day of the previous year.
% Need two times, the one that Deidre uses in the interface, argout1, 
% (year here) and the one that is used to specify the JPL day, argout2
% (yearday here).
%
%--------------------------------------------------------------------------------
%

% The preceding empty line is important.
%
% $Id: monthly.m,v 1.6 1998/11/09 20:26:56 dbyrne Exp $

% $Log: monthly.m,v $
% Revision 1.6  1998/11/09 20:26:56  dbyrne
% *** empty log message ***
%
% Revision 1.3  1998/11/05 16:01:53  root
% Changed LonVector and LatVector to be used if no Lon returned and tried the update function.
% DataScale and DataNull should now be optional!
%
% Revision 1.4  1998/09/13 21:31:12  dbyrne
% Updated to elminate global variables and add multivariables.
%
% Revision 1.2  1998/09/09 15:04:29  dbyrne
% Eliminating all global variables.
%
% Revision 1.1  1998/05/17 14:18:06  dbyrne
% *** empty log message ***
%
% Revision 1.1.1.1  1997/09/22 14:13:54  tom
% Imported Matlab GUI sources to CVS
%

StartTime = max(Time(1), ranges(4,1));
EndTime = min(Time(2), ranges(4,2));

YEAR = floor(StartTime);

if rem(YEAR,4) ~= 0 | YEAR == 1900
  decday = 1.0/365.0;                % This is not a leapyear.
  MidMonth = [ 15 46 74 105 135 166 196 227 258 288 319 349 400];
  % gm code
  MonthLastDay = [31 59 90 120 151 181 212 243 273 304 334 400];
  % end gm code
else
  decday = 1.0/366.0;                % This is a leapyear.
  MidMonth = [ 15 46 75 106 136 167 197 228 259 289 320 350 400];
  % gm code
  MonthLastDay = [31 60 91 121 152 182 213 244 274 305 335 400];
  % end gm code
end

MidMonth = MidMonth - 1;

StartDay = (StartTime - YEAR) / decday;

iMonth = 1;

%while MidMonth(iMonth) > StartDay
% gm code
while StartDay > MonthLastDay(iMonth)
  % end gm code
  iMonth = iMonth + 1;
end
if iMonth >= 13
  iMonth = 1;
  YEAR = YEAR + 1;
end
times = [];
Pass_Times = [];

times(1) = YEAR + MidMonth(iMonth) * decday;
yearday = (times(1) - floor(times(1))) / decday + 1; 
hour = (yearday - floor(yearday)) * 24.0;
minute = (hour - floor(hour)) * 60.0;
Pass_Times = [ floor(times(1)), floor(yearday), floor(hour), minute]; 
day_cnt = 1;
timecheck = times(1);
while timecheck < EndTime
  if day_cnt > 1
    times(day_cnt) = timecheck;
    yearday = (times(day_cnt) - floor(times(day_cnt))) / decday + 1; 
    hour = (yearday - floor(yearday)) * 24.0;
    minute = (hour - floor(hour)) * 60.0;
    Pass_Times = [ Pass_Times; floor(times(day_cnt)), floor(yearday), ...
	  floor(hour), minute]; 
  end
  day_cnt = day_cnt + 1;
  iMonth = iMonth + 1;
  if iMonth > 12
    iMonth = 1;
    YEAR = YEAR + 1;
  end
  timecheck = YEAR + MidMonth(iMonth) * decday;
end
i = find(times >= StartTime & times <= EndTime);
times = times(i);
Pass_Times = Pass_Times(i,:);
return
