PRO rat_example
ref_freq=10.0 ;GHz
xscale=1 ; 1- vyvesti ot limba do limba, 2- vyvesti 2 diametra solnca, 3-vyvesti ves scan
stokes=1; 0- vyvesti i, 1-i&v, 2-v
xmarg1=[0,0];[10,10]; polya po gorizontali
all=0; 1-vse volny, 0-otdelnaya chastota

pathfname='/data/ratan_600/2010/03/100306sun0.fits'
if not file_test(pathfname) then pathfname=pickfile()
print, pathfname

rat0=readfits(pathfname,hdr)
sz=size(rat0)
if sz[0] lt 2  then goto, exit
ext0=mrdfits(pathfname,1,hdr1)
rat0=rat0(*,*,sort(ext0.freq))
ext0=ext0(sort(ext0.freq))
rat1=rat0


fr=where(ext0.freq ge ref_freq-1e-4, count)
if count lt 1 then ifr0=0 else ifr0=fr[0]
az=fxpar(hdr,"AZIMUTH")
naxis=fxpar(hdr,"NAXIS")
n1=fxpar(hdr,"NAXIS1")
n2=fxpar(hdr,"NAXIS2")
n3=fxpar(hdr,"NAXIS3")
alt_shift=fxpar(hdr,"ALT_SHIF")
date_obs=fxpar(hdr,'DATE-OBS')
solar_r=fxpar(hdr,'SOLAR_R')
solar_p=fxpar(hdr,'SOLAR_P')

if solar_p eq 0 then begin
pbr=pb0r(strmid(date_obs,2,8))
print, pbr
sxaddpar, hdr, 'SOLAR_P', pbr[0]
solar_r=pbr[2]*60.0
sxaddpar, hdr, 'SOLAR_R', solar_r
sxaddpar, hdr, 'SOLAR_B', pbr[1]
endif

centr_x=fxpar(hdr,'CRPIX1')
cdelt1=fxpar(hdr,'CDELT1')

nobj=STRUPCASE(strtrim(fxpar(hdr,'OBJECT'), 2))
ddate=STREGEX(FILE_BASENAME(pathfname), '^[0-9]*', /EXTRACT, /FOLD_CASE)
if (float(az)-fix(az) eq 0) then az=strtrim(fix(az),2)
ddateaz=strtrim(ddate+az,2)

time1=fxpar(hdr,"TIME-OBS")

;if ddate gt 201003 then begin
;time2=rat_dt2ut(time1, az)
;sxaddpar,hdr,"TIME-OBS", time2
;ext1.time=time2
;endif

ndate=date_obs+", "+fxpar(hdr,"TIME-OBS")+", az"+string(fix(az),format='(I+03)')

namobj=nobj+' '+ndate

solar_rx=solar_r/cdelt1

;----------osi x
if (n1-solar_rx*2)/solar_rx lt 2.0 then short=1 else short=0
xmas=(findgen(n1)-centr_x)/solar_rx ; os' x v radiusah solnca
xmas1=(findgen(n1)-centr_x)*cdelt1 ; os' x v arcsec


case xscale of ;razmer po osi x
'1': begin
x1=round(centr_x-solar_rx)
;x2=round(centr_x+solar_rx-1)
x2=x1+solar_rx*2.0
xmas1=(findgen(x2-x1+1)-solar_rx)*cdelt1
end
'2':begin
x1=round(centr_x-solar_rx*2)
;x2=round(centr_x+solar_rx*2-1)
x2=x1+solar_rx*4.0
if centr_x lt solar_rx*2.0 then cent=centr_x else cent=solar_rx*2.0
x1=x1>0
x2=x2<(n1-1)
xmas1=(findgen(x2-x1+1)-cent)*cdelt1
end
ELSE:begin
xmas1=(findgen(n1)-centr_x)*cdelt1
x1=0
x2=n1-1
end
endcase

;rad=[-solar_rx,0,solar_rx-1]
rad=[-solar_rx,0,solar_rx]
rads=rad*cdelt1

;begin------- R&L -> I&V
;if flag_rl_iv eq 1 then begin
;rat1=rat0
;rat4=rat1
;for i=0, n3-1 do rat1(*,0,i)=(rat1(*,0,i)+rat1(*,1,i))/2.0
;for i=0, n3-1 do rat1(*,1,i)=(rat4(*,0,i)-rat4(*,1,i))/2.0
;endif

stitle=namobj+', '+strtrim(string(n3),2)
titl=FILE_BASENAME(pathfname)+' freq='+strcompress(ext0[ifr0].freq)+' GHz'
if strlen(stitle) lt 5 then stitle=nfile

if all then begin
imax=max(rat1[x1:x2,0,*], min=imin)
yri=[imin,imax]
vmax=max(rat1[x1:x2,1,*], min=vmin)
yrv=[vmin,vmax]
endif else begin
yri=[0,0]
yrv=[0,0]
endelse

device,decomposed=0
loadct,5
;rat_gr_group, rat1(x1:x2,*,*), xmas1, ext0, n3, stitle=stitle, stokes='I&V', colort=5, color_round=1, xmarg=[10,10],ytit='Ta,k', xtit='R-Rc, arcsec', chrsz=2
if stokes lt 2 then begin
plot,xmas1, rat1(x1:x2,0,ifr0),yrange=yri,title=stitle, xstyle=1, ystyle=8*stokes, xmargin=xmarg1
if all then for i=0, n3-1 do oplot,xmas1,rat1(x1:x2,0,i), color=i*2.0
endif
if stokes gt 0 then begin
if stokes eq 2 then erase
plot,xmas1, rat1(x1:x2,1,ifr0),yrange=yrv,title=stitle, xstyle=1, ystyle=4*(stokes-2),linestyle=1,xmargin=xmarg1, /noerase
AXIS, YAXIS=1, /noerase
if all then  for i=0, n3-1 do oplot,xmas1,rat1(x1:x2,1,i), color=i*2.0, linestyle=1
endif

goto, normal_end

exit:
xyouts,5,10, 'data file is bad', /device;, /norm
normal_end:
print, 'PROGRAM FINISHED'
;stop
end
