 pro tykw_rd_dat,arg0,arg1,arg2,arg3,arg4,arg5,arg6,arg7 $
   ,rdfreq=rdfreq,timerange=timerange,npa=npa,raw=data0,stat=dstt $
   ,usewk=usewk,dir=dir

;+
; NAME:
;       TYKW_RD_DAT
;
; PURPOSE:
;       This procedure is for reading and calibrating the TYKW data.
;
; CALLING SEQUENCE:
;       tykw_rd_dat,file,file0pa,filestt,mvalid,time,data
;       tykw_rd_dat,day,mvalid,time,fi,fv
;
; INPUTS:
;       file: file name of TYKW data
;       file0pa: file name of TYKW 0p0a data
;       filestt: file name of TYKW status data
;
; OPTIONAL INPUT :
;       timerange: 2-elements string array indicating the
;                  time range of the data to be read
;             e.g. ['1998-1-1 4:00','1998-1-1 5:00']
;
; 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.
;       time: structure array with 'time internal format'. Time is in UT.
;       data:  data array 
;
; OPTIONAL OUTPUT :
;       npa: 1-dimensional integer array indicating the ATT mode
;
; HISTORY:
;    4/6/79        ver.0.0 K. Shibasaki as fortran program
;    Sep. 09, 1994 ver.1.0 M. Nishio
;    Jan. 10, 1995 ver.2.0 M. Nishio
;    Apr. 13, 1995 ver.3.2 M. Nishio
;    Apr. 13, 1995 ver.4.0 M. Nishio
;    May. 23, 1995 ver.5.0 M. Nishio
;    May  01, 1995 ver.5.2 M. Nishio
;    Apr. 25, 1995 ver.5.0 M. Nishio
;
;    1999-01-12 ported to IDL by TY
;    1999-03-16 debug by TY
;    2000-04-11 by TY read by day
;
;-

if not keyword_set(rdfreq) then rdfreq=bytarr(4)+1b
freq_all=[1,2,3.75,9.4]

print,arg0

;if (n_params() eq 6) then begin  ; 'file' is date
if (strpos(arg0,'ty') eq -1) then begin  ; 'file' is date
  day=arg0
  ex=anytim(day,/ex)
  yyyy=reform(ex(6,*))
  mm=reform(ex(5,*))
  dd=reform(ex(4,*))
  yy=yyyy-yyyy/100*100
  yymmdd=$
  string(transpose([[yy],[mm],[dd]]),format="(i2.2,i2.2,i2.2)")
  year=strtrim(string(yyyy),2)
  files='ty'+yymmdd+'*'
 if not keyword_set(dir) then begin
  if keyword_set(usewk) then begin
   dir=getenv('NORP_DWK')
  endif else begin
   dir0=getenv('NORP_RAW')
   dir=[dir0+'/'+year+'/'+string(mm,format="(i2.2)")]
  endelse
 endif
  infil=file_list(dir,files)
  flist=['0pa','stt']
  for m=0,1 do begin
    pos=strpos(infil,flist(m))
    whr=where(pos ne -1,count)
    if (count eq 0) then begin
      print,'Cannot read data: 0pa or stt file does not exist'
      mvalid=-1
      arg1=mvalid
      return
    endif
    if (m eq 0) then file0pa=infil(whr(0))
    if (m eq 1) then filestt=infil(whr(0))
  endfor

  flist=['01','02','04','09']
  exlist=bytarr(4)
  filist=strarr(4)
  fvlist=strarr(4)
  for m=0,3 do begin
    pos=strpos(infil,flist(m)+'i')
    whr=where(pos ne -1,count)
    if (count eq 0) then begin
      print,' '+flist(m)+'i file does not exist'
      goto,L100
    endif
    filist(m)=infil(whr(0))

    pos=strpos(infil,flist(m)+'v')
    whr=where(pos ne -1,count)
    if (count eq 0) then begin
      print,' '+flist(m)+'v file does not exist'
      goto,L100
    endif
    fvlist(m)=infil(whr(0))
    exlist(m)=1

L100 :
  endfor

whr=where(rdfreq and exlist,mfx)
if mfx eq 0 then goto,L200

mf=0

  for m=0,3 do begin
    if (rdfreq(m) and exlist(m)) then begin
      tykw_rd_dat,filist(m),file0pa,filestt,mvd0,tim0,fi0 $
      ,timerange=timerange,usewk=usewk
      tykw_rd_dat,fvlist(m),file0pa,filestt,mvdv,timv,fv0 $
      ,timerange=timerange,usewk=usewk
      if (n_elements(tim0) ne n_elements(timv)) then begin
        print,'Warning: Number of elements different between I  and V'
        goto,L110
      endif
      if (mf eq 0) then begin
        ndx=n_elements(tim0)
        tim=tim0
        fi=fltarr(mfx,ndx)
        fv=fltarr(mfx,ndx)
        mvalid=bytarr(mfx,ndx)
        freq=fltarr(mfx)
      endif else begin
        if (n_elements(tim0) ne ndx) or (n_elements(timv) ne ndx) then begin
          print,'Warning: Number of elements different among frequencies'
          goto,L110
        endif
      endelse
      fi(mf,*)=fi0
      fv(mf,*)=fv0
      mvalid(mf,*)=mvd0
      freq(mf)=freq_all(m)
      mf=mf+1
    endif
L110:
  endfor

L200:

arg1=mvalid & arg2=tim & arg3=fi & arg4=fv & arg5=freq

endif else begin

file=arg0 & file0pa=arg1 & filestt=arg2

  infil=file

fisat=9.e5
dtatt=[0,5000] ;msec
dtclb=[100,3000] ;msec
pfact=[2.,2.,2.58,2.]   ; gain amplification ratio for V-component
fimin=[280,85,80,50]
dtmgn=lonarr(4,2) ;msec
dtmgn(*,0)=400000
dtmgn(3,1)=20000
dtmgn(1,1)=20000
dtmgn(0,1)=600000

; read raw data
tykw_rd_rdt,file,rdata
tykw_rd_0pa,file0pa,rd0pa
tykw_rd_stt,filestt,rdstt

mfreq =tykw_gt_mfreq(rdata)    ; 0: 9G, 1: 4G, 2: 2G, 3: 1G
miv   =tykw_gt_miv(rdata)      ; 0: I,  1: V
timejo=tykw_rdt2timej(rdata)
data  =tykw_reform(rdata)
d0pa  =tykw_0pa_freq(rd0pa,mfreq)
dstt  =tykw_stt_freq(rdstt,mfreq)

ndata=lonarr(2)
ndata(0)=0 & ndata(1)=n_elements(timejo)-1
if n_elements(timerange) eq 2 then begin
  timejrange=anytim2ints(anytim(timerange,/yohkoh),offset=9.*60*60)
  norp_gt_ndata,timejrange,timejo,ndata
endif
ndx=ndata(1)-ndata(0)+1

timej=timejo(ndata(0):ndata(1))
time=anytim2ints(timej,offset=-9.*60.*60)

; attenuation reconstruction
npa=tykw_gt_npa(timej,dstt)
vatt=[1,d0pa.at]
if (miv eq 0) then begin    ; I-component
  att_base=d0pa.zeroi
  if (mfreq eq 1) then att_base=d0pa.ambi   ; only for 4GHz
endif else begin            ; V-component
  att_base=d0pa.zerov
  if (mfreq eq 3) then att_base=-10. ; only for 1GHz
  if (mfreq eq 1) then att_base=d0pa.ambv   ; only for 4GHz
endelse

data0=data(ndata(0):ndata(1))
data=(data0-att_base)*vatt(npa)+att_base

; reference is quiet sun
scale=d0pa.flux/(d0pa.suni-d0pa.skyi)   ; ??? maybe incorrect
if (miv eq 0) then begin
  data=scale*(data-d0pa.skyi)<fisat
endif else begin
  data=scale*((data-d0pa.skyv)<fisat)>(-fisat)/pfact(mfreq)
endelse

; unset flag for attenuation/calibration/interuption period
natti=norp_gt_natti(npa,timej,dtatt)
mvalida=norp_mvalid(ndx,natti)
mvalidc=tykw_mvalid_clb(timej,dstt,dtclb)
mvalidi=tykw_mvalid_int(timej,d0pa)

mvalid=(mvalidc and mvalida and mvalidi)
if (miv eq 0) then mvalid=(mvalid and (data gt fimin(mfreq)))

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(mfreq,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(mfreq,1)*1.e-3)
mend=norp_t2i(timej.time,tend.time)
mvalid(mend:*)=0b
endif
endif
endif

arg3=mvalid & arg4=time & arg5=data

endelse

end

