pro mk_orb_week,year,week,orbit,asca=asca
;+
; NAME:
;	MK_ORB_WEEK
; PURPOSE:
;       Generates a weeks worth of orbital information to be stored in 
;	the ORB file. Interval used is 300 secs.
;       Structure defined in orb_struct.pro
; CALLING SEQUENCE:
;       mk_orb_week,year,week,orb_data
; INPUT:
;	year
;	week
; OUTPUT:
;	orb_data  - structure containing the orbital information
; OPTIONAL KEYWORD INPUT:
;       asca      - If set, generate data for the ASCA spacecraft
;
; HISTORY:
;       Written Sep-96 by R.D.Bentley
;
;-

;		setup to run the Fortran program DSNFIL

interval = 300		;seconds

pr_week2tim,week,year=year,t1,t2
ndays = fix(addtime(t2,diff=t1)/(60.*24.))+1
nrec  = ((24*ndays)*60L*60L)/interval

tarr = week2ex(year, week)
;;tarr = addtime(tarr,delt=-interval/60.)	;no need to step back!!

;	input is: YY, MN, DD, HH, MM, SS, Interval, n_interval
param = [reverse(tarr(1:6)), interval, nrec]
;
outdir = concat_dir(getenv('ydb'), 'ref_scratch')
;
print, 'Running RUN_DSNFIL: ', param
run_dsnfil, param, path=outdir, file_out=aoslos_file, asca=asca, /verbose


;		read in file, strip out required part and write ORB file

print,' '
print,'Reading from: ',aoslos_file
rd_orbit_event,aoslos_file,out
help,out,nrec
;;nrec = n_elements(out)

;	there is a rounding error in the fortran programme - tidy up times
extime = anytim2ex(out.timarr)
q59 = where(extime(2,*) eq 59)
if q59(0) ge 0 then extime(*,q59) = addtime(extime(*,q59),delt=1./60.)
times = anytim2ints(extime)

;;times = anytim2ints(out.timarr)
;;print,fmt_tim(times(nrec-10:nrec-1))

;	do a check printout of time, alt, long, lat, rigidity
ftimes = fmt_tim(times)
for j=0,9 do print,ftimes(j),out(j).sat_pos(1:3),out(j).rig,format='(a,x,4f7.1)'

;	
orb_struct, orb_rec=orb_rec

orbit = replicate(orb_rec,nrec)

orbit.time 	= times.time
orbit.day  	= times.day
orbit.lat  	= out.sat_pos(3)
orbit.long 	= out.sat_pos(2)
orbit.height	= out.sat_pos(1)
orbit.radius	= out.sat_pos(0)
orbit.rig	= out.rig
orbit(where(byte(out.saa) ne 32)).saa = 1	;32 == space
orbit(where(byte(out.sc_day) ne 32)).sc_day= 1

;	only have required records in the file!!
;;nmax = 2016
;;nmax = ndays*(86400L/interval)
;;orbit = orbit(1:nmax)	;only record for the days of the week of interest

print,'** Range of times in this ORB file:  ',nrec
fmt_timer,orbit

;	remove the aoslos file
;;spawn,'ls -l '+aoslos_file,resp & print,resp
cmd = 'rm '+aoslos_file
print,' '
print,'Removing file: ',aoslos_file
spawn, cmd, resp & print,resp

return
end
