; ------------------------------------------------------------------
;                             Formatage RH
; ------------------------------------------------------------------
;
;INPUTS:
; c :	compression
; nof:	numero de frequence
; h:	[hr, min, sec, cent]
; itg:	points a formatter
;
;INPUT/OUTPUT:
; rec:	structure formatee
;
; ------------------------------------------------------------------
;
; ------------------------------------------------------------------
;                            Calcul du gain
; ------------------------------------------------------------------
; Permet de cadrer sur 16 bits 
; gain = rh_gain(pt)
; ------------------------------------------------------------------

FUNCTION RH_GAIN, pt

 M = MAX(ABS(pt))
 G = 0. 
 IF M NE 0 THEN G = (ALOG(M)-ALOG(32767))/ALOG(2)
 IF G GT 0 THEN c = 1. ELSE c = 0.
 RETURN, FIX(G+c)
END

; ---------------------------------------------------------------- 
;                  g = RH_GAIN_MONO(pt)
; ---------------------------------------------------------------- 
; Calcul les gains d'une acquisition	
; pt  : points 				fltarr(4, entFI.nval/4)
; g   : gain EW, NS, 2D			intarr(entFI.typ+1)
; -----------------------------------------------------------------------------  

FUNCTION rh_gain_mono, pt

@rh_common.inc

;Correlations EW
  ind = WHERE(sFI.tFI(*,0:entFI.nval/4-1) EQ 0)
;print,ind
;stop
  gew = rh_gain(pt(ind))
  
;Correlations NS
  ind = WHERE(sFI.tFI(*,0:entFI.nval/4-1) EQ 1)
  gns = rh_gain(pt(ind))

  g = [gew, gns]
  IF entFI.typ EQ 2 THEN BEGIN
     ind = WHERE(sFI.tFI(*,0:entFI.nval/4-1) EQ 2)
     g2d = rh_gain(pt(ind))
     g = [g, g2d]
  ENDIF
  RETURN, g

END

; ------------------------------------------------------------------
;                            RH_FORMAT
; ------------------------------------------------------------------

PRO rh_format, rec, c, nof, h, itg      

@rh_common.inc
       
   rec.c = c
   rec.nof = nof
   rec.h = h
   CASE n_elements(rec) OF
     '1': BEGIN
          g = rh_gain_mono(itg)
          CASE entFI.typ OF
             '1' : rec.g = g
             '2' : rec.g = [g(2), g(0:1)]
          ENDCASE
          g = 1./(2.^g)
;          FOR j = 0, entFI.nval/4-1 DO rec.pt(*,j) = $
;              fix(g(sFI.tFI(j))*itg(*,j))
           rec.pt=fix(g(sfi.tfi)*itg)
          END
    ELSE: BEGIN
          FOR i=0, n_ELEMENTS(rec)-1 DO BEGIN
              g = rh_gain_mono(itg(*,*, i))
              CASE entFI.typ OF
                '1' : rec(i).g = g
                '2' : rec(i).g = [g(2), g(0:1)]
              ENDCASE
              g = 1./(2.^g)
;              FOR j = 0, entFI.nval/4-1 DO rec(i).pt(*,j) = $
;                      fix(g(sFI.tFI(j))*itg(*,j,i))
               rec(i).pt=fix(g(sfi.tfi)*itg(*,*,i))
          ENDFOR
          END
    ENDCASE
    IF entFI.endian EQ 1 THEN rec = SWAP_ENDIAN(rec)
    RETURN
END
