How to add a 4-D variable into the Forcing file

General scientific issues regarding ROMS

Moderators: arango, robertson

Post Reply
Message
Author
feroda

How to add a 4-D variable into the Forcing file

#1 Unread post by feroda »

Hi all,
I intend to read a 4-D(t,z,y,x) variable, MYvar, into ROMS. The NetCDF file for MYvar is in the FRCNAME list in ocean_*.in.

There comes some edits on mod_forces.F. One thing I want to check with you is: how to initialize module variables, which also have vertical levels?
I guess it is not correct as follows:
FORCES(ng) % MYvar(i,j,k) = Inival
FORCES(ng) % MYvarG(i,j,k,1) = Inival
FORCES(ng) % MYvarG(i,j,k,2) = Inival


Thank you for letting me know.

P.S. I am using ROMS-v3.5, with the last changed revision 635.

User avatar
kate
Posts: 4091
Joined: Wed Jul 02, 2003 5:29 pm
Location: CFOS/UAF, USA

Re: How to add a 4-D variable into the Forcing file

#2 Unread post by kate »

I see nothing wrong with those lines - as long as they are inside nested do loops for i,j and k.

Are they giving you an error? What error?

Also, with 2-D forcing fields, you can get help from ROMS in the form of horizontal interpolation from coarse global (or other regular lat/lon grid) to the ROMS grid. That won't happen in 3-D. You will need to have the full 3-D field sent in on the ROMS grid. Maybe you can look to the reading of climatology fields for an example.

User avatar
arango
Site Admin
Posts: 1361
Joined: Wed Feb 26, 2003 4:41 pm
Location: DMCS, Rutgers University
Contact:

Re: How to add a 4-D variable into the Forcing file

#3 Unread post by arango »

I don't know what are you doing. I assume that you are confusing climatology fields with forcing fields. The forcing fields in ROMS are (x,y,time) and never (x,y,z,time). They are always horizontal and operate always at the surface or bottom. There is not need for such 3D forcing fields in ROMS kernel. The exception is the river forcing.

3D fields are only used for climatology or background trajectory for the adjoint-based algorithms.

feroda

Re: How to add a 4-D variable into the Forcing file

#4 Unread post by feroda »

arango wrote:I don't know what are you doing.
I want to add a wave-induced mixing coefficient, Ak_wave, to Akv and Akt, which are calculated by subroutine lmd_vmix.F in the current ROMS nonlinear model. Ak_wave is a time-varying 3D (x,y,z) field calculated by a wave model. The reason for doing this is that the modeled surface mixed layer is too shallow compared to that in observations. We are expecting that situation will be improved somewhat by including the wave effect on vertical mixing dynamics.

The Ak_wave field is saved in a NetCDF file separately. Since only FRCNAME accepts multiple input files, I suspected that Ak_wave should be read as a forcing field even though it is (x,y,z,time).

Please let me know what is the correct way to add such a coefficient. Thanks very much.

User avatar
m.hadfield
Posts: 521
Joined: Tue Jul 01, 2003 4:12 am
Location: NIWA

Re: How to add a 4-D variable into the Forcing file

#5 Unread post by m.hadfield »

I have another potential use for a 4-D forcing field: to represent density of farmed mussels in order to calculate their effect on phytoplankton, nutrients, etc. The mussel density is a 3D (x,y,z) field that can vary in time.

User avatar
arango
Site Admin
Posts: 1361
Joined: Wed Feb 26, 2003 4:41 pm
Location: DMCS, Rutgers University
Contact:

Re: How to add a 4-D variable into the Forcing file

#6 Unread post by arango »

Well, all the adjoint algorithms require a basic state trajectory for all primitive variables. There are good examples for processing such variables in get_data.F and set_data.F.

For example, to read a 3D variable snapshot at U-points we have in get_data.F:

Code: Select all

        CALL get_3dfld (ng, iNLM, idUvel, TLF(ng)%ncid,                 &
     &                  1, TLF(ng), update(1),                          &
     &                  LBi, UBi, LBj, UBj, 1, N(ng), 2, 1,             &
#  ifdef MASKING
     &                  GRID(ng) % umask,                               &
#  endif
     &                  OCEAN(ng) % uG)
        IF (exit_flag.ne.NoError) RETURN
We just need to allocate uG(x,y,z,2) to hold the time snapshots and define the appropriate metadata indices. The field is linearly interpolated in set_data.F using the available two-snapshots:

Code: Select all

        CALL set_3dfld_tile (ng, tile, iNLM, idUvel,                    &
     &                       LBi, UBi, LBj, UBj, 1, N(ng),              &
     &                       OCEAN(ng)%uG,                              &
     &                       OCEAN(ng)%f_u,                             &
     &                       update)
        IF (exit_flag.ne.NoError) RETURN
Here the requested field is loaded into f_u.

Of course, you cannot use such variables and indices because they are reserved for the adjoint algorithms. You just need to define new ones.

I will add such possibility when the redtide model is added to ROMS in the future.

feroda

Re: How to add a 4-D variable into the Forcing file

#7 Unread post by feroda »

Hi Kate, Mark and Hernan,

Thanks a lot for your kind replies.

Here is the detailed information on what I have done to add a time-varying 3D field. I wish it is helpful for you, ROMS masters, as well as others to identify what's wrong with what I am doing.

step 1, define and create a NetCDF file for the 3D filed (bv, with a ID of idWbv):

$ncdump -h roms_bv.nc
netcdf roms_bv {
dimensions:
xi_rho = 927 ;
eta_rho = 736 ;
s_rho = 40 ;
ocean_time = UNLIMITED ; // (12 currently)
variables:
double bv(ocean_time, s_rho, eta_rho, xi_rho) ;
bv:long_name = "wave-induced mixing" ;
bv:time = "ocean_time" ;
bv:coordinates = "lon_rho lat_rho s_rho ocean_time" ;
// global attributes:
}

step 2, in get_data.F, add:
926 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
927 !=======================================================================
928 !
929 #if defined SOLVE3D && defined WAVE_MIXING
930 CALL get_3dfld (ng, iNLM, idWbv, ncFRCid(idWbv,ng), &
931 & nFfiles(ng), FRC(1,ng), update(1), &
932 & LBi, UBi, LBj, UBj, 1, N(ng), 2, 1, &
933 # ifdef MASKING
934 & GRID(ng) % rmask, &
935 # endif
936 & FORCES(ng) % bvG)
937 IF (exit_flag.ne.NoError) RETURN
938 #endif
939 !=======================================================================

step 3. in set_data.F, add:
1208 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1209 !=======================================================================
1210 !
1211 # if defined SOLVE3D && defined WAVE_MIXING
1212 CALL set_3dfld_tile (ng, tile, iNLM, idWbv, &
1213 & LBi, UBi, LBj, UBj, 1, N(ng), &
1214 & FORCES(ng)%bvG, &
1215 & FORCES(ng)%bv, &
1216 & update)
1217 IF (exit_flag.ne.NoError) RETURN
1218 # endif
1219 !=======================================================================

step 4. in lmd_vmix.F, add:
4.1
42 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
43 !=======================================================================
44 !
45 # ifdef WAVE_MIXING
46 USE mod_forces
47 # endif
48 !=======================================================================

4.2
84 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
85 !=======================================================================
86 !
87 # ifdef WAVE_MIXING
88 & FORCES(ng) % bv, &
89 # endif
90 !=======================================================================

4.3
121 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
122 !=======================================================================
123 !
124 # ifdef WAVE_MIXING
125 & bv, &
126 # endif
127 !=======================================================================

4.4
153 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
154 !=======================================================================
155 !
156 # ifdef WAVE_MIXING
157 real(r8), intent(in) :: bv(LBi:,LBj:,0:)
158 # endif
159 !=======================================================================

4.5
176 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
177 !=======================================================================
178 !
179 # ifdef WAVE_MIXING
180 real(r8), intent(in) :: bv(LBi:UBi,LBj:UBj,0:N(ng))
181 # endif
182 !=======================================================================

4.6
389 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
390 !=======================================================================
391 !
392 # ifdef WAVE_MIXING
393 Akv(i,j,k)=Akv(i,j,k)+bv(i,j,k)
394 Akt(i,j,k,itemp)=Akt(i,j,k,itemp)+bv(i,j,k)*0.5_r8
395 # ifdef SALINITY
396 Akt(i,j,k,isalt)=Akt(i,j,k,itemp)
397 # endif
398 # endif
399 !=======================================================================

step 5, in mod_forces.F, add:
5.1
327 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
328 !=======================================================================
329 !
330 # ifdef WAVE_MIXING
331 real(r8), pointer :: bv(:,:,:)
332 real(r8), pointer :: bvG(:,:,:,:)
333 # endif
334 !=======================================================================

5.2
650 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
651 !=======================================================================
652 !
653 # ifdef WAVE_MIXING
654 allocate ( FORCES(ng) % bv(LBi:UBi,LBj:UBj,N(ng)) )
655 allocate ( FORCES(ng) % bvG(LBi:UBi,LBj:UBj,N(ng),2) )
656 # endif
657 !=======================================================================

5.3
1024 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1025 !=======================================================================
1026 !
1027 # ifdef WAVE_MIXING
1028 DO k=1,N(ng)
1029 FORCES(ng) % bv(i,j,k) = IniVal
1030 FORCES(ng) % bvG(i,j,k,1) = IniVal
1031 FORCES(ng) % bvG(i,j,k,2) = IniVal
1032 END DO
1033 # endif
1034 !=======================================================================

step 6, in mod_ncparam.F, add:
6.1
170 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
171 !=======================================================================
172 !
173 integer :: idWbv ! wave-induced mixing coefficient
174 !=======================================================================

6.2
1341 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1342 !=======================================================================
1343 !
1344 CASE ('idWbv')
1345 idWbv=varid
1346 !=======================================================================

There is no any errors in compiling ROMS source codes after all those changes were added.

However, here are the output information and errors I got to run the model:

Model Input Parameters: ROMS/TOMS version 3.6
Thursday - January 24, 2013 - 5:06:55 PM
-----------------------------------------------------------------------------
....
....
Output/Input Files:

Output Restart File: ocean_rst.nc
Prefix for History Files: ocean_his
Prefix for Averages Files: ocean_avg
Input Grid File: ../../inputs/scs30/Luzon/roms_grid.nc
Input Nonlinear Initial File: ../../inputs/scs30/Luzon/roms_ini.nc
Input Forcing File 01: ../../inputs/scs30/roms_frc.nc
Input Forcing File 02: ../../inputs/scs30/roms_river_clm.nc
Input Forcing File 03: ../../inputs/scs30/roms_bv.nc
Input Boundary File: ../../inputs/scs30/roms_bry_clim_SODA.nc
....
....
NLM: GET_STATE - Read state initial conditions, t = 0 00:00:00
(File: roms_ini.nc, Rec=0001, Index=1)
- free-surface
....
....
GET_NGFLD - salinity northern boundary condition, t = 345 00:00:00
(Rec=0012, Index=2, File: roms_bry_clim_SODA.nc)
(Tmin= 15.0000 Tmax= 345.0000)
(Min = 0.00000000E+00 Max = 3.48276405E+01)



INQUIRE - unable to find requested variable: ^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@
in files:
../../inputs/scs30/roms_frc.nc
../../inputs/scs30/roms_river_clm.nc
../../inputs/scs30/roms_bv.nc
....
INQUIRE - unable to find requested variable: ^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@
in files:
../../inputs/scs30/roms_frc.nc
../../inputs/scs30/roms_river_clm.nc
../../inputs/scs30/roms_bv.nc
....
Node # 30 CPU: 38.736
Node #112 CPU: 39.991
ROMS/TOMS - Output NetCDF summary for Grid 01:

Analytical header files used:

ROMS/Functionals/ana_hmixcoef.h
ROMS/Functionals/ana_nudgcoef.h

ROMS/TOMS - Input error ............. exit_flag: 2


ERROR: Abnormal termination: NetCDF INPUT.


The information before ‘GET_NGFLD - salinity northern boundary condition, t = 345 00:00:00’ is generally the same with those without 'WAVE_MIXING' being defined.

For now, I could not figure out why the new define field, bv, can not be found. Thanks for all your comments and help.

User avatar
arango
Site Admin
Posts: 1361
Joined: Wed Feb 26, 2003 4:41 pm
Location: DMCS, Rutgers University
Contact:

Re: How to add a 4-D variable into the Forcing file

#8 Unread post by arango »

Well, you forgot the most important part. You need to define the metadata for the new field in varinfo.dat. This information is read in mod_ncparam.F.

feroda

Re: How to add a 4-D variable into the Forcing file

#9 Unread post by feroda »

Thank you, Hernan. The question is now solved according to your comments.

Joeailvyou
Posts: 26
Joined: Wed Jul 19, 2017 4:03 pm
Location: Zhejiang University

Re: How to add a 4-D variable into the Forcing file

#10 Unread post by Joeailvyou »

Dear feroda,
May I have a look at your modified code files that have been modified to include Bv ?
I struggled for a long time and don't know how to do it.

Thank God and you If get reply from you! If Ok, send it to my Gmail joe211010@gmail.com. Thank you again.
Best wishes!
Joe

Post Reply