5.13.2-dev0
mHM
The mesoscale Hydrological Model
Loading...
Searching...
No Matches
mo_write_fluxes_states.f90
Go to the documentation of this file.
1!> \file mo_write_fluxes_states.f90
2!> \brief \copybrief mo_write_fluxes_states
3!> \details \copydetails mo_write_fluxes_states
4
5!> \brief Creates NetCDF output for different fluxes and state variables of mHM.
6!> \details NetCDF is first initialized and later on variables are put to the NetCDF.
7!> \changelog
8!! - David Schaefer Aug 2015
9!! - major rewrite
10!! - O. Rakovec, R. Kumar Nov 2017
11!! - added project description for the netcdf outputs
12!> \authors Matthias Zink
13!> \date Apr 2013
14!> \copyright Copyright 2005-\today, the mHM Developers, Luis Samaniego, Sabine Attinger: All rights reserved.
15!! mHM is released under the LGPLv3+ license \license_note
16!> \ingroup f_mhm
18
20 use mo_kind, only : i4, dp
21 use mo_string_utils, only : num2str
23 use mo_netcdf, only : ncdataset
24 use mo_global_variables, only : &
29 use mo_file, only : file_mhm_output
30
31 implicit none
32
33contains
34
35 !> \brief Initialize mHM OutputDataset
36 !> \details Create and initialize the output file. If new a new output
37 !! variable needs to be written, this is the first of two
38 !! procedures to change (second: updateDataset)
39 !!
40 !> \changelog
41 !! - Robert Schweppe Jun 2018
42 !! - refactoring and reformatting
43 !! - Pallav Shrestha Mar 2020
44 !! - iFlag_cordinate_sys based dimensions (dims)
45 !!
46 !> \return type(OutputDataset)
47 !> \authors Matthias Zink
48 !> \date Apr 2013
49 function mhm_outputdataset(iDomain, mask1) result(out)
50 implicit none
51
52 !> domain id
53 integer(i4), intent(in) :: idomain
54 !> L1 mask to reconstruct the data
55 logical, pointer, intent(in), dimension(:, :) :: mask1
56
57 type(outputdataset) :: out
58
59 integer(i4) :: ii, nn, ncells
60 character(3) :: dtype
61 character(16), dimension(3) :: dims
62 character(16) :: unit
63 type(outputvariable), dimension(size(outputFlxState) * nSoilHorizons_mHM) :: tmpvars
64
65 ncells = level1(idomain)%nCells
66
67 out = outputdataset( &
68 idomain=idomain, &
69 level=level1, &
70 file_name=file_mhm_output, &
71 double_precision=output_double_precision, &
72 outputs_frequence=timestep_model_outputs, &
73 time_reference=output_time_reference &
74 )
75
77 unit = fluxesunit(idomain)
78 dims = data_dims()
79
80 ii = 0
81
82 if (outputflxstate(1)) then
83 ii = ii + 1
84 tmpvars(ii) = outputvariable(&
85 out%nc, "interception", dtype, dims, ncells, mask1, output_deflate_level, .true.)
86 call set_attributes(&
87 tmpvars(ii)%nc, "canopy interception storage", "mm", output_double_precision)
88 end if
89
90 if (outputflxstate(2)) then
91 ii = ii + 1
92 tmpvars(ii) = outputvariable(&
93 out%nc, "snowpack", dtype, dims, ncells, mask1, output_deflate_level, .true.)
94 call set_attributes(tmpvars(ii)%nc, "depth of snowpack", "mm", output_double_precision)
95 end if
96
97 if (outputflxstate(3)) then
98 do nn = 1, nsoilhorizons_mhm
99 ii = ii + 1
100 tmpvars(ii) = outputvariable(out%nc, "SWC_L" // trim(num2str(nn, '(i2.2)')), &
101 dtype, dims, ncells, mask1, output_deflate_level, .true.)
102 call set_attributes(tmpvars(ii)%nc, &
103 'soil water content of soil layer' // trim(num2str(nn)), "mm", output_double_precision)
104 end do
105 end if
106
107 if (outputflxstate(4)) then
108 do nn = 1, nsoilhorizons_mhm
109 ii = ii + 1
110 tmpvars(ii) = outputvariable(out%nc, "SM_L" // trim(num2str(nn, '(i2.2)')), &
111 dtype, dims, ncells, mask1, output_deflate_level, .true.)
112 call set_attributes(tmpvars(ii)%nc, &
113 'volumetric soil moisture of soil layer' // trim(num2str(nn)), "mm mm-1", output_double_precision)
114 end do
115 end if
116
117 if (outputflxstate(5)) then
118 ii = ii + 1
119 tmpvars(ii) = outputvariable(&
120 out%nc, "SM_Lall", dtype, dims, ncells, mask1, output_deflate_level, .true.)
121 call set_attributes(&
122 tmpvars(ii)%nc, "average soil moisture over all layers", "mm mm-1", output_double_precision)
123 end if
124
125 if (outputflxstate(6)) then
126 ii = ii + 1
127 tmpvars(ii) = outputvariable(&
128 out%nc, "sealedSTW", dtype, dims, ncells, mask1, output_deflate_level, .true.)
129 call set_attributes(&
130 tmpvars(ii)%nc, "reservoir of sealed areas (sealedSTW)", "mm", output_double_precision)
131 end if
132
133 if (outputflxstate(7)) then
134 ii = ii + 1
135 tmpvars(ii) = outputvariable(&
136 out%nc, "unsatSTW", dtype, dims, ncells, mask1, output_deflate_level, .true.)
137 call set_attributes(&
138 tmpvars(ii)%nc, "reservoir of unsaturated zone", "mm", output_double_precision)
139 end if
140
141 if (outputflxstate(8)) then
142 ii = ii + 1
143 tmpvars(ii) = outputvariable(&
144 out%nc, "satSTW", dtype, dims, ncells, mask1, output_deflate_level, .true.)
145 call set_attributes(&
146 tmpvars(ii)%nc, "water level in groundwater reservoir", "mm", output_double_precision)
147 end if
148
149 if (outputflxstate(18)) then
150 ii = ii + 1
151 tmpvars(ii) = outputvariable(&
152 out%nc, "neutrons", dtype, dims, ncells, mask1, output_deflate_level, .true.)
153 call set_attributes(&
154 tmpvars(ii)%nc, "ground albedo neutrons", "cph", output_double_precision)
155 end if
156
157 if (outputflxstate(9)) then
158 ii = ii + 1
159 tmpvars(ii) = outputvariable(&
160 out%nc, "PET", dtype, dims, ncells, mask1, output_deflate_level)
161 call set_attributes(&
162 tmpvars(ii)%nc, "potential Evapotranspiration", trim(unit), output_double_precision)
163 end if
164
165 if (outputflxstate(10)) then
166 ii = ii + 1
167 tmpvars(ii) = outputvariable(&
168 out%nc, "aET", dtype, dims, ncells, mask1, output_deflate_level)
169 call set_attributes(&
170 tmpvars(ii)%nc, "actual Evapotranspiration", trim(unit), output_double_precision)
171 end if
172
173 if (outputflxstate(11)) then
174 ii = ii + 1
175 tmpvars(ii) = outputvariable(&
176 out%nc, "Q", dtype, dims, ncells, mask1, output_deflate_level)
177 call set_attributes(&
178 tmpvars(ii)%nc, "total runoff generated by every cell", trim(unit), output_double_precision)
179 end if
180
181 if (outputflxstate(12)) then
182 ii = ii + 1
183 tmpvars(ii) = outputvariable(&
184 out%nc, "QD", dtype, dims, ncells, mask1, output_deflate_level)
185 call set_attributes(tmpvars(ii)%nc, &
186 "direct runoff generated by every cell (runoffSeal)", trim(unit), output_double_precision)
187 end if
188
189 if (outputflxstate(13)) then
190 ii = ii + 1
191 tmpvars(ii) = outputvariable(&
192 out%nc, "QIf", dtype, dims, ncells, mask1, output_deflate_level)
193 call set_attributes(tmpvars(ii)%nc, &
194 "fast interflow generated by every cell (fastRunoff)", trim(unit), output_double_precision)
195 end if
196
197 if (outputflxstate(14)) then
198 ii = ii + 1
199 tmpvars(ii) = outputvariable(&
200 out%nc, "QIs", dtype, dims, ncells, mask1, output_deflate_level)
201 call set_attributes(tmpvars(ii)%nc, &
202 "slow interflow generated by every cell (slowRunoff)", trim(unit), output_double_precision)
203 end if
204
205 if (outputflxstate(15)) then
206 ii = ii + 1
207 tmpvars(ii) = outputvariable(&
208 out%nc, "QB", dtype, dims, ncells, mask1, output_deflate_level)
209 call set_attributes(&
210 tmpvars(ii)%nc, "baseflow generated by every cell", trim(unit), output_double_precision)
211 end if
212
213 if (outputflxstate(16)) then
214 ii = ii + 1
215 tmpvars(ii) = outputvariable(&
216 out%nc, "recharge", dtype, dims, ncells, mask1, output_deflate_level)
217 call set_attributes(&
218 tmpvars(ii)%nc, "groundwater recharge", trim(unit), output_double_precision)
219 end if
220
221 if (outputflxstate(17)) then
222 do nn = 1, nsoilhorizons_mhm
223 ii = ii + 1
224 tmpvars(ii) = outputvariable(&
225 out%nc, "soil_infil_L" // trim(num2str(nn, '(i2.2)')), &
226 dtype, dims, ncells, mask1, output_deflate_level)
227 call set_attributes(tmpvars(ii)%nc, &
228 "infiltration flux from soil layer" // trim(num2str(nn)), unit, output_double_precision)
229 end do
230 end if
231
232 if (outputflxstate(19)) then
233 do nn = 1, nsoilhorizons_mhm
234 ii = ii + 1
235 tmpvars(ii) = outputvariable(&
236 out%nc, "aET_L" // trim(num2str(nn, '(i2.2)')), &
237 dtype, dims, ncells, mask1, output_deflate_level)
238 call set_attributes(tmpvars(ii)%nc, &
239 'actual Evapotranspiration from soil layer' // trim(num2str(nn)), &
240 "mm " // trim(unit), output_double_precision)
241 end do
242 end if
243
244 if (outputflxstate(20)) then
245 ii = ii + 1
246 tmpvars(ii) = outputvariable(&
247 out%nc, "preEffect", dtype, dims, ncells, mask1, output_deflate_level)
248 call set_attributes(&
249 tmpvars(ii)%nc, "effective precipitation", trim(unit), output_double_precision)
250 end if
251
252 if (outputflxstate(21)) then
253 ii = ii + 1
254 tmpvars(ii) = outputvariable(&
255 out%nc, "Qsm", dtype, dims, ncells, mask1, output_deflate_level)
256 call set_attributes(&
257 tmpvars(ii)%nc, "Average liquid water generated from solid to liquid phase change in the snow", &
258 trim(unit), output_double_precision)
259 end if
260
261 allocate(out%vars(ii))
262 out%vars = tmpvars(1 : ii)
263
264 end function mhm_outputdataset
265
266 !> \brief Update all variables.
267 !> \details Call the type bound procedure updateVariable for
268 !> all output variables. If a new output
269 !> variable needs to be written, this is the second
270 !> of two procedures to change (first: newOutputDataset)
271 !!
272 !> \changelog
273 !! - L. Samaniego et al. Dec 2013
274 !! - nullify pointer Matthias Zink, Feb. 2014
275 !! - added aditional output: pet V. Prykhodk, J. Mai, Nov. 2014
276 !! - adding new variable infilSoil
277 !! - case 16 David Schaefer , Jun. 2015
278 !! - major rewrite
279 !! - Robert Schweppe Jun 2018
280 !! - refactoring and reformatting
281 !> \authors Matthias Zink
282 !> \date Apr 2013
283 subroutine mhm_updatedataset(nc_mhm, sidx, eidx, L1_fSealed, L1_fNotSealed, L1_inter, L1_snowPack, L1_soilMoist, &
284 L1_soilMoistSat, L1_sealSTW, L1_unsatSTW, L1_satSTW, L1_neutrons, L1_pet, L1_aETSoil, &
285 L1_aETCanopy, L1_aETSealed, L1_total_runoff, L1_runoffSeal, L1_fastRunoff, L1_slowRunoff, &
286 L1_baseflow, L1_percol, L1_infilSoil, L1_preEffect, L1_melt)
287
288 implicit none
289
290 class(outputdataset), intent(inout), target :: nc_mhm
291
292 integer(i4), intent(in) :: sidx !< start index
293 integer(i4), intent(in) :: eidx !< end index
294 real(dp), intent(in), dimension(:) :: L1_fSealed
295 real(dp), intent(in), dimension(:) :: L1_fNotSealed
296 real(dp), intent(in), dimension(:) :: L1_inter
297 real(dp), intent(in), dimension(:) :: L1_snowPack
298 real(dp), intent(in), dimension(:, :) :: L1_soilMoist
299 real(dp), intent(in), dimension(:, :) :: L1_soilMoistSat
300 real(dp), intent(in), dimension(:) :: L1_sealSTW
301 real(dp), intent(in), dimension(:) :: L1_unsatSTW
302 real(dp), intent(in), dimension(:) :: L1_satSTW
303 real(dp), intent(in), dimension(:) :: L1_neutrons
304 real(dp), intent(in), dimension(:) :: L1_pet
305 real(dp), intent(in), dimension(:, :) :: L1_aETSoil
306 real(dp), intent(in), dimension(:) :: L1_aETCanopy
307 real(dp), intent(in), dimension(:) :: L1_aETSealed
308 real(dp), intent(in), dimension(:) :: L1_total_runoff
309 real(dp), intent(in), dimension(:) :: L1_runoffSeal
310 real(dp), intent(in), dimension(:) :: L1_fastRunoff
311 real(dp), intent(in), dimension(:) :: L1_slowRunoff
312 real(dp), intent(in), dimension(:) :: L1_baseflow
313 real(dp), intent(in), dimension(:) :: L1_percol
314 real(dp), intent(in), dimension(:, :) :: L1_infilSoil
315 real(dp), intent(in), dimension(:) :: L1_preEffect
316 real(dp), intent(in), dimension(:) :: L1_melt
317
318 type(outputvariable), pointer, dimension(:) :: vars
319 integer(i4) :: ii, nn
320
321 ii = 0
322 vars => nc_mhm%vars
323
324 if (outputflxstate(1)) then
325 ii = ii + 1
326 call vars(ii)%updateVariable(l1_inter(sidx : eidx))
327 end if
328
329 if (outputflxstate(2)) then
330 ii = ii + 1
331 call vars(ii)%updateVariable(l1_snowpack(sidx : eidx))
332 end if
333
334 if (outputflxstate(3)) then
335 do nn = 1, nsoilhorizons_mhm
336 ii = ii + 1
337 call vars(ii)%updateVariable(l1_soilmoist(sidx : eidx, nn))
338 end do
339 end if
340
341 if (outputflxstate(4)) then
342 do nn = 1, nsoilhorizons_mhm
343 ii = ii + 1
344 call vars(ii)%updateVariable(l1_soilmoist(sidx : eidx, nn) / l1_soilmoistsat(sidx : eidx, nn))
345 end do
346 end if
347
348 if (outputflxstate(5)) then
349 ii = ii + 1
350 call vars(ii)%updateVariable(sum(l1_soilmoist(sidx : eidx, :), dim = 2) / sum(l1_soilmoistsat(sidx : eidx, :), dim = 2))
351 end if
352
353 if (outputflxstate(6)) then
354 ii = ii + 1
355 call vars(ii)%updateVariable(l1_sealstw(sidx : eidx))
356 end if
357
358 if (outputflxstate(7)) then
359 ii = ii + 1
360 call vars(ii)%updateVariable(l1_unsatstw(sidx : eidx))
361 end if
362
363 if (outputflxstate(8)) then
364 ii = ii + 1
365 call vars(ii)%updateVariable(l1_satstw(sidx : eidx))
366 end if
367
368 if (outputflxstate(18)) then
369 ii = ii + 1
370 call vars(ii)%updateVariable(l1_neutrons(sidx : eidx))
371 end if
372
373 if (outputflxstate(9)) then
374 ii = ii + 1
375 call vars(ii)%updateVariable(l1_pet(sidx : eidx))
376 end if
377
378 if (outputflxstate(10)) then
379 ii = ii + 1
380 call vars(ii)%updateVariable(sum(l1_aetsoil(sidx : eidx, :), dim = 2) * l1_fnotsealed(sidx : eidx) &
381 + l1_aetcanopy(sidx : eidx) + l1_aetsealed(sidx : eidx) * l1_fsealed(sidx : eidx))
382 end if
383
384 if (outputflxstate(11)) then
385 ii = ii + 1
386 call vars(ii)%updateVariable(l1_total_runoff(sidx : eidx))
387 end if
388
389 if (outputflxstate(12)) then
390 ii = ii + 1
391 call vars(ii)%updateVariable(l1_runoffseal(sidx : eidx) * l1_fsealed(sidx : eidx))
392 end if
393
394 if (outputflxstate(13)) then
395 ii = ii + 1
396 call vars(ii)%updateVariable(l1_fastrunoff(sidx : eidx) * l1_fnotsealed(sidx : eidx))
397 end if
398
399 if (outputflxstate(14)) then
400 ii = ii + 1
401 call vars(ii)%updateVariable(l1_slowrunoff(sidx : eidx) * l1_fnotsealed(sidx : eidx))
402 end if
403
404 if (outputflxstate(15)) then
405 ii = ii + 1
406 call vars(ii)%updateVariable(l1_baseflow(sidx : eidx) * l1_fnotsealed(sidx : eidx))
407 end if
408
409 if (outputflxstate(16)) then
410 ii = ii + 1
411 call vars(ii)%updateVariable(l1_percol(sidx : eidx) * l1_fnotsealed(sidx : eidx))
412 end if
413
414 if (outputflxstate(17)) then
415 do nn = 1, nsoilhorizons_mhm
416 ii = ii + 1
417 call vars(ii)%updateVariable(l1_infilsoil(sidx : eidx, nn) * l1_fnotsealed(sidx : eidx))
418 end do
419 end if
420
421 if (outputflxstate(19)) then
422 do nn = 1, nsoilhorizons_mhm
423 ii = ii + 1
424 call vars(ii)%updateVariable(l1_aetsoil(sidx : eidx, nn) * l1_fnotsealed(sidx : eidx))
425 end do
426 end if
427
428 if (outputflxstate(20)) then
429 ii = ii + 1
430 call vars(ii)%updateVariable(l1_preeffect(sidx : eidx))
431 end if
432
433 if (outputflxstate(21)) then
434 ii = ii + 1
435 call vars(ii)%updateVariable(l1_melt(sidx : eidx))
436 end if
437
438 end subroutine mhm_updatedataset
439
440 !> \brief Generate a unit string
441 !> \details Generate the unit string for the output variable netcdf attribute based on modeling timestep
442 !!
443 !> \changelog
444 !! - Robert Schweppe Jun 2018
445 !! - refactoring and reformatting
446 !!
447 !> \return character(16)
448 !> \authors David Schaefer
449 !> \date June 2015
450 function fluxesunit(iDomain)
451
453
454 implicit none
455
456 integer(i4), intent(in) :: idomain !< selected domain
457
458 character(16) :: fluxesunit
459 real(dp) :: ntsteps
460
461 if (timestep * timestep_model_outputs .eq. 1) then
462 fluxesunit = 'mm h-1'
463 else if (timestep_model_outputs > 1) then
464 fluxesunit = 'mm ' // trim(adjustl(num2str(timestep))) // 'h-1'
465 else if (timestep_model_outputs .eq. 0) then
466 ntsteps = (simper(idomain)%julEnd - simper(idomain)%julStart + 1) * ntstepday
467 fluxesunit = 'mm ' // trim(adjustl(num2str(nint(ntsteps)))) // 'h-1'
468 else if (timestep_model_outputs .eq. -1) then
469 fluxesunit = 'mm d-1'
470 else if (timestep_model_outputs .eq. -2) then
471 fluxesunit = 'mm month-1'
472 else if (timestep_model_outputs .eq. -3) then
473 fluxesunit = 'mm a-1'
474 else
475 fluxesunit = ''
476 end if
477
478 end function fluxesunit
479
480end module mo_write_fluxes_states
Provides constants commonly used by mHM, mRM and MPR.
real(dp), parameter, public nodata_dp
Provides structures needed by mHM, mRM and/or mpr.
type(period), dimension(:), allocatable, public simper
Provides structures needed by mHM, mRM and/or mpr.
integer(i4), public iflag_cordinate_sys
type(grid), dimension(:), allocatable, target, public level1
Provides file names and units for mHM.
Definition mo_file.F90:29
character(len=*), parameter file_mhm_output
file containing mhm output
Definition mo_file.F90:54
Main global variables for mHM.
logical, dimension(noutflxstate) outputflxstate
Define model outputs see "mhm_outputs.nml" dim1 = number of output variables to be written.
integer(i4) timestep_model_outputs
timestep for writing model outputs
integer(i4) output_deflate_level
deflate level in nc files
logical output_double_precision
output precision in nc files
integer(i4) output_time_reference
time reference point location in output nc files
Global variables for mpr only.
integer(i4), public nsoilhorizons_mhm
Creates NetCDF output for different fluxes and state variables of mHM.
character(16) function, dimension(3), public data_dims()
Output variable dimension names.
character(3) function, public data_dtype(double_precision)
Output variable dtype for single or double precision.
subroutine, public set_attributes(var, long_name, unit, double_precision, add_coords, standard_name, axis, bounds)
Write output variable attributes.
Creates NetCDF output for different fluxes and state variables of mHM.
type(outputdataset) function mhm_outputdataset(idomain, mask1)
Initialize mHM OutputDataset.
character(16) function fluxesunit(idomain)
Generate a unit string.
subroutine mhm_updatedataset(nc_mhm, sidx, eidx, l1_fsealed, l1_fnotsealed, l1_inter, l1_snowpack, l1_soilmoist, l1_soilmoistsat, l1_sealstw, l1_unsatstw, l1_satstw, l1_neutrons, l1_pet, l1_aetsoil, l1_aetcanopy, l1_aetsealed, l1_total_runoff, l1_runoffseal, l1_fastrunoff, l1_slowrunoff, l1_baseflow, l1_percol, l1_infilsoil, l1_preeffect, l1_melt)
Update all variables.