;+
; NAME:
;     GET_REGN
; PURPOSE:
;     Return solar active region information from NOAA file
;     !DEFAULTS.EPHEMDIR+'REGIONS.NOA'.
; CATEGORY:
;     OVRO APC EPHEMERIS
; CALLING SEQUENCE:
;     regn = get_regn()
; INPUTS:
; OPTIONAL (KEYWORD) INPUT PARAMETERS:
; ROUTINES CALLED:
;     get_regn_struct
; OUTPUTS:
;     regn     A calibrator ephemeris structure of the form:
;               {EPOCH: STRUCTURE  ; Epoch and other info from file
;                   {YR:    INT,   ; Year of the region coords
;                    DOY:   INT,   ; Day of year of coords
;                    SEC:   INT}   ; Time of coords
;                NREGN: INT ,      ; Number of active regions
;                 INFO: STRUCTURE  ; Array of region info
;                   {NOA:   INT,   ; NOAA region number
;                    LAT:   INT,   ; Solar latitude of region (+N) [deg]
;                    LNG:   INT,   ; Solar longitude of region (+E) [deg]
;                  CARRLNG: INT,   ; Carrington longitude [deg]
;                    AREA:  INT,   ; Sunspot are of region [10-6 of disk]
;                    TYPE: STRING, ; McIntosh classification (A4)
;                 MAGTYPE: STRING} ; Magnetic class (eg BETA-GAMMA) (A12)
;               }
; COMMENTS:
;   Requires the file !DEFAULTS.EPHEMDIR+'REGIONS.NOA', which is a file
;   FTP'd from NOAA on a daily basis.
; SIDE EFFECTS:
; RESTRICTIONS:
; MODIFICATION HISTORY:
;     Written 12-Jan-1999 by Dale E. Gary
;     30-Jan-1999  DG
;       Slight change to use GET_REGN_STRUCT() routine
;     25-Nov-1999  DG
;       Updated to agree with Epoch defn in GPARM structure
;     11-Jan-2000  DG
;       Eliminated hardwired directory locations
;     03-Oct-2000  DG
;       Use STRUPCASE() to make contents of REGIONS.NOA file all
;       uppercase, to fix problems introduced by format change at
;       NOAA.
;-

function get_regn

   ON_IOERROR,bail

   ; Get a copy of a REGN structure
   regn = get_regn_struct()

   ; Open the COORDS.FTH file
   openr,/get_lun,lun,!defaults.ephemdir+'regions.noa'
   line = ''

   ; Read a line
   readf,lun,line

   MAXREGN = 25
   noa = intarr(MAXREGN)
   lat = intarr(MAXREGN)
   lng = intarr(MAXREGN)
   carrlng = intarr(MAXREGN)
   area = intarr(MAXREGN)
   type = strarr(MAXREGN)
   magtype = strarr(MAXREGN)
   typ = ''
   magt = ''

   i = -1
   ; Read down to the SRS line
   while (strupcase(strmid(line,0,10)) ne 'SRS NUMBER') do begin
      readf,lun,line
   endwhile
   year = (reverse(str_sep(line,' ',/remove_all)))(0)
   readf,lun,line
   month = (reverse(str_sep(line,' ',/remove_all)))(0)
   readf,lun,line
   datime = str_sep((reverse(str_sep(strtrim(line),' ',/remove_all)))(0),'/')
   day = datime(0)
   doy = dayofyr(year,month,day)
   sec = fix(strmid(datime(1),0,4)/100)*3600L

   readf,lun,line
   readf,lun,line
   ew = ''
   ns = ''
   while(strupcase(strmid(line,0,3)) ne 'IA.') do begin
      if (strlen(line) gt 10) then begin
         reads,line,noaa,ns,la,ew,ln,clng,ar,typ,magt,$
                format='(I4,1x,a1,I2,a1,I2,3X,I3,2x,I4,1x,A4,9x,a)'
         i = i + 1
         if (i eq MAXREGN) then begin
            i = i - 1
            goto,done
         endif
         noa(i) = noaa
         if (strupcase(ns) eq 'S') then la = -la
         if (strupcase(ew) eq 'W') then ln = -ln
         lat(i) = la
         lng(i) = ln
         carrlng(i) = clng
         area(i) = ar
         type(i) = strupcase(typ)
         magtype(i) = magt+'            '
         magtype(i) = strupcase(strmid(magtype(i),0,12))
      endif
      readf,lun,line
   endwhile
done:
   ; Close the file and free the logical unit number
   free_lun,lun

   if (i EQ -1) then i = 0

   ; Set up the structures
   for j = 0, i do begin
      regn.info(j).noa = noa(j)
      regn.info(j).lat = lat(j)
      regn.info(j).lng = lng(j)
      regn.info(j).carrlng = carrlng(j)
      regn.info(j).area = area(j)
      regn.info(j).type = type(j)
      regn.info(j).magtype = magtype(j)
   endfor
   regn.epoch.yr = year
   regn.epoch.doy = doy
   regn.epoch.sec = sec
   regn.nregn = i + 1
   if (regn.info(0).noa eq 0) then regn.nregn = 0

   return,regn
bail:
   if(n_elements(lun) ne 0) then free_lun,lun
   return,-1
end
