MODULE wrt_dai_mod ! !git $Id$ !svn $Id: wrt_dai.F 1189 2023-08-15 21:26:58Z arango $ !================================================== Hernan G. Arango === ! Copyright (c) 2002-2023 The ROMS/TOMS Group ! ! Licensed under a MIT/X style license ! ! See License_ROMS.md ! !======================================================================= ! ! ! This routine writes out Data Assimilation initial conditions ! ! (4D-Var analysis) or Ensemble Kalman Filter (EnKF) restart file ! ! using the standard NetCDF library or the Parallel-IO (PIO) library. ! ! ! !======================================================================= ! USE mod_param USE mod_parallel USE mod_grid USE mod_iounits USE mod_mixing USE mod_ncparam USE mod_ocean USE mod_scalars USE mod_stepping ! USE nf_fwrite2d_mod, ONLY : nf_fwrite2d USE nf_fwrite3d_mod, ONLY : nf_fwrite3d USE strings_mod, ONLY : FoundError ! implicit none ! PUBLIC :: wrt_dai PRIVATE :: wrt_dai_nf90 ! CONTAINS ! !*********************************************************************** SUBROUTINE wrt_dai (ng, tile) !*********************************************************************** ! ! Imported variable declarations. ! integer, intent(in) :: ng, tile ! ! Local variable declarations. ! integer :: LBi, UBi, LBj, UBj ! character (len=*), parameter :: MyFile = & & "ROMS/Utility/wrt_dai.F" ! !----------------------------------------------------------------------- ! Write out time-averaged fields according to IO type. !----------------------------------------------------------------------- ! LBi=BOUNDS(ng)%LBi(tile) UBi=BOUNDS(ng)%UBi(tile) LBj=BOUNDS(ng)%LBj(tile) UBj=BOUNDS(ng)%UBj(tile) ! SELECT CASE (DIA(ng)%IOtype) CASE (io_nf90) CALL wrt_dai_nf90 (ng, tile, & & LBi, UBi, LBj, UBj) CASE DEFAULT IF (Master) WRITE (stdout,10) DAI(ng)%IOtype exit_flag=3 END SELECT IF (FoundError(exit_flag, NoError, 83, MyFile)) RETURN ! 10 FORMAT (' WRT_DAI - Illegal output file type, io_type = ',i0, & & /,11x,'Check KeyWord ''OUT_LIB'' in ''roms.in''.') ! RETURN END SUBROUTINE wrt_dai ! !*********************************************************************** SUBROUTINE wrt_dai_nf90 (ng, tile, & & LBi, UBi, LBj, UBj) !*********************************************************************** ! USE mod_netcdf ! ! Imported variable declarations. ! integer, intent(in) :: ng, tile integer, intent(in) :: LBi, UBi, LBj, UBj ! ! Local variable declarations. ! integer :: i, j, k, itrc integer :: Fcount, gfactor, gtype, status, varid ! real(dp) :: scale ! character (len=*), parameter :: MyFile = & & "ROMS/Utility/wrt_dai.F"//", wrt_dai_nf90" ! !----------------------------------------------------------------------- ! Set lower and upper tile bounds and staggered variables bounds for ! this horizontal domain partition. Notice that if tile=-1, it will ! set the values for the global grid. !----------------------------------------------------------------------- ! integer :: Istr, IstrB, IstrP, IstrR, IstrT, IstrM, IstrU integer :: Iend, IendB, IendP, IendR, IendT integer :: Jstr, JstrB, JstrP, JstrR, JstrT, JstrM, JstrV integer :: Jend, JendB, JendP, JendR, JendT integer :: Istrm3, Istrm2, Istrm1, IstrUm2, IstrUm1 integer :: Iendp1, Iendp2, Iendp2i, Iendp3 integer :: Jstrm3, Jstrm2, Jstrm1, JstrVm2, JstrVm1 integer :: Jendp1, Jendp2, Jendp2i, Jendp3 ! Istr =BOUNDS(ng) % Istr (tile) IstrB =BOUNDS(ng) % IstrB (tile) IstrM =BOUNDS(ng) % IstrM (tile) IstrP =BOUNDS(ng) % IstrP (tile) IstrR =BOUNDS(ng) % IstrR (tile) IstrT =BOUNDS(ng) % IstrT (tile) IstrU =BOUNDS(ng) % IstrU (tile) Iend =BOUNDS(ng) % Iend (tile) IendB =BOUNDS(ng) % IendB (tile) IendP =BOUNDS(ng) % IendP (tile) IendR =BOUNDS(ng) % IendR (tile) IendT =BOUNDS(ng) % IendT (tile) Jstr =BOUNDS(ng) % Jstr (tile) JstrB =BOUNDS(ng) % JstrB (tile) JstrM =BOUNDS(ng) % JstrM (tile) JstrP =BOUNDS(ng) % JstrP (tile) JstrR =BOUNDS(ng) % JstrR (tile) JstrT =BOUNDS(ng) % JstrT (tile) JstrV =BOUNDS(ng) % JstrV (tile) Jend =BOUNDS(ng) % Jend (tile) JendB =BOUNDS(ng) % JendB (tile) JendP =BOUNDS(ng) % JendP (tile) JendR =BOUNDS(ng) % JendR (tile) JendT =BOUNDS(ng) % JendT (tile) ! Istrm3 =BOUNDS(ng) % Istrm3 (tile) ! Istr-3 Istrm2 =BOUNDS(ng) % Istrm2 (tile) ! Istr-2 Istrm1 =BOUNDS(ng) % Istrm1 (tile) ! Istr-1 IstrUm2=BOUNDS(ng) % IstrUm2(tile) ! IstrU-2 IstrUm1=BOUNDS(ng) % IstrUm1(tile) ! IstrU-1 Iendp1 =BOUNDS(ng) % Iendp1 (tile) ! Iend+1 Iendp2 =BOUNDS(ng) % Iendp2 (tile) ! Iend+2 Iendp2i=BOUNDS(ng) % Iendp2i(tile) ! Iend+2 interior Iendp3 =BOUNDS(ng) % Iendp3 (tile) ! Iend+3 Jstrm3 =BOUNDS(ng) % Jstrm3 (tile) ! Jstr-3 Jstrm2 =BOUNDS(ng) % Jstrm2 (tile) ! Jstr-2 Jstrm1 =BOUNDS(ng) % Jstrm1 (tile) ! Jstr-1 JstrVm2=BOUNDS(ng) % JstrVm2(tile) ! JstrV-2 JstrVm1=BOUNDS(ng) % JstrVm1(tile) ! JstrV-1 Jendp1 =BOUNDS(ng) % Jendp1 (tile) ! Jend+1 Jendp2 =BOUNDS(ng) % Jendp2 (tile) ! Jend+2 Jendp2i=BOUNDS(ng) % Jendp2i(tile) ! Jend+2 interior Jendp3 =BOUNDS(ng) % Jendp3 (tile) ! Jend+3 ! SourceFile=MyFile ! !----------------------------------------------------------------------- ! Write out Data Assimilation initial/restart fields. !----------------------------------------------------------------------- ! IF (FoundError(exit_flag, NoError, 121, MyFile)) RETURN ! ! Set grid type factor to write full (gfactor=1) fields. ! gfactor=1 ! ! Set time record index. ! DAI(ng)%Rindex=DAI(ng)%Rindex+1 Fcount=DAI(ng)%Fcount DAI(ng)%Nrec(Fcount)=DAI(ng)%Nrec(Fcount)+1 ! ! If requested, set time index to recycle time records in restart ! file. ! DAI(ng)%Rindex=MOD(DAI(ng)%Rindex-1,2)+1 ! ! Report. ! IF (Master) WRITE (stdout,10) kstp(ng), nrhs(ng), DAI(ng)%Rindex ! ! Write out time independent (unperturb) depths of RHO-points. ! scale=1.0_dp gtype=gfactor*r3dvar status=nf_fwrite3d(ng, iNLM, DAI(ng)%ncid, idpthR, & & DAI(ng)%Vid(idpthR), & & 0, gtype, & & LBi, UBi, LBj, UBj, 1, N(ng), scale, & & GRID(ng) % rmask, & & GRID(ng) % z0_r, & & SetFillVal = .FALSE.) IF (FoundError(status, nf90_noerr, 169, MyFile)) THEN IF (Master) THEN WRITE (stdout,20) TRIM(Vname(1,idpthR)) END IF exit_flag=3 ioerror=status RETURN END IF ! ! Write out time independent (unperturb) depths of U-points. ! scale=1.0_dp gtype=gfactor*u3dvar DO k=1,N(ng) DO j=Jstr-1,Jend+1 DO i=IstrU-1,Iend+1 GRID(ng)%z_v(i,j,k)=0.5_r8*(GRID(ng)%z0_r(i-1,j,k)+ & & GRID(ng)%z0_r(i ,j,k)) END DO END DO END DO status=nf_fwrite3d(ng, iNLM, DAI(ng)%ncid, idpthU, & & DAI(ng)%Vid(idpthU), & & 0, gtype, & & LBi, UBi, LBj, UBj, 1, N(ng), scale, & & GRID(ng) % umask, & & GRID(ng) % z_v, & & SetFillVal = .FALSE.) IF (FoundError(status, nf90_noerr, 199, MyFile)) THEN IF (Master) THEN WRITE (stdout,20) TRIM(Vname(1,idpthU)) END IF exit_flag=3 ioerror=status RETURN END IF ! ! Write out time independent (unperturb) depths of V-points. ! scale=1.0_dp gtype=gfactor*v3dvar DO k=1,N(ng) DO j=JstrV-1,Jend+1 DO i=Istr-1,Iend+1 GRID(ng)%z_v(i,j,k)=0.5_r8*(GRID(ng)%z0_r(i,j-1,k)+ & & GRID(ng)%z0_r(i,j ,k)) END DO END DO END DO status=nf_fwrite3d(ng, iNLM, DAI(ng)%ncid, idpthV, & & DAI(ng)%Vid(idpthV), & & 0, gtype, & & LBi, UBi, LBj, UBj, 1, N(ng), scale, & & GRID(ng) % vmask, & & GRID(ng) % z_v, & & SetFillVal = .FALSE.) IF (FoundError(status, nf90_noerr, 229, MyFile)) THEN IF (Master) THEN WRITE (stdout,20) TRIM(Vname(1,idpthV)) END IF exit_flag=3 ioerror=status RETURN END IF ! ! Write out time independent (unperturb) depths of W-points. ! scale=1.0_dp gtype=gfactor*w3dvar status=nf_fwrite3d(ng, iNLM, DAI(ng)%ncid, idpthW, & & DAI(ng)%Vid(idpthW), & & 0, gtype, & & LBi, UBi, LBj, UBj, 0, N(ng), scale, & & GRID(ng) % rmask, & & GRID(ng) % z0_w, & & SetFillVal = .FALSE.) IF (FoundError(status, nf90_noerr, 251, MyFile)) THEN IF (Master) THEN WRITE (stdout,20) TRIM(Vname(1,idpthW)) END IF exit_flag=3 ioerror=status RETURN END IF ! ! Write out model time (s). ! CALL netcdf_put_fvar (ng, iNLM, DAI(ng)%name, & & TRIM(Vname(1,idtime)), time(ng:), & & (/DAI(ng)%Rindex/), (/1/), & & ncid = DAI(ng)%ncid, & & varid = DAI(ng)%Vid(idtime)) IF (FoundError(exit_flag, NoError, 268, MyFile)) RETURN ! ! Write out free-surface (m). ! scale=1.0_dp gtype=gfactor*r2dvar status=nf_fwrite2d(ng, iNLM, DAI(ng)%ncid, idFsur, & & DAI(ng)%Vid(idFsur), & & DAI(ng)%Rindex, gtype, & & LBi, UBi, LBj, UBj, scale, & & GRID(ng) % rmask, & & OCEAN(ng) % zeta(:,:,kstp(ng))) IF (FoundError(status, nf90_noerr, 296, MyFile)) THEN IF (Master) THEN WRITE (stdout,30) TRIM(Vname(1,idFsur)), DAI(ng)%Rindex END IF exit_flag=3 ioerror=status RETURN END IF ! ! Write out 2D momentum component (m/s) in the XI-direction. ! scale=1.0_dp gtype=gfactor*u2dvar status=nf_fwrite2d(ng, iNLM, DAI(ng)%ncid, idUbar, & & DAI(ng)%Vid(idUbar), & & DAI(ng)%Rindex, gtype, & & LBi, UBi, LBj, UBj, scale, & & GRID(ng) % umask_full, & & OCEAN(ng) % ubar(:,:,kstp(ng))) IF (FoundError(status, nf90_noerr, 321, MyFile)) THEN IF (Master) THEN WRITE (stdout,30) TRIM(Vname(1,idUbar)), DAI(ng)%Rindex END IF exit_flag=3 ioerror=status RETURN END IF ! ! Write out 2D momentum component (m/s) in the ETA-direction. ! scale=1.0_dp gtype=gfactor*v2dvar status=nf_fwrite2d(ng, iNLM, DAI(ng)%ncid, idVbar, & & DAI(ng)%Vid(idVbar), & & DAI(ng)%Rindex, gtype, & & LBi, UBi, LBj, UBj, scale, & & GRID(ng) % vmask_full, & & OCEAN(ng) % vbar(:,:,kstp(ng))) IF (FoundError(status, nf90_noerr, 346, MyFile)) THEN IF (Master) THEN WRITE (stdout,30) TRIM(Vname(1,idVbar)), DAI(ng)%Rindex END IF exit_flag=3 ioerror=status RETURN END IF ! ! Write out 3D momentum component (m/s) in the XI-direction. ! scale=1.0_dp gtype=gfactor*u3dvar status=nf_fwrite3d(ng, iNLM, DAI(ng)%ncid, idUvel, & & DAI(ng)%Vid(idUvel), & & DAI(ng)%Rindex, gtype, & & LBi, UBi, LBj, UBj, 1, N(ng), scale, & & GRID(ng) % umask_full, & & OCEAN(ng) % u(:,:,:,nrhs(ng))) IF (FoundError(status, nf90_noerr, 373, MyFile)) THEN IF (Master) THEN WRITE (stdout,30) TRIM(Vname(1,idUvel)), DAI(ng)%Rindex END IF exit_flag=3 ioerror=status RETURN END IF ! ! Write out momentum component (m/s) in the ETA-direction. ! scale=1.0_dp gtype=gfactor*v3dvar status=nf_fwrite3d(ng, iNLM, DAI(ng)%ncid, idVvel, & & DAI(ng)%Vid(idVvel), & & DAI(ng)%Rindex, gtype, & & LBi, UBi, LBj, UBj, 1, N(ng), scale, & & GRID(ng) % vmask_full, & & OCEAN(ng) % v(:,:,:,nrhs(ng))) IF (FoundError(status, nf90_noerr, 398, MyFile)) THEN IF (Master) THEN WRITE (stdout,30) TRIM(Vname(1,idVvel)), DAI(ng)%Rindex END IF exit_flag=3 ioerror=status RETURN END IF ! ! Write out tracer type variables. ! DO itrc=1,NT(ng) scale=1.0_dp gtype=gfactor*r3dvar status=nf_fwrite3d(ng, iNLM, DAI(ng)%ncid, idTvar(itrc), & & DAI(ng)%Tid(itrc), & & DAI(ng)%Rindex, gtype, & & LBi, UBi, LBj, UBj, 1, N(ng), scale, & & GRID(ng) % rmask, & & OCEAN(ng) % t(:,:,:,nrhs(ng),itrc)) IF (FoundError(status, nf90_noerr, 424, MyFile)) THEN IF (Master) THEN WRITE (stdout,30) TRIM(Vname(1,idTvar(itrc))), DAI(ng)%Rindex END IF exit_flag=3 ioerror=status RETURN END IF END DO ! ! Write out vertical viscosity coefficient. ! scale=1.0_dp gtype=gfactor*w3dvar status=nf_fwrite3d(ng, iNLM, DAI(ng)%ncid, idVvis, & & DAI(ng)%Vid(idVvis), & & DAI(ng)%Rindex, gtype, & & LBi, UBi, LBj, UBj, 0, N(ng), scale, & & GRID(ng) % rmask, & & MIXING(ng) % Akv, & & SetFillVal = .FALSE.) IF (FoundError(status, nf90_noerr, 447, MyFile)) THEN IF (Master) THEN WRITE (stdout,30) TRIM(Vname(1,idVvis)), DAI(ng)%Rindex END IF exit_flag=3 ioerror=status RETURN END IF ! ! Write out vertical diffusion coefficient for potential temperature. ! scale=1.0_dp gtype=gfactor*w3dvar status=nf_fwrite3d(ng, iNLM, DAI(ng)%ncid, idTdif, & & DAI(ng)%Vid(idTdif), & & DAI(ng)%Rindex, gtype, & & LBi, UBi, LBj, UBj, 0, N(ng), scale, & & GRID(ng) % rmask, & & MIXING(ng) % Akt(:,:,:,itemp), & & SetFillVal = .FALSE.) IF (FoundError(status, nf90_noerr, 469, MyFile)) THEN IF (Master) THEN WRITE (stdout,30) TRIM(Vname(1,idTdif)), DAI(ng)%Rindex END IF exit_flag=3 ioerror=status RETURN END IF ! ! Write out vertical diffusion coefficient for salinity. ! scale=1.0_dp gtype=gfactor*w3dvar status=nf_fwrite3d(ng, iNLM, DAI(ng)%ncid, idSdif, & & DAI(ng)%Vid(idSdif), & & DAI(ng)%Rindex, gtype, & & LBi, UBi, LBj, UBj, 0, N(ng), scale, & & GRID(ng) % rmask, & & MIXING(ng) % Akt(:,:,:,isalt), & & SetFillVal = .FALSE.) IF (FoundError(status, nf90_noerr, 493, MyFile)) THEN IF (Master) THEN WRITE (stdout,30) TRIM(Vname(1,idSdif)), DAI(ng)%Rindex END IF exit_flag=3 ioerror=status RETURN END IF ! !----------------------------------------------------------------------- ! Synchronize restart NetCDF file to disk. !----------------------------------------------------------------------- ! CALL netcdf_sync (ng, iNLM, DAI(ng)%name, DAI(ng)%ncid) IF (FoundError(exit_flag, NoError, 509, MyFile)) RETURN ! 10 FORMAT (2x,'WRT_DAI_NF90 - writing DA INI/RST', t42, & & 'fields (Index=',i1,',',i1,') in record = ',i0) 20 FORMAT (/,' WRT_DAI_NF90 - error while writing variable: ',a, & & /,11x,'into DA initial/restart NetCDF file.') 30 FORMAT (/,' WRT_DAI_NF90 - error while writing variable: ',a, & & /,11x,'into DA initial/rstart NetCDF file for time ', & & 'record: ',i0) ! RETURN END SUBROUTINE wrt_dai_nf90 END MODULE wrt_dai_mod