!-------------------------------------- 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 --------------------------------------
!

      SUBROUTINE SUALOBS(KULOUT) 1,3
      use modmask, only : lmaskpp_in, lmaskpp_out,lmasksf_in,lmasksf_out
      use modmask, only : lmask,lmaskto,lmaskgo,lmaskro,lmaskzp,ldiagpp
      use modmask, only : ldiagsf, ldiag, lmaskgp
#if defined (DOC)
*
***s/r SUALOBS  - Memory allocation for obs. related arrays
*
*Author  : P. Gauthier *ARMA/AES  June 9, 1992
*Revision:
*     L.Fillion  *RPN/AES Feb 93  - Allocation for auxiliary fields
*     S. Pellerin *ARMA/AES Sept 97.
*                   - Control of the different model state of the 3Dvar
*                     through COMSTATE, COMSTATEC and COMSTNUM common
*                     blocks variables (comstate.cdk).
*     S. Pellerin *ARMA/AES Aug. 98.
*                   - Allocation (loc) for GOMES
*     L.Fillion  *ARMA/AES 25 nov 98  - Allocation for background fiels needed in TL and ADJ
*                 observation operators.
*     J.Halle    *CMDA/AES Oct 99
*                   - Added ground temperature to(TG) to model state
*     P. KOCLAS CMC NOV 99
*                 added stnid field
*     S. Pellerin *ARMA/SMC May 2000
*                   - Fix for F90 conversion:
*                        .Illimination of pointer to character*9 CSTNID
*                        .Introduction of distinc pointer for integer
*                         and real cma arrays (ptmobhdr,ptrobhdr,ptmobdata,ptrobdata)
*     JM Belanger CMDA/SMC  Jul 2000
*                   - 32 bits conversion
*                    (Memory allocation for ROBDATA8 array declared
*                     in comoba.cdk)
*     S. Pellerin *ARMA/SMC nov. 2001
*                   - Allocation of mvar and pexy vertors for 4Dvar book keeping
*     J. Halle    *CMDA/SMC mai 2006
*                   - Added one additionnal entry for ROBDATA8 (now 9 itemss instead of 8)
*     L. Fillion   ARMA/MSC - 25 Feb 2005. Limited Area Mesovar option.
*                 - Precomputed trigonometric factors for efficient wind rotation used when
*                   Grd_typ='LU'.
*     L. Fillion ARMA/MSC May 2006: Mesovar upgrade to v10_0_0.
*     L. Fillion ARMA/EC - 15 Aug 2007 - Update lam4d to v_10_0_3.
*     S. Pellerin *ARMA/SMC jan. 2009
*                   - Allocation of data assimilated mask
*     L. Fillion ARMA/EC - 07 May 2009 - Update 3dvar LU and GU mode to v_10_2_2.
*
*     S. Macpherson *ARMA/MRD Sep 2009
*                   - added allocation of ground-based GPS ZTD data mask
*     Bin He        *ARMA/MRB  Oct. 2009 
*                   - Implemented MPI to 3DVAR 
*     L. Fillion ARMA/EC - 09 Oct 2009 - Allocate obs arrays for fast rotations now based on grd_roule parameter.
*     L. Fillion ARMA/EC - 4 May 2010 - Upgrade on v_11_01_2b. Comment allocation printout on GOMOBS.	
*     L. Fillion ARMA/EC - 16 Aug 2010 - Comment printout of allocation dimensions of ROBHDR and MOBHDR since not OK. 
*Arguments
*     i   KULOUT: unit used for optional printing
*
#endif
C
      IMPLICIT NONE
*implicits
#include "comdim.cdk"
#include "comcva.cdk"
#include "comdimo.cdk"
#include "comoahdr.cdk"
#include "comoabdy.cdk"
#include "commvo.cdk"
#include "commvo1.cdk"
#include "commvog.cdk"
#include "comoba.cdk"
#include "comstate.cdk"
#include "comgrd_param.cdk"
#include "comroto.cdk"
*
      INTEGER KULOUT, IERR
      INTEGER ILEN, ILENU, ilen8
      INTEGER iloc, jvar
      EXTERNAL HPALLOC
C
      WRITE(KULOUT,FMT='(//,6(" ***********"))')
      WRITE(KULOUT,*)'     SUALOBS:  Memory allocation of the',
     S     ' observation arrays'
      WRITE(KULOUT,FMT='(6(" ***********"))')
C
C*    .   1.1 Auxiliary state
C     .       ---------------
C
 110  CONTINUE
C
      ILENU  = NMXOBS*NFLEV
!      WRITE(KULOUT,FMT='(/,"Arrays in COMMVO (COMMVO1) are allocated",
!     S     " the following space:")')
!      WRITE(KULOUT,FMT=9100)ILEN,ILENU, NMXOBS
! 9100 FORMAT("   GOMOBS :",I6,10x
!     S     ," (GOMU, GOMV,GOMGZ,GOMQ):",I10
!     S     ," (GOMPS,GOMTGR):",I10)
C
C
C*    2. Arrays containing the observations themselves
C     .   (COMDECK COMOBA)
C     .  ---------------------------------------------
C
*     Memory allocation for ROBDATA8.

      ALLOCATE(cstnid(NMXOBS),STAT=ierr)
      ALLOCATE(ROBHDR(NCMLET,NMXOBS),STAT=ierr)
      ROBHDR=0.0
      ALLOCATE(ROBDATA(NCMLBO,NDATAMX),STAT=ierr)
      ROBDATA=0.0
      ALLOCATE(ROBDATA8(3:11,NDATAMX),STAT=ierr)
      ROBDATA8=0.0D0
      ALLOCATE(MOBHDR(NCMLET,NMXOBS),STAT=ierr)
      MOBHDR=0
      ALLOCATE(MOBDATA(NCMLBO,NDATAMX),STAT=ierr)
      MOBDATA=0

!
      allocate(lmaskpp_in(ndatamx),lmaskpp_out(ndatamx))
      lmaskpp_in = .false.
      lmaskpp_out = .false.
      allocate(lmasksf_in(ndatamx),lmasksf_out(ndatamx))
      lmasksf_in = .false.
      lmasksf_out = .false.
      allocate(lmask(ndatamx),lmaskto(ndatamx),lmaskgo(ndatamx))
      lmaskto = .false.
      lmaskgo = .false.
      allocate(lmaskro(ndatamx),lmaskzp(ndatamx),ldiagpp(ndatamx))
      lmaskzp = .false.
      lmaskro = .false.
      ldiagpp = .false.
      allocate(ldiagsf(ndatamx),ldiag(ndatamx))
      ldiagsf = .false.
      ldiag = .false.
      allocate(lmaskgp(ndatamx))
      lmaskgp = .false.
      WRITE(KULOUT,FMT='(/,"  Arrays in COMOBA are allocated",
     S     " the following space:")')
!      WRITE(KULOUT,FMT=9200)ILEN,ILENU,ilen8
! 9200 FORMAT("   ROBHDR/MOBHDR: ",I10,10x
!     S     ," ROBDATA/MOBDATA : ",I10,10x
!     S     ," ROBDATA8 : ",I10,10x)
!
!*    3. Arrays for precomputed trigonometric factors for efficient
!        wind rotation when in 'LU' mode
!        ----------------------------------------------------------
      if(Grd_roule) then
        ilen=NMXOBS
        call hpalloc(ptrcoslatr, max(ilen,1), ierr, 8)
        call hpalloc(ptrsinlatr, max(ilen,1), ierr, 8)
        call hpalloc(ptrcoslonr, max(ilen,1), ierr, 8)
        call hpalloc(ptrsinlonr, max(ilen,1), ierr, 8)
        call hpalloc(ptrcoslonob, max(ilen,1), ierr, 8)
        call hpalloc(ptrsinlonob, max(ilen,1), ierr, 8)
        call hpalloc(ptrrsinlatob, max(ilen,1), ierr, 8)
      endif
!
      RETURN
      END