!--------------------------------------- LICENCE BEGIN -----------------------------------
!Environment Canada - Atmospheric Science and Technology License/Disclaimer,
!                     version 3; Last Modified: May 7, 2008.
!This is free but copyrighted software; you can use/redistribute/modify it under the terms
!of the Environment Canada - Atmospheric Science and Technology License/Disclaimer
!version 3 or (at your option) any later version that should be found at:
!http://collaboration.cmc.ec.gc.ca/science/rpn.comm/license.html
!
!This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
!without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
!See the above mentioned License/Disclaimer for more details.
!You should have received a copy of the License/Disclaimer along with this software;
!if not, you can write to: EC-RPN COMM Group, 2121 TransCanada, suite 500, Dorval (Quebec),
!CANADA, H9P 1J3; or send e-mail to service.rpn@ec.gc.ca
!-------------------------------------- LICENCE END --------------------------------------


module modgpsro_mod 8,2
  use mpi_mod
  use modgps04profile, only: gpsprofile
  implicit none
  save
  private

  ! public variables
  public :: numGPSROProfiles,vGPSRO_IndexPrf,vGPSRO_Jacobian,vGPSRO_lJac
  public :: LEVELGPSRO,GPSRO_MAXPRFSIZE,NUMGPSSATS,IGPSSAT,SURFMIN,HSFMIN,HTPMAX,BGCKBAND,WGPS

  ! public procedures
  public :: sugpsro,iprofile_from_index


! Values determined through namelist:
! 
! INTEGER LEVELGPSRO
! REAL*8  SURFMIN, HSFMIN, HTPMAX, BGCKBAND

!
! Values determined by input data:
!
  integer                                :: numGPSROProfiles
  integer         , allocatable          :: vGPSRO_IndexPrf(:)    ! index for each profile
  real*8          , allocatable          :: vGPSRO_Jacobian(:,:,:)
  logical         , allocatable          :: vGPSRO_lJac(:)

!     Contents of previous comdeck comgpsro
!     -------------------------------------
!*    Control variables for GPSRO observations - constant within job
!
!     LEVELGPSRO: Data level to use (1 for bending angle, 2 for refractivity)
!     GPSRO_MAXPRFSIZE: Maximal number of data that is expected from a profile (default 300)
!     SURFMIN:  Minimum allowed distance to the model surface (default 1000 m)
!     HSFMIN:   Minimum allowed MSL height of an obs          (default 4000 m)
!     HTPMAX:   Maximum allowed MSL height of an obs          (default 40000 m)
!     BGCKBAND: Maximum allowed deviation abs(O-P)/P          (default 0.05)
!
!     J.M. Aparicio, Apr 2008
!          
  INTEGER LEVELGPSRO, GPSRO_MAXPRFSIZE,NUMGPSSATS,IGPSSAT(50)
  REAL*8  SURFMIN, HSFMIN, HTPMAX, BGCKBAND, WGPS(50)

  NAMELIST /NAMGPSRO/ LEVELGPSRO,GPSRO_MAXPRFSIZE,SURFMIN,HSFMIN,HTPMAX,BGCKBAND,NUMGPSSATS,IGPSSAT,WGPS

contains


  subroutine sugpsro 1,1
    implicit none
    integer nulnam,ierr,fnom,fclos,j
!
!   Define default values:
!
    LEVELGPSRO = 2
    GPSRO_MAXPRFSIZE = 300
    SURFMIN    = 0.d0
    HSFMIN     = 0.d0
    HTPMAX     = 70000.d0
    BGCKBAND   = 0.05d0
    NUMGPSSATS = 0
!
!   Override with NML values:
!     
    nulnam=0
    ierr=fnom(nulnam,'./flnml','FTN+SEQ+R/O',0)
    read(nulnam,nml=NAMGPSRO,iostat=ierr)
    if(ierr.ne.0) call abort3d('sugpsro: Error reading namelist')
    if(mpi_myid.eq.0) write(*,nml=NAMGPSRO)
    ierr=fclos(nulnam)
    if(mpi_myid.eq.0) write(*,*)'NAMGPSRO',LEVELGPSRO,GPSRO_MAXPRFSIZE,SURFMIN,HSFMIN,HTPMAX,BGCKBAND,NUMGPSSATS
!
!   Force a min/max values for the effective Fresnel widths per satellite:
!
    DO J=1,NUMGPSSATS
       IF (WGPS(J).LT. 100.d0) WGPS(J)= 100.d0
       IF (WGPS(J).GT.1500.d0) WGPS(J)=1500.d0
    ENDDO

  end subroutine sugpsro


  integer function iProfile_from_index(index) 4
    implicit none
    integer, intent(in) :: index
    integer i

    iProfile_from_index=-1
    do i=1,size(vGPSRO_IndexPrf)
       if (index.eq.vGPSRO_IndexPrf(i)) then
          iProfile_from_index=i
          return
       endif
    enddo
    return
  end function iProfile_from_index

end module modgpsro_mod