Function CAL, agc, auto, bande, ant, freq, nfilters, filter
common KRONOSCAL, a1h1,a1h2,ai,dbcal_abc,dbcal_h
if n_elements(ai) eq 0 then restore,getenv('ROOT_RPWS')+'/bin/constants.xdr'
ncal=n_elements(agc)
if n_elements(att) eq 0 then att=intarr(ncal)
a123=fltarr(3,ncal)
iant=byte(ant/3)
for i=0L,ncal-1L do a123(*,i)=ai(*,iant(i),bande(i),att(i))
ifreq=freq
test=where(bande eq 3)
if test(0) ne -1 then begin
testfreq=where(bande eq 3 and ifreq gt 4300)
if testfreq(0) ne -1 then ifreq(testfreq)=4300
ntest=n_elements(test)
for i=0L,ntest-1L do a123(0,test(i))= $
a1h1(ant(test(i)),fix(ifreq(test(i))/25),att(test(i)))
endif
test=where(bande eq 4)
if test(0) ne -1 then begin
testfreq=where(bande eq 4 and ifreq gt 16075)
if testfreq(0) ne -1 then ifreq(testfreq)=16075
ntest=n_elements(test)
for i=0L,ntest-1L do a123(0,test(i))= $
a1h2(ant(test(i)),fix((ifreq(test(i))-25)/50),att(test(i)))
endif
dBcal=fltarr(ncal)
test=where(bande le 2)
ntest=n_elements(test)
if test(0) ne -1 then for i=0L,ntest-1L do dBcal(test(i))= $
dBcal_abc(filter(test(i)),ant(test(i)),bande(test(i)), $
fix(alog(nfilters(test(i))*1.)/alog(2.)+0.5)-3)
test=where(bande gt 2)
ntest=n_elements(test)
if test(0) ne -1 then for i=0L,ntest-1L do dBcal(test(i))= $
dBcal_h(filter(test(i)),iant(test(i)), $
fix(alog(nfilters(test(i))*1.)/alog(2.)+0.5))
dB=fltarr(ncal)
test=where(agc ne 255)
if test(0) ne -1 then $
dB(test)=AGC_dB(agc(test),a123(0,test),a123(1,test),a123(2,test))
test=where(auto ne 255)
if test(0) ne -1 then $
dB(test)=dB(test)+AUTO_dB(auto(test))-dBcal(test)
test=where((agc ne 255 or auto ne 255) and ant ne 2)
if test(0) ne -1 then dB(test)=dB(test)+3.
return, dB
end