pro norp_rdt2data_35g,rdata,timejo,mvalid,fi,fv $
    ,nocalib=nocalib,ndata=ndata,npa=npa,stat=stt0 $
    ,smax=smax,fimin=fimin,calibration=calibration $
    ,firaw=amp0,fvraw=pol0

;+
; NAME:
;       NORP_RDT2DATA_35G
;
; PURPOSE:
;       This procedure is for getting 35GHz data from the raw NoRP data
;
; CALLING SEQUENCE:
;       norp_rdt2data_35g,rdata,mvalid,timejo,fi,fv
;
; INPUTS:
;       rdata: structure of the raw data
;       timejo: structure array with 'time internal format'
;               that must be given by 'norp_rdt2timej'.
;
; OPTIONAL INPUT:
;       ndata: 2-elements long-integer array indicating the
;              position of data corresponding to the given timerange
;
; OUTPUTS:
;       mvalid: byte array indicating the data is valid or not.
;              e.g. if the data(123) obtained at time(123)
;                   was the calibration data mvalid(123)=0.
;                   If the data(124) was the real Sun flux mvalid(124)=1.
;       fi:  I (R+L) data array (mfreq,ndata)
;               data(0,*) is the time-sequencial array of the 1st
;               frequency which are read (indicated by 'rdfreq').
;       fv:  V (R-L) data array (mfreq,ndata)
;
; OPTIONAL OUTPUT:
;
; HISTORY:
;       programmed by T. Kosugi in 1984 May as Fortran program 
;       for old NoRP data format
;       re-programmed by H.Sekiguchi 1987 July - 1988 Dec.
;
;       1995 July,20    N,Shinox for new format NoRP data
;       95.04.19   ed.  add ATT correction for pol data ,correction data type
;                    changed intarr() to fltarr().,
;       95.09.04 Ver.2.0  M. Nishio and K. Hori
;       95.11.10 Ver.3.0  M. Nishio refernce level at 9.4GHz= amb -> zero
;  <<Caution: ambient level at 9.4GHz is incorrect. Use zero as ref. level>>
;       95.12.12 Ver.3.1  M. Nishio and K. Shibasaki (1GHz ATT 7dB -> 4.3)
;       98.09.03          sekix  & shinox   add 80GHz
;       98.11.20  K. Hori
;       1999-1-12 TY unify program for old/new NoRP data
;       1999-3-12 TY fimin,smax
;       2000-3-15 TY fisun 10min
;       2002-8-10 TY limit stat<32B; 
;         some datapoint had stat=48B on 2002-7-21 due to hardware error
;       2002-9-02 TY fix; wrong ATT status detection
;           the comment on 2002-8-10 is WRONG!
;
;-

if not keyword_set(smax) then smax=50
if not keyword_set(fimin) then fimin=2000

if not keyword_set(ndata) then begin
  ndata=lonarr(2)
  ndata(0)=0 & ndata(1)=n_elements(timejo)-1
endif

case tag_names(rdata,/structure_name) of
'NORP0' : version =0
'NORP' : version =1
endcase

date=gt_day(timejo(0),/st)

flxref=2400.0      ; SFU of reference device
sunmin=-50.
sunmax=200.
;skymax=-250.
skymax=0. ; 2000/4/6
wscalemax=0.03

tjaccept=['9:00','15:00']
tjaccept0=reform((anytim('1999-1-1 '+tjaccept,/_2xn,/time_only))(0,*))

case version of

0: begin   ; old format
vatt=[1.0,4.0,16.0]
npclb=0
iatt=[-512,512]
timejsky=date+' '+[ $
  [' 8:00:08',' 8:00:55'], ['10:00:08','10:00:55'], ['12:00:08','12:00:55'], $
  ['14:00:08','14:00:55'], ['16:00:08','16:00:55']]
;timejsun=date+' '+[ $
;  [' 8:09:59',' 8:10:14'], $
;  ['10:09:59','10:10:14'], $
;  ['12:09:59','12:10:14'], $
;  ['14:09:59','14:10:14'], $
;  ['16:09:59','16:10:14']]
mcx=69 & tmp=21900000+600000*lindgen(mcx) ; 6:05 JST - 10min - 17:05
  timec0=replicate(timejo(0),mcx) & timec0.time=tmp
  timec1=timec0 & timec1.time=tmp+15000
timejsun= transpose([[anytim(timec0,/ccsds)],[anytim(timec1,/ccsds)]])
dtsky=[6000,8000] ; msec
dtsun=[0,0] ; msec
dtatt=[1500,1500] ;msec
dtmgn=[900000,600000] ;msec
end

1: begin   ; new format
vatt=[1.0,4.0,16.0]
iatt=0
npclb=0
;timejsun=date+' '+[ $
;  [' 6:39:59',' 6:40:14'], $
;  [' 8:09:59',' 8:10:14'], $; [' 9:29:59',' 9:30:14'], $
;  ['10:09:59','10:10:14'], $; ['11:29:59','11:30:14'], $
;  ['12:09:59','12:10:14'], $; ['13:29:59','13:30:14'], $
;  ['14:09:59','14:10:14'], $
;  ['16:09:59','16:10:14'], $
;  ['17:09:59','17:10:14']]
mcx=69 & tmp=21900000+600000*lindgen(mcx) ; 6:05 JST - 10min - 17:05
  timec0=replicate(timejo(0),mcx) & timec0.time=tmp
  timec1=timec0 & timec1.time=tmp+15000
timejsun= transpose([[anytim(timec0,/ccsds)],[anytim(timec1,/ccsds)]])

dtsky=[30000,8000] ; msec
dtsun=[0,0] ; msec
dtatt=[0,0] ;msec
dtmgn=[900000,600000] ;msec
flagsky=1
end

endcase

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
case version of
0: intego=rdata.integ
1: intego=bytarr(n_elements(rdata))+1b
endcase
integ=intego(ndata(0):ndata(1))

timej=timejo(ndata(0):ndata(1))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
amp=rdata.amp35 & pol=rdata.pol35 & stat=rdata.stat35 

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;  extract the calibration data

if (version eq 0) then norp_nclb_time,timejsky,dtsky,timejo,mskyx,nsky,nskyi $
       else norp_nclb_stat,stat,flagsky,dtsky,timejo,mskyx,nsky,nskyi

norp_nclb_time,timejsun,dtsun,timejo,msunx,nsun,nsuni

if keyword_set(nocalib) then begin
  scale=1.
  scavg=1.
  fisky=0.
  fvsky=0.
  fizero=0.
  fvzero=0.
  goto,L100
endif
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;  check the calibration data

if ((mskyx eq 0) or (msunx eq 0)) then begin
  print,'***************************************'
  print,'  There is no calibration data 35G ! '
  print,'  # of valid data for SKY ',mskyx
  print,'  # of valid data for SUN ',msunx
  print,'***************************************'
  ndx=ndata(1)-ndata(0)+1
  fi=fltarr(ndx)
  fv=fltarr(ndx)
  mvalid=bytarr(ndx)
  return
endif

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;  integarage the calibration data

norp_clb_integ,mskyx,nsky,npclb,stat,vatt,oksky,intego $
  ,timejo,timejsky_m,amp,fisky_m,pol,fvsky_m,iatt=iatt,freq=35

norp_clb_integ,msunx,nsun,npclb,stat,vatt,oksun,intego $
  ,timejo,timejsun_m,amp,fisun_m,iatt=iatt,freq=35

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; check calibration data

whr=where((timejsky_m.time gt tjaccept0(1)) $
       or (timejsky_m.time lt tjaccept0(0)),cnt)
if cnt ge 1 then oksky(whr)=0  

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; check calibration data

whr=where(fisun_m lt sunmin,cnt)
if cnt ge 1 then oksun(whr)=0
whr=where(fisun_m gt sunmax,cnt)
if cnt ge 1 then oksun(whr)=0
whr=where(fisky_m gt skymax,cnt)
if cnt ge 1 then oksky(whr)=0

whr=where(oksky,moksky)
whr=where(oksun,moksun)
if ((moksky eq 0) or (moksun eq 0)) then begin
  print,'***************************************'
  print,'  There is no calibration data 35G ! '
  print,'  # of OK valid data for SKY ',moksky
  print,'  # of OK valid data for SUN ',moksun
  print,'***************************************'
  ndx=ndata(1)-ndata(0)+1
  fi=fltarr(ndx)
  fv=fltarr(ndx)
  mvalid=bytarr(ndx)
  return
endif
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; spline fit

whr=where(oksky,mok)
case 1 of
(mok ge 3) : begin
; fisky=spline(timejsky_m(whr).time,fisky_m(whr),timej.time)
; fvsky=spline(timejsky_m(whr).time,fvsky_m(whr),timej.time)
; res=linfit(timejsky_m(whr).time,fisky_m(whr))
; fisky=res(0)+res(1)*timej.time
; res=linfit(timejsky_m(whr).time,fvsky_m(whr))
; fvsky=res(0)+res(1)*timej.time
  fisky=total(fisky_m(whr))/n_elements(fisky_m(whr))
  fvsky=total(fvsky_m(whr))/n_elements(fvsky_m(whr))
end
(mok eq 2) : begin
  fisky=total(fisky_m(whr))*0.5
  fvsky=total(fvsky_m(whr))*0.5
end
(mok eq 1) : begin
  fisky=(fisky_m(whr))(0)
  fvsky=(fvsky_m(whr))(0)
end
endcase

whr=where(oksun,mok)
case 1 of
(mok ge 3) : begin
; fisun=spline(timejsun_m(whr).time,fisun_m(whr),timej.time)
; res=linfit(timejsun_m(whr).time,fisun_m(whr))
; fisun=res(0)+res(1)*timej.time
  fisun=total(fisun_m(whr))/n_elements(fisun_m(whr))
end
(mok eq 2) : begin
  fisun=total(fisun_m(whr))*0.5
end
(mok eq 1) : begin
  fisun=(fisun_m(whr))(0)
end
endcase

fizero=0
fvzero=0

scale=flxref/(fisun-fisky) ; ??? maybe incorrect

;scavg=total(scale)/n_elements(scale)

L100:
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;  restore the attainator, and check the valid data

stt0=stat(ndata(0):ndata(1))
amp0=float(amp(ndata(0):ndata(1)))/float(integ)
pol0=float(pol(ndata(0):ndata(1)))/float(integ)

npa=norp_gt_npa(stt0,iatt=iatt,freq=35)
natti=norp_gt_natti(npa,timej,dtatt)
fio=vatt(npa)*(amp0-fizero)+fizero
fvo=vatt(npa)*(pol0-fvzero)+fvzero

fi=scale*(fio-fisky)
fv=scale*(fvo-fvsky)

ndx=ndata(1)-ndata(0)+1
mvalid=(norp_mvalid(ndx,natti) $
    and norp_mvalid(ndx,nskyi-ndata(0)) $
    and (fi gt fimin) $
    and (scale gt 0) $
    and (scale lt smax))
;   and ((scale-scavg)/scavg lt wscalemax))

if n_elements(scale) eq 1 then begin
if (scale ge smax) then begin
  print,'***************************************'
  print,' 35G scale to large: scale =',scale
  print,'  scale limit : <',smax
  print,'***************************************'
endif
endif

if 0 then begin
if (ndata(0) eq 0) then begin
whr=min(where(mvalid,cnt))
if cnt ge 1 then begin
tstt=anytim2ints(timej(whr),offset=dtmgn(0)*1.e-3)
mstt=norp_t2i(timej.time,tstt.time)
mvalid(0:mstt)=0b
endif
endif

if (ndata(1) eq n_elements(timejo)-1) then begin
whr=max(where(mvalid,cnt))
if cnt ge 1 then begin
tend=anytim2ints(timej(whr),offset=-dtmgn(1)*1.e-3)
mend=norp_t2i(timej.time,tend.time)
mvalid(mend:*)=0b
endif
endif
endif

if not keyword_set(nocalib) then begin
calibration={ $
scale:scale $
,oksky:oksky,timejsky_m:timejsky_m,fisky_m:fisky_m,fvsky_m:fvsky_m $
,fisky:fisky,fvsky:fvsky $
,oksun:oksun,timejsun_m:timejsun_m,fisun_m:fisun_m,fisun:fisun $
}
endif

end
