; DS5.PRO  -  make figure 5

; set the color table 
  
  device,true=24
  device,de=0
  device,retain=2
  loadct,39  ; the one mei-ching fok used
;  loadct,41 ;our usual

  nz  = 304
  nf  = 89
  nt  =  200

  dir  = './' 

  day1 = 279  

  close,1
  time = fltarr(5,nt)
  openr,1,dir+'ds2_time.dat'
  print,' reading time'
  readf,1,time
  close,1

  day = fltarr(nt)
  day(0) = 279.
  for i = 1,nt-1 do begin
    day(i) = day(i-1)
    if (time(1,i) lt time(1,i-1)) then day(i) = day(i) + 1.
  endfor

; latitude

  close,1
  glat = fltarr(nz,nf)
  openr,1,dir+'ds2_glat.dat',/f77_unformatted
  print,' reading glat'
  readu,1,glat

; altitude

  close,1
  zalt = fltarr(nz,nf)
  openr,1,dir+'ds2_zalt.dat',/f77_unformatted
  print,' reading zalt'
  readu,1,zalt

; electron density data

  deni2  = fltarr(nz,nf,nt)
  deni7  = fltarr(nz,nf,nt)
  vsi2   = fltarr(nz,nf,nt)
  vsi7   = fltarr(nz,nf,nt)
  close,1
  openr,1,dir+'ds4_deno.dat',/f77_unformatted
  print,' reading deno'
  readu,1,deni2
  close,1
  openr,1,dir+'ds5_denn.dat',/f77_unformatted
  print,' reading denn'
  readu,1,deni7
  close,1
  openr,1,dir+'ds5_vo.dat',/f77_unformatted
  print,' reading vo'
  readu,1,vsi2
  close,1
  openr,1,dir+'ds5_vn.dat',/f77_unformatted
  print,' reading vn'
  readu,1,vsi7

; we've got the info, lets get to work
; set up arrays to be plotted

  ntma = [94,123]
  nfm = 67
  fna1  = reform(deni2(*,nfm,*))
  fna2  = reform(deni7(*,nfm,*))
  fnc1  = reform(vsi2 (*,nfm,*))/1.e05
  fnc2  = reform(vsi7 (*,nfm,*))/1.e05
  yt  = '!NO!U+!N, N!U+!N (cm!U-3!N)'
  yr  = [0.01,1000.]  
  ytp = 1
  yt2  = '!Nv!BO!N+, v!BN!N+ (km/s)'
  yr2  = [-4,4]
  ytp2 = 0

  re = 6370. 
; -----------------------
; set plot to the screen
; -----------------------

  set_plot,'x'

; set to a single window

  !p.multi=0

; set the background to white 
; (instead of the default which is black)

  !p.background=65535

; set the window size

  xwin = 600 
  ywin = 600
  window,xsize=xwin,ysize=ywin

; scale the window for the postscript plot

  xps  = 6.0
  yps  = xps * ywin / xwin

; positions
  pos1 = [.16,.53,.56,.92]
  pos3 = [.16,.12,.56,.51]
  pos2 = [.59,.53,.99,.92]
  pos4 = [.59,.12,.99,.51]

; position and size of the color bar

  posc = [.40,.98,.70,.98]
  xszc = ( posc(2) - posc(0) ) * xps
  yszc = ( posc(3) - posc(1) ) * yps
  xstc = posc(0) * xps
  ystc = posc(1) * yps
  xszw = ( posc(2) - posc(0) ) * xwin

; set font to simplex roman

  xyouts,.5,.5,'!3 ',/normal 

  xt = '!NGeographic Latitude'
  samis  = '!NSAMI3 (One fluid H!U+!N)'

; set specific minimum and maximun values

;  fmn    =  0.
;  fmx    =  1.e4
  fmn    = -2.
  fmx    = 5.
  glatmn  = -64.
  glatmx  = 62.
  zaltmn  = 0.  ; in Re
  zaltmx  = 4.5
  
;  yr = [-10,10]  
  
; define the universal times
  uts  = [' ',' ',' ',' ']
  uts2 = [' ',' ',' ',' ']
  for j=0,1 do begin
    ntm = ntma(j)
    hr   = time(1,ntm)
    min  = time(2,ntm)
    if min ge 59.0 then hr = hr+1
    if min ge 59.0 then min = 0.
    phr  = string(format='(i2)',hr)
    while(((i=strpos(phr,' '))) ne -1) do strput,phr,'0',i
    pmin = string(format='(i2)',min)
    while(((i=strpos(pmin,' '))) ne -1) do strput,pmin,'0',i
    days = 'Day '+string(format='(i3)',day(ntm))
    uts(j)    = days+'  '+phr+pmin+' UT' ;+days
  endfor
  print,' uts  = ',uts
  
; label the L value of the field line
  al = zalt(nz/2,nfm)/re + 1.0
  als = 'L = '+string(format='(f3.1)',al)
  print,als
  
; plot the lines
 
  lst2 = 2
  lcol2 = 0
  ics = 80  ;80
  icn = 160   ;160
  plot,glat(*,nfm),fna1(*,ntma(0)),$
          yrange=yr,ystyle=1,ytype=ytp, $
          xrange=[glatmn,glatmx],xstyle=1,$
          xticknam=[' ',' ',' ',' ',' ',' ',' ',' '], $
          xtitle=' ',$
          ytitle=yt,$
          color=0,pos=pos1
  oplot,glat(*,nfm),fna2(*,ntma(0)),linestyle=lst2,color=lcol2

  plot,glat(*,nfm),fna1(*,ntma(1)),$
          yrange=yr,ystyle=1,ytype=ytp, $
          xrange=[glatmn,glatmx],xstyle=1,$
          xticknam=[' ',' ',' ',' ',' ',' ',' ',' '], $
          yticknam=[' ',' ',' ',' ',' ',' ',' ',' '], $
          xtitle=' ',$
          ytitle=' ',$
          color=0,pos=pos2,/noerase
  oplot,glat(*,nfm),fna2(*,ntma(1)),linestyle=lst2,color=lcol2
  
  plot,glat(*,nfm),fnc1(*,ntma(0)), $
          yrange=yr2,ystyle=1,ytype=ytp2, $
          xrange=[glatmn,glatmx],xstyle=1,$
 ;         xticknam=[' ',' ',' ',' ',' ',' ',' ',' '], $
          xtitle=xt,$
          ytitle=yt2,$
          color=0,pos=pos3,/noerase
  oplot,glat(*,nfm),fnc2(*,ntma(0)),linestyle=lst2,color=lcol2

  plot,glat(*,nfm),fnc1(*,ntma(1)), $
          yrange=yr2,ystyle=1,ytype=ytp2, $
          xrange=[glatmn,glatmx],xstyle=1,$
;          xticknam=[' ',' ',' ',' ',' ',' ',' ',' '], $
          yticknam=[' ',' ',' ',' ',' ',' ',' ',' '], $
          xtitle=xt,$
          ytitle=' ',$
          color=0,pos=pos4,/noerase
  oplot,glat(*,nfm),fnc2(*,ntma(1)),linestyle=lst2,color=lcol2
  
; print the times

  xp1 = .18
  xp2 = .61
  xp3 = .50
  xp4 = .93
  yp1 = .265
  yp2 = .475
  yp3 = .685
  yp4 = .885
  yp5 = .935

  sz = 1.2
  xyouts,xp1+0.03,yp4,uts(0),size=sz,color=0,/normal
  xyouts,xp2+0.03,yp4,uts(1),size=sz,color=0,/normal
  
  xyouts,xp1-0.01,yp5,samis,color=0,/normal
  xyouts,xp3+0.10,yp5,als,color=0,/normal
  
  xyouts,xp3,yp4,'!3(a)',size=sz,color=0,/normal
  xyouts,xp4,yp4,'!3(c)',size=sz,color=0,/normal
  xyouts,xp3,yp2,'!3(b)',size=sz,color=0,/normal
  xyouts,xp4,yp2,'!3(d)',size=sz,color=0,/normal
  
; -----------------------
; set plot to postscript
; -----------------------

  !p.thick=4
  !p.charthick=4
  !x.thick=4
  !y.thick=4
  !p.charsize = 1.0

  set_plot,'ps'
  device,file='fig.ps',bits_per_pixel=8,/color, $
         xsize=xps,ysize=yps,/inches,xoffset=1.25,yoffset=1.25

; plot the line
  plot,glat(*,nfm),fna1(*,ntma(0)),$
          yrange=yr,ystyle=1,ytype=ytp, $
          xrange=[glatmn,glatmx],xstyle=1,$
          xticknam=[' ',' ',' ',' ',' ',' ',' ',' '], $
          xtitle=' ',$
          ytitle=yt,$
          color=0,pos=pos1
  oplot,glat(*,nfm),fna2(*,ntma(0)),linestyle=lst2,color=lcol2

  plot,glat(*,nfm),fna1(*,ntma(1)),$
          yrange=yr,ystyle=1,ytype=ytp, $
          xrange=[glatmn,glatmx],xstyle=1,$
          xticknam=[' ',' ',' ',' ',' ',' ',' ',' '], $
          yticknam=[' ',' ',' ',' ',' ',' ',' ',' '], $
          xtitle=' ',$
          ytitle=' ',$
          color=0,pos=pos2,/noerase
  oplot,glat(*,nfm),fna2(*,ntma(1)),linestyle=lst2,color=lcol2

  plot,glat(*,nfm),fnc1(*,ntma(0)), $
          yrange=yr2,ystyle=1,ytype=ytp2, $
          xrange=[glatmn,glatmx],xstyle=1,$
;          xticknam=[' ',' ',' ',' ',' ',' ',' ',' '], $
          xtitle=xt,$
          ytitle=yt2,$
          color=0,pos=pos3,/noerase
  oplot,glat(*,nfm),fnc2(*,ntma(0)),linestyle=lst2,color=lcol2
  
  plot,glat(*,nfm),fnc1(*,ntma(1)), $
          yrange=yr2,ystyle=1,ytype=ytp2, $
          xrange=[glatmn,glatmx],xstyle=1,$
;          xticknam=[' ',' ',' ',' ',' ',' ',' ',' '], $
          yticknam=[' ',' ',' ',' ',' ',' ',' ',' '], $
          xtitle=xt,$
          ytitle=' ',$
          color=0,pos=pos4,/noerase
  oplot,glat(*,nfm),fnc2(*,ntma(1)),linestyle=lst2,color=lcol2
   
; print the labels

  sz = 1.0
  ush = 0.003
  xyouts,xp1+0.03,yp4,uts(0),size=sz,color=0,/normal
  xyouts,xp2+0.03,yp4,uts(1),size=sz,color=0,/normal

  xyouts,xp1-0.01,yp5,samis,color=0,/normal
  xyouts,xp3+0.10,yp5,als,color=0,/normal

  xyouts,xp3,yp4,'!3(a)',size=sz,color=0,/normal
  xyouts,xp4,yp4,'!3(c)',size=sz,color=0,/normal
  xyouts,xp3,yp2,'!3(b)',size=sz,color=0,/normal
  xyouts,xp4,yp2,'!3(d)',size=sz,color=0,/normal

; close the postscript file 'contour.ps'

  device,/close

; reset the display to the screen

  set_plot,'x'

  !p.charthick = 1
  !p.thick     = 1
  !x.thick=1
  !y.thick=1
  !p.charsize = 1.2

end

