pro pulse,file
;this code plots a pulse number
;created by Justin Mabie 
;CIRES @ NOAA/NESDIS/NGDC
;30 October 2013
;argument 'file' is the name of the IDL save file'

print,'This code will plot the peak return of the chosen pulse.  The plot range is from 30Km below the peak to 30Km above the peak.  Power from each of the 8 receive antennas are plotted separately.'

on_ioerror,bad
restore,file


;;;;;;;;;;;;;;;;;;;SET PARAMETERS

;PLOT PARAMETERS
device,decomposed=0
;set plot and device parameters - to make it look nice
  !p.font=1 ;set font
  !p.charsize=2 ;set size of charachters
  !p.thick=3 ;set line thickness
  !x.thick=1
  !y.thick=2
  !x.style=1
;define plot colors
  tvlct,255,0,0,1 ;red
  tvlct,0,255,0,3 ;green
  tvlct,0,0,255,4 ;blue
  tvlct,255,255,0,5 ;yellow
  tvlct,255,0,255,6
  tvlct,0,255,255,7
  tvlct,255,255,255,8
  tvlct,100,50,200,9
  tvlct,0,0,0,2

;create an x-axis array for plotting
size=sct.timing.pri_count
size=size/2

xaxis=findgen(size)

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

;;;;;;;;;;;;;;;;;;;
print,'enter a pulse number from 1 to ',size
read,pnum
freq=strcompress(sct.frequency.base_table[pnum-1],/remove_all)
print,'frequency is ',freq,'KHz'

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
ngates=sct.timing.gate_count
pulse=make_array(8,ngates) ;even pulses array of values
mock=make_array(ngates,value=0)
xax=findgen(ngates)
xax=xax*1.5  ;xaxis in kilometers

;for each antenna
for i=0,7 do begin ;each antenna - start with 0 only
j=pnum

    ii=pulse_i[pnum,0:ngates-1,i] ;I
    qq=pulse_q[pnum,0:ngates-1,i] ;Q
    mm=sqrt(abs(1.0*ii*ii)+abs(1.0*qq*qq))     ;Power
pulse[i,30:ngates-1]=mm[30:ngates-1]

endfor   ;each antenna

ymin=0
ymax=max(pulse)
a=pulse[0,*]
center=where(a eq max(a))
cc=center[0]
gatemin=cc-20
gatemax=cc+20

plot,xax[gatemin:gatemax],mock[gatemin:gatemax]+ymax/2.0,yrange=[ymin,ymax],xstyle=1,title='Peak of Pulse',background=255,color=0 ,xtitle='Range in Kilometers',ytitle='Total Power'
oplot,xax[gatemin:gatemax],mock[gatemin:gatemax]+ymax/2.0,color=255
;;;;;;;;;;
for i=0,7 do begin
  line=i
   if (line eq 1) then begin  ;this is because I don't like linestyle=1
    line=8
   endif

print,'receive antenna ',i+1
oplot,xax[gatemin:gatemax],pulse[i,gatemin:gatemax],linestyle=line,color=i+1
print,'enter any number'
  if (i ne 7) then begin
    read,k
  endif
endfor


goto,endfile

bad:print,'INPUT ERROR: YOU MUST PROVIDE A VALID IDL SAV FILE TO RUN THIS CODE!'
endfile:
close,/all
end