function norp_gt_npa,stat,iatt=iatt,freq=freq

;+
; NAME:
;       NORP_GT_NPA
;
; PURPOSE:
;       This function is for getting the mode number of ATT
;
; CALLING SEQUENCE:
;       npa=norp_gt_npa(stat)
;
; INPUTS:
;       stat: 1-dimensional integer array indicating the status of observation
;
; OPTIONAL INPUT:
;       iatt: 1-dimensional integer array indicating 
;             threshold value for the status of ATT (only for old NoRP)
;
; OUTPUTS:
;       npa: 1-dimensional integer array indicating the ATT mode
;
; 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)
;       95.09.06 V1.0 M. Nishio
;       95.11.10 V1.1 K. Hori and M. Nishio debug
;       96.04.01      M. Nishio (bug fix)
;       98.08.28      H.Sekiguchi: add 80GHz
;       98.11.17      KH:   Done only for specified data period
;       98.09.03          sekix  & shinox   add 80GHz
;       98.11.20  K. Hori
;       1999-1-12 TY unify program for old/new NoRP data
;       2002-9-02 TY fix; wrong ATT status detection
;
;-

npa=intarr(n_elements(stat))
if (n_elements(freq) eq 0) then freq=0 ; defarult 1-17 GHz

if keyword_set(iatt) then begin ;old

for matt=0,n_elements(iatt)-1 do begin
  whr=where((stat gt iatt(matt)),count)
  if (count ge 1) then npa(whr)=matt+1
endfor

endif else begin ;new

  case freq of
    35 : begin
      for m=0,2 do begin
       whr=where((stat and 'F0'xb) eq 16*(2^(m+1)-1),count)
       if (count ge 1) then npa(whr)=m+1
      endfor
    end
    80 : begin
      for m=0,2 do begin
       whr=where((stat and 'F0'xb) eq 16*(2^m),count)
       if (count ge 1) then npa(whr)=m+1
      endfor
    end
    else : begin
      for m=0,7 do begin
       whr=where((stat and 'F0'xb) eq 16*(m+1),count)
       if (count ge 1) then npa(whr)=m+1
      endfor
    end
  endcase

endelse

return,npa
end
