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 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641
|
"""
@package animation.temporal_manager
@brief Management of temporal datasets used in animation
Classes:
- temporal_manager::DataMode
- temporal_manager::GranularityMode
- temporal_manager::TemporalManager
(C) 2012 by the GRASS Development Team
This program is free software under the GNU General Public License
(>=v2). Read the file COPYING that comes with GRASS for details.
@author Anna Kratochvilova <kratochanna gmail.com>
"""
import datetime
import grass.script as grass
import grass.temporal as tgis
from core.gcmd import GException
from core.settings import UserSettings
from animation.utils import validateTimeseriesName, TemporalType
class DataMode:
SIMPLE = 1
MULTIPLE = 2
class GranularityMode:
ONE_UNIT = 1
ORIGINAL = 2
class TemporalManager:
"""Class for temporal data processing."""
def __init__(self):
self.timeseriesList = []
self.timeseriesInfo = {}
self.dataMode = None
self.temporalType = None
self.granularityMode = GranularityMode.ORIGINAL
def GetTemporalType(self):
"""Get temporal type (TemporalType.ABSOLUTE,
TemporalType.RELATIVE)
"""
return self._temporalType
def SetTemporalType(self, ttype):
self._temporalType = ttype
temporalType = property(fget=GetTemporalType, fset=SetTemporalType)
def AddTimeSeries(self, timeseries, etype):
"""Add space time dataset
and collect basic information about it.
Raises GException (e.g. with invalid topology).
:param timeseries: name of timeseries (with or without mapset)
:param etype: element type (strds, stvds)
"""
self._gatherInformation(
timeseries, etype, self.timeseriesList, self.timeseriesInfo
)
def EvaluateInputData(self):
"""Checks if all timeseries are compatible (raises GException).
Sets internal variables.
"""
timeseriesCount = len(self.timeseriesList)
if timeseriesCount == 1:
self.dataMode = DataMode.SIMPLE
elif timeseriesCount > 1:
self.dataMode = DataMode.MULTIPLE
else:
self.dataMode = None
ret, message = self._setTemporalState()
if not ret:
raise GException(message)
if message: # warning
return message
return None
def _setTemporalState(self):
# check for absolute x relative
absolute, relative = 0, 0
for infoDict in self.timeseriesInfo.values():
if infoDict["temporal_type"] == "absolute":
absolute += 1
else:
relative += 1
if bool(absolute) == bool(relative):
message = _(
"It is not allowed to display data with different "
"temporal types (absolute and relative)."
)
return False, message
if absolute:
self.temporalType = TemporalType.ABSOLUTE
else:
self.temporalType = TemporalType.RELATIVE
# check for units for relative type
if relative:
units = set()
for infoDict in self.timeseriesInfo.values():
units.add(infoDict["unit"])
if len(units) > 1:
message = _(
"It is not allowed to display data with different units (%s)."
) % ",".join(units)
return False, message
# check for interval x point
interval, point = 0, 0
for infoDict in self.timeseriesInfo.values():
if infoDict["map_time"] == "interval":
interval += 1
else:
point += 1
if bool(interval) == bool(point):
message = _(
"You are going to display data with different "
"temporal types of maps (interval and point)."
" It is recommended to use data of one temporal type to avoid "
"confusion."
)
return True, message # warning
return True, None
def GetGranularity(self):
"""Returns temporal granularity of currently loaded timeseries."""
if self.dataMode == DataMode.SIMPLE:
gran = self.timeseriesInfo[self.timeseriesList[0]]["granularity"]
if "unit" in self.timeseriesInfo[self.timeseriesList[0]]: # relative:
granNum = gran
unit = self.timeseriesInfo[self.timeseriesList[0]]["unit"]
if self.granularityMode == GranularityMode.ONE_UNIT:
granNum = 1
else: # absolute
granNum, unit = gran.split()
if self.granularityMode == GranularityMode.ONE_UNIT:
granNum = 1
return (int(granNum), unit)
if self.dataMode == DataMode.MULTIPLE:
return self._getCommonGranularity()
def _getCommonGranularity(self):
allMaps = []
for dataset in self.timeseriesList:
maps = self.timeseriesInfo[dataset]["maps"]
allMaps.extend(maps)
if self.temporalType == TemporalType.ABSOLUTE:
gran = tgis.compute_absolute_time_granularity(allMaps)
granNum, unit = gran.split()
if self.granularityMode == GranularityMode.ONE_UNIT:
granNum = 1
return int(granNum), unit
if self.temporalType == TemporalType.RELATIVE:
unit = self.timeseriesInfo[self.timeseriesList[0]]["unit"]
granNum = tgis.compute_relative_time_granularity(allMaps)
if self.granularityMode == GranularityMode.ONE_UNIT:
granNum = 1
return (granNum, unit)
def GetLabelsAndMaps(self):
"""Returns time labels and map names."""
mapLists = []
labelLists = []
labelListSet = set()
for dataset in self.timeseriesList:
grassLabels, listOfMaps = self._getLabelsAndMaps(dataset)
mapLists.append(listOfMaps)
labelLists.append(tuple(grassLabels))
labelListSet.update(grassLabels)
# combine all timeLabels and fill missing maps with None
# BUT this does not work properly if the datasets have
# no temporal overlap! We would need to sample all datasets
# by a temporary dataset, I don't know how it would work with point
# data
if self.temporalType == TemporalType.ABSOLUTE:
timestamps = sorted(list(labelListSet), key=lambda x: x[0])
else:
timestamps = sorted(list(labelListSet), key=lambda x: x[0])
newMapLists = []
for mapList, labelList in zip(mapLists, labelLists):
newMapList = [None] * len(timestamps)
i = 0
# compare start time
while timestamps[i][0] != labelList[0][0]: # compare
i += 1
newMapList[i : i + len(mapList)] = mapList
newMapLists.append(newMapList)
mapDict = {}
for i, dataset in enumerate(self.timeseriesList):
mapDict[dataset] = newMapLists[i]
if self.temporalType == TemporalType.ABSOLUTE:
# ('1996-01-01 00:00:00', '1997-01-01 00:00:00', 'year'),
formatString = UserSettings.Get(
group="animation", key="temporal", subkey="format"
)
timestamps = [
(
datetime.datetime.strftime(st, formatString),
(
datetime.datetime.strftime(end, formatString)
if end is not None
else None
),
unit,
)
for (st, end, unit) in timestamps
]
else:
# ('15', '16', u'years'),
timestamps = [
(str(st), end if end is None else str(end), unit)
for st, end, unit in timestamps
]
return timestamps, mapDict
def _getLabelsAndMaps(self, timeseries):
"""Returns time labels and map names (done by sampling)
for both interval and point data.
"""
sp = tgis.dataset_factory(self.timeseriesInfo[timeseries]["etype"], timeseries)
if sp.is_in_db() is False:
raise GException(_("Space time dataset <%s> not found.") % timeseries)
sp.select()
listOfMaps = []
timeLabels = []
granNum, unit = self.GetGranularity()
if self.temporalType == TemporalType.ABSOLUTE:
if self.granularityMode == GranularityMode.ONE_UNIT:
gran = "%(one)d %(unit)s" % {"one": 1, "unit": unit}
else:
gran = "%(num)d %(unit)s" % {"num": granNum, "unit": unit}
elif self.temporalType == TemporalType.RELATIVE:
unit = self.timeseriesInfo[timeseries]["unit"]
if self.granularityMode == GranularityMode.ONE_UNIT:
gran = 1
else:
gran = granNum
# start sampling - now it can be used for both interval and point data
# after instance, there can be a gap or an interval
# if it is a gap we remove it and put there the previous instance instead
# however the first gap must be removed to avoid duplication
maps = sp.get_registered_maps_as_objects_by_granularity(gran=gran)
if maps and len(maps) > 0:
lastTimeseries = None
followsPoint = False # indicates that we are just after finding a point
afterPoint = False # indicates that we are after finding a point
for mymap in maps:
if isinstance(mymap, list):
if len(mymap) > 0:
map = mymap[0]
else:
map = mymap
series = map.get_id()
start, end = map.get_temporal_extent_as_tuple()
if self.timeseriesInfo[timeseries]["map_time"] == "point":
# point data
listOfMaps.append(series)
afterPoint = True
followsPoint = True
lastTimeseries = series
end = None
else:
end = end
# interval data
if series:
# map exists, stop point mode
listOfMaps.append(series)
afterPoint = False
else:
# check point mode
if afterPoint:
if followsPoint:
# skip this one, already there
followsPoint = False
continue
else:
# append the last one (of point time)
listOfMaps.append(lastTimeseries)
end = None
else:
# append series which is None
listOfMaps.append(series)
timeLabels.append((start, end, unit))
return timeLabels, listOfMaps
def _pretifyTimeLabels(self, labels):
"""Convert absolute time labels to grass time and
leave only datum when time is 0.
"""
grassLabels = []
isTime = False
for start, end, unit in labels:
start = tgis.string_to_datetime(start)
start = tgis.datetime_to_grass_datetime_string(start)
if end is not None:
end = tgis.string_to_datetime(end)
end = tgis.datetime_to_grass_datetime_string(end)
grassLabels.append((start, end, unit))
if "00:00:00" not in start or (end is not None and "00:00:00" not in end):
isTime = True
if not isTime:
for i, (start, end, unit) in enumerate(grassLabels):
start = start.replace("00:00:00", "").strip()
if end is not None:
end = end.replace("00:00:00", "").strip()
grassLabels[i] = (start, end, unit)
return grassLabels
def _gatherInformation(self, timeseries, etype, timeseriesList, infoDict):
"""Get info about timeseries and check topology (raises GException)"""
id = validateTimeseriesName(timeseries, etype)
sp = tgis.dataset_factory(etype, id)
# Insert content from db
sp.select()
# Get ordered map list
maps = sp.get_registered_maps_as_objects()
if not sp.check_temporal_topology(maps):
raise GException(_("Topology of Space time dataset %s is invalid." % id))
timeseriesList.append(id)
infoDict[id] = {}
infoDict[id]["etype"] = etype
infoDict[id]["temporal_type"] = sp.get_temporal_type()
if sp.is_time_relative():
infoDict[id]["unit"] = sp.get_relative_time_unit()
infoDict[id]["granularity"] = sp.get_granularity()
infoDict[id]["map_time"] = sp.get_map_time()
infoDict[id]["maps"] = maps
def test():
from pprint import pprint
# Make sure the temporal database exists
tgis.init()
temp = TemporalManager()
# timeseries = createAbsolutePoint()
# timeseries = createRelativePoint()
# timeseries1, timeseries2 = createAbsoluteInterval()
timeseries1, timeseries2 = createRelativeInterval()
temp.AddTimeSeries(timeseries1, "strds")
temp.AddTimeSeries(timeseries2, "strds")
try:
warn = temp.EvaluateInputData()
print(warn)
except GException as e:
print(e)
return
print("///////////////////////////")
gran = temp.GetGranularity()
print("granularity: " + str(gran))
pprint(temp.GetLabelsAndMaps())
def createAbsoluteInterval():
grass.run_command(
"g.region",
s=0,
n=80,
w=0,
e=120,
b=0,
t=50,
res=10,
res3=10,
flags="p3",
quiet=True,
)
grass.mapcalc(exp="prec_1 = rand(0, 550)", overwrite=True)
grass.mapcalc(exp="prec_2 = rand(0, 450)", overwrite=True)
grass.mapcalc(exp="prec_3 = rand(0, 320)", overwrite=True)
grass.mapcalc(exp="prec_4 = rand(0, 510)", overwrite=True)
grass.mapcalc(exp="prec_5 = rand(0, 300)", overwrite=True)
grass.mapcalc(exp="prec_6 = rand(0, 650)", overwrite=True)
grass.mapcalc(exp="temp_1 = rand(0, 550)", overwrite=True)
grass.mapcalc(exp="temp_2 = rand(0, 450)", overwrite=True)
grass.mapcalc(exp="temp_3 = rand(0, 320)", overwrite=True)
grass.mapcalc(exp="temp_4 = rand(0, 510)", overwrite=True)
grass.mapcalc(exp="temp_5 = rand(0, 300)", overwrite=True)
grass.mapcalc(exp="temp_6 = rand(0, 650)", overwrite=True)
n1 = grass.read_command("g.tempfile", pid=1, flags="d").strip()
fd = open(n1, "w")
fd.write(
"prec_1|2001-01-01|2001-02-01\n"
"prec_2|2001-04-01|2001-05-01\n"
"prec_3|2001-05-01|2001-09-01\n"
"prec_4|2001-09-01|2002-01-01\n"
"prec_5|2002-01-01|2002-05-01\n"
"prec_6|2002-05-01|2002-07-01\n"
)
fd.close()
n2 = grass.read_command("g.tempfile", pid=2, flags="d").strip()
fd = open(n2, "w")
fd.write(
"temp_1|2000-10-01|2001-01-01\n"
"temp_2|2001-04-01|2001-05-01\n"
"temp_3|2001-05-01|2001-09-01\n"
"temp_4|2001-09-01|2002-01-01\n"
"temp_5|2002-01-01|2002-05-01\n"
"temp_6|2002-05-01|2002-07-01\n"
)
fd.close()
name1 = "absinterval1"
name2 = "absinterval2"
grass.run_command(
"t.unregister",
type="raster",
maps="prec_1,prec_2,prec_3,prec_4,prec_5,prec_6,"
"temp_1,temp_2,temp_3,temp_4,temp_5,temp_6",
)
for name, fname in zip((name1, name2), (n1, n2)):
grass.run_command(
"t.create",
overwrite=True,
type="strds",
temporaltype="absolute",
output=name,
title="A test with input files",
descr="A test with input files",
)
grass.run_command(
"t.register", flags="i", input=name, file=fname, overwrite=True
)
return name1, name2
def createRelativeInterval():
grass.run_command(
"g.region",
s=0,
n=80,
w=0,
e=120,
b=0,
t=50,
res=10,
res3=10,
flags="p3",
quiet=True,
)
grass.mapcalc(exp="prec_1 = rand(0, 550)", overwrite=True)
grass.mapcalc(exp="prec_2 = rand(0, 450)", overwrite=True)
grass.mapcalc(exp="prec_3 = rand(0, 320)", overwrite=True)
grass.mapcalc(exp="prec_4 = rand(0, 510)", overwrite=True)
grass.mapcalc(exp="prec_5 = rand(0, 300)", overwrite=True)
grass.mapcalc(exp="prec_6 = rand(0, 650)", overwrite=True)
grass.mapcalc(exp="temp_1 = rand(0, 550)", overwrite=True)
grass.mapcalc(exp="temp_2 = rand(0, 450)", overwrite=True)
grass.mapcalc(exp="temp_3 = rand(0, 320)", overwrite=True)
grass.mapcalc(exp="temp_4 = rand(0, 510)", overwrite=True)
grass.mapcalc(exp="temp_5 = rand(0, 300)", overwrite=True)
grass.mapcalc(exp="temp_6 = rand(0, 650)", overwrite=True)
n1 = grass.read_command("g.tempfile", pid=1, flags="d").strip()
fd = open(n1, "w")
fd.write(
"prec_1|1|4\n"
"prec_2|6|7\n"
"prec_3|7|10\n"
"prec_4|10|11\n"
"prec_5|11|14\n"
"prec_6|14|17\n"
)
fd.close()
n2 = grass.read_command("g.tempfile", pid=2, flags="d").strip()
fd = open(n2, "w")
fd.write(
"temp_1|5|6\n"
"temp_2|6|7\n"
"temp_3|7|10\n"
"temp_4|10|11\n"
"temp_5|11|18\n"
"temp_6|19|22\n"
)
fd.close()
name1 = "relinterval1"
name2 = "relinterval2"
grass.run_command(
"t.unregister",
type="raster",
maps="prec_1,prec_2,prec_3,prec_4,prec_5,prec_6,"
"temp_1,temp_2,temp_3,temp_4,temp_5,temp_6",
)
for name, fname in zip((name1, name2), (n1, n2)):
grass.run_command(
"t.create",
overwrite=True,
type="strds",
temporaltype="relative",
output=name,
title="A test with input files",
descr="A test with input files",
)
grass.run_command(
"t.register",
flags="i",
input=name,
file=fname,
unit="years",
overwrite=True,
)
return name1, name2
def createAbsolutePoint():
grass.run_command(
"g.region",
s=0,
n=80,
w=0,
e=120,
b=0,
t=50,
res=10,
res3=10,
flags="p3",
quiet=True,
)
grass.mapcalc(exp="prec_1 = rand(0, 550)", overwrite=True)
grass.mapcalc(exp="prec_2 = rand(0, 450)", overwrite=True)
grass.mapcalc(exp="prec_3 = rand(0, 320)", overwrite=True)
grass.mapcalc(exp="prec_4 = rand(0, 510)", overwrite=True)
grass.mapcalc(exp="prec_5 = rand(0, 300)", overwrite=True)
grass.mapcalc(exp="prec_6 = rand(0, 650)", overwrite=True)
n1 = grass.read_command("g.tempfile", pid=1, flags="d").strip()
fd = open(n1, "w")
fd.write(
"prec_1|2001-01-01\n"
"prec_2|2001-03-01\n"
"prec_3|2001-04-01\n"
"prec_4|2001-05-01\n"
"prec_5|2001-08-01\n"
"prec_6|2001-09-01\n"
)
fd.close()
name = "abspoint"
grass.run_command(
"t.create",
overwrite=True,
type="strds",
temporaltype="absolute",
output=name,
title="A test with input files",
descr="A test with input files",
)
grass.run_command("t.register", flags="i", input=name, file=n1, overwrite=True)
return name
def createRelativePoint():
grass.run_command(
"g.region",
s=0,
n=80,
w=0,
e=120,
b=0,
t=50,
res=10,
res3=10,
flags="p3",
quiet=True,
)
grass.mapcalc(exp="prec_1 = rand(0, 550)", overwrite=True)
grass.mapcalc(exp="prec_2 = rand(0, 450)", overwrite=True)
grass.mapcalc(exp="prec_3 = rand(0, 320)", overwrite=True)
grass.mapcalc(exp="prec_4 = rand(0, 510)", overwrite=True)
grass.mapcalc(exp="prec_5 = rand(0, 300)", overwrite=True)
grass.mapcalc(exp="prec_6 = rand(0, 650)", overwrite=True)
n1 = grass.read_command("g.tempfile", pid=1, flags="d").strip()
fd = open(n1, "w")
fd.write(
"prec_1|1\n" "prec_2|3\n" "prec_3|5\n" "prec_4|7\n" "prec_5|11\n" "prec_6|13\n"
)
fd.close()
name = "relpoint"
grass.run_command(
"t.create",
overwrite=True,
type="strds",
temporaltype="relative",
output=name,
title="A test with input files",
descr="A test with input files",
)
grass.run_command("t.register", unit="day", input=name, file=n1, overwrite=True)
return name
if __name__ == "__main__":
test()
|