;+
; NAME:
;     QUICKLOOK
; PURPOSE:
;     Routine to create a lightcurve GIF file of quicklook SOLAR data.
; CATEGORY:
;     OVRO APC WEB
; CALLING SEQUENCE:
;     quicklook,filename,nplot
; INPUTS
;     filename   The name of the data file (.ARC) for which the quicklook
;                  lightcurves should be generated.
; OPTIONAL (KEYWORD) INPUT PARAMETERS:
; ROUTINES CALLED:
;     filename2date, openarc, get_segments, findsegentries, top20, plotasc
; OUTPUTS:
;     nplot      The number of lightcurve files that were generated (0 if none)
; COMMENTS:
; SIDE EFFECTS:
;     Creates .ASC files (if they do not already exist), and creates a
;     GIF file that is a plot of the lightcurves.
; RESTRICTIONS:
; MODIFICATION HISTORY:
;     Written 26-Jul-1999 by Dale E. Gary
;     24-Aug-1999  DG
;       Added NPLOT output, so that OVIEW2WEB could be conditionally run
;     27-Oct-1999  DG
;       Changed to handle filenames as dates, e.g. YYYYMMDD*.ASC,
;       YYYYMMDD*.GIF rather than using esoteric .ARC file names.
;       This requires corresponding changes to TOP20 and PLOTASC
;-

pro quicklook,filename,nplot

   ; Convert filename to a date string of form YYYY/MM/DD
   datestr = filename2date(filename)

   ; Remove slashes, so that date string is YYYYMMDD
   junk = str_sep(datestr,'/')
   datestr = junk[0]+junk[1]+junk[2]
   f = findfile(!defaults.dbdir+datestr+'*.asc',count=n)
   ans = 'Yes'
   if (n ne 0) then begin
      ans = widget_message(['Database files were found for '+datestr+' in directory '+!defaults.dbdir,$
      'Do you want to create new ones?'],/question)
   endif

   nplot = 0
   if (ans eq 'Yes') then begin
      lun = openarc(filename,a,nrec)
      if (lun eq 0) then return

      seglist = get_segments(a,nrec)
      free_lun,lun

      if (size(seglist,/type) ne 7) then return  ; If seglist is not a string type

      ; Get indexes of header lines (IHED)
      n = findsegentries(seglist,ihed,iseg)

      nhed = n_elements(ihed)

      if (ihed[0] eq -1) then return   ; No headers in file (?)

      for i = 0, nhed-1 do begin
         if (strpos(seglist[ihed[i]],'SOLAR DATA') ne -1) then begin
            reads,seglist[ihed[i]],format='(13x,i5,1x,i5)',hrec,erec
            ; Run TOP20 only if this scan has more than 90 records
            ; (which nominally means at least 5 minutes of solar data)
            if ((erec - hrec) gt 90) then begin
               nplot = nplot + 1
               top20,filename,hrec,erec
            endif
         endif
      endfor
   endif else nplot = nplot + 1
   ; Call PLOT_ASC if at least one ASC file was created.
   if (nplot ge 1) then plot_asc,filename
return
end
