!-------------------------------------- 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 CSIMOBS2(KSTAMP) 1,2
#if defined (DOC) 
*
***s/r CSE1  - Create data arrays from a model state
*
*
*Author  : P. Gauthier *ARMA/AES  May 8, 1995
*Revision:
*     . 
*               
*    -------------------
**    Purpose: to simulate observations by using a model state
*     .        to which noise is simulated and added to the 
*     .        synthetic observation. This configuration can also
*     .        be employed to transfer observation in grid format
*     .        to a CMA format (NCONF/100 = 4)
*Arguments
*     KSTAMP:   datestamp of the observation time
#endif
      IMPLICIT NONE
*implicits
#include "pardim.cdk"
#include "comdim.cdk"
#include "comdimo.cdk"
#include "comlun.cdk"
#include "comcst.cdk"
#include "comleg.cdk"
#include "comgem.cdk"
#include "comgd0.cdk"
#include "comoahdr.cdk"
#include "comoabdy.cdk"
#include "comoba.cdk"
#include "comnumbr.cdk"
*
      INTEGER KSTAMP
*
      INTEGER JLON, JLAT, IOBS, IERR, JO, IDATA
      REAL*8 ZLAT,ZLON, ZPPOBS
*
*     .  1.  Define the headers of all observations
*     .      
      DO JO = 1, NOBTOT
         MOBHDR(NCMNLV,JO) = 1
      END DO
      MOBHDR(NCMRLN,1) = 1
      DO JO = 2, NOBTOT
         MOBHDR(NCMRLN,JO) = MOBHDR(NCMRLN,JO-1) 
     S        + MOBHDR(NCMNLV,JO-1)
      END DO
*      
      ZPPOBS = 0.
      IOBS = 0
      DO JLAT = 1, NJ
         ZLAT = RLATI(JLAT)
         DO JLON = 1, NI
            IOBS =IOBS + 1
            ZLON =2.*RPI*(JLON-1)/FLOAT(NILON(JLAT))
            ROBHDR(NCMLON,IOBS) = ZLON
            ROBHDR(NCMLAT,IOBS) = ZLAT
            ROBHDR(NCMALT,IOBS) = 999999.
            ROBHDR(NCMTLO,IOBS) = ZLON
            ROBHDR(NCMTLA,IOBS) = ZLAT
C     
            MOBHDR(NCMONM,IOBS) = IOBS
            MOBHDR(NCMBOX,IOBS) = 999999
            MOBHDR(NCMOTP,IOBS) = 999999
            MOBHDR(NCMITY,IOBS) = 999999
            MOBHDR(NCMDAT,IOBS) = KSTAMP
            MOBHDR(NCMETM,IOBS) = 1200
            MOBHDR(NCMSID,IOBS) = 999999
            MOBHDR(NCMOEC,IOBS) = 999999
            MOBHDR(NCMOFL,IOBS) = 999999
            MOBHDR(NCMST1,IOBS) = 999999
         END DO
      END DO
*
*     .  2.  Define the data records of each observation
*     .
      IDATA = 0
      DO JLAT = 1, NJ
         DO JLON = 1, NI
            IDATA = IDATA + 1
C     
C     *    Variable Number
C     
            MOBDATA(NCMVNM,IDATA) = NVNUMB(17)
C     
C     *    Pressure level
C     
            ROBDATA8(NCMPPP,IDATA) = ZPPOBS
C     
C     *    Reference Pressure level
C     
            ROBDATA8(NCMPRL,IDATA) = 999.
C     
C     *    Pressure/Geopotential
C     
            ROBDATA(NCMPOB,IDATA) = 999.0
C     
C     *    Observed value
C     
            ROBDATA8(NCMVAR,IDATA) = Q0(JLON,NFLEV,JLAT)
C     
C     *    Observed - First-Guess
C     
            ROBDATA8(NCMOMF,IDATA) = 999.
C     
C     *    Observed - Analyzed value
C     
            ROBDATA8(NCMOMA,IDATA) = 999.
C     
C     *    Observed - Initialized value
C     
            ROBDATA8(NCMOMI,IDATA) = 999.
c     
c     *    observed - Iteration N value
C     
            ROBDATA8(NCMOMN,IDATA) = 999.
C     
C     *    Observation error standard deviation
C     
            ROBDATA8(NCMOER,IDATA) = 1.58
C     
C     *    Representativeness error standard deviation
C     
            ROBDATA(NCMRER,IDATA) = 999.
C     
C     *    First-guess error standard deviation
C     
            ROBDATA(NCMFGE,IDATA) = 999.
C     
C     *    Persistence error standard deviation
C     
            ROBDATA(NCMPER,IDATA) = 999.
C     
C     *    Flags
C     
            MOBDATA(NCMFLG,IDATA) = 999
C     
         END DO
      END DO
*
*     3. Verification
*
      WRITE(NULOUT,9300)NOBTOT,IOBS, NDATA, IDATA
 9300 FORMAT(//,12X,'--- Total number of observations: NOBTOT =',i7
     S     ,3x,'IOBS = ',I7,//,12X,'--- Total number of data: NDATA = '
     S     ,I7,3X,'IDATA = ',I7)
*
      DO JO=1, NOBTOT,1000
         CALL PRNTHDR(JO,NULOUT)
         CALL PRNTBDY(JO,NULOUT)
      END DO
      RETURN
      END