Skip to content
Merged
22 changes: 15 additions & 7 deletions transfers/permissions_transfer.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
Expand All @@ -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(
Expand All @@ -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(
Expand Down
35 changes: 20 additions & 15 deletions transfers/well_transfer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -122,19 +122,26 @@ def _extract_casing_materials(row) -> list[str]:
return materials


pattern = re.compile(
r"\b(?P<term>jet|hand|submersible)\b|\b(?P<phrase>line[-\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
Expand Down Expand Up @@ -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
Expand Down
Loading