SUBROUTINE SUARG

!**** *SUARG*   - Routine to initialize common containing command line argument

!     Purpose.
!     --------
!           Initialize common YOMARG
!**   Interface.
!     ----------
!        *CALL* *SUARG

!        Explicit arguments :
!        --------------------
!        None

!        Implicit arguments :
!        --------------------
!        COMMON YOMLUN

!     Method.
!     -------
!        See documentation

!     Externals.
!     ----------
!        Called by SU0YOMA.

!     Reference.
!     ----------
!        ECMWF Research Department documentation of the IFS

!     Author.
!     -------
!        Ryad El Khatib *METEO-FRANCE*

!     Modifications.
!     --------------
!        Original : 93-05-06
!        Modified : 93-07-05 M.Hamrud (Using GRIB files not ARPEGE)
!        Modified : 93-09-16 M.Hortal (Flexibility in argument reading)
!        Modified : 95-02-06 Ryad El Khatib (LELAM in argument reading)
!        Modified : 95-11-16 Ryad El Khatib (Fullpos LFPART2 without arguments)
!        Modified : 95-11-16 Ryad El Khatib (Fullpos LFPART2 case ALADIN)
!        Modified : 96-01-03 Ryad El Khatib (Substential rewrite for GRIB)
!        Modified : 97-12-04 Ryad El Khatib (LFI portability)
!        Modified 98-08-10 by K. YESSAD: removal of LRPOLE option.
!         - removal of LLPOLE in 'CALL CHIEN'.
!        Modified : 98-08-26 Ryad El Khatib (correct comment on UGEMU)
!        Modified : 00-09-01 by D.Dent : MPE to MPL conversion
!        Modified : 01-02-06 by S.Martinez : move GETOPT to GET_OPT (!!!)
!        Modified : 01-03-20 Ryad El Khatib : "927 without command line options
!        M.Hamrud      01-Oct-2003 CY28 Cleaning
!        G.Mozdzynski  03-12-12 Broadcast commmand line arguments
!        Modified : 05-03-01 Ryad El Khatib : Cleanups
!        M.Hamrud      01-May-2006 Generalized IO scheme
!        R. El Ouaraini & JM. Audoin 03-Oct-06: NVGRIB default value is that of
!                                                              the initial file
!        R. El Khatib  27-Apr-2007 Re-enable the possibility to switch off MPI
!     ------------------------------------------------------------------

USE PARKIND1  ,ONLY : JPIM     ,JPRB
USE YOMHOOK   ,ONLY : LHOOK,   DR_HOOK

USE PARDIM   , ONLY : JPMXLE    ,JPMXGL
USE YOMARG   , ONLY :  NULOEN   ,NUMEN    ,NULIM    ,NUCONF   ,NUSTOP   ,&
 & NUECMWF  ,NUSLAG   ,NARGS    ,CUNMEXP  ,LUELAM   ,UVALH    ,UVBH     ,&
 & UGEMU    ,UTSTEP   ,NUTMAX   ,NUCMAX   ,NUDGL    ,NUDLON   ,NUFLEV   ,&
 & NUSMAX   ,NUHTYP   ,NUSTTYP  ,NUDATE   ,NUSSSS   ,USTRET   ,UMUCEN   ,&
 & ULOCEN  
USE YOMLUN   , ONLY : NULOUT   ,NINISH   ,NPPPSH   ,&
 & NULDILA
USE YOMCT0   , ONLY : LARPEGEF ,NPROC    ,NSTEPINI,LFPART2,LMPOFF
USE YOMOP    , ONLY : CFNPL
USE YOMFPC   , ONLY : CFPFMT
USE YOMMP    , ONLY : MYPROC   
USE YOMFPCT0 , ONLY : NFPCT0
USE IOSTREAM_MIX , ONLY : SETUP_IOSTREAM, SETUP_IOREQUEST,  &
 & CLOSE_IOSTREAM, TYPE_IOSTREAM , TYPE_IOREQUEST, IO_INQUIRE, &
 & CLOSE_IOREQUEST, INI_IOSTREAM
USE MPL_MODULE, ONLY : MPL_BROADCAST,MPL_ALLGATHERV

IMPLICIT NONE

INTEGER(KIND=JPIM) :: IDATEF(11), ILMO(12)
INTEGER(KIND=JPIM) :: IULIM(JPMXGL/2), IULOEN(JPMXGL),IUMEN(JPMXGL)

CHARACTER (LEN = 11) :: CLNOM
CHARACTER (LEN = 80) :: CLARG
CHARACTER (LEN = 50) :: CLFNISH
CHARACTER (LEN = 16) :: CLLEC
CHARACTER (LEN = 14) :: CLFNIGG
CHARACTER (LEN = 1)  :: CLOPT
CHARACTER (LEN = 16) :: CLOPTP='c:v:e:t:f:a:m:n:'
CHARACTER (LEN = 8)  :: CLSTOP='-9'
CHARACTER (LEN =4 ), PARAMETER :: CDPREF='SURF'
CHARACTER (LEN =16), PARAMETER :: CDSUFF='PRESSION        '

REAL(KIND=JPRB) :: ZGEMU(JPMXGL), ZVALH(0:JPMXLE), ZVBH (0:JPMXLE)

INTEGER(KIND=JPIM) :: IABORT, IDGNH, IIAUX, ILEN, ILEN1, &
 & ILEN2, ILEN4, ILENR2, ILEVMN, ILEVMX, INBARI, INC, IOPTVAL, IREP, &
 & ISDAY, ISHOUR, ISSSS, ITAG, IUDATE, IUDGL, IUDLON, IUFLEV, &
 & IUNTIN, IUQUAD, IUSMAX, IUSSSS, IUSTTYP, JGL, JLEV,ISTEP  ,&
 & IKNGRIB, INGRIB, IKNBPDG, IKNBCSP, INBITS, IKSTRON, ISTRON, IKPUILA, IPUILA, IKDMOPL

INTEGER(KIND=JPIM) :: IARGS=0
INTEGER(KIND=JPIM) :: GET_OPT
INTEGER(KIND=JPIM) :: IEXIST,IIEXIST(NPROC)

LOGICAL :: LLEXIST, LLFICP, LLGGEXIST, LLMAP, LLSPEXIST, LLARPEGE
LOGICAL :: LLARGBROADCAST
LOGICAL :: LLEXIS, LLCOSP

REAL(KIND=JPRB) :: Z_RINC, ZEPS, ZLOCEN, ZMUCEN, ZPREF, ZSTRET
REAL(KIND=JPRB) :: ZHOOK_HANDLE

TYPE(TYPE_IOSTREAM) :: YL_IOSTREAM
TYPE(TYPE_IOREQUEST) :: YL_IOREQUEST

#include "abor1.intfb.h"
#include "chien.intfb.h"
#include "echien.intfb.h"
#include "mod_ini.intfb.h"
#include "updcal.intfb.h"

!   c: configuration (default=1)
!   v: version of model (no default)
!   e: experiment identifier (no default)
!   s: time step (default -999., it will be set according to resolution
!         and advection scheme)
!   f: forecast span (dxx: days; hxxx: hours; txxx: time-steps)
!   a: advection scheme (eul: Eulerian; sli: interpolating semi-Lag.
!   m: model (arpifs: ARPEGE/IFS ; aladin: ALADIN ; default=arpifs)
!      ----------------------------------------------------------------

!*       1.    DEFAULT VALUES FOR PARAMETERS ARE IN DATA
!              -----------------------------------------

IF (LHOOK) CALL DR_HOOK('SUARG',0,ZHOOK_HANDLE)
LLARGBROADCAST=.FALSE.
ZEPS=EPSILON(1.0_JPRB)*10000._JPRB
ISHOUR=3600
ISDAY=3600*24
ILEN1=125
ILEN2=22+JPMXGL
ILENR2=10+2*(JPMXLE+1)
ILEN4=1
ILEVMN=0
ILEVMX=JPMXLE

!*       2.    GET THE ARGUMENTS IN COMMAND LINE
!              ---------------------------------

200 CONTINUE
IF( LLARGBROADCAST )THEN
  if( myproc == 1 )then
    IOPTVAL=GET_OPT(CLOPTP,CLARG,LMPOFF)
  endif
  if( nproc > 1 )then
    ITAG=1234
    CALL MPL_BROADCAST(IOPTVAL,KROOT=1,KTAG=ITAG,CDSTRING='SUARG:IOPTVAL')
    CALL MPL_BROADCAST(CLARG,KROOT=1,KTAG=ITAG,CDSTRING='SUARG:CLARG')
  endif
ELSE
  IOPTVAL=GET_OPT(CLOPTP,CLARG,LMPOFF)
ENDIF
CLOPT=CHAR(IOPTVAL)
IF(IOPTVAL > 0) THEN
!             command line option '-n' is included to allow IFS to be called
!             using 'mpirun -np NPROC ifsMASTER -v .........'
  IF(CLOPT /= 'c'.AND.CLOPT /= 'v'.AND.CLOPT /= 'e'&
     & .AND.CLOPT /= 't'.AND.CLOPT /= 'f'.AND.CLOPT /= 'a'&
     & .AND.CLOPT /= 'm'.AND.CLOPT /= 'n') THEN  
    WRITE(NULOUT,*) ' WRONG OPTION SPECIFIED ',CLOPT
    WRITE(NULOUT,*) ' POSSIBLE VALUES ARE:'
    WRITE(NULOUT,*) ' c: Configuration of Job'
    WRITE(NULOUT,*) ' v: Version of Model (ecmwf or meteo)'
    WRITE(NULOUT,*) ' e: Experiment Identifier'
    WRITE(NULOUT,*) ' t: Time step (seconds)'
    WRITE(NULOUT,*) ' f: Forecast span:'
    WRITE(NULOUT,*) '       d... in days'
    WRITE(NULOUT,*) '       h... in hours'
    WRITE(NULOUT,*) '       t... in time-steps'
    WRITE(NULOUT,*) ' a: Advection scheme:'
    WRITE(NULOUT,*) '       eul: Eulerian'
    WRITE(NULOUT,*) '       sli: Interpolating semi-Lag.'
    WRITE(NULOUT,*) ' m: Model:'
    WRITE(NULOUT,*) '       arpifs: ARPEGE/IFS'
    WRITE(NULOUT,*) '       aladin : ALADIN'
    CALL ABOR1('SUARG: ABOR1 CALLED')
  ELSE
    IARGS=IARGS+1
    IF(CLOPT == 'c') THEN
      READ(UNIT=CLARG,FMT='(I3)') NUCONF
    ELSEIF(CLOPT == 'v') THEN
      NARGS=NARGS+1
      IF(CLARG(1:1) == 'e') THEN
        NUECMWF=1
      ELSEIF(CLARG(1:1) == 'm') THEN
        NUECMWF=2
      ELSE
        WRITE(NULOUT,*) ' WRONG VERSION OF THE MODEL'
        WRITE(NULOUT,*) ' IT SHOULD BE EITHER'
        WRITE(NULOUT,*) '      ecmwf     or'
        WRITE(NULOUT,*) '      meteo'
        CALL ABOR1('SUARG: ABOR1 CALLED')
      ENDIF
    ELSEIF(CLOPT == 'e') THEN
      NARGS=NARGS+1
      READ(UNIT=CLARG,FMT='(A4)') CUNMEXP
    ELSEIF(CLOPT == 't') THEN
      READ(UNIT=CLARG,FMT='(f10.0)') UTSTEP
    ELSEIF(CLOPT == 'f') THEN
      CLSTOP=CLARG
    ELSEIF(CLOPT == 'a') THEN
      IF(CLARG == 'eul') THEN
        NUSLAG=1
      ELSEIF(CLARG == 'sli') THEN
        NUSLAG=3
      ELSE
        WRITE(NULOUT,*) ' WRONG ADVECTION SCHEME',CLARG
        WRITE(NULOUT,*) ' POSSIBLE VALUES ARE'
        WRITE(NULOUT,*) '       eul: Eulerian scheme'
        WRITE(NULOUT,*) '       sli: semi-Lag. Interpol.'
        CALL ABOR1('SUARG: ABOR1 CALLED')
      ENDIF
    ELSEIF(CLOPT == 'm') THEN
      IF(CLARG == 'arpifs') THEN
        LUELAM=.FALSE.
      ELSEIF(CLARG == 'aladin') THEN
        LUELAM=.TRUE.
      ELSE
        WRITE(NULOUT,*) ' WRONG MODEL ',CLARG
        WRITE(NULOUT,*) ' POSSIBLE VALUES ARE'
        WRITE(NULOUT,*) '       arpifs: ARPEGE/IFS'
        WRITE(NULOUT,*) '       aladin : ALADIN'
        CALL ABOR1('SUARG: ABOR1 CALLED')
      ENDIF
    ENDIF
    GO TO 200
  ENDIF
ENDIF
IF(IARGS == 0) THEN
  WRITE(NULOUT,*) ' MASTER CALLED WITH NO OPTIONS '
  WRITE(NULOUT,*) ' IN COMMAND LINE'
  IF (NFPCT0 /= 0) THEN
    IF (LHOOK) CALL DR_HOOK('SUARG',1,ZHOOK_HANDLE)
    RETURN
  ENDIF
ELSE
  WRITE(NULOUT,*) ' MASTER CALLED WITH ',IARGS,' OPTIONS'
  WRITE(NULOUT,*) ' IN COMMAND LINE'
ENDIF

!        2.1   Information from command line itself

!         Convert forecast range into number of time steps
IF (CLSTOP /= '-9') THEN
  IF(CLSTOP(1:1) == 't') THEN
    READ(UNIT=CLSTOP(2:),FMT='(I5)') NUSTOP
  ELSE
    IF(UTSTEP > 0.0_JPRB) THEN
      READ(UNIT=CLSTOP(2:),FMT='(I5)') NUSTOP
      IF(CLSTOP(1:1) == 'h') THEN
        NUSTOP=NINT(NUSTOP*ISHOUR/UTSTEP)
      ELSEIF(CLSTOP(1:1) == 'd') THEN
        NUSTOP=NINT(NUSTOP*ISDAY/UTSTEP)
      ELSE
        WRITE(NULOUT,*) ' WRONG FORMAT FOR THE FORECAST'
        WRITE(NULOUT,*) ' SPAN.'
        WRITE(NULOUT,*) ' IT MUST START WITH EITHER'
        WRITE(NULOUT,*) '     t  FOR TIME STEPS or'
        WRITE(NULOUT,*) '     d  FOR DAYS or'
        WRITE(NULOUT,*) '     h  FOR HOURS'
        CALL ABOR1('SUARG: ABOR1 CALLED')
      ENDIF
    ELSE
      WRITE(NULOUT,*) '  ERROR'
      WRITE(NULOUT,*) '  IF YOU SUPPLY THE FORECAST'
      WRITE(NULOUT,*) '  SPAN IN OTHER THAN TIME STEPS'
      WRITE(NULOUT,*) '  YOU HAVE TO SUPPLY THE VALUE'
      WRITE(NULOUT,*) '  OF THE TIME STEP WITH -t ...'
    ENDIF
  ENDIF
ENDIF

!        2.2   Information from initial conditions file
CALL INI_IOSTREAM(KULOUT=NULOUT,LDIFS_INTERNAL=.TRUE.)
IF(NARGS > 0.OR.NFPCT0==0) THEN
  IF(NARGS /= 2.AND.NFPCT0 /= 0) THEN
    WRITE(NULOUT,*) '  ERROR IN PARAMETER INPUT'
    WRITE(NULOUT,*) '  IF YOU SUPPLY THE VERSION OF THE MODEL'
    WRITE(NULOUT,*) '  OR THE EXPERIMENT IDENTIFIER'
    WRITE(NULOUT,*) '  YOU MUST SUPPLY BOTH'
    CALL ABOR1('SUARG: ABOR1 CALLED')
  ENDIF
  IF (NFPCT0==0) THEN
    LUELAM=CFPFMT /= 'GAUSS'
  ENDIF
  IF (NARGS > 0) THEN
    LLARPEGE=(NUECMWF == 2)
  ELSE
    LLARPEGE=LARPEGEF
  ENDIF

  IF (LLARPEGE) THEN
    IF (NFPCT0 /= 0) THEN
      CLFNISH='ICMSH'//CUNMEXP//'INIT'
      IUNTIN=NINISH
    ELSE
      CLFNISH=CFNPL
      IUNTIN=NPPPSH
    ENDIF

    IF ( NPROC > 1 ) THEN

!      Wait until all nodes gives the same answer

       CHECK_EXIST : DO
         IEXIST = 0
         INQUIRE(FILE=CLFNISH,EXIST=LLEXIST)
         IF ( LLEXIST ) IEXIST=1
         CALL MPL_ALLGATHERV(IEXIST,IIEXIST,CDSTRING='SUARG:')
         IF ( ALL(IIEXIST==1) .OR. ALL(IIEXIST==0) ) THEN
            LLEXIST = ( IIEXIST(1) == 1 )
            EXIT CHECK_EXIST
         ENDIF
       ENDDO CHECK_EXIST
    ELSE
       INQUIRE(FILE=CLFNISH,EXIST=LLEXIST)
    ENDIF

!     WAIT UNTIL THE FILE CLFNISH EXIST. This part allows to execute
!     different applications in the same time without waiting that
!     the file CLFNISH exists.

    IF (LLEXIST) THEN
      CLLEC='CADRE LECTURE   '
      INBARI=0
      CALL FAITOU(IREP,IUNTIN,.TRUE.,CLFNISH,'OLD',.TRUE.,&
       & .TRUE.,1,1,INBARI,CLLEC)  
      CALL LFIMST(IREP,IUNTIN,.FALSE.)
      NUFLEV=JPMXLE
      IF (.NOT.LUELAM) THEN
        CALL CHIEN(CLLEC,NUSTTYP,UMUCEN,ULOCEN,USTRET,&
         & NUSMAX,NUDGL,NUDLON,IULOEN,IUMEN,ZGEMU,&
         & NUHTYP,NUFLEV,ZPREF,UVALH,UVBH,&
         & IUQUAD,1,1,JPMXGL,ZEPS,LLFICP,&
         & NULOUT)  
        DO JGL=1,(NUDGL+1)/2
          NULOEN(JGL)=IULOEN(JGL)
          NUMEN(JGL) =IUMEN(JGL)
        ENDDO
      ELSE
        CALL ECHIEN(CLLEC,IIAUX,NUSTTYP,LLMAP,NUSMAX,NUDGL,&
         & NUDLON,IULIM,IUMEN,UGEMU,NUFLEV,ZPREF,UVALH,UVBH,&
         & 1,ZEPS,NULOUT)  
        UMUCEN=1._JPRB
        ULOCEN=0._JPRB
        IF (LLMAP) THEN
          USTRET=1._JPRB
        ELSE
          USTRET=-1.0_JPRB
        ENDIF
        NUHTYP=0
        DO JGL=1,8
          NULIM(JGL)=IULIM(JGL)
        ENDDO
        NULOEN(:)=NUDLON
      ENDIF
      CALL FADIES(IREP,IUNTIN,IDATEF)
      IF (.NOT. LFPART2)  THEN
        CALL FAVORI(IKNGRIB,IKNBPDG,IKNBCSP,IKSTRON,IKPUILA,IKDMOPL)
        CALL FANION(IREP,IUNTIN,CDPREF,1,CDSUFF,LLEXIS,LLCOSP,INGRIB,INBITS,ISTRON,IPUILA)
        IF (IKNGRIB /= INGRIB) THEN
          CALL FAGIOT(INGRIB,IKNBPDG,IKNBCSP,IKSTRON,IKPUILA,IKDMOPL)
          WRITE(NULOUT,*) 'The default value of NVGRIB is not that given by FAVORI:',&
           & IKNGRIB,'but that of the initial file :',INGRIB
        ENDIF
      ENDIF
      CALL FAIRME(IREP,IUNTIN,'UNKNOWN')
      ISSSS=IDATEF(7)*ISHOUR+IDATEF(4)*ISHOUR+IDATEF(5)*60
      NUSSSS=MOD(ISSSS,ISDAY)
      Z_RINC=REAL(ISSSS,JPRB)/REAL(ISDAY,JPRB)
      INC=INT(Z_RINC)
      IF (INC /= 0) THEN
        CALL UPDCAL(IDATEF(3),IDATEF(2),IDATEF(1),INC,&
         & IDATEF(3),IDATEF(2),IDATEF(1),ILMO,NULOUT)  
      ENDIF
      NUDATE=IDATEF(1)*10000+IDATEF(2)*100+IDATEF(3)
      DO JLEV=0,NUFLEV
        UVALH(JLEV)=UVALH(JLEV)*ZPREF
      ENDDO
    ELSE
      WRITE(NULOUT,*) ' INPUT FILE NOT FOUND '
      CALL ABOR1 ('INPUT FILE NOT FOUND')
    ENDIF

  ELSE

!*       2.4   Information from GRIB initial conditions files
!              LECMWF 

!        2.4.1 Information on spectral data          

    IF (NFPCT0 /= 0) THEN
      CLFNISH='ICMSH'//CUNMEXP//'INIT'
    ELSE
      ILEN=INDEX(CFNPL,' ')-1
      CLFNISH='SH'//CFNPL(3:ILEN)
    ENDIF
    ITAG = 88888
    IF (NUCONF == 901) THEN
      INQUIRE(FILE=CLFNISH,EXIST=LLSPEXIST)
    ELSE
      LLSPEXIST = .TRUE.
    ENDIF
    IF (LLSPEXIST) THEN
      CALL SETUP_IOSTREAM(YL_IOSTREAM,'CIO',TRIM(CLFNISH),CDMODE='r',KIOMASTER=1)
      CALL SETUP_IOREQUEST(YL_IOREQUEST,'SPECTRAL_FIELDS',LDGRIB=.TRUE.,&
       & LDINTONLY=.TRUE.,KCHUNKSIZE=1)
      CALL IO_INQUIRE(YL_IOSTREAM,YL_IOREQUEST,&
       & KDATE=IUDATE,KTIME=IUSSSS,KSTEP=ISTEP,&
       & KSMAX=NUSMAX,KFLEV=IUFLEV,PVALH=ZVALH,PVBH=ZVBH)
      CALL CLOSE_IOREQUEST(YL_IOREQUEST)
      CALL CLOSE_IOSTREAM(YL_IOSTREAM)
      NUSTTYP=1
      USTRET=1.0_JPRB
      UMUCEN=1.0_JPRB
      ULOCEN=0.0_JPRB
      CALL MOD_INI(IUDATE,IUSSSS,ISTEP,NUCONF,NUSTOP,&
       & NSTEPINI,UTSTEP,NULOUT)
! obsolete, Nils ?!
!      CALL SUCST(NULOUT,IUDATE,IUSSSS,0)
      IF (LUELAM) THEN
        ! Needs remake to new LAM domain definition parameters
        CALL ABOR1('SUARG: REMAKE ETGBSEC2')
!       CALL ETGBSEC2(ISEC2,ILEN2,ZSEC2,ILENR2,IUDGL,IUDLON,&
!        &UBETA,IUSMAX,IUSTTYP,NULIM(1),JPMXGL,&
!        &UGEMU,JPMXGL,IUFLEV,ZVALH,ZVBH,&
!        &ILEVMN,ILEVMX,NULOUT)
!                 Truncations
        NUSMAX=IUSMAX
        NUSTTYP=IUSTTYP
!                 Horizontal geometry
        USTRET=1._JPRB
        UMUCEN=1._JPRB
        ULOCEN=0._JPRB
      ENDIF

!              Vertical coordinate
      IF    (NUCONF == 201.OR.NUCONF == 202 &
         & .OR.NUCONF == 421.OR.NUCONF == 422 &
         & .OR.NUCONF == 521.OR.NUCONF == 522 &
         & )THEN  
        NUFLEV=1
        UVALH(0)=0._JPRB
        UVALH(1)=0._JPRB
        UVBH (0)=0._JPRB
        UVBH (1)=1._JPRB
      ELSE
        NUFLEV=IUFLEV
        DO JLEV=0,NUFLEV
          UVALH(JLEV)=ZVALH(JLEV)
          UVBH (JLEV)=ZVBH(JLEV)
        ENDDO
      ENDIF
!              Date
      NUDATE=IUDATE
      NUSSSS=IUSSSS

    ELSE
      NUSMAX=1
    ENDIF

!        2.4.2 Information on gridpoint data         

    IF (NFPCT0 /= 0) THEN
      CLFNIGG='ICMGG'//CUNMEXP//'INIT'
    ELSE
      ILEN=INDEX(CFNPL,' ')-1
      CLFNIGG='GG'//CFNPL(3:ILEN)
    ENDIF
    IF (NUCONF == 901) THEN
      INQUIRE(FILE=CLFNIGG,EXIST=LLGGEXIST)
    ELSE
      LLGGEXIST = .TRUE.
    ENDIF
    IF (LLGGEXIST) THEN
      CALL SETUP_IOSTREAM(YL_IOSTREAM,'CIO',TRIM(CLFNIGG),CDMODE='r',KIOMASTER=1)
      CALL SETUP_IOREQUEST(YL_IOREQUEST,'GRIDPOINT_FIELDS',LDGRIB=.TRUE.,&
       & LDINTONLY=.TRUE.,KCHUNKSIZE=1)
      CALL IO_INQUIRE(YL_IOSTREAM,YL_IOREQUEST,&
       & KDATE=IUDATE,KTIME=IUSSSS,KSTEP=ISTEP,&
       & KLATS=IUDGL,KLONS=IULOEN)
      CALL CLOSE_IOREQUEST(YL_IOREQUEST)
      CALL CLOSE_IOSTREAM(YL_IOSTREAM)
      CALL MOD_INI(IUDATE,IUSSSS,ISTEP,NUCONF,NUSTOP,&
       & NSTEPINI,UTSTEP,NULOUT)
! obsolete, Nils ?!
!      IF (.NOT.LLSPEXIST) THEN
!        CALL SUCST(NULOUT,IUDATE,IUSSSS,0)
!      ENDIF
      IF (.NOT.LUELAM) THEN

!                 Grid definition
        NUHTYP=0
        NUDGL =IUDGL
        NUDLON=IUDGL*2
        DO JGL=1,(NUDGL+1)/2
          NULOEN(JGL)=IULOEN(JGL)
          IF(NULOEN(JGL) /= NULOEN(1)) NUHTYP=2
        ENDDO
      ELSE
        ! Needs remake to new LAM domain definition parameters
        CALL ABOR1('SUARG: REMAKE ETGBSEC2')
!       CALL ETGBSEC2(ISEC2,ILEN2,ZSEC2,ILENR2,IUDGL,IUDLON,&
!        &UBETA,IUSMAX,IUSTTYP,NULIM,JPMXGL,&
!        &UGEMU,JPMXGL,IUFLEV,ZVALH,ZVBH,&
!        &ILEVMN,ILEVMX,NULOUT)

!                 Grid definition
        NUHTYP=0
        NUDGL =IUDGL
        NUDLON=IUDLON
        NULOEN(:)=NUDLON
      ENDIF
      IF (LLSPEXIST) THEN
        IABORT=0
        IF (NUDATE /= IUDATE) THEN
          WRITE(NULOUT,*) ' NOT THE SAME DATE !'
          WRITE(NULOUT,*) ' IN SP FILE : ',NUDATE
          WRITE(NULOUT,*) ' IN GG FILE : ',IUDATE
!                    IABORT=1 
        ELSE
          IF (NUSSSS /= IUSSSS) THEN
            WRITE(NULOUT,*) ' NOT THE SAME TIME !'
            WRITE(NULOUT,*) ' IN SP FILE : ', NUSSSS
            WRITE(NULOUT,*) ' IN GG FILE : ', IUSSSS
!                      IABORT=1
          ENDIF
        ENDIF
        IF (IABORT == 1) THEN
          CALL ABOR1('SUARG: ABOR1 CALLED')
        ENDIF
      ELSE
!                Geometry
        IF (.NOT.LUELAM) THEN
          NUSTTYP=IUSTTYP
          USTRET=ZSTRET
          UMUCEN=ZMUCEN
          ULOCEN=ZLOCEN
        ELSE
          USTRET=1._JPRB
          UMUCEN=1._JPRB
          ULOCEN=0._JPRB
        ENDIF
        IUFLEV=NUFLEV
        DO JLEV=0,NUFLEV
          UVALH(JLEV)=ZVALH(JLEV)
          UVBH (JLEV)=ZVBH(JLEV)
        ENDDO
!                Date
!                 NUDATE=IUDATE
!                 NUSSSS=IUSSSS
      ENDIF

    ELSE
      WRITE(NULOUT,*) ' GRIDPOINT FILE SEARCHED:',CLFNIGG
      WRITE(NULOUT,*) ' GRIDPOINT INPUT FILE NOT FOUND'
      IF (LLSPEXIST) THEN
        NUHTYP=0
        NUDGL =1
        NUDLON=2
      ELSE
        WRITE(NULOUT,*) ' >>> NO INPUT FILE AT ALL !'
        CALL ABOR1('SUARG: ABOR1 CALLED')
      ENDIF
    ENDIF

  ENDIF

!        2.3   Information from matrix files

  CLNOM='MATDILA'
  IF (NFPCT0 /= 0.AND.NUECMWF /= 1) THEN
    INQUIRE(FILE=CLNOM,EXIST=LLEXIST)
  ELSE
    LLEXIST=.FALSE.
  ENDIF
  IF (LLEXIST) THEN
    INBARI=1
    CALL LFIOUV(IREP,NULDILA,.TRUE.,CLNOM,'OLD',.TRUE.,.FALSE.,0,1,INBARI)
    CALL LFIINTLEC(IREP,NULDILA,'NSMIN',NUTMAX,1)
    CALL LFIINTLEC(IREP,NULDILA,'NSMAX',NUCMAX,1)
    CALL LFIFER(IREP,NULDILA,'UNKNOWN')
  ELSE
    WRITE(NULOUT,FMT='('' NO FILE '',A)') CLNOM
    NUTMAX=NUSMAX
    NUCMAX=NUSMAX
  ENDIF
ENDIF

!*       3.    PRINT FINAL VALUES
!              ------------------

WRITE(UNIT=NULOUT,FMT='('' COMMON YOMARG1 '')')
IF(NUCONF /= -1) WRITE(UNIT=NULOUT,FMT='('' NUCONF = '',I3)')NUCONF
IF(NUECMWF == 1) WRITE(UNIT=NULOUT,FMT='('' ECMWF '')')
IF(NUECMWF == 2) WRITE(UNIT=NULOUT,FMT='('' METEO-FRANCE '')')
IF(CUNMEXP /= '-9') WRITE(UNIT=NULOUT,FMT='('' CUNMEXP = '',A4)')CUNMEXP
IF(UTSTEP > 0.0_JPRB) WRITE(UNIT=NULOUT,FMT='('' UTSTEP = '',E16.8)')UTSTEP
IF(NUSTOP > 0) WRITE(UNIT=NULOUT,FMT='('' NUSTOP = '',I5)')NUSTOP
IF(NUSLAG == 1) WRITE(UNIT=NULOUT,FMT='('' EULERIAN '')')
IF(NUSLAG == 3) WRITE(UNIT=NULOUT,FMT='('' INTERP. SL '')')
IF (.NOT.LUELAM) THEN
  WRITE(UNIT=NULOUT,FMT='('' ARPEGE/IFS MODEL '')')
ELSE
  WRITE(UNIT=NULOUT,FMT='('' ALADIN MODEL '')')
ENDIF
IF(NARGS > 0 .OR. NFPCT0==0) THEN
  WRITE(UNIT=NULOUT,FMT='('' COMMON YOMARG2 '')')
  WRITE(UNIT=NULOUT,FMT='('' NUTMAX = '',I3,&
   & '' NUCMAX = '',I5)') NUTMAX,NUCMAX  
  WRITE(UNIT=NULOUT,FMT='('' COMMON YOMARG3 '')')
  WRITE(UNIT=NULOUT,FMT='('' NUDATE = '',I8 &
   & ,'' NUSSSS = '',I6)') NUDATE, NUSSSS  
  WRITE(UNIT=NULOUT,FMT='('' NSTEPINI = '',I5)') NSTEPINI
  WRITE(UNIT=NULOUT,FMT='('' NUSMAX = '',I5,'' NUDGL = ''&
   & ,I4,'' NUDLON = '',I5,'' NUFLEV = '',I3)')&
   & NUSMAX,NUDGL,NUDLON,NUFLEV  
  DO JLEV=0,NUFLEV
    WRITE(NULOUT,FMT='('' UVALH('',I3,'') = '',F12.6,&
     & ''     UVBH('',I3,'') = '',F12.10)')&
     & JLEV,UVALH(JLEV),JLEV,UVBH(JLEV)  
  ENDDO
  IF(.NOT.LUELAM) THEN
    WRITE(UNIT=NULOUT,FMT='('' NUSTTYP = '',I1,'' USTRET = ''&
     & ,F9.6,'' UMUCEN = '',F9.6,'' ULOCEN = '',F9.6)')&
     & NUSTTYP,USTRET,UMUCEN,ULOCEN  
    WRITE(UNIT=NULOUT,FMT='('' NUHTYP = '',I1)') NUHTYP
    IDGNH=(NUDGL+1)/2
    DO JGL=1,IDGNH
      WRITE(NULOUT,&
       & FMT='(2('' NULOEN ('',I4,'') = '',I4,&
       & '' NUMEN   ('',I4,'') = '',I4,3X))')&
       & JGL,NULOEN(JGL),JGL,NUMEN(JGL),&
       & JGL+IDGNH,NULOEN(IDGNH+1-JGL),JGL+IDGNH,NUMEN(IDGNH+1-JGL)  
    ENDDO
  ELSE
    WRITE(UNIT=NULOUT,FMT='('' NUSTTYP = '',I3)') NUSTTYP
    WRITE(UNIT=NULOUT,FMT='(&
     & '' ISTROE = '',I3, '' IDOM   = '',I3, &
     & '' IDLUN  = '',I3, '' IDLUX  = '',I3)')&
     & NULIM(1), NULIM(2), NULIM(3), NULIM(4)   
    WRITE(UNIT=NULOUT,FMT='(&
     & '' IDGUN  = '',I3, '' IDGUX  = '',I3,&
     & '' IDZONL = '',I3, '' IDZONG = '',I3)')&
     & NULIM(5), NULIM(6), NULIM(7), NULIM(8)   
    WRITE(UNIT=NULOUT,FMT='('' ZRPK = '',E13.3)') UGEMU(2)
    WRITE(UNIT=NULOUT,FMT='(&
     & '' ZLON0 = '',E13.3, '' ZLAT0 = '',E13.3, &
     & '' ZLONC = '',E13.3, '' ZLATC = '',E13.3)') &
     & UGEMU(3), UGEMU(4), UGEMU(5), UGEMU(6)    
    WRITE(UNIT=NULOUT,FMT='(&
     & '' ZDELX = '',E13.3, '' ZDELY = '',E13.3)') &
     & UGEMU(7), UGEMU(8)  
  ENDIF
ENDIF
IF (LHOOK) CALL DR_HOOK('SUARG',1,ZHOOK_HANDLE)
END SUBROUTINE SUARG
