diff --git a/transfers/permissions_transfer.py b/transfers/permissions_transfer.py index 1dcca2b8f..18daa1040 100644 --- a/transfers/permissions_transfer.py +++ b/transfers/permissions_transfer.py @@ -1,5 +1,6 @@ from sqlalchemy.orm import Session from datetime import datetime +from pandas import isna from db import Thing, PermissionHistory from transfers.util import read_csv, logger, replace_nans @@ -15,8 +16,10 @@ def transfer_permissions(session: Session): """ - The transferred wells and contacts need to be queried to know who gave - permission to which well since contact_id is required for PermissionHistory + The transferred wells and contacts need to be transferred first + - to access the auto-generated well IDs + - to know who gave permission to which well since contact_id is required for + PermissionHistory """ wdf = read_csv("WellData", dtype={"OSEWelltagID": str}) wdf = replace_nans(wdf) @@ -38,7 +41,11 @@ def transfer_permissions(session: Session): allow_water_level_samples = wdf.loc[ wdf["PointID"] == well.name, "MonitorOK" ].values - if len(allow_water_level_samples) > 0 and allow_water_level_samples is not None: + if len(allow_water_level_samples) == 0: + pass + elif isna(allow_water_level_samples[0]): + pass + else: try: permission_allowed = bool(allow_water_level_samples[0]) permission = PermissionHistory( @@ -61,10 +68,11 @@ def transfer_permissions(session: Session): allow_water_chemistry_samples = wdf.loc[ wdf["PointID"] == well.name, "SampleOK" ].values - if ( - len(allow_water_chemistry_samples) > 0 - and allow_water_chemistry_samples is not None - ): + if len(allow_water_chemistry_samples) == 0: + pass + elif isna(allow_water_chemistry_samples[0]): + pass + else: try: permission_allowed = bool(allow_water_chemistry_samples[0]) permission = PermissionHistory( diff --git a/transfers/well_transfer.py b/transfers/well_transfer.py index e07706321..79fa4842f 100644 --- a/transfers/well_transfer.py +++ b/transfers/well_transfer.py @@ -16,7 +16,7 @@ import json import time from datetime import datetime, UTC - +import re import pandas as pd from pandas import isna from pydantic import ValidationError @@ -122,19 +122,26 @@ def _extract_casing_materials(row) -> list[str]: return materials +pattern = re.compile( + r"\b(?Pjet|hand|submersible)\b|\b(?Pline[-\s]+shaft)\b", re.IGNORECASE +) + + +def first_matched_term(text: str): + m = pattern.search(text) + if not m: + return None + return m.group("term") or m.group("phrase") + + +PUMP_MAPPING = {"jet": "Jet", "hand": "Hand", "submersible": "Submersible"} + + def _extract_well_pump_type(row) -> str | None: + if isna(row.ConstructionNotes): + return None construction_notes = row.ConstructionNotes.lower() - if "pump" in construction_notes: - if "submersible" in construction_notes: - return "Submersible" - elif "jet" in construction_notes: - return "Jet" - elif "line shaft" in construction_notes or "lineshaft" in construction_notes: - return "Line Shaft" - elif "hand" in construction_notes: - return "Hand" - else: - return None + return PUMP_MAPPING.get(first_matched_term(construction_notes), None) # Parse aquifer codes @@ -357,9 +364,7 @@ def transfer_wells(session: Session, flags: dict = None, limit: int = 0) -> None well_casing_materials = ( [] if isna(row.CasingDescription) else _extract_casing_materials(row) ) - well_pump_type = ( - _extract_well_pump_type(row) if row.ConstructionNotes else None - ) + well_pump_type = _extract_well_pump_type(row) # manually add the well rather than add_well from services/thing_helper.py # so that effective_start can be set on the location assocation