ERF
Energy Research and Forecasting: An Atmospheric Modeling Code
ERF_MetgridLandUtils.H
Go to the documentation of this file.
1 #ifndef ERF_METGRID_LAND_UTILS_H_
2 #define ERF_METGRID_LAND_UTILS_H_
3 
4 /**
5  * \file ERF_MetgridLandUtils.H
6  *
7  * Interpolation in depth and in time used to build SLM's initial state from a met_em file
8  * (ERF::init_lsm_from_metgrid).
9  */
10 #include <AMReX_REAL.H>
11 #include <AMReX_Vector.H>
12 
13 #include <string>
14 
15 namespace metgrid_land {
16 
17 // The Noah soil layers real.exe writes to wrfinput (DZS) for a 4-layer LSM, which SLM's
18 // initial state from met_em files is interpolated to, and so the layers SLM takes then
19 constexpr int nsoil_noah = 4;
21  amrex::Real(0.6), amrex::Real(1.0)};
22 
23 /**
24  * Linear interpolation in depth from the source layer midpoints to z, held constant
25  * beyond the shallowest and deepest midpoints.
26  *
27  * @param z_src depths of the source layer midpoints, increasing
28  * @param v_src values at those depths
29  * @param z depth to interpolate to
30  */
31 inline amrex::Real
32 interp_in_depth (const amrex::Vector<amrex::Real>& z_src,
33  const amrex::Vector<amrex::Real>& v_src,
34  amrex::Real z)
35 {
36  const int n = z_src.size();
37  if (z <= z_src[0]) { return v_src[0]; }
38  if (z >= z_src[n-1]) { return v_src[n-1]; }
39  int m = 0;
40  while (z > z_src[m+1]) { ++m; }
41  const amrex::Real w = (z - z_src[m]) / (z_src[m+1] - z_src[m]);
42  return (amrex::Real(1.0) - w) * v_src[m] + w * v_src[m+1];
43 }
44 
45 /**
46  * The two months whose mid-month values bracket the given date, and the weight of the
47  * second, as in real.exe's monthly_interp_to_date: each monthly value is taken to apply on
48  * the 15th of its month, and values in between are interpolated linearly in time.
49  *
50  * @param date "YYYY-MM-DD_hh:mm:ss"
51  * @param m0 the earlier month (0 = January)
52  * @param m1 the later month
53  * @param w1 the weight of the later month
54  */
55 inline void
56 monthly_interp_weights (const std::string& date, int& m0, int& m1, amrex::Real& w1)
57 {
58  const int year = std::stoi(date.substr(0,4));
59  const int month = std::stoi(date.substr(5,2));
60  const int day = std::stoi(date.substr(8,2));
61  const int hour = std::stoi(date.substr(11,2));
62 
63  const bool leap = (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
64  const int days_in_month[12] = {31, leap ? 29 : 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
65 
66  // Day of the year (starting from 0) of the date and of the middle of each month
67  amrex::Real mid[12];
68  int start = 0;
69  for (int m = 0; m < 12; ++m) {
70  mid[m] = amrex::Real(start + 14);
71  start += days_in_month[m];
72  }
73  const int days_in_year = start;
74  int doy = day - 1;
75  for (int m = 0; m < month-1; ++m) { doy += days_in_month[m]; }
76  const amrex::Real t = amrex::Real(doy) + amrex::Real(hour) / amrex::Real(24.0);
77 
78  if (t < mid[0] || t >= mid[11]) {
79  // Between mid-December and mid-January, wrapping around the end of the year
80  m0 = 11;
81  m1 = 0;
82  const amrex::Real t0 = mid[11];
83  const amrex::Real t1 = mid[0] + amrex::Real(days_in_year);
84  const amrex::Real tt = (t < mid[0]) ? t + amrex::Real(days_in_year) : t;
85  w1 = (tt - t0) / (t1 - t0);
86  } else {
87  m0 = 0;
88  while (t >= mid[m0+1]) { ++m0; }
89  m1 = m0 + 1;
90  w1 = (t - mid[m0]) / (mid[m1] - mid[m0]);
91  }
92 }
93 
94 } // namespace metgrid_land
95 
96 #endif
Real w
Definition: ERF_Plotfile2DInterpolator.cpp:23
amrex::Real Real
Definition: ERF_ShocInterface.H:19
@ t
Definition: ERF_WSM6.H:293
Definition: ERF_MetgridLandUtils.H:15
constexpr int nsoil_noah
Definition: ERF_MetgridLandUtils.H:19
void monthly_interp_weights(const std::string &date, int &m0, int &m1, amrex::Real &w1)
Definition: ERF_MetgridLandUtils.H:56
amrex::Real interp_in_depth(const amrex::Vector< amrex::Real > &z_src, const amrex::Vector< amrex::Real > &v_src, amrex::Real z)
Definition: ERF_MetgridLandUtils.H:32
constexpr amrex::Real dzs_noah[nsoil_noah]
Definition: ERF_MetgridLandUtils.H:20
real(c_double), parameter t0
Definition: ERF_module_model_constants.F90:39