pro fmt_rasm,file_in,file_out, path_out=path_out, $
		qtest=qtest, all=all, chmod=chmod, asca=asca
;+
; Name:
;  fmt_rasm
; Purpose:
;  Format a weekly rasm file (from JPL) which contains all spacecraft.
;  Produce an output file which contains only Yohkoh Pass entries.
;
;  Can read solass or 7dayss files.
;  Will write solass or rasmss-stye files.
;
; Calling Sequence:
;  fmt_rasm,file_in,file_out		; Write rasmss (edited) file
;  fmt_rasm,file_in,file_out, /all	; Write solass file
;  fmt_rasm,file_in,file_out, /chmod	; Make output file chmod 666
;  fmt_rasm,file_in,file_out, /asca	; Write asca file from 7dayss
;
; Default output name is the rasmss_fin.wXX.
;
; Normally the following convention is used:
;  Input			Output
;  ----------			----------
;  7dayss.wXX /all		solass.wXX
;  7dayss.wXX /all /asca	astdss.wXX
;  7dayss.wXX			rasmss.wXX
;  7dayss.wXX /asca		rasmss_astd.wXX
;  forecast.wXX /all		solasf.wXX
;  forecast.wXX /all /asca	astdsf.wXX
;  forecast.wXX 		rasmsf.wXX
;  forecast.wXX /asca		rasmsf_astd.wXX
;
; INPUTS:
;  input	- Input  solass or 7dayss file name
;  output	- Output rasmss or solass file name
; OPTIONAL INPUTS:
;  all		- Set for solass or ascass-style
;  path_out	- Use to specify output directory if file_out is
;		  not specified (uses default output name).
;
; Modification History:
;  21-sep-92, Written, J.R. Lemen, LPARL
;   2-jul-93, JRL, Modified to use rd_rasm.
;   3-Feb-96, JRL, Modified to be able to read 7dayss.wXX files.
;  
;-
break_file,file_in,disk,dir,filnam,ext			; Parse the filename
if n_elements(file_out) eq 0 then begin
  if n_elements(path_out) ne 0 then dir = path_out
  file_out = concat_dir( dir,  'rasm_fin' + ext)
  print,'Will write to: ',file_out
endif  

; -- Set up so file can be read by rd_rasm
week = fix(strmid(ext,2,2))				; Get the week number
fn_prefix = filnam
select = 'SOLA'						; Default to Yohkoh
if strlowcase(strmid(fn_prefix,0,4)) eq 'astd' or $
   keyword_set(asca) then select = 'ASTD'		; ASCA
rd_rasm,fn_prefix,week,pass,select=select,path=dir
; --
openr,lun0,file_in, /get_lun

first_head = 1
FF = string(12B)                			; Form-feed character
descrip = file_in + ' ('
if strpos(file_in,'ss') gt -1 then descrip = descrip + 'Final)' 	else $
if strpos(file_in,'forecast') gt -1 then descrip = descrip + 'Preliminary)' 	else $
if strpos(file_in,'sf') gt -1 then descrip = descrip + 'Preliminary)' 	else $
				   descrip = descrip + 'Unknow)'
descrip = descrip + ' '+fmt_tim(!stime)		; Add on the current time

; -------------------------------------------
; Find the first "ACTIVITIES LISTING" header
; -------------------------------------------
header = ['','']		; Added for the 7dayss file case.
while (not EOF(lun0)) and first_head do begin
  buf = '' & readf,lun0,buf
  nlen = strlen(strtrim(buf))
  if strmid(buf,0,1) eq "*" and (nlen gt 11) then begin
     header = buf
; Read until 2nd *-- is detected
     icnt = 0
     while( icnt lt 2 ) do begin
       buf = '' & readf,lun0,buf
       header = [header,buf]
       if strmid(buf,0,3) eq '*--' then icnt = icnt + 1
     endwhile
  endif						; strmid(buf,0,1) eq "*" ...
  if (strpos(header(1),'ACTIVITIES LISTING') gt 0) or		$
     (strpos(header(0),'ACTIVITIES LISTING') gt 0) then begin
	first_head = 0				; Now we have the header
        if keyword_set(qtest) then print,header,format='(a)'
  endif
endwhile
free_lun,lun0

page = 0			; Page counter
new_page = 1			; New page flag

if not keyword_set(all) then begin
  ii = where(pass.wrk eq '1A1')	; These are the actual passes
  pass = pass(ii)
  head0= 'RASM EDITED'
endif else head0= '           '
openw,lun1,file_out,/get_lun

for i=0,n_elements(pass)-1 do begin
  if new_page then begin
	page = page + 1
	head_line = string(head0,strjustify(descrip,wid=60,/center),'PAGE:',page,format='(a,x,a,t72,a,i2)')
	if page gt 1 then char = FF else char = ' '	; Throw a form-feed
	printf,lun1,char,head_line,format='(2a)'	; Header + blank line
	printf,lun1,' '					; Blank line
	Count = 2					; Line counter
	printf,lun1,header,format='(a)'
	count = count + n_elements(header)	; Increment line counter
	new_page = 0
	sub_head = 1
  endif

; See if the day number has changed:"
  if not sub_head then if pass(i-1).doy ne pass(i).doy then sub_head = 1

  if sub_head then begin
	extime = doytim2ex(pass(i).doy,year=pass(i).year)	; Date in external
        day_of_week = addtime(extime,	$
		      diff=week2ex(pass(i).year,ex2week(extime)))/(60.*24.)
	day_name = ['SUN','MON','TUE','WED','THU','FRI','SAT']
	month	 = ['JAN','FEB','MAR','APR','MAY','JUN','JUL','AUG','SEP','OCT','NOV','DEC']
	printf,lun1,strmid(string('*----------',' ',format='(a,a80)'),0,80)
	printf,lun1,strmid(string('*'+day_name(day_of_week),extime(4),	$
			month(extime(5)-1),' ',format='(a,i3,x,a,a80)'),0,80)
	printf,lun1,strmid(string('*----------',' ',format='(a,a80)'),0,80)
	count = count + 3
	sub_head = 0
  endif

  printf,lun1,pass(i).doy,pass(i).start,pass(i).bot,pass(i).eot,	$
	      pass(i).endd,'  DSS-',pass(i).dss,			$
		strmid(pass(i).user+'             ',0,7),		$
		strmid(pass(i).activity+'                  ',0,18),	$
		strmid(pass(i).pass+'      ',0,6),			$
		strmid(pass(i).config+'   ',0,6)+'- ',			$
		strmid(pass(i).wrk+'      ',0,6),			$
	      format='(i4,4i5.4,a,i2,3x,5a)'
  count = count + 1
  if count ge 45 then new_page = 1
endfor

free_lun,lun1
if keyword_set(chmod) then spawn,'chmod 666 '+file_out

end
