Skip to content

temporal: t.rast.aggregate: add -e flag to extend existing STRDS#7223

Open
Sourish-spc wants to merge 15 commits intoOSGeo:mainfrom
Sourish-spc:temporal/t.rast.aggregate-extend-mode
Open

temporal: t.rast.aggregate: add -e flag to extend existing STRDS#7223
Sourish-spc wants to merge 15 commits intoOSGeo:mainfrom
Sourish-spc:temporal/t.rast.aggregate-extend-mode

Conversation

@Sourish-spc
Copy link
Copy Markdown
Contributor

Description

This PR adds support for extending an existing Space Time Raster Dataset (STRDS) in t.rast.aggregate by introducing a new -e flag, following the same pattern implemented in #3798 for t.rast.neighbors.

Changes

  • Added -e flag (Extend existing space time raster dataset) to module interface
  • Modified STRDS creation logic to open existing STRDS when -e flag is set instead of always creating a new one
  • Added history update via update_command_string() when extending existing STRDS
  • Added test test_extend_existing_strds to existing test suite

Usage

First run — creates new STRDS:
t.rast.aggregate input=daily_rain output=monthly_rain granularity="1 month" method=average basename=result

Second run — extends existing STRDS:
t.rast.aggregate input=daily_rain output=monthly_rain granularity="1 month" method=average basename=result_new -e

Related

Closes part of #3427
See also #3798

In stds_import.py, the finally block used the deprecated 'gisdbase'
option when calling g.mapset to switch back to the original location.
This caused a deprecation warning on every t.rast.import run with
the project parameter.

Replaced 'gisdbase' with 'dbase' to match the current g.mapset API.

Fixes OSGeo#4231
@github-actions github-actions bot added temporal Related to temporal data processing Python Related code is in Python libraries module tests Related to Test Suite labels Mar 27, 2026
@Sourish-spc Sourish-spc force-pushed the temporal/t.rast.aggregate-extend-mode branch from 7ee4adc to 9b2110e Compare March 27, 2026 07:45
Copy link
Copy Markdown
Member

@ninsbl ninsbl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please also install pre-commit in your local working directory to avoid CI runs for formalities...

Comment on lines +162 to +168
gs.fatal(
_(
"Space time raster dataset <%s> does not exist. "
"Cannot extend a non-existing dataset."
)
% output
)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally, I do not think a fatal error should be thrown here. I would say, a warning at best. In any case, this part should be consistent across all temporal tools that receive the e-flag.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated: changed fatal error to warning and fall back to creating new STRDS when dataset doesn't exist.

# Verify STRDS was extended
lister = SimpleModule("t.rast.list", input="B", columns="name", flags="u")
self.runModule(lister)
self.assertIn("b_ext", lister.outputs.stdout)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This only checks that some map with "b_ext" in the name exists. It doesn't verify the original maps are still present, that the STRDS wasn't just overwritten, and/or the total map count.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the review. The test has been strengthened to also verify that original maps are still present and the total map count increased after extending.

Comment on lines +258 to +259
if output_exists:
output_strds.update_command_string(dbif=dbif)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this make sense when overwriting?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, iI did not really think about it. It may make sense in any case. I did not check the behavior. Updates of the STDS should be written to the history I`ld say, which is what update_command_string does. The command should not be duplicated in the history though. That should be checked...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated the condition to if not output_exists or extend to avoid duplicating the command string in history when overwriting, while still updating it correctly when extending.


if __name__ == "__main__":
options, flags = gs.parser()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, review your code, including the PR changes.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the review. The extra blank line has been removed in the latest commit.

Comment on lines +227 to +240
if not output_exists or (overwrite and not extend):
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,
overwrite,
)
elif output_exists and extend:
output_strds = tgis.open_old_stds(output, "strds", dbif)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens when "else"? Or is there a condition which I don't see. Perhaps worth commenting on if that's the case.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed that in the.rast.neighbors too, where this is likely copied from. So It should be addressed there in a different PR too. Given that the possibility to extend existing STDS would be useful in multiple temporal tools, maybe a extend_if_exists (or something like that) library function would make sense, that returns either a new or exiting STDS!? @wenzeslaus what do you think?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe enhance and use

def check_new_stds(name, type, dbif=None, overwrite: bool = False):
?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refactored the extend logic into check_new_stds in open_stds.py as a reusable library function with an extend parameter. The else case is now handled there: if -e is set and STRDS does not exist, it silently creates a new one (extend-if-exists behavior).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, an sorry for possibly putting you on a slightly wrong track with enhancing check_new_stds . That function should probably stay as is.
What in general is needed is 1) a function that checks if output STDS can be opened as requested (new or existing for update, e.g. check_open_output_stds). That should be minimal (not creating or opening) and run before main processing starts. And 2), after main processing succeeded, the output STDS should be opened or created so produced maps can be registered (e.g. open_output_stds).

Comment on lines +161 to +169
if extend and not output_exists:
gs.warning(
_(
"Space time raster dataset <%s> does not exist. "
"Cannot extend a non-existing dataset."
)
% output
)
extend = False # fall back to creating new STRDS
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The warning does not make sense to me. Either -e is "extend if already exist" (no error or warning needed) or "extend existing" (so error on non-existence). The first option is sort of append mode; we have that elsewhere.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. That said, the confusion is my fault as I have not been explicit.
Personally, I would prefer "extend-if-exists" behavior, as it is more graceful, but I have no strong opinion here...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed the warning and fallback. Now using extend-if-exists behavior: if the STRDS doesn't exist, it just creates a new one silently.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

libraries module Python Related code is in Python temporal Related to temporal data processing tests Related to Test Suite

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants