function CALDATG, Julian, form=form, separator=separator
;+
; NAME:
;	CALDATG
;
; PURPOSE:
;	Return the month, day and year corresponding to a given julian date.
;	This is the inverse of the function JULDAY.
; CATEGORY:
;	Misc.
;
; CALLING SEQUENCE:
;	CALDAT, Julian, Month, Day, Year
;
; INPUTS:
;	JULIAN contains the Julian Day Number (which begins at noon) of the
;	specified calendar date.  It should be a long integer.
; OUTPUTS:
;	MONTH:	Number of the desired month (1 = January, ..., 12 = December).
;
;	DAY:	Number of day of the month.
;
;	YEAR:	Number of the desired year.
;
; COMMON BLOCKS:
;	None.
;
; SIDE EFFECTS:
;	None.
;
; RESTRICTIONS:
;	None.
;
; 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.
;
;	ISTP, Corrected by V.Grechnev to process arrays. Now it is called as a function
;	returning DATE - string-type array.
;-
;
ON_ERROR, 2		; Return to caller if errors

IGREG = 2299161L	;Beginning of Gregorian calendar

julian = long(julian)	;Better be long



  jalpha = long(((julian - 1867216) - 0.25d0) / 36524.25)
  ja = julian + ( 1 + jalpha - long(0.25d0 * jalpha) )*( julian ge igreg )

jalpha=0

jb = temporary(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 = fix(temporary(jb) - temporary(jd) - long(30.6001 * je))
  month = fix(temporary(je) -1)
  month=month - 12*(month gt 12)
  year = fix(temporary(jc) - 4715)
  year=year-(month gt 2)
  year=year-(year le 0)

	if n_elements(form) le 0 then form='yyyymmdd'
	if n_elements(separator) le 0 then separator='/'

space=(byte(' '))(0)

year=byte(strmid(string(year), 4, 4))
index=where(year eq space)
if index(0) ge 0 then year(index)=(byte('0'))(0)
index=0
year=string(year)

month=byte(strmid(string(month), 6, 2))
index=where(month eq space)
if index(0) ge 0 then month(index)=(byte('0'))(0)
index=0
month=string(month)

day=byte(strmid(string(day), 6, 2))
index=where(day eq space)
if index(0) ge 0 then day(index)=(byte('0'))(0)
index=0
day=string(day)

	CASE strlowcase(form) OF

'yyyymmdd': 	Output=year+separator+month+separator+day

'yymmdd': 	Output=strmid(year, 2, 2)+separator+month+separator+day

'ddmmyyyy': 	Output=day+separator+month+separator+year

'ddmmyy': 	Output=day+separator+month+separator+strmid(year, 2, 2)

'mmddyyyy': 	Output=month+separator+day+separator+year

'mmddyy': 	Output=month+separator+day+separator+strmid(year, 2, 2)

	ELSE:	message, 'Unknown form. Returning...'

	ENDCASE

if n_elements(Output) eq 1 then Output=Output(0)

return, Output

end