FMS  2026.01.01-dev
Flexible Modeling System
horiz_interp_bicubic.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_bicubic_mod horiz_interp_bicubic_mod
19 !! @ingroup horiz_interp
20 !! @{
21 !!
22 !! @author martin.schmidt@io-warnemuende.de (2004)
23 !!
24 !! @parblock
25 !! Horiz_interp_bicubic_mod contains methods called from horiz_interp_mod for bicubic interpolation
26 !! from a coarse regular grid on a fine regular grids. Users are recommended to use the top-level
27 !! module horiz_interp_mod with "interp" set to "bicubic" for bicubic interpolation.
28 !! Internally used subroutines bcuint and bcuof, used internally, have been implemented following
29 !!
30 !! W. H. Press, S. A. Teukolski, W. T. Vetterling and B. P. Flannery,
31 !! Numerical Recipies in FORTRAN, The Art of Scientific Computing.
32 !! Cambridge University Press, 1992
33 !!
34 !! All public interfaces and subroutines in this module can be accessed from public interfaces
35 !! and subroutines in horiz_interp_mod. For simplicity, users are recommended to call
36 !! all procedures throug horiz_interp_mod.
37 !! Note from the author: The module is thought to interact with MOM-4.
38 !! Alle benotigten Felder werden extern von MOM verwaltet, da sie
39 !! nicht fur alle interpolierten Daten die gleiche Dimension haben mussen.
40 !! @endparblock
41 module horiz_interp_bicubic_mod
42 
43  use mpp_mod, only: mpp_error, fatal, stdout, mpp_pe, mpp_root_pe
44  use fms_mod, only: write_version_number
45  use horiz_interp_type_mod, only: horiz_interp_type, bicubic
46  use constants_mod, only: pi
47  use platform_mod, only: r4_kind, r8_kind
48 
49 
50  implicit none
51 
52  private
53 
56 
57  !> Generic interface to horiz_interp_bicubic_new_* to compute interpolation weights and,
58  !! mapping indices. The member subroutines and their main arguments are:
59  !! horiz_interp_bicubic_new_1d_r8:
60  !! input and output grids provided as 1D arrays in 64-bit precision.
61  !! horiz_interp_bicubic_new_1d_r4:
62  !! input and output grids provided as 1D arrays in 32-bit precision.
63  !! horiz_interp_bicubic_new_1d_s_r8:
64  !! input grid provided as 1D arrays, output grids as 2D arrays, both in 64-bit precision.
65  !! horiz_interp_bicubic_new_1d_s_r4:
66  !! input grid provided as 1D arrays, output grids as 2D arrays, both in 32-bit precision.
68  module procedure horiz_interp_bicubic_new_1d_r8
69  module procedure horiz_interp_bicubic_new_1d_s_r8
70  module procedure horiz_interp_bicubic_new_1d_r4
71  module procedure horiz_interp_bicubic_new_1d_s_r4
72  end interface horiz_interp_bicubic_new
73 
74  !> Generic interface to horiz_interp_bicubic_* to interpolate data using the weights
75  !! stored in Interp. Calls horiz_interp_bicubic_r4 if input and output data are 2D arrays
76  !! in 32-bit precision. Calls horiz_interp_bicubic_r8 if input and output data are 2D
77  !! arrays in 64-bit precision.
79  module procedure horiz_interp_bicubic_r4
80  module procedure horiz_interp_bicubic_r8
81  end interface horiz_interp_bicubic
82 
83 !> Include variable "version" to be written to log file.
84 #include<file_version.h>
85  logical :: module_is_initialized = .false. !< is a flag to prevent module re-initialization.
86  integer :: verbose_bicubic = 0 !< sets the default verbosity level.
87 
88  real(r8_kind) :: tpi !< is 2*PI.
89 
90  !> Unused interface.
91  interface fill_xy
92  module procedure fill_xy_r4
93  module procedure fill_xy_r8
94  end interface
95 
96  !> Generic interface used internally when interpolating data from source grid to
97  !! target grid. Calls bcuint_r4 when input and output data are in 32-bit precision.
98  !! Calls bcuint_r8 when input and output data are in 64-bit precision.
99  interface bcuint
100  module procedure bcuint_r4
101  module procedure bcuint_r8
102  end interface
103 
104  !> Generic interface used internally in bcuint to compute bicubic coefficients when
105  !! interpolating data from source grid to target grid. Calls bcuof_r4 when all arguments
106  !! are in 32-bit precision. Calls bcuof_r8 when all arguemnts are in 64-bit precision.
107  interface bcucof
108  module procedure bcucof_r4
109  module procedure bcucof_r8
110  end interface
111 
112  !> Generic interface used internally in finding neighbors
113  interface indl
114  module procedure indl_r4
115  module procedure indl_r8
116  end interface
117 
118  !> Generic interface used internally in finding neighbors
119  interface indu
120  module procedure indu_r4
121  module procedure indu_r8
122  end interface
123 
124  contains
125 
126  !> @parblock
127  !! Initializes horiz_interp_bicubic_mod including setting tpi.
128  !! Called from horiz_interp_init in horiz_interp_mod.
129  !! @endparblock
131 
132  if(module_is_initialized) return
133  call write_version_number("HORIZ_INTERP_BICUBIC_MOD", version)
134  module_is_initialized = .true.
135  tpi = real(2.0_r8_kind*pi, r8_kind)
136 
137  end subroutine horiz_interp_bicubic_init
138 
139  !> @parblock
140  !! Deallocates arrays holding bicubic interpolation weights and mapping indices in Interp.
141  !! Resets %is_allocated to .false. Called from horiz_interp_del in horiz_interp_mod.
142  !! @endparblock
143  subroutine horiz_interp_bicubic_del( Interp )
144  type(horiz_interp_type), intent(inout) :: interp !< will be reset with deallocated memory.
145 
146  if(interp%horizInterpReals8_type%is_allocated) then
147  if(allocated(interp%horizInterpReals8_type%rat_x)) deallocate ( interp%horizInterpReals8_type%rat_x )
148  if(allocated(interp%horizInterpReals8_type%rat_y)) deallocate ( interp%horizInterpReals8_type%rat_y )
149  if(allocated(interp%horizInterpReals8_type%lon_in)) deallocate ( interp%horizInterpReals8_type%lon_in )
150  if(allocated(interp%horizInterpReals8_type%lat_in)) deallocate ( interp%horizInterpReals8_type%lat_in )
151  if(allocated(interp%horizInterpReals8_type%wti)) deallocate ( interp%horizInterpReals8_type%wti )
152  else if(interp%horizInterpReals4_type%is_allocated) then
153  if(allocated(interp%horizInterpReals4_type%rat_x)) deallocate ( interp%horizInterpReals4_type%rat_x )
154  if(allocated(interp%horizInterpReals4_type%rat_y)) deallocate ( interp%horizInterpReals4_type%rat_y )
155  if(allocated(interp%horizInterpReals4_type%lon_in)) deallocate ( interp%horizInterpReals4_type%lon_in )
156  if(allocated(interp%horizInterpReals4_type%lat_in)) deallocate ( interp%horizInterpReals4_type%lat_in )
157  if(allocated(interp%horizInterpReals4_type%wti)) deallocate ( interp%horizInterpReals4_type%wti )
158  endif
159  if( allocated(interp%i_lon) ) deallocate( interp%i_lon )
160  if( allocated(interp%j_lat) ) deallocate( interp%j_lat )
161 
162  interp%horizInterpReals8_type%is_allocated = .false.
163  interp%horizInterpReals4_type%is_allocated = .false.
164 
165  end subroutine horiz_interp_bicubic_del
166 
167 #include "horiz_interp_bicubic_r4.fh"
168 #include "horiz_interp_bicubic_r8.fh"
169 
170 end module horiz_interp_bicubic_mod
171 !> @}
172 ! close documentation
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
subroutine, public horiz_interp_bicubic_del(Interp)
integer verbose_bicubic
sets the default verbosity level.
logical module_is_initialized
Include variable "version" to be written to log file.
subroutine, public horiz_interp_bicubic_init
Generic interface used internally in bcuint to compute bicubic coefficients when interpolating data f...
Generic interface used internally when interpolating data from source grid to target grid....
Generic interface to horiz_interp_bicubic_* to interpolate data using the weights stored in Interp....
Generic interface used internally in finding neighbors.
Generic interface used internally in finding neighbors.
integer, parameter, public bicubic
is an internally used parameter to mark bicubic interpolation
Datatype holding interpolation weights, mapping indices, and metadata for horizontal interpolation....
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:385
Generic interface to horiz_interp_bicubic_new_* to compute interpolation weights and,...