;+
; NAME:
;     GETGNP
; PURPOSE:
;     From specified date, searches the !DEFAULTS.DBDIR directory
;     for the most appropriate *.GNP file, reads the most appropriate
;     entry, and returns the gain parameters.
; CATEGORY:
;     OVRO APC DATA ANALYSIS
; CALLING SEQUENCE:
;     gnp = getgnp(yrday)
; INPUTS:
;     yrday    A string of the form in TLS.YRDAY giving the year and
;                day number of the data for which gain parameters are
;                desired.
; OPTIONAL (KEYWORD) INPUT PARAMETERS:
; ROUTINES CALLED:
; OUTPUTS:
;     gnp      The gain parameter structure as read from the file.
; COMMENTS:
;     The gain parms will ultimately be contained in a segment in the
;     data, so reading from a file on disk should not be necessary,
;     but for now...
; SIDE EFFECTS:
; RESTRICTIONS:
; MODIFICATION HISTORY:
;     Written 31-Jul-1998 by Dale E. Gary
;     26-Nov-1999  DG
;       Changed to return a new form of GNP structure (introduced due
;       to creation of GAINPARM segment).
;-
function getgnp,this_yrday

   ; Look for files with extension '.gnp' in !DEFAULTS.DBDIR
   f = findfile(!defaults.dbdir+'*.gnp',count=n)
   if (n le 0) then return,-1

   ; Strip out year and doy from yrday string, to use as arguments to
   ; CVDOY routine.  CVDOY returns a three element array with year, month,
   ; and day, all numeric.
   year = strmid(this_yrday,0,4)
   doy = strmid(this_yrday,5,3)
   out = cvdoy(year,doy)
   this_yrmo = string(out(0),out(1),format='(I4,I2.2)')

   ; Loop over filenames
   f = reverse(f(sort(f)))             ; Sort into newest first

   ; Declare some storage for use in loop below
   line = (yrday = (yrdaysav = (tstr = '')))
   nant = 5
   gnparm = (gnpsav = fltarr(6*nant))
   ; This is the Gain Parameter structure definition
   gnp = replicate({gnp},nant)
;   {nlparm: fltarr(nant)    ,$  ; Nonlinearity parameter for each ant
;            attn: fltarr(nant,4)  ,$  ; 5, 10, 20, 20*DB factors for each ant
;           resid: fltarr(nant)    }   ; The residual of the fit

   FOR i = 0, n-1 DO BEGIN
      break_file,f(i),disk,direc,filnam,ext
      IF (filnam LE this_yrmo) THEN BEGIN
         ; This is a possible file to contain valid data.  Search is
         ; in order of newest first, so first agreement gives valid
         ; match.  Open file.
         openr,lun,/get_lun,f(i)

         ; Skip the three lines of header inforation
         hedlines = strarr(3)
         readf,lun,hedlines

         WHILE(NOT EOF(lun)) DO BEGIN
            readf,lun,line
            reads,line,yrday,tstr,gnparm,format='(a8,1x,a8,5(1x,6f6.3))'
            IF (yrday LE this_yrday) THEN BEGIN
               yrdaysav = yrday
               gnpsav = gnparm
            ENDIF ELSE goto,out
         ENDWHILE
         IF (yrdaysav NE '') THEN goto,out
         free_lun,lun
      ENDIF
   ENDFOR

   IF (yrdaysav EQ '') THEN return,-1

out: free_lun,lun
   indx = indgen(5)*6
   gnp.nlparm = gnpsav(indx)
   for i = 0, 2 do begin
      gnp.attn[i] = gnpsav[indx+i+1]
   endfor
   gnp.ndfac = gnpsav[indx+4]
   gnp.resid = gnpsav[indx+5]

return,gnp
end
