Skip to content
Snippets Groups Projects
obs_surf_def.F90 16.9 KiB
Newer Older
Guillaume Samson's avatar
Guillaume Samson committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530
MODULE obs_surf_def
   !!=====================================================================
   !!                       ***  MODULE  obs_surf_def  ***
   !! Observation diagnostics: Storage handling for surface observation
   !!                          arrays and additional flags etc.
   !!                          This module only defines the data type and
   !!                          operations on the data type. There is no
   !!                          actual data in the module.
   !!===================================================================== 

   !!----------------------------------------------------------------------
   !!   obs_surf            : F90 type containing the surface information
   !!   obs_surf_alloc      : Allocates surface data arrays
   !!   obs_surf_dealloc    : Deallocates surface data arrays
   !!   obs_surf_compress   : Extract sub-information from a obs_surf type
   !!                         to a new obs_surf type
   !!   obs_surf_decompress : Reinsert sub-information from a obs_surf type
   !!                         into the original obs_surf type
   !!----------------------------------------------------------------------
   !! * Modules used
   USE par_kind, ONLY : & ! Precision variables
      & wp         
   USE obs_mpp, ONLY : &  ! MPP tools 
      obs_mpp_sum_integer

   IMPLICIT NONE

   !! * Routine/type accessibility
   PRIVATE

   PUBLIC &
      & obs_surf,           &
      & obs_surf_alloc,     &
      & obs_surf_dealloc,   &
      & obs_surf_compress,  &
      & obs_surf_decompress

   !! * Type definition for surface observation type

   TYPE obs_surf

      ! Bookkeeping

      INTEGER :: nsurf      !: Local number of surface data within window
      INTEGER :: nsurfmpp   !: Global number of surface data within window
      INTEGER :: nvar       !: Number of variables at observation points
      INTEGER :: nextra     !: Number of extra fields at observation points
      INTEGER :: nstp       !: Number of time steps
      INTEGER :: npi        !: Number of 3D grid points
      INTEGER :: npj
      INTEGER :: nsurfup    !: Observation counter used in obs_oper
      INTEGER :: nrec       !: Number of surface observation records in window

      ! Arrays with size equal to the number of surface observations

      INTEGER, POINTER, DIMENSION(:) :: &
         & mi,   &        !: i-th grid coord. for interpolating to surface observation
         & mj,   &        !: j-th grid coord. for interpolating to surface observation
         & mt,   &        !: time record number for gridded data
         & nsidx,&        !: Surface observation number
         & nsfil,&        !: Surface observation number in file
         & nyea, &        !: Year of surface observation
         & nmon, &        !: Month of surface observation 
         & nday, &        !: Day of surface observation
         & nhou, &        !: Hour of surface observation
         & nmin, &        !: Minute of surface observation
         & mstp, &        !: Time step nearest to surface observation
         & nqc,  &        !: Surface observation qc flag
         & ntyp           !: Type of surface observation product

      CHARACTER(len=8), POINTER, DIMENSION(:) :: &
         & cvars          !: Variable names

      CHARACTER(LEN=8), POINTER, DIMENSION(:) :: &
         & cwmo           !: WMO indentifier
         
      REAL(KIND=wp), POINTER, DIMENSION(:) :: &
         & rlam, &        !: Longitude coordinate of surface observation
         & rphi           !: Latitude coordinate of surface observation

      REAL(KIND=wp), POINTER, DIMENSION(:,:) :: &
         & robs, &        !: Surface observation 
         & rmod           !: Model counterpart of the surface observation vector

      REAL(KIND=wp), POINTER, DIMENSION(:,:) :: &
         & rext           !: Extra fields interpolated to observation points

      REAL(KIND=wp), POINTER, DIMENSION(:,:) :: &
         & vdmean         !: Time averaged of model field

      ! Arrays with size equal to the number of time steps in the window

      INTEGER, POINTER, DIMENSION(:) :: &
         & nsstp,     &   !: Local number of surface observations per time step
         & nsstpmpp       !: Global number of surface observations per time step

      ! Arrays with size equal to the number of observation records in the window
      INTEGER, POINTER, DIMENSION(:) :: &
         & mrecstp   ! Time step of the records

      ! Arrays used to store source indices when 
      ! compressing obs_surf derived types
      
      ! Array with size nsurf

      INTEGER, POINTER, DIMENSION(:) :: &
         & nsind          !: Source indices of surface data in compressed data

      ! Is this a gridded product?
     
      LOGICAL :: lgrid

   END TYPE obs_surf

   !!----------------------------------------------------------------------
   !! NEMO/OCE 4.0 , NEMO Consortium (2018)
   !! $Id: obs_surf_def.F90 10068 2018-08-28 14:09:04Z nicolasmartin $
   !! Software governed by the CeCILL license (see ./LICENSE)
   !!----------------------------------------------------------------------

CONTAINS
   
   SUBROUTINE obs_surf_alloc( surf, ksurf, kvar, kextra, kstp, kpi, kpj )
      !!----------------------------------------------------------------------
      !!                     ***  ROUTINE obs_surf_alloc  ***
      !!                      
      !! ** Purpose : - Allocate data for surface data arrays
      !! 
      !! ** Method  : - Fortran-90 dynamic arrays
      !!
      !! History :
      !!        !  07-03  (K. Mogensen, A. Weaver, E. Remy, S. Ricci) original
      !!----------------------------------------------------------------------
      !! * Arguments
      TYPE(obs_surf), INTENT(INOUT) ::  surf      ! Surface data to be allocated
      INTEGER, INTENT(IN) :: ksurf   ! Number of surface observations
      INTEGER, INTENT(IN) :: kvar    ! Number of surface variables
      INTEGER, INTENT(IN) :: kextra  ! Number of extra fields at observation points
      INTEGER, INTENT(IN) :: kstp    ! Number of time steps
      INTEGER, INTENT(IN) :: kpi     ! Number of 3D grid points
      INTEGER, INTENT(IN) :: kpj

      !!* Local variables
      INTEGER :: ji
      INTEGER :: jvar

      ! Set bookkeeping variables

      surf%nsurf    = ksurf
      surf%nsurfmpp = 0
      surf%nextra   = kextra
      surf%nvar     = kvar
      surf%nstp     = kstp
      surf%npi      = kpi
      surf%npj      = kpj

      ! Allocate arrays of size number of variables

      ALLOCATE( &
         & surf%cvars(kvar)    &
         & )

      DO jvar = 1, kvar
         surf%cvars(jvar) = "NotSet"
      END DO
      
      ! Allocate arrays of number of surface data size

      ALLOCATE( &
         & surf%mi(ksurf),      &
         & surf%mj(ksurf),      &
         & surf%mt(ksurf),      &
         & surf%nsidx(ksurf),   &
         & surf%nsfil(ksurf),   &
         & surf%nyea(ksurf),    &
         & surf%nmon(ksurf),    &
         & surf%nday(ksurf),    &
         & surf%nhou(ksurf),    &
         & surf%nmin(ksurf),    &
         & surf%mstp(ksurf),    &
         & surf%nqc(ksurf),     &
         & surf%ntyp(ksurf),    &
         & surf%cwmo(ksurf),    &
         & surf%rlam(ksurf),    &
         & surf%rphi(ksurf),    &
         & surf%nsind(ksurf)    &
         & )

      surf%mt(:) = -1


      ! Allocate arrays of number of surface data size * number of variables

      ALLOCATE( & 
         & surf%robs(ksurf,kvar), &
         & surf%rmod(ksurf,kvar)  &
         & )   

      ! Allocate arrays of number of extra fields at observation points

      ALLOCATE( & 
         & surf%rext(ksurf,kextra) &
         & )

      surf%rext(:,:) = 0.0_wp 

      ! Allocate arrays of number of time step size

      ALLOCATE( &
         & surf%nsstp(kstp),     &
         & surf%nsstpmpp(kstp)   &
         & )

      ! Allocate arrays of size number of grid points

      ALLOCATE( &
         & surf%vdmean(kpi,kpj) &
         & )

      ! Set defaults for compression indices
      
      DO ji = 1, ksurf
         surf%nsind(ji) = ji
      END DO
      
      ! Set defaults for number of observations per time step

      surf%nsstp(:)     = 0
      surf%nsstpmpp(:)  = 0

      ! Set the observation counter used in obs_oper

      surf%nsurfup     = 0
      
      ! Not gridded by default
          
      surf%lgrid       = .FALSE.
              
   END SUBROUTINE obs_surf_alloc

   SUBROUTINE obs_surf_dealloc( surf )
      !!----------------------------------------------------------------------
      !!                     ***  ROUTINE obs_surf_dealloc  ***
      !!                      
      !! ** Purpose : - Deallocate data for surface data arrays
      !! 
      !! ** Method  : - Fortran-90 dynamic arrays
      !!
      !! History :
      !!        !  07-03  (K. Mogensen, A. Weaver, E. Remy, S. Ricci) original
      !!----------------------------------------------------------------------
      !! * Arguments
      TYPE(obs_surf), INTENT(INOUT) :: &
         & surf      ! Surface data to be allocated

      !!* Local variables

      ! Deallocate arrays of number of surface data size

      DEALLOCATE( &
         & surf%mi,      &
         & surf%mj,      &
         & surf%mt,      &
         & surf%nsidx,   &
         & surf%nsfil,   &
         & surf%nyea,    &
         & surf%nmon,    &
         & surf%nday,    &
         & surf%nhou,    &
         & surf%nmin,    &
         & surf%mstp,    &
         & surf%nqc,     &
         & surf%ntyp,    &
         & surf%cwmo,    &
         & surf%rlam,    &
         & surf%rphi,    &
         & surf%nsind    &
         & )

      ! Allocate arrays of number of surface data size * number of variables

      DEALLOCATE( & 
         & surf%robs,    &
         & surf%rmod     &
         & )

      ! Deallocate arrays of number of extra fields at observation points

      DEALLOCATE( & 
         & surf%rext &
         & )

      ! Deallocate arrays of size number of grid points size times
      ! number of variables

      DEALLOCATE( &
         & surf%vdmean &
         & )

      ! Deallocate arrays of number of time step size

      DEALLOCATE( &
         & surf%nsstp,     &
         & surf%nsstpmpp   &
         & )

      ! Dellocate arrays of size number of variables

      DEALLOCATE( &
         & surf%cvars     &
         & )

   END SUBROUTINE obs_surf_dealloc

   SUBROUTINE obs_surf_compress( surf, newsurf, lallocate, kumout, lvalid )
      !!----------------------------------------------------------------------
      !!                     ***  ROUTINE obs_surf_compress  ***
      !!                      
      !! ** Purpose : - Extract sub-information from a obs_surf type
      !!                into a new obs_surf type
      !! 
      !! ** Method  : - The data is copied from surf to new surf.
      !!                In the case of lvalid being present only the 
      !!                selected data will be copied.
      !!                If lallocate is true the data in the newsurf is 
      !!                allocated either with the same number of elements
      !!                as surf or with only the subset of elements defined
      !!                by the optional selection.
      !!
      !! History :
      !!        !  07-03  (K. Mogensen, A. Weaver, E. Remy, S. Ricci) original
      !!----------------------------------------------------------------------
      !! * Arguments
      TYPE(obs_surf), INTENT(IN)    :: surf      ! Original surface data
      TYPE(obs_surf), INTENT(INOUT) :: newsurf   ! New surface data with a subset of the original data
      LOGICAL :: lallocate     ! Allocate newsurf data
      INTEGER,INTENT(IN) :: kumout        ! Fortran unit for messages
      LOGICAL, OPTIONAL, INTENT(in), DIMENSION(:) :: &
         & lvalid         ! Valid of surface observations
      
      !!* Local variables
      INTEGER :: insurf
      INTEGER :: ji
      INTEGER :: jk
      LOGICAL, DIMENSION(:), ALLOCATABLE :: llvalid

      ! Count how many elements there should be in the new data structure

      IF ( PRESENT(lvalid) ) THEN
         insurf = 0
         DO ji = 1, surf%nsurf
            IF ( lvalid(ji) ) THEN
               insurf = insurf + 1
            ENDIF
         END DO
      ELSE
         insurf = surf%nsurf
      ENDIF

      ! Optionally allocate data in the new data structure

      IF ( lallocate ) THEN
         CALL obs_surf_alloc( newsurf,  insurf, surf%nvar, &
            & surf%nextra, surf%nstp, surf%npi, surf%npj )
      ENDIF

      ! Allocate temporary valid array to unify the code for both cases

      ALLOCATE( llvalid(surf%nsurf) )
      IF ( PRESENT(lvalid) ) THEN
         llvalid(:)  = lvalid(:)
      ELSE
         llvalid(:)  = .TRUE.
      ENDIF

      ! Setup bookkeeping variables

      insurf = 0

      ! Loop over source surface data

      DO ji = 1, surf%nsurf

         IF ( llvalid(ji) ) THEN

            ! Copy the header information

            insurf = insurf + 1

            newsurf%mi(insurf)    = surf%mi(ji)
            newsurf%mj(insurf)    = surf%mj(ji)
            newsurf%mt(insurf)    = surf%mt(ji)
            newsurf%nsidx(insurf) = surf%nsidx(ji)
            newsurf%nsfil(insurf) = surf%nsfil(ji)
            newsurf%nyea(insurf)  = surf%nyea(ji)
            newsurf%nmon(insurf)  = surf%nmon(ji)
            newsurf%nday(insurf)  = surf%nday(ji)
            newsurf%nhou(insurf)  = surf%nhou(ji)
            newsurf%nmin(insurf)  = surf%nmin(ji)
            newsurf%mstp(insurf)  = surf%mstp(ji)
            newsurf%nqc(insurf)   = surf%nqc(ji)
            newsurf%ntyp(insurf)  = surf%ntyp(ji)
            newsurf%cwmo(insurf)  = surf%cwmo(ji)
            newsurf%rlam(insurf)  = surf%rlam(ji)
            newsurf%rphi(insurf)  = surf%rphi(ji)

            DO jk = 1, surf%nvar

               newsurf%robs(insurf,jk)  = surf%robs(ji,jk)
               newsurf%rmod(insurf,jk)  = surf%rmod(ji,jk)
               
            END DO

            DO jk = 1, surf%nextra

               newsurf%rext(insurf,jk) = surf%rext(ji,jk)

            END DO
            
            ! nsind is the index of the original surface data
            
            newsurf%nsind(insurf) = ji

         ENDIF

      END DO

      ! Update MPP counters

      newsurf%nsurf = insurf
      CALL obs_mpp_sum_integer ( newsurf%nsurf, newsurf%nsurfmpp )

      ! Set book keeping variables which do not depend on number of obs.

      newsurf%nstp     = surf%nstp
      newsurf%cvars(:) = surf%cvars(:)
      
      ! Set gridded stuff
      
      newsurf%mt(insurf)    = surf%mt(ji)
 
      ! Deallocate temporary data

      DEALLOCATE( llvalid )
     
   END SUBROUTINE obs_surf_compress

   SUBROUTINE obs_surf_decompress( surf, oldsurf, ldeallocate, kumout )
      !!----------------------------------------------------------------------
      !!                     ***  ROUTINE obs_surf_decompress  ***
      !!                      
      !! ** Purpose : - Copy back information to original surface data type
      !!
      !! ** Method  : - Reinsert updated information from a previous
      !!                copied/compressed surface data type into the original
      !!                surface data and optionally deallocate the surface
      !!                data input
      !! 
      !! History :
      !!        !  07-03  (K. Mogensen, A. Weaver, E. Remy, S. Ricci) original
      !!----------------------------------------------------------------------
      !! * Arguments
      TYPE(obs_surf),INTENT(INOUT) :: surf       ! Updated surface data
      TYPE(obs_surf),INTENT(INOUT) :: oldsurf    ! Original surface data
      LOGICAL :: ldeallocate ! Deallocate the updated data of insertion
      INTEGER,INTENT(in) :: kumout      ! Output unit
      
      !!* Local variables
      INTEGER :: ji
      INTEGER :: jj
      INTEGER :: jk

      ! Copy data from surf to old surf

      DO ji = 1, surf%nsurf

         jj=surf%nsind(ji)

         oldsurf%mi(jj)    = surf%mi(ji)
         oldsurf%mj(jj)    = surf%mj(ji)
         oldsurf%mt(jj)    = surf%mt(ji)
         oldsurf%nsidx(jj) = surf%nsidx(ji)
         oldsurf%nsfil(jj) = surf%nsfil(ji)
         oldsurf%nyea(jj)  = surf%nyea(ji)
         oldsurf%nmon(jj)  = surf%nmon(ji)
         oldsurf%nday(jj)  = surf%nday(ji)
         oldsurf%nhou(jj)  = surf%nhou(ji)
         oldsurf%nmin(jj)  = surf%nmin(ji)
         oldsurf%mstp(jj)  = surf%mstp(ji)
         oldsurf%nqc(jj)   = surf%nqc(ji)
         oldsurf%ntyp(jj)  = surf%ntyp(ji)
         oldsurf%cwmo(jj)  = surf%cwmo(ji)
         oldsurf%rlam(jj)  = surf%rlam(ji)
         oldsurf%rphi(jj)  = surf%rphi(ji)

      END DO

      DO jk = 1, surf%nvar

         DO ji = 1, surf%nsurf
            
            jj=surf%nsind(ji)

            oldsurf%robs(jj,jk)  = surf%robs(ji,jk)
            oldsurf%rmod(jj,jk)  = surf%rmod(ji,jk)

         END DO

      END DO

      DO jk = 1, surf%nextra

         DO ji = 1, surf%nsurf
            
            jj=surf%nsind(ji)

            oldsurf%rext(jj,jk)  = surf%rext(ji,jk)

         END DO

      END DO

      ! Optionally deallocate the updated surface data

      IF ( ldeallocate ) CALL obs_surf_dealloc( surf )
      
   END SUBROUTINE obs_surf_decompress
   
END MODULE obs_surf_def