;+
; NAME:               BCS_LOAD_CAT
;
; PURPOSE:            To read the BCS catalogue into an IDL structure.
;
; CALLING SEQUENCE:   IDL> bcs_load_cat, str_name, year
;
; INPUTS:             str_name              name of the structure loaded
;                     year                  year of the data to load. If this
;                                           input is type integer then the 
;                                           standard catalogue for that year
;                                           is loaded.  If it is type string
;                                           then it is taken to be the name of
;                                           a user catalogue file.
;
; OPTIONAL INPUTS:    none
;
; OUTPUTS:            IDL structure is returned
;
; OPTIONAL OUTPUTS:   None
;
; RESTRICTIONS:       Note that files without an optically identified event
;                     have their GOES position fields set to a value of 99
;                     in both Latitude and Longitude.
;
; PROCEDURE:          Reads the catalogue file and loads structure fields
;
; MODIFICATION HISTORY:   Written:  Feb 93  CDP
;                                   Apr 93  Include user catalogues
;
;-
pro bcs_load_cat,bcs,year

;
;  prompt if no parameters
;
if n_params() eq 0 then begin
   print,'Use:   bcs_load_cat, str_name, year'
   print,"or     bcs_load_cat, str_name, 'user_file'"
   print,' '
   return
endif
;
;  check valid year
;
s = size(year)
file = ' '
if s(0) gt 0 then begin
   bell
   print,'Only scalar integer or string allowed for the second parameter.'
   return
endif
if s(1) eq 2 then begin
   if year gt 1900 then year = year - 1900
;
;  set up input file
;
   file = concat_dir('$DIR_BCS_CAT','bcs_catalogue_')$
                                +string(year,format='(i2)')+'.dat'
endif 

if s(1) eq 7 then file = year
;
;  check invalid input
;
if file eq ' ' then begin
   bell
   print,'Only scalar integer or string allowed for the second parameter.'
   return
endif

print,'Loading data from ',file,' ....'
openr,lun,file,/get_lun
text = strarr(10000)
;
;  skip 5 header lines in file
;
line = ' '
for i=0,4 do readf,lun,line

;
;  read file and store (would be more efficient to read whole array, but
;  need to know size and this may change??)
;
i = 0
while not eof(lun) do begin
   readf,lun,line 
   text(i) = line
   i = i + 1
endwhile
free_lun,lun

;
;  trim text array
;
n = where(strmid(text,0,1) eq '9')
text = text(n)

;
; set up structure
;
bs = {bs_dum,fid:' ',$
             week:' ',$
             date:' ',$
             f_st:' ',$
             f_end:' ',$
             day:0,$
             time:long(0),$
             fe26:0,$
             fe25:0,$
             ca19:0,$
             s15:0,$
             g_st:' ',$
             g_max:' ',$
             g_end:' ',$
             g_class:' ',$
             g_opt:' ',$
             g_lat:99,$
             g_long:99,$
             g_ar:0}

bcs = replicate({bs_dum},n_elements(text))
;
;  bad ones have a line of dashes
;
ok = where(strpos(text,'--------') eq -1)
print,'Loading structure tags....'
print,'fid'
bcs.fid = strmid(text,0,11)
print,'week
bcs(ok).week= strmid(text(ok),13,5)
print,'date
bcs(ok).date= strmid(text(ok),20,9)
print,'f_st
bcs(ok).f_st= strmid(text(ok),31,8)
print,'f_end
bcs(ok).f_end= strmid(text(ok),41,8)
print,'day, time'
t = anytim2ex(bcs(ok).date+' '+bcs(ok).f_st)
ex2int,t,m,d
bcs(ok).day = d
bcs(ok).time = m
print,'fe26
bcs(ok).fe26= strmid(text(ok),53,5)
print,'fe25
bcs(ok).fe25= strmid(text(ok),60,5)
print,'ca19
bcs(ok).ca19= strmid(text(ok),67,5)
print,'s15'
bcs(ok).s15= strmid(text(ok),74,5)
print,'g_st
bcs(ok).g_st= strmid(text(ok),82,5)
print,'g_max
bcs(ok).g_max= strmid(text(ok),88,5)
print,'g_end
bcs(ok).g_end= strmid(text(ok),94,5)
print,'g_class
bcs(ok).g_class= strmid(text(ok),101,4)
print,'g_opt'
bcs(ok).g_opt= strmid(text(ok),106,2)
;
;  redefine ok records to handle blank position field
;
print,'g_lat
ok = where(strpos(text,'-------') eq -1 and strmid(text,109,1) ne '' $
                                        and strmid(text,109,1) ne ' ')
bcs(ok).g_lat= strmid(text(ok),110,2)
s = where(strmid(text,109,1) eq 'S')
bcs(s).g_lat = -bcs(s).g_lat
print,'g_long
bcs(ok).g_long= strmid(text(ok),113,2)
e = where(strmid(text,112,1) eq 'E')
bcs(e).g_long = -bcs(e).g_long
;
;  redefine ok records to handle blank active region field
;
print,'g_ar
ok = where(strpos(text,'-------') eq -1 and strmid(text,116,1) ne '' $
                                        and strmid(text,116,1) ne ' ')
bcs(ok).g_ar= strmid(text(ok),116,4)


end

