readwind_gfs.f90 33.9 KB
Newer Older
Matthias Langer's avatar
 
Matthias Langer committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
!**********************************************************************
! Copyright 1998,1999,2000,2001,2002,2005,2007,2008,2009,2010         *
! Andreas Stohl, Petra Seibert, A. Frank, Gerhard Wotawa,             *
! Caroline Forster, Sabine Eckhardt, John Burkhart, Harald Sodemann   *
!                                                                     *
! This file is part of FLEXPART.                                      *
!                                                                     *
! FLEXPART is free software: you can redistribute it and/or modify    *
! it under the terms of the GNU General Public License as published by*
! the Free Software Foundation, either version 3 of the License, or   *
! (at your option) any later version.                                 *
!                                                                     *
! FLEXPART 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       *
! GNU General Public License for more details.                        *
!                                                                     *
! You should have received a copy of the GNU General Public License   *
! along with FLEXPART.  If not, see <http://www.gnu.org/licenses/>.   *
!**********************************************************************

22
subroutine readwind_gfs(indj,n,uuh,vvh,wwh)
Matthias Langer's avatar
 
Matthias Langer committed
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39

  !***********************************************************************
  !*                                                                     *
  !*             TRAJECTORY MODEL SUBROUTINE READWIND                    *
  !*                                                                     *
  !***********************************************************************
  !*                                                                     *
  !*             AUTHOR:      G. WOTAWA                                  *
  !*             DATE:        1997-08-05                                 *
  !*             LAST UPDATE: 2000-10-17, Andreas Stohl                  *
  !*             CHANGE: 01/02/2001, Bernd C. Krueger, Variables tth and *
  !*                     qvh (on eta coordinates) in common block        *
  !*             CHANGE: 16/11/2005, Caroline Forster, GFS data          *
  !*             CHANGE: 11/01/2008, Harald Sodemann, Input of GRIB1/2   *
  !*                     data with the ECMWF grib_api library            *
  !*             CHANGE: 03/12/2008, Harald Sodemann, update to f90 with *
  !*                                 ECMWF grib_api                      *
40 41 42 43
  !                                                                      *
  !   Unified ECMWF and GFS builds                                       *
  !   Marian Harustak, 12.5.2017                                         *
  !     - Renamed routine from readwind to readwind_gfs                  *
Espen Sollum's avatar
Espen Sollum committed
44 45 46 47 48
  !                                                                      *
  !   Implementation of the Vtables approach                             *
  !   D. Morton, D. Arnold 17.11.2017                                    *
  !     - Inclusion of specific code and usage of class_vtable           *
  !                                                                      *
Matthias Langer's avatar
 
Matthias Langer committed
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
  !*                                                                     *
  !***********************************************************************
  !*                                                                     *
  !* DESCRIPTION:                                                        *
  !*                                                                     *
  !* READING OF ECMWF METEOROLOGICAL FIELDS FROM INPUT DATA FILES. THE   *
  !* INPUT DATA FILES ARE EXPECTED TO BE AVAILABLE IN GRIB CODE          *
  !*                                                                     *
  !* INPUT:                                                              *
  !* indj               indicates number of the wind field to be read in *
  !* n                  temporal index for meteorological fields (1 to 3)*
  !*                                                                     *
  !* IMPORTANT VARIABLES FROM COMMON BLOCK:                              *
  !*                                                                     *
  !* wfname             File name of data to be read in                  *
  !* nx,ny,nuvz,nwz     expected field dimensions                        *
  !* nlev_ec            number of vertical levels ecmwf model            *
  !* uu,vv,ww           wind fields                                      *
  !* tt,qv              temperature and specific humidity                *
  !* ps                 surface pressure                                 *
  !*                                                                     *
  !***********************************************************************

  use grib_api
  use par_mod
  use com_mod
Espen Sollum's avatar
Espen Sollum committed
75
  use class_vtable
Matthias Langer's avatar
 
Matthias Langer committed
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

  implicit none

  !HSO  new parameters for grib_api
  integer :: ifile
  integer :: iret
  integer :: igrib
  integer :: gribVer,parCat,parNum,typSurf,valSurf,discipl
  !HSO end edits
  real :: uuh(0:nxmax-1,0:nymax-1,nuvzmax)
  real :: vvh(0:nxmax-1,0:nymax-1,nuvzmax)
  real :: wwh(0:nxmax-1,0:nymax-1,nwzmax)
  integer :: ii,indj,i,j,k,n,levdiff2,ifield,iumax,iwmax

  ! NCEP
  integer :: numpt,numpu,numpv,numpw,numprh
  real :: help, temp, ew
  real :: elev
  real :: ulev1(0:nxmax-1,0:nymax-1),vlev1(0:nxmax-1,0:nymax-1)
  real :: tlev1(0:nxmax-1,0:nymax-1)
  real :: qvh2(0:nxmax-1,0:nymax-1)

  integer :: i179,i180,i181

  ! VARIABLES AND ARRAYS NEEDED FOR GRIB DECODING
  !HSO kept isec1, isec2 and zsec4 for consistency with gribex GRIB input

Espen Sollum's avatar
Espen Sollum committed
103 104 105 106 107


  !!!!!  DJM - ultimately I will remove isec1
  !!!!!integer :: isec1(8),isec2(3)
  integer :: isec2(3)
Matthias Langer's avatar
 
Matthias Langer committed
108 109 110 111 112 113 114 115 116 117 118 119 120 121
  real(kind=4) :: zsec4(jpunp)
  real(kind=4) :: xaux,yaux,xaux0,yaux0
  real(kind=8) :: xauxin,yauxin
  real,parameter :: eps=1.e-4
  real(kind=4) :: ewss(0:nxmax-1,0:nymax-1),nsss(0:nxmax-1,0:nymax-1)
  real :: plev1,hlev1,ff10m,fflev1

  logical :: hflswitch,strswitch

  !HSO  for grib api error messages
  character(len=24) :: gribErrorMsg = 'Error reading grib file'
  character(len=20) :: gribFunction = 'readwind_gfs'


Espen Sollum's avatar
Espen Sollum committed
122 123 124 125 126
  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  !!!!  Vtable related variables
  !
  !  Path to Vtable - current implementation assumes it's in cwd, named
  !  "Vtable"
127 128 129 130 131
  ! ESO: Changed to use default Vtable file in options directory
  ! CHARACTER(LEN=255), PARAMETER :: VTABLE_PATH = "Vtable"
  character(LEN=255) :: VTABLE_PATH
  character(LEN=15) :: fpname      ! stores FLEXPART name for curr grib mesg.
  type(Vtable),save :: my_vtable    ! unallocated
Espen Sollum's avatar
Espen Sollum committed
132 133 134
  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

  !!  DJM
135
  integer current_grib_level   ! this was isec1(8) in previous versions
136
  logical :: linit=.true.
Espen Sollum's avatar
Espen Sollum committed
137 138 139



Matthias Langer's avatar
 
Matthias Langer committed
140 141 142 143 144 145 146
  hflswitch=.false.
  strswitch=.false.
  levdiff2=nlev_ec-nwz+1
  iumax=0
  iwmax=0


147 148 149
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!  Vtable code
  if (linit) then
150
    VTABLE_PATH = path(1)(1:length(1))//'Vtables/Vtable.gfs'
151 152 153 154
     PRINT *, 'Loading Vtable: ', VTABLE_PATH
     call vtable_load_by_name(VTABLE_PATH, my_vtable)
     linit=.false.
  end if
Espen Sollum's avatar
Espen Sollum committed
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
  !! Debugging tool
!  PRINT *, 'Dump of Vtable...'
!  call vtable_dump_records(my_vtable)
  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!



  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  !!!!!!!!!!!!!!!!!!!  VTABLE code
  ! This is diagnostic/debugging code, and will normally be commented out.
  ! It's purpose is to look at the provided grib file and produce an
  ! inventory of the FP-related messages, relative to the Vtable that's
  ! already been open.

!  CALL vtable_gribfile_inventory(path(3)(1:length(3)) // trim(wfname(indj)), &
! &                                my_vtable)

  !!!!!!!!!!!!!!!!!!!  VTABLE code
  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!




Matthias Langer's avatar
 
Matthias Langer committed
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
  ! OPENING OF DATA FILE (GRIB CODE)

  !HSO
5   call grib_open_file(ifile,path(3)(1:length(3)) &
         //trim(wfname(indj)),'r',iret)
  if (iret.ne.GRIB_SUCCESS) then
    goto 888   ! ERROR DETECTED
  endif
  !turn on support for multi fields messages
  call grib_multi_support_on

  numpt=0
  numpu=0
  numpv=0
  numpw=0
  numprh=0
  ifield=0
10   ifield=ifield+1
  !
  ! GET NEXT FIELDS
  !
  call grib_new_from_file(ifile,igrib,iret)
  if (iret.eq.GRIB_END_OF_FILE)  then
    goto 50    ! EOF DETECTED
  elseif (iret.ne.GRIB_SUCCESS) then
    goto 888   ! ERROR DETECTED
  endif

Espen Sollum's avatar
Espen Sollum committed
206 207 208 209 210 211 212 213 214 215 216 217 218


  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  !!!!!!!!!!!!!!!!!!!  VTABLE code
  ! Get the fpname
  fpname = vtable_get_fpname(igrib, my_vtable)
  !print *, 'fpname: ', trim(fpname)
  !!!!!!!!!!!!!!!!!!!  VTABLE code
  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!




Matthias Langer's avatar
 
Matthias Langer committed
219 220
  !first see if we read GRIB1 or GRIB2
  call grib_get_int(igrib,'editionNumber',gribVer,iret)
221
!  call grib_check(iret,gribFunction,gribErrorMsg)
Matthias Langer's avatar
 
Matthias Langer committed
222

Espen Sollum's avatar
Espen Sollum committed
223

Matthias Langer's avatar
 
Matthias Langer committed
224 225
  if (gribVer.eq.1) then ! GRIB Edition 1

Espen Sollum's avatar
Espen Sollum committed
226 227 228 229 230 231 232 233 234 235
      call grib_get_int(igrib,'level',current_grib_level,iret)
      call grib_check(iret,gribFunction,gribErrorMsg)

      !!! Added by DJM 2017-08-08 - if this is GRIB1 we assume that
      !!! level units are hPa and need to be multiplied by 100 for Pa
      !!! As currently implemented, ALL levels (even non isobaric) are transformed.
      !!! I "think" that's OK, because I don't think levels are used for any of the
      !!! 2D fields.  But, I need to double check.  I could always make this
      !!! conditional on whether the level type is isobaric (type 105 I think)
      current_grib_level = current_grib_level*100.0
Matthias Langer's avatar
 
Matthias Langer committed
236 237 238

  else ! GRIB Edition 2

Espen Sollum's avatar
Espen Sollum committed
239 240 241
      call grib_get_int(igrib,'scaledValueOfFirstFixedSurface', &
           current_grib_level,iret)
      call grib_check(iret,gribFunction,gribErrorMsg)
Matthias Langer's avatar
 
Matthias Langer committed
242

Espen Sollum's avatar
Espen Sollum committed
243 244
     !!! Added by DJM 2017-08-08 - if this is GRIB2 we assume that
      !!! level units are Pa and don't need to be modified
Matthias Langer's avatar
 
Matthias Langer committed
245 246 247

  endif ! gribVer

Espen Sollum's avatar
Espen Sollum committed
248 249 250 251 252 253 254





!!!!!!  DJM - orig -   if (isec1(6).ne.-1) then
  IF (TRIM(fpname) .NE. 'NOFP') THEN
Matthias Langer's avatar
 
Matthias Langer committed
255 256
  !  get the size and data of the values array
    call grib_get_real4_array(igrib,'values',zsec4,iret)
Espen Sollum's avatar
Espen Sollum committed
257
    call grib_check(iret,gribFunction,gribErrorMsg)
Matthias Langer's avatar
 
Matthias Langer committed
258 259 260 261 262 263 264 265
  endif

  if(ifield.eq.1) then

  !get the required fields from section 2
  !store compatible to gribex input
  call grib_get_int(igrib,'numberOfPointsAlongAParallel', &
       isec2(2),iret)
266
!  call grib_check(iret,gribFunction,gribErrorMsg)
Matthias Langer's avatar
 
Matthias Langer committed
267 268
  call grib_get_int(igrib,'numberOfPointsAlongAMeridian', &
       isec2(3),iret)
269
!  call grib_check(iret,gribFunction,gribErrorMsg)
Matthias Langer's avatar
 
Matthias Langer committed
270 271
  call grib_get_real8(igrib,'longitudeOfFirstGridPointInDegrees', &
       xauxin,iret)
272
!  call grib_check(iret,gribFunction,gribErrorMsg)
Matthias Langer's avatar
 
Matthias Langer committed
273 274
  call grib_get_real8(igrib,'latitudeOfLastGridPointInDegrees', &
       yauxin,iret)
275
!  call grib_check(iret,gribFunction,gribErrorMsg)
Matthias Langer's avatar
 
Matthias Langer committed
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
  xaux=xauxin+real(nxshift)*dx
  yaux=yauxin

  ! CHECK GRID SPECIFICATIONS

    if(isec2(2).ne.nxfield) stop 'READWIND: NX NOT CONSISTENT'
    if(isec2(3).ne.ny) stop 'READWIND: NY NOT CONSISTENT'
    if(xaux.eq.0.) xaux=-179.0     ! NCEP DATA
    xaux0=xlon0
    yaux0=ylat0
    if(xaux.lt.0.) xaux=xaux+360.
    if(yaux.lt.0.) yaux=yaux+360.
    if(xaux0.lt.0.) xaux0=xaux0+360.
    if(yaux0.lt.0.) yaux0=yaux0+360.
    if(abs(xaux-xaux0).gt.eps) &
         stop 'READWIND: LOWER LEFT LONGITUDE NOT CONSISTENT'
    if(abs(yaux-yaux0).gt.eps) &
         stop 'READWIND: LOWER LEFT LATITUDE NOT CONSISTENT'
  endif
  !HSO end of edits

  i179=nint(179./dx)
  if (dx.lt.0.7) then
    i180=nint(180./dx)+1    ! 0.5 deg data
  else
    i180=nint(179./dx)+1    ! 1 deg data
  endif
  i181=i180+1

Espen Sollum's avatar
Espen Sollum committed
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 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627
  !!!!!! DJM - orig - if (isec1(6).ne.-1) then
  IF (TRIM(fpname) .NE. 'NOFP') THEN

      IF (TRIM(fpname) .EQ. 'TT') THEN

          DO ii=1,nuvz
              if (current_grib_level .EQ. akz(ii)) numpt=ii
          END DO

          DO j=0,nymin1
              DO i=0,nxfield-1
                  help=zsec4(nxfield*(ny-j-1)+i+1)
                  if(i.le.i180) then
                      tth(i179+i,j,numpt,n)=help
                  else
                      tth(i-i181,j,numpt,n)=help
                  endif
              END DO
          END DO


      ELSEIF (TRIM(fpname) .EQ. 'UU') THEN

          DO ii=1,nuvz
              if (current_grib_level .EQ. akz(ii)) numpu=ii
          END DO

          DO j=0,nymin1
              DO i=0,nxfield-1
                  help=zsec4(nxfield*(ny-j-1)+i+1)
                  if(i.le.i180) then
                      uuh(i179+i,j,numpu)=help
                  else
                      uuh(i-i181,j,numpu)=help
                  endif
              END DO
          END DO

      ELSEIF (TRIM(fpname) .EQ. 'VV') THEN

          DO ii=1,nuvz
              if (current_grib_level .EQ. akz(ii)) numpv=ii
          END DO

          DO j=0,nymin1
              DO i=0,nxfield-1
                  help=zsec4(nxfield*(ny-j-1)+i+1)
                  if(i.le.i180) then
                      vvh(i179+i,j,numpv)=help
                  else
                      vvh(i-i181,j,numpv)=help
                  endif
              END DO
          END DO


      ELSEIF (TRIM(fpname) .EQ. 'RH') THEN

          DO ii=1,nuvz
              if (current_grib_level .EQ. akz(ii)) numprh=ii
          END DO

          DO j=0,nymin1
              DO i=0,nxfield-1
                  help=zsec4(nxfield*(ny-j-1)+i+1)
                  if(i.le.i180) then
                      qvh(i179+i,j,numprh,n)=help
                  else
                      qvh(i-i181,j,numprh,n)=help
                  endif
              END DO
          END DO

      ELSEIF (TRIM(fpname) .EQ. 'WW') THEN

          DO ii=1,nuvz
              if (current_grib_level .EQ. akz(ii)) numpw=ii
          END DO

          DO j=0,nymin1
              DO i=0,nxfield-1
                  help=zsec4(nxfield*(ny-j-1)+i+1)
                  if(i.le.i180) then
                      wwh(i179+i,j,numpw)=help
                  else
                      wwh(i-i181,j,numpw)=help
                  endif
              END DO
          END DO

      ELSEIF (TRIM(fpname) .EQ. 'PS') THEN

          DO j=0,nymin1
              DO i=0,nxfield-1
                  help=zsec4(nxfield*(ny-j-1)+i+1)
                  if(i.le.i180) then
                      ps(i179+i,j,1,n)=help
                  else
                      ps(i-i181,j,1,n)=help
                  endif
              END DO
          END DO

      ELSEIF (TRIM(fpname) .EQ. 'SD') THEN

          DO j=0,nymin1
              DO i=0,nxfield-1
                  help=zsec4(nxfield*(ny-j-1)+i+1)
                  if(i.le.i180) then
                      sd(i179+i,j,1,n)=help
                  else
                      sd(i-i181,j,1,n)=help
                  endif
              END DO
          END DO

      ELSEIF (TRIM(fpname) .EQ. 'SLP') THEN

          DO j=0,nymin1
              DO i=0,nxfield-1
                  help=zsec4(nxfield*(ny-j-1)+i+1)
                  if(i.le.i180) then
                      msl(i179+i,j,1,n)=help
                  else
                      msl(i-i181,j,1,n)=help
                  endif
              END DO
          END DO

      ELSEIF (TRIM(fpname) .EQ. 'TCC') THEN

          DO j=0,nymin1
              DO i=0,nxfield-1
                  help=zsec4(nxfield*(ny-j-1)+i+1)
                  if(i.le.i180) then
                      tcc(i179+i,j,1,n)=help
                  else
                      tcc(i-i181,j,1,n)=help
                  endif
              END DO
          END DO

      ELSEIF (TRIM(fpname) .EQ. 'U10') THEN

          DO j=0,nymin1
              DO i=0,nxfield-1
                  help=zsec4(nxfield*(ny-j-1)+i+1)
                  if(i.le.i180) then
                      u10(i179+i,j,1,n)=help
                  else
                      u10(i-i181,j,1,n)=help
                  endif
              END DO
          END DO

      ELSEIF (TRIM(fpname) .EQ. 'V10') THEN

          DO j=0,nymin1
              DO i=0,nxfield-1
                  help=zsec4(nxfield*(ny-j-1)+i+1)
                  if(i.le.i180) then
                      v10(i179+i,j,1,n)=help
                  else
                      v10(i-i181,j,1,n)=help
                  endif
              END DO
          END DO

      ELSEIF (TRIM(fpname) .EQ. 'T2') THEN

          DO j=0,nymin1
              DO i=0,nxfield-1
                  help=zsec4(nxfield*(ny-j-1)+i+1)
                  if(i.le.i180) then
                      tt2(i179+i,j,1,n)=help
                  else
                      tt2(i-i181,j,1,n)=help
                  endif
              END DO
          END DO

      ELSEIF (TRIM(fpname) .EQ. 'TD2') THEN

          DO j=0,nymin1
              DO i=0,nxfield-1
                  help=zsec4(nxfield*(ny-j-1)+i+1)
                  if(i.le.i180) then
                      td2(i179+i,j,1,n)=help
                  else
                      td2(i-i181,j,1,n)=help
                  endif
              END DO
          END DO

      ELSEIF (TRIM(fpname) .EQ. 'LSPREC') THEN

          DO j=0,nymin1
              DO i=0,nxfield-1
                  help=zsec4(nxfield*(ny-j-1)+i+1)
                  if(i.le.i180) then
                      lsprec(i179+i,j,1,n)=help
                  else
                      lsprec(i-i181,j,1,n)=help
                  endif
              END DO
          END DO

      ELSEIF (TRIM(fpname) .EQ. 'CONVPREC') THEN

          DO j=0,nymin1
              DO i=0,nxfield-1
                  help=zsec4(nxfield*(ny-j-1)+i+1)
                  if(i.le.i180) then
                      convprec(i179+i,j,1,n)=help
                  else
                      convprec(i-i181,j,1,n)=help
                  endif
              END DO
          END DO

      ELSEIF (TRIM(fpname) .EQ. 'ORO') THEN

          DO j=0,nymin1
              DO i=0,nxfield-1
                  help=zsec4(nxfield*(ny-j-1)+i+1)
                  if(i.le.i180) then
                      oro(i179+i,j)=help
                      excessoro(i179+i,j)=0.0 ! ISOBARIC SURFACES: SUBGRID TERRAIN DISREGARDED
                  else
                      oro(i-i181,j)=help
                      excessoro(i-i181,j)=0.0 ! ISOBARIC SURFACES: SUBGRID TERRAIN DISREGARDED
                  endif
              END DO
          END DO

      ELSEIF (TRIM(fpname) .EQ. 'LSM') THEN

          DO j=0,nymin1
              DO i=0,nxfield-1
                  help=zsec4(nxfield*(ny-j-1)+i+1)
                  if(i.le.i180) then
                      lsm(i179+i,j)=help
                  else
                      lsm(i-i181,j)=help
                  endif
              END DO
          END DO

      ELSEIF (TRIM(fpname) .EQ. 'HMIX') THEN

          DO j=0,nymin1
              DO i=0,nxfield-1
                  help=zsec4(nxfield*(ny-j-1)+i+1)
                  if(i.le.i180) then
                      hmix(i179+i,j,1,n)=help
                  else
                      hmix(i-i181,j,1,n)=help
                  endif
              END DO
          END DO

      ELSEIF (TRIM(fpname) .EQ. 'RH2') THEN

          DO j=0,nymin1
              DO i=0,nxfield-1
                  help=zsec4(nxfield*(ny-j-1)+i+1)
                  if(i.le.i180) then
                      qvh2(i179+i,j)=help
                  else
                      qvh2(i-i181,j)=help
                  endif
              END DO
          END DO

      ELSEIF (TRIM(fpname) .EQ. 'TSIG1') THEN

          DO j=0,nymin1
              DO i=0,nxfield-1
                  help=zsec4(nxfield*(ny-j-1)+i+1)
                  if(i.le.i180) then
                      tlev1(i179+i,j)=help
                  else
                      tlev1(i-i181,j)=help
                  endif
              END DO
          END DO

      ELSEIF (TRIM(fpname) .EQ. 'USIG1') THEN

          DO j=0,nymin1
              DO i=0,nxfield-1
                  help=zsec4(nxfield*(ny-j-1)+i+1)
                  if(i.le.i180) then
                      ulev1(i179+i,j)=help
                  else
                      ulev1(i-i181,j)=help
                  endif
              END DO
          END DO

      ELSEIF (TRIM(fpname) .EQ. 'VSIG1') THEN

          DO j=0,nymin1
              DO i=0,nxfield-1
                  help=zsec4(nxfield*(ny-j-1)+i+1)
                  if(i.le.i180) then
                      vlev1(i179+i,j)=help
                  else
                      vlev1(i-i181,j)=help
                  endif
              END DO
          END DO











Matthias Langer's avatar
 
Matthias Langer committed
628

Espen Sollum's avatar
Espen Sollum committed
629 630 631 632 633
      END IF


!!!!!!!  BEGIN OLD LOOP STRUCTURE - WILL EVENTUALLY BE DELETED
#ifdef NO_LONGER_NEEDED
Matthias Langer's avatar
 
Matthias Langer committed
634 635
  do j=0,nymin1
    do i=0,nxfield-1
Espen Sollum's avatar
Espen Sollum committed
636 637 638 639 640

!!!!!! DJM - orig -       if((isec1(6).eq.011).and.(isec1(7).eq.100)) then


      IF (TRIM(fpname) .EQ. 'TT') THEN
Matthias Langer's avatar
 
Matthias Langer committed
641 642 643
  ! TEMPERATURE
         if((i.eq.0).and.(j.eq.0)) then
            do ii=1,nuvz
Espen Sollum's avatar
Espen Sollum committed
644 645
              !!!!!! DJM - orig - if ((isec1(8)*100.0).eq.akz(ii)) numpt=ii
              if (current_grib_level .EQ. akz(ii)) numpt=ii
Matthias Langer's avatar
 
Matthias Langer committed
646 647 648 649 650 651 652 653 654
            end do
        endif
        help=zsec4(nxfield*(ny-j-1)+i+1)
        if(i.le.i180) then
          tth(i179+i,j,numpt,n)=help
        else
          tth(i-i181,j,numpt,n)=help
        endif
      endif
Espen Sollum's avatar
Espen Sollum committed
655 656 657

!!!!!! DJM - orig      if((isec1(6).eq.033).and.(isec1(7).eq.100)) then
      IF (TRIM(fpname) .EQ. 'UU') THEN
Matthias Langer's avatar
 
Matthias Langer committed
658 659 660
  ! U VELOCITY
         if((i.eq.0).and.(j.eq.0)) then
            do ii=1,nuvz
Espen Sollum's avatar
Espen Sollum committed
661 662
              !!!!!! DJM - orig if ((isec1(8)*100.0).eq.akz(ii)) numpu=ii
              if (current_grib_level .EQ. akz(ii)) numpu=ii
Matthias Langer's avatar
 
Matthias Langer committed
663 664 665 666 667 668 669 670 671
            end do
        endif
        help=zsec4(nxfield*(ny-j-1)+i+1)
        if(i.le.i180) then
          uuh(i179+i,j,numpu)=help
        else
          uuh(i-i181,j,numpu)=help
        endif
      endif
Espen Sollum's avatar
Espen Sollum committed
672 673 674

!!!!!! DJM - orig -       if((isec1(6).eq.034).and.(isec1(7).eq.100)) then
      IF (TRIM(fpname) .EQ. 'VV') THEN
Matthias Langer's avatar
 
Matthias Langer committed
675 676 677
  ! V VELOCITY
         if((i.eq.0).and.(j.eq.0)) then
            do ii=1,nuvz
Espen Sollum's avatar
Espen Sollum committed
678 679
              !!!!!!  DJM - orig - if ((isec1(8)*100.0).eq.akz(ii)) numpv=ii
              if (current_grib_level .EQ. akz(ii)) numpv=ii
Matthias Langer's avatar
 
Matthias Langer committed
680 681 682 683 684 685 686 687 688
            end do
        endif
        help=zsec4(nxfield*(ny-j-1)+i+1)
        if(i.le.i180) then
          vvh(i179+i,j,numpv)=help
        else
          vvh(i-i181,j,numpv)=help
        endif
      endif
Espen Sollum's avatar
Espen Sollum committed
689 690 691 692 693 694




!!!!!! DJM - orig -       if((isec1(6).eq.052).and.(isec1(7).eq.100)) then
      IF (TRIM(fpname) .EQ. 'RH') THEN
Matthias Langer's avatar
 
Matthias Langer committed
695 696 697
  ! RELATIVE HUMIDITY -> CONVERT TO SPECIFIC HUMIDITY LATER
         if((i.eq.0).and.(j.eq.0)) then
            do ii=1,nuvz
Espen Sollum's avatar
Espen Sollum committed
698 699
              !!!!!! DJM - orig - if ((isec1(8)*100.0).eq.akz(ii)) numprh=ii
              if (current_grib_level .EQ. akz(ii)) numprh=ii
Matthias Langer's avatar
 
Matthias Langer committed
700 701 702 703 704 705 706 707 708
            end do
        endif
        help=zsec4(nxfield*(ny-j-1)+i+1)
        if(i.le.i180) then
          qvh(i179+i,j,numprh,n)=help
        else
          qvh(i-i181,j,numprh,n)=help
        endif
      endif
Espen Sollum's avatar
Espen Sollum committed
709 710 711

!!!!!! DJM - orig      if((isec1(6).eq.039).and.(isec1(7).eq.100)) then
      IF (TRIM(fpname) .EQ. 'WW') THEN
Matthias Langer's avatar
 
Matthias Langer committed
712 713 714
  ! W VELOCITY
         if((i.eq.0).and.(j.eq.0)) then
            do ii=1,nuvz
Espen Sollum's avatar
Espen Sollum committed
715 716
              !!!!!! DJM - orig - if ((isec1(8)*100.0).eq.akz(ii)) numpw=ii
              if (current_grib_level .EQ. akz(ii)) numpw=ii
Matthias Langer's avatar
 
Matthias Langer committed
717 718 719 720 721 722 723 724 725
            end do
        endif
        help=zsec4(nxfield*(ny-j-1)+i+1)
        if(i.le.i180) then
          wwh(i179+i,j,numpw)=help
        else
          wwh(i-i181,j,numpw)=help
        endif
      endif
Espen Sollum's avatar
Espen Sollum committed
726 727 728 729 730 731 732 733 734 735 736 737 738 739

!!!!!! DJM - orig -       if((isec1(6).eq.001).and.(isec1(7).eq.001)) then
      IF (TRIM(fpname) .EQ. 'PS') THEN
  ! SURFACE PRESSURE
        help=zsec4(nxfield*(ny-j-1)+i+1)
        if(i.le.i180) then
          ps(i179+i,j,1,n)=help
        else
          ps(i-i181,j,1,n)=help
        endif
      endif

!!!!!! DJM - orig      if((isec1(6).eq.066).and.(isec1(7).eq.001)) then
      IF (TRIM(fpname) .EQ. 'SD') THEN
Matthias Langer's avatar
 
Matthias Langer committed
740 741 742 743 744 745 746 747
  ! SNOW DEPTH
        help=zsec4(nxfield*(ny-j-1)+i+1)
        if(i.le.i180) then
          sd(i179+i,j,1,n)=help
        else
          sd(i-i181,j,1,n)=help
        endif
      endif
Espen Sollum's avatar
Espen Sollum committed
748 749 750

!!!!!! DJM - orig -       if((isec1(6).eq.002).and.(isec1(7).eq.102)) then
      IF (TRIM(fpname) .EQ. 'SLP') THEN
Matthias Langer's avatar
 
Matthias Langer committed
751 752 753 754 755 756 757 758
  ! MEAN SEA LEVEL PRESSURE
        help=zsec4(nxfield*(ny-j-1)+i+1)
        if(i.le.i180) then
          msl(i179+i,j,1,n)=help
        else
          msl(i-i181,j,1,n)=help
        endif
      endif
Espen Sollum's avatar
Espen Sollum committed
759 760 761

!!!!!! DJM - orig      if((isec1(6).eq.071).and.(isec1(7).eq.244)) then
      IF (TRIM(fpname) .EQ. 'TCC') THEN
Matthias Langer's avatar
 
Matthias Langer committed
762 763 764 765 766 767 768 769
  ! TOTAL CLOUD COVER
        help=zsec4(nxfield*(ny-j-1)+i+1)
        if(i.le.i180) then
          tcc(i179+i,j,1,n)=help
        else
          tcc(i-i181,j,1,n)=help
        endif
      endif
Espen Sollum's avatar
Espen Sollum committed
770 771 772 773

!!!!!! DJM - orig      if((isec1(6).eq.033).and.(isec1(7).eq.105).and. &
!!!!!!           (isec1(8).eq.10)) then
      IF (TRIM(fpname) .EQ. 'U10') THEN
Matthias Langer's avatar
 
Matthias Langer committed
774 775 776 777 778 779 780 781
  ! 10 M U VELOCITY
        help=zsec4(nxfield*(ny-j-1)+i+1)
        if(i.le.i180) then
          u10(i179+i,j,1,n)=help
        else
          u10(i-i181,j,1,n)=help
        endif
      endif
Espen Sollum's avatar
Espen Sollum committed
782 783 784 785

!!!!!! DJM - orig      if((isec1(6).eq.034).and.(isec1(7).eq.105).and. &
!!!!!!           (isec1(8).eq.10)) then
      IF (TRIM(fpname) .EQ. 'V10') THEN
Matthias Langer's avatar
 
Matthias Langer committed
786 787 788 789 790 791 792 793
  ! 10 M V VELOCITY
        help=zsec4(nxfield*(ny-j-1)+i+1)
        if(i.le.i180) then
          v10(i179+i,j,1,n)=help
        else
          v10(i-i181,j,1,n)=help
        endif
      endif
Espen Sollum's avatar
Espen Sollum committed
794 795 796 797

!!!!!! DJM - orig      if((isec1(6).eq.011).and.(isec1(7).eq.105).and. &
!!!!!!           (isec1(8).eq.02)) then
      IF (TRIM(fpname) .EQ. 'T2') THEN
Matthias Langer's avatar
 
Matthias Langer committed
798 799 800 801 802 803 804 805
  ! 2 M TEMPERATURE
        help=zsec4(nxfield*(ny-j-1)+i+1)
        if(i.le.i180) then
          tt2(i179+i,j,1,n)=help
        else
          tt2(i-i181,j,1,n)=help
        endif
      endif
Espen Sollum's avatar
Espen Sollum committed
806 807 808 809

!!!!!! DJM - orig -       if((isec1(6).eq.017).and.(isec1(7).eq.105).and. &
!!!!!!           (isec1(8).eq.02)) then
      IF (TRIM(fpname) .EQ. 'TD2') THEN
Matthias Langer's avatar
 
Matthias Langer committed
810 811 812 813 814 815 816 817
  ! 2 M DEW POINT TEMPERATURE
        help=zsec4(nxfield*(ny-j-1)+i+1)
        if(i.le.i180) then
          td2(i179+i,j,1,n)=help
        else
          td2(i-i181,j,1,n)=help
        endif
      endif
Espen Sollum's avatar
Espen Sollum committed
818 819 820

!!!!!! DJM - orig      if((isec1(6).eq.062).and.(isec1(7).eq.001)) then
     IF (TRIM(fpname) .EQ. 'LSPREC') THEN
Matthias Langer's avatar
 
Matthias Langer committed
821 822 823 824 825 826 827 828
  ! LARGE SCALE PREC.
        help=zsec4(nxfield*(ny-j-1)+i+1)
        if(i.le.i180) then
          lsprec(i179+i,j,1,n)=help
        else
          lsprec(i-i181,j,1,n)=help
        endif
      endif
Espen Sollum's avatar
Espen Sollum committed
829 830 831

!!!!!! DJM - orig -       if((isec1(6).eq.063).and.(isec1(7).eq.001)) then
     IF (TRIM(fpname) .EQ. 'CONVPREC') THEN
Matthias Langer's avatar
 
Matthias Langer committed
832 833 834 835 836 837 838 839
  ! CONVECTIVE PREC.
        help=zsec4(nxfield*(ny-j-1)+i+1)
        if(i.le.i180) then
          convprec(i179+i,j,1,n)=help
        else
          convprec(i-i181,j,1,n)=help
        endif
      endif
Espen Sollum's avatar
Espen Sollum committed
840 841 842

!!!!!! DJM - orig -       if((isec1(6).eq.007).and.(isec1(7).eq.001)) then
     IF (TRIM(fpname) .EQ. 'ORO') THEN
Matthias Langer's avatar
 
Matthias Langer committed
843 844 845 846 847 848 849 850 851 852
  ! TOPOGRAPHY
        help=zsec4(nxfield*(ny-j-1)+i+1)
        if(i.le.i180) then
          oro(i179+i,j)=help
          excessoro(i179+i,j)=0.0 ! ISOBARIC SURFACES: SUBGRID TERRAIN DISREGARDED
        else
          oro(i-i181,j)=help
          excessoro(i-i181,j)=0.0 ! ISOBARIC SURFACES: SUBGRID TERRAIN DISREGARDED
        endif
      endif
Espen Sollum's avatar
Espen Sollum committed
853 854 855

!!!!!! DJM - orig      if((isec1(6).eq.081).and.(isec1(7).eq.001)) then
     IF (TRIM(fpname) .EQ. 'LSM') THEN
Matthias Langer's avatar
 
Matthias Langer committed
856 857 858 859 860 861 862 863
  ! LAND SEA MASK
        help=zsec4(nxfield*(ny-j-1)+i+1)
        if(i.le.i180) then
          lsm(i179+i,j)=help
        else
          lsm(i-i181,j)=help
        endif
      endif
Espen Sollum's avatar
Espen Sollum committed
864 865 866

!!!!!! DJM - orig      if((isec1(6).eq.221).and.(isec1(7).eq.001)) then
     IF (TRIM(fpname) .EQ. 'HMIX') THEN
Matthias Langer's avatar
 
Matthias Langer committed
867 868 869 870 871 872 873 874
  ! MIXING HEIGHT
        help=zsec4(nxfield*(ny-j-1)+i+1)
        if(i.le.i180) then
          hmix(i179+i,j,1,n)=help
        else
          hmix(i-i181,j,1,n)=help
        endif
      endif
Espen Sollum's avatar
Espen Sollum committed
875 876 877 878

!!!!!! DJM - orig -      if((isec1(6).eq.052).and.(isec1(7).eq.105).and. &
!!!!!!           (isec1(8).eq.02)) then
     IF (TRIM(fpname) .EQ. 'RH2') THEN
Matthias Langer's avatar
 
Matthias Langer committed
879 880 881 882 883 884 885 886
  ! 2 M RELATIVE HUMIDITY
        help=zsec4(nxfield*(ny-j-1)+i+1)
        if(i.le.i180) then
          qvh2(i179+i,j)=help
        else
          qvh2(i-i181,j)=help
        endif
      endif
Espen Sollum's avatar
Espen Sollum committed
887 888 889

!!!!!! DJM - orig -       if((isec1(6).eq.011).and.(isec1(7).eq.107)) then
     IF (TRIM(fpname) .EQ. 'TSIG1') THEN
Matthias Langer's avatar
 
Matthias Langer committed
890 891 892 893 894 895 896 897
  ! TEMPERATURE LOWEST SIGMA LEVEL
        help=zsec4(nxfield*(ny-j-1)+i+1)
        if(i.le.i180) then
          tlev1(i179+i,j)=help
        else
          tlev1(i-i181,j)=help
        endif
      endif
Espen Sollum's avatar
Espen Sollum committed
898 899 900

!!!!!! DJM - orig      if((isec1(6).eq.033).and.(isec1(7).eq.107)) then
     IF (TRIM(fpname) .EQ. 'USIG1') THEN
Matthias Langer's avatar
 
Matthias Langer committed
901 902 903 904 905 906 907 908
  ! U VELOCITY LOWEST SIGMA LEVEL
        help=zsec4(nxfield*(ny-j-1)+i+1)
        if(i.le.i180) then
          ulev1(i179+i,j)=help
        else
          ulev1(i-i181,j)=help
        endif
      endif
Espen Sollum's avatar
Espen Sollum committed
909 910 911

!!!!!! DJM - orig      if((isec1(6).eq.034).and.(isec1(7).eq.107)) then
     IF (TRIM(fpname) .EQ. 'VSIG1') THEN
Matthias Langer's avatar
 
Matthias Langer committed
912 913 914 915 916 917 918 919 920 921 922 923
  ! V VELOCITY LOWEST SIGMA LEVEL
        help=zsec4(nxfield*(ny-j-1)+i+1)
        if(i.le.i180) then
          vlev1(i179+i,j)=help
        else
          vlev1(i-i181,j)=help
        endif
      endif

    end do
  end do

Espen Sollum's avatar
Espen Sollum committed
924 925 926 927 928 929 930
#endif
!!!!!!!  END OLD LOOP STRUCTURE - WILL EVENTUALLY BE DELETED




  endif !!!!! (TRIM(fpname) .NE. 'NOFP')
Matthias Langer's avatar
 
Matthias Langer committed
931

Espen Sollum's avatar
Espen Sollum committed
932 933
!!!!!! DJM - orig -   if((isec1(6).eq.33).and.(isec1(7).eq.100)) then
     IF (TRIM(fpname) .EQ. 'UU') THEN
Matthias Langer's avatar
 
Matthias Langer committed
934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098
  ! NCEP ISOBARIC LEVELS
    iumax=iumax+1
  endif

  call grib_release(igrib)
  goto 10                      !! READ NEXT LEVEL OR PARAMETER
  !
  ! CLOSING OF INPUT DATA FILE
  !

  !HSO close grib file
50   continue
  call grib_close_file(ifile)

  ! SENS. HEAT FLUX
  sshf(:,:,1,n)=0.0     ! not available from gfs.tccz.pgrbfxx files
  hflswitch=.false.    ! Heat flux not available
  ! SOLAR RADIATIVE FLUXES
  ssr(:,:,1,n)=0.0      ! not available from gfs.tccz.pgrbfxx files
  ! EW SURFACE STRESS
  ewss=0.0         ! not available from gfs.tccz.pgrbfxx files
  ! NS SURFACE STRESS
  nsss=0.0         ! not available from gfs.tccz.pgrbfxx files
  strswitch=.false.    ! stress not available

  ! CONVERT TP TO LSP (GRIB2 only)
  if (gribVer.eq.2) then
    do j=0,nymin1
    do i=0,nxfield-1
     if(i.le.i180) then
     if (convprec(i179+i,j,1,n).lt.lsprec(i179+i,j,1,n)) then ! neg precip would occur
         lsprec(i179+i,j,1,n)= &
              lsprec(i179+i,j,1,n)-convprec(i179+i,j,1,n)
     else
         lsprec(i179+i,j,1,n)=0
     endif
     else
     if (convprec(i-i181,j,1,n).lt.lsprec(i-i181,j,1,n)) then
          lsprec(i-i181,j,1,n)= &
               lsprec(i-i181,j,1,n)-convprec(i-i181,j,1,n)
     else
          lsprec(i-i181,j,1,n)=0
     endif
     endif
    enddo
    enddo
  endif
  !HSO end edits


  ! TRANSFORM RH TO SPECIFIC HUMIDITY

  do j=0,ny-1
    do i=0,nxfield-1
      do k=1,nuvz
        help=qvh(i,j,k,n)
        temp=tth(i,j,k,n)
        plev1=akm(k)+bkm(k)*ps(i,j,1,n)
        elev=ew(temp)*help/100.0
        qvh(i,j,k,n)=xmwml*(elev/(plev1-((1.0-xmwml)*elev)))
      end do
    end do
  end do

  ! CALCULATE 2 M DEW POINT FROM 2 M RELATIVE HUMIDITY
  ! USING BOLTON'S (1980) FORMULA
  ! BECAUSE td2 IS NOT AVAILABLE FROM NCEP GFS DATA

  do j=0,ny-1
    do i=0,nxfield-1
        help=qvh2(i,j)
        temp=tt2(i,j,1,n)
        elev=ew(temp)/100.*help/100.   !vapour pressure in hPa
        td2(i,j,1,n)=243.5/(17.67/log(elev/6.112)-1)+273.
        if (help.le.0.) td2(i,j,1,n)=tt2(i,j,1,n)
    end do
  end do

  if(levdiff2.eq.0) then
    iwmax=nlev_ec+1
    do i=0,nxmin1
      do j=0,nymin1
        wwh(i,j,nlev_ec+1)=0.
      end do
    end do
  endif


  ! For global fields, assign the leftmost data column also to the rightmost
  ! data column; if required, shift whole grid by nxshift grid points
  !*************************************************************************

  if (xglobal) then
    call shift_field_0(ewss,nxfield,ny)
    call shift_field_0(nsss,nxfield,ny)
    call shift_field_0(oro,nxfield,ny)
    call shift_field_0(excessoro,nxfield,ny)
    call shift_field_0(lsm,nxfield,ny)
    call shift_field_0(ulev1,nxfield,ny)
    call shift_field_0(vlev1,nxfield,ny)
    call shift_field_0(tlev1,nxfield,ny)
    call shift_field_0(qvh2,nxfield,ny)
    call shift_field(ps,nxfield,ny,1,1,2,n)
    call shift_field(sd,nxfield,ny,1,1,2,n)
    call shift_field(msl,nxfield,ny,1,1,2,n)
    call shift_field(tcc,nxfield,ny,1,1,2,n)
    call shift_field(u10,nxfield,ny,1,1,2,n)
    call shift_field(v10,nxfield,ny,1,1,2,n)
    call shift_field(tt2,nxfield,ny,1,1,2,n)
    call shift_field(td2,nxfield,ny,1,1,2,n)
    call shift_field(lsprec,nxfield,ny,1,1,2,n)
    call shift_field(convprec,nxfield,ny,1,1,2,n)
    call shift_field(sshf,nxfield,ny,1,1,2,n)
    call shift_field(ssr,nxfield,ny,1,1,2,n)
    call shift_field(hmix,nxfield,ny,1,1,2,n)
    call shift_field(tth,nxfield,ny,nuvzmax,nuvz,2,n)
    call shift_field(qvh,nxfield,ny,nuvzmax,nuvz,2,n)
    call shift_field(uuh,nxfield,ny,nuvzmax,nuvz,1,1)
    call shift_field(vvh,nxfield,ny,nuvzmax,nuvz,1,1)
    call shift_field(wwh,nxfield,ny,nwzmax,nwz,1,1)
  endif

  do i=0,nxmin1
    do j=0,nymin1
  ! Convert precip. from mm/s -> mm/hour
      convprec(i,j,1,n)=convprec(i,j,1,n)*3600.
      lsprec(i,j,1,n)=lsprec(i,j,1,n)*3600.
      surfstr(i,j,1,n)=sqrt(ewss(i,j)**2+nsss(i,j)**2)
    end do
  end do

  if ((.not.hflswitch).or.(.not.strswitch)) then
  !  write(*,*) 'WARNING: No flux data contained in GRIB file ',
  !    +  wfname(indj)

  ! CALCULATE USTAR AND SSHF USING THE PROFILE METHOD
  !***************************************************************************

    do i=0,nxmin1
      do j=0,nymin1
        hlev1=30.0                     ! HEIGHT OF FIRST MODEL SIGMA LAYER
        ff10m= sqrt(u10(i,j,1,n)**2+v10(i,j,1,n)**2)
        fflev1=sqrt(ulev1(i,j)**2+vlev1(i,j)**2)
        call pbl_profile(ps(i,j,1,n),td2(i,j,1,n),hlev1, &
             tt2(i,j,1,n),tlev1(i,j),ff10m,fflev1, &
             surfstr(i,j,1,n),sshf(i,j,1,n))
        if(sshf(i,j,1,n).gt.200.) sshf(i,j,1,n)=200.
        if(sshf(i,j,1,n).lt.-400.) sshf(i,j,1,n)=-400.
      end do
    end do
  endif

  if(iumax.ne.nuvz) stop 'READWIND: NUVZ NOT CONSISTENT'
  if(iumax.ne.nwz)    stop 'READWIND: NWZ NOT CONSISTENT'

  return
888   write(*,*) ' #### FLEXPART MODEL ERROR! WINDFIELD         #### '
  write(*,*) ' #### ',wfname(indj),'                    #### '
  write(*,*) ' #### IS NOT GRIB FORMAT !!!                  #### '
  stop 'Execution terminated'
999   write(*,*) ' #### FLEXPART MODEL ERROR! WINDFIELD         #### '
  write(*,*) ' #### ',wfname(indj),'                    #### '
  write(*,*) ' #### CANNOT BE OPENED !!!                    #### '
  stop 'Execution terminated'

1099
end subroutine readwind_gfs