Debian Codegraph @

temporal/t.rast.series/t.rast.series.py

raw
1 #!/usr/bin/env python3
2
3 ############################################################################
4 #
5 # MODULE: t.rast.series
6 # AUTHOR(S): Soeren Gebbert
7 #
8 # PURPOSE: Perform different aggregation algorithms from r.series on all or a
9 # selected subset of raster maps in a space time raster dataset
10 # COPYRIGHT: (C) 2011-2017 by the GRASS Development Team
11 #
12 # This program is free software; you can redistribute it and/or modify
13 # it under the terms of the GNU General Public License as published by
14 # the Free Software Foundation; either version 2 of the License, or
15 # (at your option) any later version.
16 #
17 # This program is distributed in the hope that it will be useful,
18 # but WITHOUT ANY WARRANTY; without even the implied warranty of
19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 # GNU General Public License for more details.
21 #
22 #############################################################################
23
24 # %module
25 # % description: Performs different aggregation algorithms from r.series on all or a subset of raster maps in a space time raster dataset.
26 # % keyword: temporal
27 # % keyword: aggregation
28 # % keyword: series
29 # % keyword: raster
30 # % keyword: time
31 # %end
32
33 # %option G_OPT_STRDS_INPUT
34 # %end
35
36 # %option
37 # % key: method
38 # % type: string
39 # % description: Aggregate operation to be performed on the raster maps
40 # % required: yes
41 # % multiple: yes
42 # % 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
43 # % answer: average
44 # %end
45
46 # %option
47 # % key: quantile
48 # % type: double
49 # % description: Quantile to calculate for method=quantile
50 # % required: no
51 # % multiple: yes
52 # % options: 0.0-1.0
53 # %end
54
55 # %option
56 # % key: order
57 # % type: string
58 # % description: Sort the maps by category
59 # % required: no
60 # % multiple: yes
61 # % options: id, name, creator, mapset, creation_time, modification_time, start_time, end_time, north, south, west, east, min, max
62 # % answer: start_time
63 # %end
64
65 # %option G_OPT_M_NPROCS
66 # %end
67
68 # %option G_OPT_MEMORYMB
69 # %end
70
71 # %option G_OPT_T_WHERE
72 # %end
73
74 # %option G_OPT_R_OUTPUTS
75 # %end
76
77 # %option
78 # % key: file_limit
79 # % type: integer
80 # % description: The maximum number of open files allowed for each r.series process
81 # % required: no
82 # % answer: 1000
83 # %end
84
85 # %flag
86 # % key: t
87 # % description: Do not assign the space time raster dataset start and end time to the output map
88 # %end
89
90 # %flag
91 # % key: n
92 # % description: Propagate NULLs
93 # %end
94
95 from pathlib import Path
96
97 import grass.script as gs
98 from grass.exceptions import CalledModuleError
99
100 ############################################################################
101
102
103 def main():
104 # lazy imports
105 import grass.temporal as tgis
106
107 # Get the options
108 input = options["input"]
109 output = options["output"]
110 method = options["method"]
111 quantile = options["quantile"]
112 order = options["order"]
113 memory = options["memory"]
114 nprocs = options["nprocs"]
115 where = options["where"]
116 max_files_open = int(options["file_limit"])
117 add_time = flags["t"]
118 nulls = flags["n"]
119
120 # Check if number of methods and output maps matches
121 if "quantile" in method:
122 len_method = len(method.split(",")) - 1
123 else:
124 len_method = len(method.split(","))
125
126 if (len(list(filter(None, quantile.split(",")))) + len_method) != len(
127 output.split(",")
128 ):
129 gs.fatal(_("Number requested methods and output maps do not match."))
130
131 # Make sure the temporal database exists
132 tgis.init()
133
134 sp = tgis.open_old_stds(input, "strds")
135
136 rows = sp.get_registered_maps("id", where, order, None)
137
138 if rows:
139 # Create the r.series input file
140 filename = gs.tempfile(True)
141 Path(filename).write_text("\n".join(str(row["id"]) for row in rows))
142
143 flag = ""
144 if len(rows) > max_files_open:
145 gs.warning(
146 _(
147 "Processing over {} maps: activating -z flag of r.series which "
148 "slows down processing."
149 ).format(max_files_open)
150 )
151 flag += "z"
152 if nulls:
153 flag += "n"
154
155 try:
156 gs.run_command(
157 "r.series",
158 flags=flag,
159 file=filename,
160 output=output,
161 overwrite=gs.overwrite(),
162 method=method,
163 quantile=quantile,
164 memory=memory,
165 nprocs=nprocs,
166 )
167 except CalledModuleError:
168 gs.fatal(_("%s failed. Check above error messages.") % "r.series")
169
170 if not add_time:
171 # We need to set the temporal extent from the subset of selected maps
172 maps = sp.get_registered_maps_as_objects(
173 where=where, order=order, dbif=None
174 )
175 first_map = maps[0]
176 last_map = maps[-1]
177 start_a, end_a = first_map.get_temporal_extent_as_tuple()
178 start_b, end_b = last_map.get_temporal_extent_as_tuple()
179
180 if end_b is None:
181 end_b = start_b
182
183 if first_map.is_time_absolute():
184 extent = tgis.AbsoluteTemporalExtent(start_time=start_a, end_time=end_b)
185 else:
186 extent = tgis.RelativeTemporalExtent(
187 start_time=start_a,
188 end_time=end_b,
189 unit=first_map.get_relative_time_unit(),
190 )
191
192 for out_map in output.split(","):
193 # Create the time range for the output map
194 if out_map.find("@") >= 0:
195 id = out_map
196 else:
197 mapset = gs.gisenv()["MAPSET"]
198 id = out_map + "@" + mapset
199
200 map = sp.get_new_map_instance(id)
201 map.load()
202
203 map.set_temporal_extent(extent=extent)
204
205 # Register the map in the temporal database
206 if map.is_in_db():
207 map.update_all()
208 else:
209 map.insert()
210
211
212 if __name__ == "__main__":
213 options, flags = gs.parser()
214 main()