! $Id$
MODULE open_climoz_m

  USE print_control_mod, ONLY: lunout
  IMPLICIT NONE

CONTAINS

  !-------------------------------------------------------------------------------
  !
  SUBROUTINE open_climoz(ncid, press_in_cen, time_in)
    !
    !-------------------------------------------------------------------------------
    USE netcdf95, ONLY: nf95_open, nf95_close, nf95_gw_var, nf95_inq_varid
    USE netcdf,   ONLY: nf90_nowrite
    use conf_phys_m, only: ok_daily_climoz, adjust_tropopause
    use mod_phys_lmdz_omp_data, only: is_omp_root
    USE mod_phys_lmdz_mpi_data,      ONLY: is_mpi_root
    USE mod_phys_lmdz_transfert_para, ONLY: bcast
    USE phys_cal_mod,                ONLY: calend, year_len, year_cur
    !-------------------------------------------------------------------------------
    ! Purpose: This procedure should be called once per "gcm" run, by a single
    !          thread of each MPI process.
    !          The root MPI process opens "climoz_LMDZ.nc", reads the pressure
    ! levels and the times and broadcasts them to the other processes.
    !          We assume that, in "climoz_LMDZ.nc", the pressure levels are in hPa
    ! and in strictly ascending order.
    !-------------------------------------------------------------------------------
    INTEGER, INTENT(OUT):: ncid      !--- "climoz_LMDZ.nc" identifier
    REAL, allocatable, intent(out):: press_in_cen(:) !--- at cells centers
    REAL, allocatable, intent(out):: time_in(:)      !--- records times, in days since Jan. 1st
    !   pressure levels press_in_cen/edg are in Pa a,d strictly ascending order.
    !   time_in is only used for monthly files (14 records)
    !-------------------------------------------------------------------------------
    ! Local variables:
    INTEGER :: varid                 !--- NetCDF variables identifier
    INTEGER :: nlev, ntim            !--- pressure levels and time records numbers
    CHARACTER(LEN=80)  :: sub
    CHARACTER(LEN=320) :: msg
    !-------------------------------------------------------------------------------
    sub="open_climoz"
    WRITE(lunout,*)"Entering routine "//TRIM(sub)

    IF (is_mpi_root .and. is_omp_root) THEN

       !--- OPEN FILE, READ PRESSURE LEVELS AND TIME VECTOR
       CALL nf95_open("climoz_LMDZ.nc", nf90_nowrite, ncid)
       CALL nf95_inq_varid(ncid, "plev", varid)
       CALL nf95_gw_var(ncid, varid, press_in_cen)
       ! Convert from hPa to Pa because "paprs" and "pplay" are in Pa:
       press_in_cen = press_in_cen * 100.
       nlev = SIZE(press_in_cen)
       CALL NF95_INQ_VARID(ncID, "time", varID)
       CALL NF95_GW_VAR(ncid, varid, time_in)
       ntim = SIZE(time_in)

       !--- CHECK RECORDS NUMBER AND DISPLAY CORRESPONDING INFORMATION
       IF(ok_daily_climoz.AND.ntim/=year_len) THEN
          WRITE(msg,'(a,3(i4,a))')TRIM(sub)//': Expecting a daily ozone file with',&
               &year_len,' records (year ',year_cur,') ; found ',ntim,' instead'
          CALL abort_physic(sub, msg, 1)
       ELSE IF(ALL([360,14]/=ntim)) THEN
          WRITE(msg,'(a,i4,a)')TRIM(sub)//': Expecting an ozone file with 14 (mont'&
               &//'hly case) or 360 (old style files) records ; found ',ntim,' instead'
          CALL abort_physic(sub, msg, 1)
       ELSE
          IF(ok_daily_climoz) THEN
             WRITE(msg,'(a,2(i4,a))')'daily file (',ntim,' days in ',year_cur,')'
          ELSE IF(ntim==14) THEN
             msg='14 records monthly file'
          ELSE
             msg='360 records files (old convention)'
          END IF
          WRITE(lunout,*)TRIM(sub)//': Using a '//TRIM(msg)
       END IF

       !--- MESSAGE ABOUT OPTIONAL STRETCHING FOR TROPOPAUSES MATCHING
       IF(adjust_tropopause) THEN
          WRITE(lunout,*)TRIM(sub)//': Adjusting O3 field to match gcm tropopause.'
       ELSE
          WRITE(lunout,*)TRIM(sub)//': Interpolating O3 field directly on gcm levels.'
       END IF
    END IF

    CALL bcast(nlev)
    CALL bcast(ntim)

    IF (.NOT. is_mpi_root .or. .not. is_omp_root) then
       ALLOCATE(press_in_cen(nlev  ))
       ALLOCATE(time_in(ntim))
    end IF

    CALL bcast(press_in_cen)
    CALL bcast(time_in)

  END SUBROUTINE open_climoz
  !
  !-------------------------------------------------------------------------------

END MODULE open_climoz_m
