; sim2raw.pro
; Written 2015 July 01 by Than Putzig
;
; This procedure converts a UT simulated radargram (png) to a binary file.

pro sim2raw,input,output,diag=diag

; prompt for input filename if not specified
if n_elements(input) eq 0 then $
   input=dialog_pickfile(title='Simulated radargram PNG',filter='UTS_*.png')
if n_elements(output) eq 0 then output=file_basename(input,'png')+'raw'

diag=keyword_set(diag)
if diag then begin
   print,'Reading the input PNG from ',input
   p=query_png(input,info)
   help,info,/str
endif
rgb=read_png(input)
if diag then help,rgb
r=reform(rgb[0,*,*])
g=reform(rgb[1,*,*])
b=reform(rgb[2,*,*])
sim=byte(sqrt(r^2+g^2+b^2))
if diag then begin
   print,'Scaled radargram from R-G-B channels:'
   help,sim
   print,'Writing the output binary to ',output
endif
openw,olun,output,/get_lun
writeu,olun,sim
free_lun,olun
end
