5.13.3
mHM
The mesoscale Hydrological Model
Loading...
Searching...
No Matches
mo_common_read_data.f90
Go to the documentation of this file.
1!> \file mo_common_read_data.f90
2!> \brief \copybrief mo_common_read_data
3!> \details \copydetails mo_common_read_data
4
5!> \brief Common reading routines
6!> \details Routines to read the DEM and landcover files.
7!> \authors Robert Schweppe
8!> \date Jun 2018
9!> \copyright Copyright 2005-\today, the mHM Developers, Luis Samaniego, Sabine Attinger: All rights reserved.
10!! mHM is released under the LGPLv3+ license \license_note
11!> \ingroup f_common
13 USE mo_kind, ONLY : i4, dp
14 use mo_message, only: message, error_message
16
17 IMPLICIT NONE
18
19 PRIVATE
20
21 PUBLIC :: read_dem, read_lcover
22
23 ! ------------------------------------------------------------------
24
25CONTAINS
26
27 ! NAME
28 ! read_dem
29
30 ! PURPOSE
31 !> \brief TODO: add description
32
33 !> \details TODO: add description
34
35 ! HISTORY
36 !> \authors Robert Schweppe
37
38 !> \date Jun 2018
39
40 ! Modifications:
41
42 subroutine read_dem
43
44 use mo_append, only : append
46 use mo_common_file, only : file_dem, udem
47 use mo_common_types, only: grid
52 use mo_string_utils, only : num2str
53
54 implicit none
55
56 ! loop variables
57 integer(i4) :: domainid, idomain, ndemcells
58
59 ! file name of file to read
60 character(256) :: fname
61
62 real(dp), dimension(:, :), allocatable :: data_dp_2d
63
64 type(grid), pointer :: level0_idomain
65
66
67 ! ************************************************
68 ! READ SPATIAL DATA FOR EACH DOMAIN
69 ! ************************************************
70 ! allocate necessary variables at Level0
71 allocate(level0(domainmeta%nDomains))
72
73 do idomain = 1, domainmeta%nDomains
74 domainid = domainmeta%indices(idomain)
75
76 level0_idomain => level0(domainmeta%L0DataFrom(idomain))
77
78 ! check whether L0 data is shared
79 if (idomain .gt. 1) then
80 if (domainmeta%L0DataFrom(idomain) < idomain) then
81 !
82 call message(' Using dem of domain ', &
83 trim(adjustl(num2str(domainmeta%indices(domainmeta%L0DataFrom(idomain))))), ' for domain: ',&
84 trim(adjustl(num2str(idomain))), '...')
85
86 ! DO NOT read L0 data
87 cycle
88
89 end if
90 end if
91
92 call message(' Reading dem for domain: ', trim(adjustl(num2str(domainid))), ' ...')
93
94 ! Header (to check consistency)
95 fname = trim(adjustl(dirmorpho(idomain))) // trim(adjustl(file_dem))
96 call read_header_nc_or_ascii(trim(fname), udem, &
97 level0_idomain%nrows, level0_idomain%ncols, level0_idomain%xllcorner, &
98 level0_idomain%yllcorner, level0_idomain%cellsize, level0_idomain%nodata_value)
99
100 ! check for L0 and L1 scale consistency
101 if(resolutionhydrology(idomain) .LT. level0_idomain%cellsize) then
102 call error_message('***ERROR: resolutionHydrology (L1) should be coarser than the input data resolution (L0)', raise=.false.)
103 call error_message(' check set-up (in mhm.nml) for domain: ', trim(adjustl(num2str(domainid))), ' ...')
104 end if
105
106 ! DEM + overall mask creation
107 fname = trim(adjustl(dirmorpho(idomain))) // trim(adjustl(file_dem))
108 call read_spatial_data_nc_or_ascii(trim(fname), udem, &
109 level0_idomain%nrows, level0_idomain%ncols, level0_idomain%xllcorner, &
110 level0_idomain%yllcorner, level0_idomain%cellsize, data_dp_2d, level0_idomain%mask)
111
112 level0_idomain%nCells = count(level0_idomain%mask)
113 if (level0_idomain%nCells .eq. 0_i4) then
114 ndemcells = level0_idomain%nrows * level0_idomain%ncols
115 call error_message('***ERROR: Domain ', trim(adjustl(num2str(domainid))), &
116 ' contains no valid cells. All ', trim(adjustl(num2str(ndemcells))), ' cells in the dem are masked')
117 end if
118
119 ! put global nodata value into array (probably not all grid cells have values)
120 data_dp_2d = merge(data_dp_2d, nodata_dp, level0_idomain%mask)
121 ! put data in variable
122 call append(l0_elev, pack(data_dp_2d, level0_idomain%mask))
123 ! deallocate arrays
124 deallocate(data_dp_2d)
125
126 end do
127
128 call set_domain_indices(level0, indices=domainmeta%L0DataFrom)
129
130 end subroutine read_dem
131
132 ! NAME
133 ! read_lcover
134
135 ! PURPOSE
136 !> \brief TODO: add description
137
138 !> \details TODO: add description
139
140 ! HISTORY
141 !> \authors Robert Schweppe
142
143 !> \date Jun 2018
144
145 ! Modifications:
146
147 subroutine read_lcover
148
149 use mo_append, only : append, paste
151 use mo_common_file, only : ulcoverclass
152 use mo_common_types, only: grid
155 use mo_string_utils, only : num2str
156
157 implicit none
158
159 ! loop variables
160 integer(i4) :: domainid, idomain, ivar
161
162 ! file name of file to read
163 character(256) :: fname
164
165 integer(i4), dimension(:, :), allocatable :: data_i4_2d
166
167 integer(i4), dimension(:, :), allocatable :: datamatrix_i4
168
169 logical, dimension(:, :), allocatable :: mask_2d
170
171 type(grid), pointer :: level0_idomain
172
173
174 do idomain = 1, domainmeta%nDomains
175 domainid = domainmeta%indices(idomain)
176
177 level0_idomain => level0(domainmeta%L0DataFrom(idomain))
178
179 ! check whether L0 data is shared
180 ! ToDo: check change
181 if (domainmeta%L0DataFrom(idomain) < idomain) then
182 call message(' Using lcover of domain ', &
183 trim(adjustl(num2str(domainmeta%L0DataFrom(idomain)))), ' for domain: ',&
184 trim(adjustl(num2str(domainid))), '...')
185 ! DO NOT read L0 data
186 cycle
187
188 end if
189
190 call message(' Reading lcover for domain: ', trim(adjustl(num2str(domainid))), ' ...')
191
192 ! LCover read in is realized seperated because of unknown number of scenes
193 do ivar = 1, nlcoverscene
194 fname = trim(adjustl(dirlcover(idomain))) // trim(adjustl(lcfilename(ivar)))
196 level0_idomain%nrows, level0_idomain%ncols, level0_idomain%xllcorner, &
197 level0_idomain%yllcorner, level0_idomain%cellsize, data_i4_2d, mask_2d)
198 ! put global nodata value into array (probably not all grid cells have values)
199 data_i4_2d = merge(data_i4_2d, nodata_i4, mask_2d)
200 call paste(datamatrix_i4, pack(data_i4_2d, level0_idomain%mask), nodata_i4)
201 deallocate(data_i4_2d)
202 end do
203 call append(l0_lcover, datamatrix_i4)
204 deallocate(datamatrix_i4)
205
206 end do
207
208 end subroutine read_lcover
209
210end module mo_common_read_data
Reads spatial data files of nc or ASCII format.
Provides constants commonly used by mHM, mRM and MPR.
real(dp), parameter, public nodata_dp
integer(i4), parameter, public nodata_i4
Provides file names and units for mRM.
integer, parameter ulcoverclass
Unit for LCover input data file.
character(len= *), parameter file_dem
DEM input data file.
integer, parameter udem
Unit for DEM input data file.
gridding tools
subroutine, public set_domain_indices(grids, indices)
TODO: add description.
Common reading routines.
subroutine, public read_lcover
TODO: add description.
subroutine, public read_dem
TODO: add description.
Provides common types needed by mHM, mRM and/or mpr.
Provides structures needed by mHM, mRM and/or mpr.
real(dp), dimension(:), allocatable, public resolutionhydrology
character(256), dimension(:), allocatable, public lcfilename
type(domain_meta), public domainmeta
integer(i4), public nlcoverscene
character(256), dimension(:), allocatable, public dirlcover
real(dp), dimension(:), allocatable, public l0_elev
character(256), dimension(:), allocatable, public dirmorpho
integer(i4), dimension(:, :), allocatable, public l0_lcover
type(grid), dimension(:), allocatable, target, public level0
Reads spatial input data.
subroutine, public read_header_nc_or_ascii(filepath, fileunit, header_ncols, header_nrows, header_xllcorner, header_yllcorner, header_cellsize, header_nodata, varname)
Read header information from either NetCDF or ASCII depending on file name/presence.