SUBROUTINE GETMINI (YDVAZX, CDFILE)

!***  *GETMINI* get starting point of minimisation.

!**   Purpose.
!     --------
!     this routine reads in the control variable.

!**   Interface.
!     *CALL* *GETMINI* from CVA1

!        Explicit arguments :
!        --------------------
!        YDVAZX  : control variable
!        CDFILE : file name (optional)

!        Implicit arguments :
!        --------------------
!        none.

!     Method.
!     -------
!        See documentation : The modules M1QN2,N1QN2,M1QN3,N1QN3
!                            version 1.1.b (February 1989)
!                            J.C Gilbert and C. Lemarechal

!     Externals.
!     ----------

!     Reference.
!     ----------
!        ECMWF research department documentation of the IFS

!     Author.
!     -------
!        Jean-Noel Thepaut *ECMWF*

!     Modifications.
!     --------------
!        Original : 93-03-04
!        Modified 95-06-06 by L.Isaksen : Reordering of spectral arrays
!        Modified 96-10-11 by F.Rabier  : do not use MIO if not on Cray
!        Modified 96-10-15 by F.Rabier  : move read tbmin to suhess
!        Modified 98-01-10 by M.Hamrud  : Minimisation parallelized
!        Modified 00-08-09 by Y.Tremolet: Cleaning
!        Modified 01-02-07 by M.Fisher  : Combine GETMINC and GETMINI
!        Modified 01-02-12 by M.Fisher  : Arguments YDVAZX, CDFILE and KDIM
!        Modified 02-08-06 by Y.Tremolet: Use control_vectors, KDIM from file.
!        M.Hamrud      01-Oct-2003 CY28 Cleaning
!        M.Fisher 17-Mar-2004 Wavelet Jb
!        M.Hamrud      01-Dec-2005 Generalized IO scheme
!        D.Salmond     18-Apr-2007 Use multiple processors for I/O
!        T.Wilhelmsson 28-Aug-2009 Move multiprocessor I/O to GETMINI2
!     ------------------------------------------------------------------

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

USE YOMCT0 , ONLY: NCONF    ,NSTART   ,NPROC
USE YOMNMIA, ONLY: NITNMI   ,LNMIRQ
USE YOMVAR , ONLY: NUPTRA   ,LTRREF
USE YOMLUN , ONLY: NULOUT
USE YOMMP  , ONLY: MYPROC
USE IOSTREAM_MIX , ONLY : SETUP_IOSTREAM, SETUP_IOREQUEST, IO_GET, &
 & CLOSE_IOSTREAM, TYPE_IOSTREAM , TYPE_IOREQUEST,CLOSE_IOREQUEST
USE YOMTAG , ONLY: MTAGGETV
USE YOMWAVELET, ONLY: LJBWAVELET, N_WAVELET_CUTOFFS
USE CONTROL_VECTORS, ONLY : CONTROL_VECTOR, ALLOCATE_CTLVEC, DEALLOCATE_CTLVEC, &
                          & ASSIGNMENT(=)
USE CONTROL_VECTORS_COMM_MOD, ONLY : SCATTER_CTLVEC
USE MPL_MODULE, ONLY : MPL_BROADCAST

IMPLICIT NONE

TYPE(CONTROL_VECTOR),INTENT(INOUT) :: YDVAZX 
CHARACTER(LEN=*)  ,OPTIONAL,INTENT(IN)    :: CDFILE 

TYPE(control_vector) :: YL_ZVAZXD

CHARACTER :: CLTIME*8
CHARACTER :: CLUPTRA*2
CHARACTER :: CLFN*128
REAL(KIND=JPRB),ALLOCATABLE ::  ZVAZX(:)

INTEGER(KIND=JPIM) :: IOMASTER, ISTEP
INTEGER(KIND=JPIM) :: IBUF(3), ITLMAX, ITLERR,  IVADIM, INSCALES
TYPE(TYPE_IOSTREAM) :: YL_IOSTREAM
TYPE(TYPE_IOREQUEST) :: YL_IOREQUEST
REAL(KIND=JPRB) :: ZHOOK_HANDLE


!     ------------------------------------------------------------------

!*       1   DEFINE FILE NAME.
!           --------------------

IF (LHOOK) CALL DR_HOOK('GETMINI',0,ZHOOK_HANDLE)
IF (PRESENT(CDFILE)) THEN
  CLFN=CDFILE
ELSE
  IF(.NOT.LNMIRQ) THEN
    ISTEP=NSTART
  ELSE
    ISTEP=NSTART-NITNMI
  ENDIF
  IF(ISTEP >= 0) THEN
    WRITE(CLTIME,'(''+'',I7.7)') ISTEP
  ELSE
    WRITE(CLTIME,'(''-'',I7.7)') -ISTEP
  ENDIF
  WRITE(CLUPTRA,'(I2.2)') NUPTRA-1
  
  IF(LTRREF) THEN
    CLFN='tref'//CLTIME
  ELSEIF(NCONF == 131)THEN
    CLFN='traj'//CLUPTRA//CLTIME
  ELSE
    CLFN='traj'//CLTIME
  ENDIF
ENDIF

WRITE(NULOUT,*)'GETMINI: Read control variable from file ',CLFN

!     ------------------------------------------------------------------

!*    3.  OPEN FILES AND READ IN CONTROL VARIABLE
!     -------------------------------------------

!*       3.1   OPEN FILE
!              ---------
IOMASTER = MIN(1,NPROC)
CALL SETUP_IOSTREAM(YL_IOSTREAM,'CIO',TRIM(CLFN),CDMODE='r',KIOMASTER=IOMASTER)
CALL SETUP_IOREQUEST(YL_IOREQUEST,'RAW')
CALL IO_GET(YL_IOSTREAM,YL_IOREQUEST,KR1=IBUF)

CALL GSTATS(626,0)
CALL MPL_BROADCAST(IBUF,MTAGGETV,KROOT=IOMASTER,CDSTRING='GETMINI')
CALL GSTATS(626,1)
IVADIM = IBUF(1)
ITLMAX = IBUF(2)
ITLERR = IBUF(3)

!*       3.2   READ  CONTROL VARIABLE.
!              ----------------------
IF(MYPROC == IOMASTER) THEN
   ALLOCATE(ZVAZX(IVADIM))
ELSE
   ALLOCATE(ZVAZX(1))
ENDIF
CALL IO_GET(YL_IOSTREAM,YL_IOREQUEST,PR1=ZVAZX)
CALL CLOSE_IOREQUEST(YL_IOREQUEST)
CALL CLOSE_IOSTREAM(YL_IOSTREAM)

!*      MESSAGE-PASS THE CONTROL VARIABLE

IF (IVADIM == YDVAZX%NSIZEG) THEN
   CALL SCATTER_CTLVEC(YDVAZX,IOMASTER,ZVAZX)
ELSE
   IF (LJBWAVELET) THEN
      INSCALES=COUNT(N_WAVELET_CUTOFFS<=ITLMAX)-1
      WRITE (NULOUT,*) 'INPUT CONTROL VECTOR HAS ',INSCALES,' WAVELET SCALES'
      CALL ALLOCATE_CTLVEC(YL_ZVAZXD,KNSCALES=INSCALES+1,KSERR=ITLERR)
   ELSE
      CALL ALLOCATE_CTLVEC(YL_ZVAZXD,KSMAX=ITLMAX,KSERR=ITLERR)
   ENDIF
   CALL SCATTER_CTLVEC(YL_ZVAZXD,IOMASTER,ZVAZX)
   CALL GSTATS(1839,0)
   YDVAZX = YL_ZVAZXD ! Change of resolution
   CALL DEALLOCATE_CTLVEC(YL_ZVAZXD)
   CALL GSTATS(1839,1)
ENDIF

DEALLOCATE(ZVAZX)

!     ------------------------------------------------------------------

IF (LHOOK) CALL DR_HOOK('GETMINI',1,ZHOOK_HANDLE)
END SUBROUTINE GETMINI
