-
-
Notifications
You must be signed in to change notification settings - Fork 426
Expand file tree
/
Copy patht.rast.aggregate.py
More file actions
executable file
·234 lines (193 loc) · 6.34 KB
/
t.rast.aggregate.py
File metadata and controls
executable file
·234 lines (193 loc) · 6.34 KB
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
#!/usr/bin/env python3
############################################################################
#
# MODULE: t.rast.aggregate
# AUTHOR(S): Soeren Gebbert
#
# PURPOSE: Temporally aggregates the maps of a space time raster dataset by a user defined granularity.
# COPYRIGHT: (C) 2011-2017 by the GRASS Development Team
#
# This program 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.
#
#############################################################################
# %module
# % description: Aggregates temporally the maps of a space time raster dataset by a user defined granularity.
# % keyword: temporal
# % keyword: aggregation
# % keyword: raster
# % keyword: time
# %end
# %option G_OPT_STRDS_INPUT
# %end
# %option G_OPT_STRDS_OUTPUT
# %end
# %option
# % key: basename
# % type: string
# % label: Basename of the new generated output maps
# % description: Either a numerical suffix or the start time (s-flag) separated by an underscore will be attached to create a unique identifier
# % required: yes
# % multiple: no
# %end
# %option G_OPT_T_SUFFIX
# %end
# %option
# % key: granularity
# % type: string
# % description: Aggregation granularity, format absolute time "x years, x months, x weeks, x days, x hours, x minutes, x seconds" or an integer value for relative time
# % required: yes
# % multiple: no
# %end
# %option
# % key: method
# % type: string
# % description: Aggregate operation to be performed on the raster maps
# % required: yes
# % multiple: no
# % options: average,count,median,mode,minimum,min_raster,maximum,max_raster,stddev,range,sum,variance,diversity,slope,offset,detcoeff,quart1,quart3,perc90,quantile,skewness,kurtosis
# % answer: average
# %end
# %option
# % key: offset
# % type: integer
# % description: Offset that is used to create the output map ids, output map id is generated as: basename_ (count + offset)
# % required: no
# % multiple: no
# % answer: 0
# %end
# %option
# % key: nprocs
# % type: integer
# % description: Number of r.series processes to run in parallel
# % required: no
# % multiple: no
# % answer: 1
# %end
# %option
# % key: file_limit
# % type: integer
# % description: The maximum number of open files allowed for each r.series process
# % required: no
# % multiple: no
# % answer: 1000
# %end
# %option G_OPT_T_SAMPLE
# % options: equal,overlaps,overlapped,starts,started,finishes,finished,during,contains
# % answer: contains
# %end
# %option G_OPT_T_WHERE
# %end
# %flag
# % key: n
# % description: Register Null maps
# %end
import grass.script as gs
############################################################################
def main():
# lazy imports
import grass.temporal as tgis
# Get the options
input = options["input"]
output = options["output"]
where = options["where"]
gran = options["granularity"]
base = options["basename"]
register_null = flags["n"]
method = options["method"]
sampling = options["sampling"]
offset = options["offset"]
nprocs = options["nprocs"]
file_limit = options["file_limit"]
time_suffix = options["suffix"]
topo_list = sampling.split(",")
tgis.init()
dbif = tgis.SQLDatabaseInterfaceConnection()
dbif.connect()
sp = tgis.open_old_stds(input, "strds", dbif)
map_list = sp.get_registered_maps_as_objects(
where=where, order="start_time", dbif=dbif
)
if not map_list:
dbif.close()
gs.fatal(_("Space time raster dataset <%s> is empty") % input)
# We will create the strds later, but need to check here
tgis.check_new_stds(output, "strds", dbif, gs.overwrite())
start_time = map_list[0].temporal_extent.get_start_time()
if sp.is_time_absolute():
start_time = tgis.adjust_datetime_to_granularity(start_time, gran)
# We use the end time first
end_time = map_list[-1].temporal_extent.get_end_time()
has_end_time = True
# In case no end time is available, then we use the start time of the last map layer
if end_time is None:
end_time = map_list[-1].temporal_extent.get_start_time()
has_end_time = False
granularity_list = []
# Build the granularity list
while True:
if has_end_time is True:
if start_time >= end_time:
break
else: # noqa: PLR5501
if start_time > end_time:
break
granule = tgis.RasterDataset(None)
start = start_time
if sp.is_time_absolute():
end = tgis.increment_datetime_by_string(start_time, gran)
granule.set_absolute_time(start, end)
else:
end = start_time + int(gran)
granule.set_relative_time(start, end, sp.get_relative_time_unit())
start_time = end
granularity_list.append(granule)
output_list = tgis.aggregate_by_topology(
granularity_list=granularity_list,
granularity=gran,
map_list=map_list,
topo_list=topo_list,
basename=base,
time_suffix=time_suffix,
offset=offset,
method=method,
nprocs=nprocs,
spatial=None,
overwrite=gs.overwrite(),
file_limit=file_limit,
)
if output_list:
temporal_type, semantic_type, title, description = sp.get_initial_values()
output_strds = tgis.open_new_stds(
output,
"strds",
temporal_type,
title,
description,
semantic_type,
dbif,
gs.overwrite(),
)
register_null = not register_null
tgis.register_map_object_list(
"rast",
output_list,
output_strds,
register_null,
sp.get_relative_time_unit(),
dbif,
)
# Update the raster metadata table entries with aggregation type
output_strds.set_aggregation_type(method)
output_strds.metadata.update(dbif)
dbif.close()
if __name__ == "__main__":
options, flags = gs.parser()
main()