Viewing contents of file '../idllib/idl_5.2/lib/caldat.pro'
; $Id: caldat.pro,v 1.7.6.1 1999/01/16 16:37:36 scottm Exp $
;
; Copyright (c) 1992-1999, Research Systems, Inc.  All rights reserved.
;	Unauthorized reproduction prohibited.
;

;+
; NAME:
;	CALDAT
;
; PURPOSE:
;	Return the calendar date and time given julian date.
;	This is the inverse of the function JULDAY.
; CATEGORY:
;	Misc.
;
; CALLING SEQUENCE:
;	CALDAT, Julian, Month, Day, Year, Hour, Minute, Second
;	See also: julday, the inverse of this function.
;
; INPUTS:
;	JULIAN contains the Julian Day Number (which begins at noon) of the 
;	specified calendar date.  It should be a long integer.
; OUTPUTS:
;	(Trailing parameters may be omitted if not required.)
;	MONTH:	Number of the desired month (1 = January, ..., 12 = December).
;
;	DAY:	Number of day of the month.
;
;	YEAR:	Number of the desired year.
;
;	HOUR:	Hour of the day
;	Minute: Minute of the day
;	Second: Second (and fractions) of the day.
;
; COMMON BLOCKS:
;	None.
;
; SIDE EFFECTS:
;	None.
;
; RESTRICTIONS:
;	Accuracy using IEEE double precision numbers is approximately
;	1/10000th of a second.
;
; MODIFICATION HISTORY:
;	Translated from "Numerical Recipies in C", by William H. Press,
;	Brian P. Flannery, Saul A. Teukolsky, and William T. Vetterling.
;	Cambridge University Press, 1988 (second printing).
;
;	DMS, July 1992.
;	DMS, April 1996, Added HOUR, MINUTE and SECOND keyword
;	AB, 7 December 1997, Generalized to handle array input.
;-
;


pro CALDAT_SCALAR, Julian, Month, Day, Year, Hour, Minute, Second
  ; Internal variant of CALDAT that does the actual work on a single
  ; value.

  ON_ERROR, 2		; Return to caller if errors

  IGREG = 2299161L	;Beginning of Gregorian calendar

  jul = long(julian + .5d)	;Better be long
  f = julian + .5d - jul
  if f ne 0.0 then begin	;Get hours, minutes, seconds.
    hour = floor(f * 24.)
    f = f - hour / 24.d
    minute = floor(f*1440)
    second = (f - minute/1440.d0) * 86400.0d0
  endif else begin
    hour = 0L
    minute = 0L
    second = 0L
  endelse
   
   
  if jul ge igreg then begin
    jalpha = long(((jul - 1867216) - 0.25d0) / 36524.25)
    ja = jul + 1 + jalpha - long(0.25d0 * jalpha)
  endif else ja = jul

  jb = ja + 1524
  jc = long(6680.0 + ((jb-2439870)-122.1)/365.25)
  jd = long(365 * jc + (0.25 * jc))
  je = long((jb - jd) / 30.6001)

  day = jb - jd - long(30.6001 * je)
  month = je -1
  if (month gt 12) then month = month - 12
  year = jc - 4715
  if month gt 2 then year = year - 1
  if year le 0 then year = year - 1
end







pro CALDAT, Julian, Month, Day, Year, Hour, Minute, Second

  ON_ERROR, 2		; Return to caller if errors

  ; Determine shape of input and construct longword output variables of
  ; the same shape.

  s = size(julian)
  if (s[0] eq 0) then begin
    ; Julian is scalar. Just call CALDAT_SCALAR and pass our arguments through.
    CALDAT_SCALAR, Julian, Month, Day, Year, Hour, Minute, Second
    return
  endif

  ; It's an array. Construct result variables
  ndim = s[0]		; Number or array dimensions
  n = s[ndim + 2]	; # of elements 
  s[ndim + 1] = 3	; Change the type to LONG
  MONTH = (DAY = (YEAR = (HOUR = (MINUTE = (SECOND = MAKE_ARRAY(SIZE=s))))))

  ; Loop over the input
  for i = 0, n-1 do begin
    CALDAT_SCALAR, julian[i], month_tmp, day_tmp, year_tmp, $
	hour_tmp, minute_tmp, second_tmp
    month[i]  = month_tmp
    day[i]    = day_tmp
    year[i]   = year_tmp
    hour[i]   = hour_tmp
    minute[i] = minute_tmp
    second[i] = second_tmp
  endfor
end