pro norp_nclb_time $
   ,timejclb,dtclb,timej $
   ,mclbx,nclb,nclbi

;+
; NAME:
;       NORP_NCLB_TIME
;
; PURPOSE:
;       This procedure is to searching for the position index of 
;       calibration sequence in time array by the time information array
;
; CALLING SEQUENCE:
;       norp_nclb_time,timejclb,dtclb,timej,mclbx,nclb,nclbi
;
; INPUTS:
;       timejclb: array of 2-elements string arrays indicating the
;          start and end of the calibration.
;          e.g. ['1998-1-4 11:30:00','1998-1-4 11:31:00']
;       dtclb: time margin for nclbi. 2 elements floating array.
;              dtclb(0) is before the calibration, dtclb(1) is after.
;       timej: structure array with 'time internal format'.
;
; OPTIONAL INPUT KEYWORDS:
;
; OUTPUTS:
;       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.
;       nclbi: 2-dimensional array with time mergin corresponding to 'nclb'
;
; 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 make faster
;
;-

sz=size(timejclb) & if (sz(0) eq 2) then mclbx=sz(2) else mclbx=1
if mclbx ne 0 then begin
  nclb=lonarr(2,mclbx)
  nclbi=lonarr(2,mclbx)
  mvld=bytarr(mclbx)+1b

  dt00=timej(1).time-timej(0).time
  dt01=fix(dt00*0.9)
  dt02=fix(dt00*1.1)
  tmp00=(timej.time-shift(timej.time,1))(1:*)
  whr1=where(tmp00 gt dt02 or tmp00 lt dt01, count)
 if (count eq 0) then begin ; faster uniform temporal spacing

  tmp0=anytim(reform(timejclb(0,*)),/ints)
  tmp1=anytim(reform(timejclb(1,*)),/ints)
  dt=(tmp1.time-tmp0.time)/dt00
  dtclb0=dtclb/dt00

  nclb(0,*)=norp_t2i(timej.time,tmp0.time)
  nclb(1,*)=nclb(0,*)+dt
  nclbi(0,*)=nclb(0,*)-dtclb0(0)
  nclbi(1,*)=nclb(1,*)+dtclb0(1)

  whr=where( $
    ((tmp0.time gt timej(n_elements(timej)-1).time) or $
     (tmp1.time lt timej(0                  ).time)) ,count)
  if (count ge 1) then mvld(whr)=0
 endif else begin

  for mclb=0,mclbx-1 do begin
    tmp=anytim(reform(timejclb(*,mclb)),/ints)
    nclb(0,mclb)=norp_t2i(timej.time,tmp(0).time)
    nclb(1,mclb)=norp_t2i(timej.time,tmp(1).time)

    tclbi_st=timej(nclb(0,mclb)).time-dtclb(0)
    nclbi(0,mclb)=norp_t2i(timej.time,tclbi_st)
    tclbi_ed=timej(nclb(1,mclb)).time+dtclb(1)
    nclbi(1,mclb)=norp_t2i(timej.time,tclbi_ed)
    if ((tmp(0).time gt timej(n_elements(timej)-1).time) or $
        (tmp(1).time lt timej(0                  ).time)) then mvld(mclb)=0
  endfor
 endelse
endif

mclbx=fix(total(mvld))
if mclbx ne 0 then begin
  nclb=nclb(*,where(mvld))
  nclbi=nclbi(*,where(mvld))
endif


return
end
