pro norp_rd_rdt,file,rdata,date,version

;+
; NAME:
;       NORP_RD_RDT
;
; PURPOSE:
;       This procedure is for reading the raw data of NoRP.
;
; CALLING SEQUENCE:
;       norp_rd_rdt,file,rdata,date,version
;
; INPUTS:
;       file: file name of NoRP data
;
; OPTIONAL INPUT KEYWORDS:
;
; OUTPUTS:
;       rdata: structure of the raw data
;       date: observation date of the data
;       versionNoRP data ata format version
;
; 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-5-10 TY bug fix for filename
;       2002-4-11 TY, endian-free for x86 architecture
;
;-

len=strlen(file)
pos0=0 & point=0
while 1 do begin
  pos0=strpos(strmid(file,point,len-point),'pl')
  if (pos0 eq -1) then goto,label0
  pos=point+pos0
  point=pos+2
endwhile
label0:
yyst= strmid(file,pos+2,2)
mnst= strmid(file,pos+4,2)
ddst= strmid(file,pos+6,2)
yy= fix(yyst) & mn= fix(mnst) & dd= fix(ddst)
if (yy ge 50) then ccyy=1900+yy else ccyy=2000+yy
ccyyst=string(ccyy,form='(i4.4)')
date=ccyyst+'-'+mnst+'-'+ddst

date_renew='1994-3-1'

if (anytim(date) lt anytim(date_renew)) then version=0 else version=1

case version of 

 0: begin  ; old format

  inrec = { norp0, $
     dummy: byte(0), hours: byte(0), minut: byte(0), secnd: byte(0), $
     msec1: byte(0), msec2: byte(0), $
     integ:0, $
     amp17:0, pol17:0, stat17:0, $
     amp35:0, pol35:0, stat35:0, $
     rc80:0, rs80:0, lc80:0, ls80:0, sqd80:0, stat80:0, $
     pad: intarr(4) }

  openr,uin,file,/get_lun
    file_info= fstat(uin)
    nmax=file_info.size/40
    rdata = replicate( {norp0} , nmax )
    readu,uin,rdata
  free_lun,uin

  if (!version.arch eq 'x86') then begin ; swap endian
    tmp=rdata.integ & byteorder,/ntohs,tmp & rdata.integ=tmp
    tmp=rdata.amp17 & byteorder,/ntohs,tmp & rdata.amp17=tmp
    tmp=rdata.pol17 & byteorder,/ntohs,tmp & rdata.pol17=tmp
    tmp=rdata.stat17 & byteorder,/ntohs,tmp & rdata.stat17=tmp
    tmp=rdata.amp35 & byteorder,/ntohs,tmp & rdata.amp35=tmp
    tmp=rdata.pol35 & byteorder,/ntohs,tmp & rdata.pol35=tmp
    tmp=rdata.stat35 & byteorder,/ntohs,tmp & rdata.stat35=tmp
    tmp=rdata.rc80  & byteorder,/ntohs,tmp & rdata.rc80 =tmp
    tmp=rdata.rs80  & byteorder,/ntohs,tmp & rdata.rs80 =tmp
    tmp=rdata.lc80  & byteorder,/ntohs,tmp & rdata.lc80 =tmp
    tmp=rdata.ls80  & byteorder,/ntohs,tmp & rdata.ls80 =tmp
    tmp=rdata.sqd80 & byteorder,/ntohs,tmp & rdata.sqd80=tmp
    tmp=rdata.stat80 & byteorder,/ntohs,tmp & rdata.stat80=tmp
  endif
 end

 1 : begin  ; new format

  inrec = { norp, $
      hours: byte(0), minut: byte(0),$
      secnd: byte(0), msec1: byte(0), msec2: byte(0),$
      stat1g: byte(0), stat2g: byte(0), stat4g: byte(0),$
      stat9g: byte(0), stat17: byte(0), stat35: byte(0),$
      stat80: byte(0),$
      amp1g:0, pol1g:0, amp2g:0, pol2g:0, amp4g:0, pol4g:0,$
      amp9g:0, pol9g:0, amp17:0, pol17:0, amp35:0, pol35:0,$
      rc80:0, rs80:0, lc80:0, ls80:0, sqd80:0 }

  openr,uin,file,/get_lun
    file_info= fstat(uin)
    nmax=file_info.size/46
    rdata = replicate( {norp} , nmax )
    readu,uin,rdata
  free_lun,uin

  if (!version.arch eq 'x86') then begin ; swap endian
    tmp=rdata.amp1g & byteorder,/ntohs,tmp & rdata.amp1g=tmp
    tmp=rdata.pol1g & byteorder,/ntohs,tmp & rdata.pol1g=tmp
    tmp=rdata.amp2g & byteorder,/ntohs,tmp & rdata.amp2g=tmp
    tmp=rdata.pol2g & byteorder,/ntohs,tmp & rdata.pol2g=tmp
    tmp=rdata.amp4g & byteorder,/ntohs,tmp & rdata.amp4g=tmp
    tmp=rdata.pol4g & byteorder,/ntohs,tmp & rdata.pol4g=tmp
    tmp=rdata.amp9g & byteorder,/ntohs,tmp & rdata.amp9g=tmp
    tmp=rdata.pol9g & byteorder,/ntohs,tmp & rdata.pol9g=tmp
    tmp=rdata.amp17 & byteorder,/ntohs,tmp & rdata.amp17=tmp
    tmp=rdata.pol17 & byteorder,/ntohs,tmp & rdata.pol17=tmp
    tmp=rdata.amp35 & byteorder,/ntohs,tmp & rdata.amp35=tmp
    tmp=rdata.pol35 & byteorder,/ntohs,tmp & rdata.pol35=tmp
    tmp=rdata.rc80  & byteorder,/ntohs,tmp & rdata.rc80 =tmp
    tmp=rdata.rs80  & byteorder,/ntohs,tmp & rdata.rs80 =tmp
    tmp=rdata.lc80  & byteorder,/ntohs,tmp & rdata.lc80 =tmp
    tmp=rdata.ls80  & byteorder,/ntohs,tmp & rdata.ls80 =tmp
    tmp=rdata.sqd80 & byteorder,/ntohs,tmp & rdata.sqd80=tmp
  endif

 end

endcase

return
end
