FMS  2025.04
Flexible Modeling System
horiz_interp_bilinear.F90
1 !***********************************************************************
2 !* Apache License 2.0
3 !*
4 !* This file is part of the GFDL Flexible Modeling System (FMS).
5 !*
6 !* Licensed under the Apache License, Version 2.0 (the "License");
7 !* you may not use this file except in compliance with the License.
8 !* You may obtain a copy of the License at
9 !*
10 !* http://www.apache.org/licenses/LICENSE-2.0
11 !*
12 !* FMS is distributed in the hope that it will be useful, but WITHOUT
13 !* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied;
14 !* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
15 !* PARTICULAR PURPOSE. See the License for the specific language
16 !* governing permissions and limitations under the License.
17 !***********************************************************************
18 !> @defgroup horiz_interp_bilinear_mod horiz_interp_bilinear_mod
19 !> @ingroup horiz_interp
20 !> @brief Performs spatial interpolation between grids using bilinear interpolation
21 !!
22 !> @author Zhi Liang <Zhi.Liang@noaa.gov>
23 !> This module can interpolate data from regular rectangular grid
24 !! to rectangular/tripolar grid. The interpolation scheme is bilinear interpolation.
25 !! There is an optional mask field for missing input data.
26 !! An optional output mask field may be used in conjunction with
27 !! the input mask to show where output data exists.
28 
29 module horiz_interp_bilinear_mod
30 
31  use mpp_mod, only: mpp_error, fatal, stdout, mpp_pe, mpp_root_pe
32  use fms_mod, only: write_version_number
33  use constants_mod, only: pi
34  use horiz_interp_type_mod, only: horiz_interp_type, stats, bilinear
35  use platform_mod, only: r4_kind, r8_kind
36  use axis_utils2_mod, only: nearest_index
37  use fms2_io_mod, only: open_file, close_file, read_data, fmsnetcdffile_t, get_dimension_size
38  use fms_string_utils_mod, only: string
39 
40  implicit none
41  private
42 
43 
46 
47  !> Creates a @ref horiz_interp_type for bilinear interpolation.
48  !> @ingroup horiz_interp_bilinear_mod
50  module procedure horiz_interp_bilinear_new_1d_r4
51  module procedure horiz_interp_bilinear_new_1d_r8
52  module procedure horiz_interp_bilinear_new_2d_r4
53  module procedure horiz_interp_bilinear_new_2d_r8
54  end interface
55 
56  !> Subroutines for reading in weight files and using that to fill in the horiz_interp type instead
57  !! calculating it
58  !> @ingroup horiz_interp_bilinear_mod
60  module procedure horiz_interp_read_weights_bilinear_r4
61  module procedure horiz_interp_read_weights_bilinear_r8
62  end interface
63 
65  module procedure horiz_interp_bilinear_r4
66  module procedure horiz_interp_bilinear_r8
67  end interface
68 
69 !> @addtogroup horiz_interp_bilinear_mod
70 !> @{
71 
72  real(r8_kind), parameter :: epsln=1.e-10_r8_kind
73  real(r4_kind), parameter :: epsln_r4=1.e-4_r4_kind
74  integer, parameter :: DUMMY = -999
75 
76 !! Private helper routines, interfaces for mixed real precision support
77  interface intersect
78  module procedure intersect_r4
79  module procedure intersect_r8
80  end interface
81 
82  !-----------------------------------------------------------------------
83 ! Include variable "version" to be written to log file.
84 #include<file_version.h>
85  logical :: module_is_initialized = .false.
86 
87 contains
88 
89  !> Initialize this module and writes version number to logfile.
91 
92  if(module_is_initialized) return
93  call write_version_number("HORIZ_INTERP_BILINEAR_MOD", version)
94  module_is_initialized = .true.
95 
96  end subroutine horiz_interp_bilinear_init
97 
98  !> @brief Deallocates memory used by "horiz_interp_type" variables.
99  !!
100  !> Must be called before reinitializing with horiz_interp_bilinear_new.
101  subroutine horiz_interp_bilinear_del( Interp )
102 
103  type (horiz_interp_type), intent(inout) :: interp!< A derived-type variable returned by previous
104  !! call to horiz_interp_bilinear_new. The input variable must
105  !! have allocated arrays. The returned variable will contain
106  !! deallocated arrays
107 
108  if( interp%horizInterpReals4_type%is_allocated) then
109  if(allocated(interp%horizInterpReals4_type%wti)) deallocate(interp%horizInterpReals4_type%wti)
110  if(allocated(interp%horizInterpReals4_type%wtj)) deallocate(interp%horizInterpReals4_type%wtj)
111  else if (interp%horizInterpReals8_type%is_allocated) then
112  if(allocated(interp%horizInterpReals8_type%wti)) deallocate(interp%horizInterpReals8_type%wti)
113  if(allocated(interp%horizInterpReals8_type%wtj)) deallocate(interp%horizInterpReals8_type%wtj)
114  endif
115  if(allocated(interp%i_lon)) deallocate(interp%i_lon)
116  if(allocated(interp%j_lat)) deallocate(interp%j_lat)
117 
118  interp%horizInterpReals4_type%is_allocated = .false.
119  interp%horizInterpReals8_type%is_allocated = .false.
120 
121  end subroutine horiz_interp_bilinear_del
122 
123 #include "horiz_interp_bilinear_r4.fh"
124 #include "horiz_interp_bilinear_r8.fh"
125 
126 end module horiz_interp_bilinear_mod
127 !> @}
128 ! close documentation grouping
Close a netcdf or domain file opened with open_file or open_virtual_file.
Definition: fms2_io.F90:165
Opens a given netcdf or domain file.
Definition: fms2_io.F90:121
Read data from a defined field in a file.
Definition: fms2_io.F90:291
subroutine, public write_version_number(version, tag, unit)
Prints to the log file (or a specified unit) the version id string and tag name.
Definition: fms.F90:701
character(:) function, allocatable, public string(v, fmt)
Converts a number or a Boolean value to a string.
subroutine, public horiz_interp_bilinear_del(Interp)
Deallocates memory used by "horiz_interp_type" variables.
subroutine, public horiz_interp_bilinear_init
Initialize this module and writes version number to logfile.
Creates a horiz_interp_type for bilinear interpolation.
Subroutines for reading in weight files and using that to fill in the horiz_interp type instead calcu...
Holds data pointers and metadata for horizontal interpolations, passed between the horiz_interp modul...
integer function stdout()
This function returns the current standard fortran unit numbers for output.
Definition: mpp_util.inc:42
integer function mpp_pe()
Returns processor ID.
Definition: mpp_util.inc:406
Error handler.
Definition: mpp.F90:381