FMS  2025.01.02-dev
Flexible Modeling System
data_override.F90
1 !***********************************************************************
2 !* GNU Lesser General Public License
3 !*
4 !* This file is part of the GFDL Flexible Modeling System (FMS).
5 !*
6 !* FMS is free software: you can redistribute it and/or modify it under
7 !* the terms of the GNU Lesser General Public License as published by
8 !* the Free Software Foundation, either version 3 of the License, or (at
9 !* your option) any later version.
10 !*
11 !* FMS is distributed in the hope that it will be useful, but WITHOUT
12 !* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 !* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 !* for more details.
15 !*
16 !* You should have received a copy of the GNU Lesser General Public
17 !* License along with FMS. If not, see <http://www.gnu.org/licenses/>.
18 !***********************************************************************
19 
20 ! data_override_r4 and data_override_r8 are not intended to be used directly -
21 ! they should be used through the data_override_mod API. The body of
22 ! data_override_r4 and data_override_r8 is contained in data_override.inc.
23 
24 module data_override_r4
25 #include "data_override_r4.fh"
26 end module data_override_r4
27 
28 module data_override_r8
29 #include "data_override_r8.fh"
30 end module data_override_r8
31 
32 !> @defgroup data_override_mod data_override_mod
33 !> @ingroup data_override
34 !! @brief Routines to get data in a file whose path is described in a user-provided data_table
35 !! and do spatial and temporal interpolation if necessary to convert data to model's grid and time.
36 !! @author Z. Liang, M.J. Harrison, M. Winton
37 !!
38 !! Before using @ref data_override a data_table must be created with the following entries:
39 !! gridname, fieldname_code, fieldname_file, file_name, ongrid, factor.
40 !!
41 !! More explainations about data_table entries can be found in the source code (defining data_type)
42 !!
43 !! If user wants to override fieldname_code with a const, set fieldname_file in data_table = ""
44 !! and factor = const
45 !!
46 !! If user wants to override fieldname_code with data from a file, set fieldname_file = name in
47 !! the netCDF data file, factor then will be for unit conversion (=1 if no conversion required)
48 !!
49 !! A field can be overriden globally (by default) or users can specify one or two regions in which
50 !! data_override will take place, field values outside the region will not be affected.
51 
52 module data_override_mod
53  use data_override_r4
54  use data_override_r8
55  use platform_mod, only: r4_kind, r8_kind
56  use mpp_mod, only: mpp_error, fatal
57  use mpp_domains_mod, only : domain2d, domainug
58  use time_manager_mod, only: time_type
59 
60 implicit none
61 private
62 
63 !> Interface for inserting and interpolating data into a file
64 !! for a model's grid and time. Data path must be described in
65 !! a user-provided data_table, see @ref data_override_mod "module description"
66 !! for more information.
67 !> @ingroup data_override_mod
68 interface data_override
69  module procedure data_override_0d_r4
70  module procedure data_override_0d_r8
71  module procedure data_override_2d_r4
72  module procedure data_override_2d_r8
73  module procedure data_override_3d_r4
74  module procedure data_override_3d_r8
75 end interface
76 
77 !> Version of @ref data_override for unstructured grids
78 !> @ingroup data_override_mod
80  module procedure data_override_ug_1d_r4
81  module procedure data_override_ug_1d_r8
82  module procedure data_override_ug_2d_r4
83  module procedure data_override_ug_2d_r8
84 end interface
85 
86 integer, parameter :: mode_r4 = 1 !> Indicates that a domain has been enabled for r4 mode
87 integer, parameter :: mode_r8 = 2 !> Indicates that a domain has been enabled for r8 mode
88 
89 integer :: atm_mode = 0 !> Atmosphere mode: possible values are 0 (uninitialized),
90  !! mode_r4, mode_r8, or ior(mode_r4, mode_r8)
91 integer :: ocn_mode = 0 !> Ocean mode: possible values are 0 (uninitialized),
92  !! mode_r4, mode_r8, or ior(mode_r4, mode_r8)
93 integer :: lnd_mode = 0 !> Land mode: possible values are 0 (uninitialized),
94  !! mode_r4, mode_r8, or ior(mode_r4, mode_r8)
95 integer :: ice_mode = 0 !> Ice mode: possible values are 0 (uninitialized),
96  !! mode_r4, mode_r8, or ior(mode_r4, mode_r8)
97 
98 !> @addtogroup data_override_mod
99 !> @{
100 
102 public :: data_override_ug
103 
104 contains
105 
106 !> @brief Initialize data_override. Users should call data_override_init before
107 !! calling data_override.
108 !!
109 !! This subroutine should be called in coupler_init after
110 !! (ocean/atmos/land/ice)_model_init have been called.
111 !!
112 !! data_override_init can be called more than once. In one call the user can pass
113 !! up to 4 domains of component models. At least one domain must be present in
114 !! any call. The real precision of initialized domains can be specified via the
115 !! optional mode argument. If no mode is specified, both r4 and r8 modes are initialized.
116 !!
117 !! Data_table is initialized with default values in DATA_OVERRIDE_INIT_IMPL_. Users should
118 !! provide "real" values that will override the default values. Real values can be
119 !! specified in either data_table or data_table.yaml. Each line of data_table contains one
120 !! data_entry. Items of data_entry are comma-separated.
121 subroutine data_override_init(Atm_domain_in, Ocean_domain_in, Ice_domain_in, Land_domain_in, Land_domainUG_in, mode)
122  type (domain2d), intent(in), optional :: atm_domain_in !< Atmosphere domain
123  type (domain2d), intent(in), optional :: ocean_domain_in !< Ocean domain
124  type (domain2d), intent(in), optional :: ice_domain_in !< Ice domain
125  type (domain2d), intent(in), optional :: land_domain_in !< Land domain
126  type(domainug) , intent(in), optional :: land_domainug_in !< Land domain, unstructured grid
127  integer, intent(in), optional :: mode !< Real precision of initialized domains. Possible values are r4_kind or
128  !! r8_kind. If omitted, both r4 and r8 modes are initialized.
129  integer :: mode_flags
130 
131  if (present(mode)) then
132  select case(mode)
133  case (r4_kind)
134  mode_flags = mode_r4
135  case (r8_kind)
136  mode_flags = mode_r8
137  case default
138  call mpp_error(fatal, "data_override_init: Unsupported mode argument")
139  end select
140  else
141  mode_flags = ior(mode_r4, mode_r8)
142  endif
143 
144  if (iand(mode_flags, mode_r4).ne.0) then
145  call data_override_init_r4(atm_domain_in, ocean_domain_in, ice_domain_in, land_domain_in, land_domainug_in)
146  endif
147 
148  if (iand(mode_flags, mode_r8).ne.0) then
149  call data_override_init_r8(atm_domain_in, ocean_domain_in, ice_domain_in, land_domain_in, land_domainug_in)
150  endif
151 
152  if (present(atm_domain_in)) atm_mode = ior(atm_mode, mode_flags)
153  if (present(ocean_domain_in)) ocn_mode = ior(ocn_mode, mode_flags)
154  if (present(ice_domain_in)) ice_mode = ior(ice_mode, mode_flags)
155  if (present(land_domain_in)) lnd_mode = ior(lnd_mode, mode_flags)
156 end subroutine data_override_init
157 
158 !> @brief Unset domains that had previously been set for use by data_override.
159 !!
160 !! This subroutine deallocates any data override domains that have been set.
161 subroutine data_override_unset_domains(unset_Atm, unset_Ocean, &
162  unset_Ice, unset_Land, must_be_set)
163  logical, intent(in), optional :: unset_atm, unset_ocean, unset_ice, unset_land !< Set to true to unset the
164  !! respective domain
165  logical, intent(in), optional :: must_be_set !< Set to false to suppress the error when attempting to unset
166  !! an uninitialized domain
167  logical :: fail_if_not_set
168 
169  fail_if_not_set = .true. ; if (present(must_be_set)) fail_if_not_set = must_be_set
170 
171  if (present(unset_atm)) then ; if (unset_atm) then
172  if (atm_mode.eq.0 .and. fail_if_not_set) call mpp_error(fatal, &
173  "data_override_unset_domains: attempted to unset an Atm_domain that has not been set.")
174 
175  if (iand(atm_mode, mode_r4).ne.0) call data_override_unset_atm_r4
176  if (iand(atm_mode, mode_r8).ne.0) call data_override_unset_atm_r8
177 
178  atm_mode = 0
179  endif ; endif
180  if (present(unset_ocean)) then ; if (unset_ocean) then
181  if (ocn_mode.eq.0 .and. fail_if_not_set) call mpp_error(fatal, &
182  "data_override_unset_domains: attempted to unset an Ocn_domain that has not been set.")
183 
184  if (iand(ocn_mode, mode_r4).ne.0) call data_override_unset_ocn_r4
185  if (iand(ocn_mode, mode_r8).ne.0) call data_override_unset_ocn_r8
186 
187  ocn_mode = 0
188  endif ; endif
189  if (present(unset_land)) then ; if (unset_land) then
190  if (lnd_mode.eq.0 .and. fail_if_not_set) call mpp_error(fatal, &
191  "data_override_unset_domains: attempted to unset an Land_domain that has not been set.")
192 
193  if (iand(lnd_mode, mode_r4).ne.0) call data_override_unset_lnd_r4
194  if (iand(lnd_mode, mode_r8).ne.0) call data_override_unset_lnd_r8
195 
196  lnd_mode = 0
197  endif ; endif
198  if (present(unset_ice)) then ; if (unset_ice) then
199  if (ice_mode.eq.0 .and. fail_if_not_set) call mpp_error(fatal, &
200  "data_override_unset_domains: attempted to unset an Ice_domain that has not been set.")
201 
202  if (iand(ice_mode, mode_r4).ne.0) call data_override_unset_ice_r4
203  if (iand(ice_mode, mode_r8).ne.0) call data_override_unset_ice_r8
204 
205  ice_mode = 0
206  endif ; endif
207 end subroutine data_override_unset_domains
208 
209 end module data_override_mod
210 !> @}
211 ! close documentation grouping
subroutine, public data_override_init(Atm_domain_in, Ocean_domain_in, Ice_domain_in, Land_domain_in, Land_domainUG_in, mode)
Initialize data_override. Users should call data_override_init before calling data_override.
subroutine, public data_override_unset_domains(unset_Atm, unset_Ocean, unset_Ice, unset_Land, must_be_set)
Unset domains that had previously been set for use by data_override.
Interface for inserting and interpolating data into a file for a model's grid and time....
Version of Data Override for unstructured grids.
The domain2D type contains all the necessary information to define the global, compute and data domai...
Domain information for managing data on unstructured grids.
Error handler.
Definition: mpp.F90:382
Type to represent amounts of time. Implemented as seconds and days to allow for larger intervals.