pro norp_clb_integ $
  ,mclbx,nclb,npclb,stat,vatt,okclb,integ $
  ,timej,timejclb_m,amp,ficlb_m,pol,fvclb_m $
  ,sqd,sqdclb_m $
  ,iatt=iatt,freq=freq

;+
; NAME:
;       NORP_CLB_INTEG
;
; PURPOSE:
;       This procedure is for integrating the calibration data
;       in each sequences
;
; CALLING SEQUENCE:
;       norp_clb_integ,mclbx,nclb,npclb,stat,vatt,okclb,integ $
;         ,timej,timejclb_m,amp,ficlb_m,pol,fvclb_m 
;             for 1-35 GHz
;       norp_clb_integ,mclbx,nclb,npclb,stat,vatt,okclb,integ $
;         ,timej,timejclb_m,rs,rsclb_m,ls,lsclb_m,sqd,sqdclb_m
;             for 80 GHz
;
; INPUTS:
;       mclbx: number of calibration sequences
;       nclb: 2-dimensional array. nclb(0,*) is the start position index,
;             nclb(1,*) is the end of calibration time in 'time' array.
;       npclb: the mode of ATT which should be at the calibration
;       stat: 1-dimensional integer array indicating the status of observation
;       vatt: ATT shift value in each mode
;       okclb: if 1 the calibration was successful.
;       integ: exposure
;       timej: structure array with 'time internal format'
;               that must be given by 'norp_rdt2timej'.
;       amp,pol,sqd : data value at the calibration
;
; OPTIONAL INPUT :
;       iatt: 1-dimensional integer array indicating
;             threshold value for the status of ATT (only for old NoRP)
;
; OUTPUTS:
;       timejclb_m: median time of calibration
;       amp_m,pol_m,sqd_m : integrated average in calibration sequence
;
; 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
;       2000-4-11 TY 
;       2002-9-02 TY fix; wrong ATT status detection
;
;-

if (n_params() ge 13) then fvflg=1 else fvflg=0
if (n_params() ge 15) then sqdflg=1 else sqdflg=0

okclb=bytarr(mclbx)+1b
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

whr=[-1]
for mclb=0,mclbx-1 do begin
  if (nclb(1,mclb) gt nclb(0,mclb)) then begin
    whr=[whr,lindgen(nclb(1,mclb)-nclb(0,mclb))+nclb(0,mclb)]
  endif else begin
    okclb(mclb)=0b
  endelse
endfor
whr=whr(1:*)

timejclb=timej(whr)
ampclb=amp(whr)
statclb=stat(whr)
integclb=integ(whr)
if fvflg then polclb=pol(whr)
if sqdflg then sqdclb=sqd(whr)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

npa=norp_gt_npa(statclb,iatt=iatt,freq=freq)

ficlb=vatt(npa)*float(ampclb)
if fvflg then fvclb=vatt(npa)*float(polclb)
if sqdflg then sqdclb=vatt(npa)*float(sqdclb)

ficlb_m=fltarr(mclbx)
if fvflg then fvclb_m=fltarr(mclbx)
if sqdflg then sqdclb_m=fltarr(mclbx)
timejclb_m=replicate(timejclb(0),mclbx)

nclb_st=0 & nclb_ed=-1
for mclb=0,mclbx-1 do begin
 if (okclb(mclb) ne 0) then begin
  nclb_st=nclb_ed+1
  nclb_ed=nclb_st+(nclb(1,mclb)-nclb(0,mclb))-1

  whr=where((npa(nclb_st:nclb_ed) ne npclb),count)
  if (count ge 1) then okclb(mclb)=0

  timejclb_m(mclb) $
   =anytim((anytim(timejclb(nclb_st))+anytim(timejclb(nclb_ed)))/2.0d,/ints)
  ficlb_m(mclb)=total(ficlb(nclb_st:nclb_ed))/total(integclb(nclb_st:nclb_ed))
 if fvflg then $
  fvclb_m(mclb)=total(fvclb(nclb_st:nclb_ed))/total(integclb(nclb_st:nclb_ed))
 if sqdflg then $
  sqdclb_m(mclb)=total(sqdclb(nclb_st:nclb_ed))/total(integclb(nclb_st:nclb_ed))

 endif
endfor

return
end
