forked from bcgov/wps
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: dgboss <[email protected]> Summarizes advisory area based on TPI. Uses the snow-masked pmtiles hfi layer for the given date to mask contributing TPI pixels. After finding out the initial implementation uses up to 20GB of memory, commit bcgov@ab72307 and onwards introduce a more memory optimized implementation. Basically all datasets are loaded from S3, but data is only read in and transformed a chunk at a time. This implementation stays under 4.5GB of memory usage.
- Loading branch information
Showing
11 changed files
with
519 additions
and
218 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
api/alembic/versions/6910d017b626_add_advisory_tpi_table.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
"""add advisory tpi table | ||
Revision ID: 6910d017b626 | ||
Revises: be128a7bb4fd | ||
Create Date: 2024-07-31 16:27:31.642156 | ||
""" | ||
|
||
from alembic import op | ||
import sqlalchemy as sa | ||
from sqlalchemy.dialects import postgresql | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = "6910d017b626" | ||
down_revision = "be128a7bb4fd" | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade(): | ||
# ### commands auto generated by Alembic ### | ||
op.create_table( | ||
"advisory_tpi_stats", | ||
sa.Column("id", sa.Integer(), nullable=False), | ||
sa.Column("advisory_shape_id", sa.Integer(), nullable=False), | ||
sa.Column("run_parameters", sa.Integer(), nullable=False), | ||
sa.Column("valley_bottom", sa.Integer(), nullable=False), | ||
sa.Column("mid_slope", sa.Integer(), nullable=False), | ||
sa.Column("upper_slope", sa.Integer(), nullable=False), | ||
sa.Column("pixel_size_metres", sa.Integer(), nullable=False), | ||
sa.ForeignKeyConstraint( | ||
["advisory_shape_id"], | ||
["advisory_shapes.id"], | ||
), | ||
sa.ForeignKeyConstraint( | ||
["run_parameters"], | ||
["run_parameters.id"], | ||
), | ||
sa.PrimaryKeyConstraint("id"), | ||
comment="Elevation TPI stats per fire shape", | ||
) | ||
op.create_index(op.f("ix_advisory_tpi_stats_advisory_shape_id"), "advisory_tpi_stats", ["advisory_shape_id"], unique=False) | ||
op.create_index(op.f("ix_advisory_tpi_stats_id"), "advisory_tpi_stats", ["id"], unique=False) | ||
op.create_index(op.f("ix_advisory_tpi_stats_run_parameters"), "advisory_tpi_stats", ["run_parameters"], unique=False) | ||
# ### end Alembic commands ### | ||
|
||
|
||
def downgrade(): | ||
# ### commands auto generated by Alembic ### | ||
op.drop_index(op.f("ix_advisory_tpi_stats_run_parameters"), table_name="advisory_tpi_stats") | ||
op.drop_index(op.f("ix_advisory_tpi_stats_id"), table_name="advisory_tpi_stats") | ||
op.drop_index(op.f("ix_advisory_tpi_stats_advisory_shape_id"), table_name="advisory_tpi_stats") | ||
op.drop_table("advisory_tpi_stats") | ||
# ### end Alembic commands ### |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,27 @@ | ||
""" Code relating to processing HFI data related to elevation | ||
""" | ||
"""Code relating to processing HFI data related to elevation""" | ||
|
||
import logging | ||
from datetime import date, datetime | ||
from time import perf_counter | ||
from app.auto_spatial_advisory.common import get_s3_key | ||
from app.auto_spatial_advisory.elevation import process_elevation | ||
from app.auto_spatial_advisory.elevation import process_elevation_tpi | ||
from app.auto_spatial_advisory.run_type import RunType | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
async def process_hfi_elevation(run_type: RunType, run_date: date, run_datetime: datetime, for_date: date): | ||
""" Create a new elevation based hfi analysis records for the given date. | ||
"""Create a new elevation based hfi analysis records for the given date. | ||
:param run_type: The type of run to process. (is it a forecast or actual run?) | ||
:param run_date: The date of the run to process. (when was the hfi file created?) | ||
:param for_date: The date of the hfi to process. (when is the hfi for?) | ||
""" | ||
|
||
logger.info('Processing HFI elevation %s for run date: %s, for date: %s', run_type, run_date, for_date) | ||
logger.info("Processing HFI elevation %s for run date: %s, for date: %s", run_type, run_date, for_date) | ||
perf_start = perf_counter() | ||
|
||
key = get_s3_key(run_type, run_date, for_date) | ||
logger.info(f'Key to HFI in object storage: {key}') | ||
|
||
await process_elevation(key, run_type, run_datetime, for_date) | ||
await process_elevation_tpi(run_type, run_datetime, for_date) | ||
|
||
perf_end = perf_counter() | ||
delta = perf_end - perf_start | ||
logger.info('%f delta count before and after processing HFI elevation', delta) | ||
logger.info("%f delta count before and after processing HFI elevation", delta) |
Oops, something went wrong.