-
Notifications
You must be signed in to change notification settings - Fork 371
/
cesm_put_data.c
269 lines (233 loc) · 10.1 KB
/
cesm_put_data.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/******************************************************************************
* @section DESCRIPTION
*
* Collect coupled fields and export to coupler
*
* Sign convention: positive value <=> downward flux
* Units: see notes in code or seq_flds_mod.F90
*
* @section LICENSE
*
* The Variable Infiltration Capacity (VIC) macroscale hydrological model
* Copyright (C) 2016 The Computational Hydrology Group, Department of Civil
* and Environmental Engineering, University of Washington.
*
* The VIC model is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*****************************************************************************/
#include <vic_driver_cesm.h>
void
vic_cesm_put_data()
{
extern all_vars_struct *all_vars;
extern force_data_struct *force;
extern dmy_struct dmy_current;
extern domain_struct local_domain;
extern soil_con_struct *soil_con;
extern veg_con_struct **veg_con;
extern veg_lib_struct **veg_lib;
extern l2x_data_struct *l2x_vic;
extern x2l_data_struct *x2l_vic;
extern global_param_struct global_param;
extern option_struct options;
extern parameters_struct param;
extern double ***out_data;
bool IsWet = false; // TODO: add lake fraction
bool overstory;
bool HasVeg;
size_t i;
size_t veg;
size_t band;
size_t index;
double AreaFactor;
double AreaFactorSum;
double TreeAdjustFactor = 1.;
double lakefactor = 1.;
double rad_temp;
double albedo;
double aero_resist;
double roughness;
double wind_stress;
double wind_stress_x;
double wind_stress_y;
cell_data_struct cell;
energy_bal_struct energy;
snow_data_struct snow;
veg_var_struct veg_var;
for (i = 0; i < local_domain.ncells_active; i++) {
// Zero l2x vars
l2x_vic[i].l2x_Sl_t = 0;
l2x_vic[i].l2x_Sl_tref = 0;
l2x_vic[i].l2x_Sl_qref = 0;
l2x_vic[i].l2x_Sl_avsdr = 0;
l2x_vic[i].l2x_Sl_anidr = 0;
l2x_vic[i].l2x_Sl_avsdf = 0;
l2x_vic[i].l2x_Sl_anidf = 0;
l2x_vic[i].l2x_Sl_snowh = 0;
l2x_vic[i].l2x_Sl_u10 = 0;
l2x_vic[i].l2x_Sl_ddvel = 0;
l2x_vic[i].l2x_Sl_fv = 0;
l2x_vic[i].l2x_Sl_ram1 = 0;
l2x_vic[i].l2x_Sl_logz0 = 0;
l2x_vic[i].l2x_Fall_taux = 0;
l2x_vic[i].l2x_Fall_tauy = 0;
l2x_vic[i].l2x_Fall_lat = 0;
l2x_vic[i].l2x_Fall_sen = 0;
l2x_vic[i].l2x_Fall_lwup = 0;
l2x_vic[i].l2x_Fall_evap = 0;
l2x_vic[i].l2x_Fall_swnet = 0;
l2x_vic[i].l2x_Fall_fco2_lnd = 0;
l2x_vic[i].l2x_Fall_flxdst1 = 0;
l2x_vic[i].l2x_Fall_flxdst2 = 0;
l2x_vic[i].l2x_Fall_flxdst3 = 0;
l2x_vic[i].l2x_Fall_flxdst4 = 0;
l2x_vic[i].l2x_Fall_flxvoc = 0;
l2x_vic[i].l2x_Flrl_rofliq = 0;
l2x_vic[i].l2x_Flrl_rofice = 0;
// populate reference values
// 10m wind, VIC: m/s, CESM: m/s
l2x_vic[i].l2x_Sl_u10 = out_data[i][OUT_WIND][0];
// 2m reference temperature, VIC: C, CESM: K
l2x_vic[i].l2x_Sl_tref = out_data[i][OUT_AIR_TEMP][0] + CONST_TKFRZ;
// 2m reference specific humidity, VIC: kg/kg, CESM: g/g
l2x_vic[i].l2x_Sl_qref = CONST_EPS *
out_data[i][OUT_VP][0] /
out_data[i][OUT_PRESSURE][0];
// band-specific quantities
// note that these include a veg correction (AreaFactor)
// that is already in the put_data routine
// temperature, VIC: K, CESM: K
l2x_vic[i].l2x_Sl_t = out_data[i][OUT_RAD_TEMP][0];
// albedo, VIC: fraction, CESM: fraction
// Note: VIC does not partition its albedo, thus all types are
// the same value
// TBD: this will be fixed in a subsequent PR
albedo = out_data[i][OUT_ALBEDO][0];
// albedo: direct, visible
l2x_vic[i].l2x_Sl_avsdr = albedo;
// albedo: direct , near-ir
l2x_vic[i].l2x_Sl_anidr = albedo;
// albedo: diffuse, visible
l2x_vic[i].l2x_Sl_avsdf = albedo;
// albedo: diffuse, near-ir
l2x_vic[i].l2x_Sl_anidf = albedo;
// snow height, VIC: mm, CESM: m
// convert to VIC units
l2x_vic[i].l2x_Sl_snowh = out_data[i][OUT_SWE][0] / MM_PER_M;
// net shortwave, VIC: W/m2, CESM: W/m2
l2x_vic[i].l2x_Fall_swnet = out_data[i][OUT_SWNET][0];
// longwave up, VIC: W/m2, CESM: W/m2
// adjust sign for CESM sign convention
l2x_vic[i].l2x_Fall_lwup = -1 *
(out_data[i][OUT_LWDOWN][0] -
out_data[i][OUT_LWNET][0]);
// turbulent heat fluxes
// latent heat, VIC: W/m2, CESM: W/m2
l2x_vic[i].l2x_Fall_lat = out_data[i][OUT_LATENT][0];
// sensible heat, VIC: W/m2, CESM: W/m2
l2x_vic[i].l2x_Fall_sen += -1 * out_data[i][OUT_SENSIBLE][0];
// evaporation, VIC: mm, CESM: kg m-2 s-1
// TO-DO should we incorporate bare soil evap?
l2x_vic[i].l2x_Fall_evap += -1 *
(out_data[i][OUT_EVAP][0] * MM_PER_M /
global_param.dt);
// lnd->rtm input fluxes
l2x_vic[i].l2x_Flrl_rofliq = out_data[i][OUT_RUNOFF][0] +
out_data[i][OUT_BASEFLOW][0] /
global_param.dt;
// running sum to make sure we get the full grid cell
AreaFactorSum = 0;
for (veg = 0; veg <= local_domain.locations[i].nveg; veg++) {
overstory = veg_lib[i][veg_con[i][veg].veg_class].overstory;
if (veg <= local_domain.locations[i].nveg - 1) {
HasVeg = true;
}
else {
HasVeg = false;
}
for (band = 0; band < options.SNOW_BAND; band++) {
cell = all_vars[i].cell[veg][band];
energy = all_vars[i].energy[veg][band];
snow = all_vars[i].snow[veg][band];
veg_var = all_vars[i].veg_var[veg][band];
// TODO: Consider treeline and lake factors
AreaFactor = (veg_con[i][veg].Cv *
soil_con[i].AreaFract[band] *
TreeAdjustFactor * lakefactor);
if (AreaFactor < DBL_EPSILON) {
// Skip this patch since the area factor is zero
continue;
}
AreaFactorSum += AreaFactor;
// aerodynamical resistance, VIC: s/m, CESM: s/m
// TO-DO: update in future PR
if (overstory) {
aero_resist = cell.aero_resist[1];
}
else {
aero_resist = cell.aero_resist[0];
}
if (aero_resist < DBL_EPSILON) {
log_warn("aero_resist (%f) is < %f", aero_resist,
DBL_EPSILON);
aero_resist = param.HUGE_RESIST;
}
l2x_vic[i].l2x_Sl_ram1 += AreaFactor * aero_resist;
// log z0
// CESM units: m
if (snow.snow) {
// snow roughness
roughness = soil_con[i].snow_rough;
}
else if (HasVeg) {
// bare soil roughness
roughness =
veg_lib[i][veg_con[i][veg].veg_class].roughness[
dmy_current.month - 1];
}
else {
roughness = soil_con[i].rough;
}
if (roughness < DBL_EPSILON) {
log_warn("roughness (%f) is < %f", roughness, DBL_EPSILON);
roughness = DBL_EPSILON;
}
l2x_vic[i].l2x_Sl_logz0 += AreaFactor * log(roughness);
// wind stress, zonal
// CESM units: N m-2
wind_stress_x = -1 * out_data[i][OUT_DENSITY][0] *
x2l_vic[i].x2l_Sa_u / aero_resist;
l2x_vic[i].l2x_Fall_taux += AreaFactor * wind_stress_x;
// wind stress, meridional
// CESM units: N m-2
wind_stress_y = -1 * out_data[i][OUT_DENSITY][0] *
x2l_vic[i].x2l_Sa_v / aero_resist;
l2x_vic[i].l2x_Fall_tauy += AreaFactor * wind_stress_y;
// friction velocity
// CESM units: m s-1
wind_stress =
sqrt(pow(wind_stress_x, 2) + pow(wind_stress_y, 2));
l2x_vic[i].l2x_Sl_fv += AreaFactor *
(wind_stress /
out_data[i][OUT_DENSITY][0]);
}
}
// set variables-set flag
l2x_vic[i].l2x_vars_set = true;
if (!assert_close_double(AreaFactorSum, 1., 0., 1e-3)) {
log_warn("AreaFactorSum (%f) is not 1",
AreaFactorSum);
}
}
}