;***************************** Read_F2_1Hz.pro ********************************
;******************************************************************************
; Description: Read and plot Freja F2 magnetic field data. The perturbation
;              magnetic field data is the result of subtracting the 
;              magnetic field model from the data.
;                                            - S.-I. Ohtani, J.B. Gary, JHU/APL
;
;   1Hz resolution data files are available via World Wide Web at:
;
;           http://sd-www.jhuapl.edu/Freja/
;
;
; This software is available on an "as is" basis. Neither the author  nor
; The Johns Hopkins/Applied Physics Laboratory makes any guarantee, nor 
; assumes any responsibility for the use of this software.
;******************************************************************************

infile = ''
read,infile,prompt="Enter Input Filename: "      ; Get from keyboard
openr, unit,infile , /get_lun

;____________________________________________


arrsz  = 10000                    ; size of arrays
zro    = intarr(arrsz)            ; zero baseline
utime  = dblarr(arrsz)            ; UT (seconds)
BN     = fltarr(arrsz)            ; Perturbation North component (nT)
BE     = fltarr(arrsz)            ; Perturbation East component (nT)
BP     = fltarr(arrsz)            ; Perturbation Parallel component (nT)
BXT    = fltarr(arrsz)            ; BX total in geographic XYZ (GEO) system
BYT    = fltarr(arrsz)            ; BY total in geographic XYZ (GEO) system
BZT    = fltarr(arrsz)            ; BZ total in geographic XYZ (GEO) system
bmlat  = fltarr(arrsz)            ; PACE Magnetic latitude (deg.)
bmlt   = fltarr(arrsz)            ; PACE Magnetic local time (hours)
blat   = fltarr(arrsz)            ; Geographic latitude (deg.)
blong  = fltarr(arrsz)            ; Geographic longitude (deg.)
balt   = fltarr(arrsz)            ; s/c altitude (km)

iline  = lonarr(12)               ; 1st two lines, integers
fline  = dblarr(11)               ; bulk of lines, floating point

  openr, unit, infile, /get_lun
  readf, unit, format = '(12(i8))',iline           ; some useful stuff
  date = iline(4)                                  ; date
  sttm = iline(5)                                  ; start time
  readf, unit, format = '(12(i8))',iline           ; some useless stuff

  i=0L
  while (not eof(unit)) do begin
    readf, unit, format = '(1e15.7,11(e12.4))', atime,fline
    utime(i)  = atime
    BN(i)     = fline(0)
    BE(i)     = fline(1)
    BP(i)     = fline(2)
    BXT(i)     = fline(3)
    BYT(i)     = fline(4)
    BZT(i)     = fline(5)
    bmlat(i)  = fline(6)
    bmlt(i)   = fline(7)
    blat(i)   = fline(8)
    blong(i)  = fline(9)
    balt(i)   = fline(10)
    i=i+1
  endwhile
  utime = utime(0:i-1)
  BN = BN(0:i-1)
  BE = BE(0:i-1)
  BP = BP(0:i-1)
  BXT = BXT(0:i-1)
  BYT = BYT(0:i-1)
  BZT = BZT(0:i-1)
  bmlat = bmlat(0:i-1)
  bmlt = bmlt(0:i-1)
  blat = blat(0:i-1)
  blong = blong(0:i-1)
  balt = balt(0:i-1)
  free_lun,unit


x1=long(utime(0)/3600.)*3600 + $
   long(utime(1)/3600. mod long(utime(1)/3600.)*60)*60
last=size(utime)
last=last(1)-20
x2=long(utime(last)/3600.)*3600 + $
   long(utime(last)/3600. mod long(utime(last)/3600.)*60+1)*60

if x2 lt x1 then x1=x1-86400

  !p.multi=[0,0,4]

  ttle="Freja F2 1Hz Data - "+infile

  plot,utime(0:i-20),bn(0:i-20),title=ttle,ytitle='Bn (nT)'

  plot,utime(0:i-20),bn(0:i-20)-smooth(bn(0:i-20),100), $
    ytitle='Detrended Bn (nT)'

  plot,utime(0:i-20),be(0:i-20),ytitle='Be (nT)'

  plot,utime(0:i-20),be(0:i-20)-smooth(be(0:i-20),100), $
    ytitle='Detrended Be (nT)', xtitle=" Universal Time", $
    subtitle='Freja F2 Perturbation Magetic Field Data, JHU/APL'

end; 

;******************************************************************************
;******************************************************************************