|
18 | 18 | import time |
19 | 19 | import traceback |
20 | 20 | from concurrent.futures import ThreadPoolExecutor, as_completed |
21 | | -from datetime import datetime, UTC |
| 21 | +from datetime import date, datetime, UTC |
22 | 22 | from zoneinfo import ZoneInfo |
23 | 23 |
|
24 | 24 | import pandas as pd |
|
95 | 95 | ] |
96 | 96 |
|
97 | 97 |
|
| 98 | +def _normalize_completion_date(value): |
| 99 | + if value is None or pd.isna(value): |
| 100 | + return None |
| 101 | + |
| 102 | + if isinstance(value, pd.Timestamp): |
| 103 | + return value.date() |
| 104 | + |
| 105 | + if isinstance(value, datetime): |
| 106 | + return value.date() |
| 107 | + |
| 108 | + if isinstance(value, date): |
| 109 | + return value |
| 110 | + |
| 111 | + if isinstance(value, str): |
| 112 | + parsed = pd.to_datetime(value.strip(), errors="coerce") |
| 113 | + if not pd.isna(parsed): |
| 114 | + return parsed.date() |
| 115 | + |
| 116 | + return value |
| 117 | + |
| 118 | + |
98 | 119 | class WellTransferer(Transferer): |
99 | 120 | source_table = "WellData" |
100 | 121 |
|
@@ -279,123 +300,6 @@ def _get_dfs(self): |
279 | 300 | cleaned_df = cleaned_df[cleaned_df["PointID"].isin(self.pointids)] |
280 | 301 | return input_df, cleaned_df |
281 | 302 |
|
282 | | - # def _step(self, session: Session, df: pd.DataFrame, i: int, row: pd.Series): |
283 | | - # |
284 | | - # try: |
285 | | - # first_visit_date = get_first_visit_date(row) |
286 | | - # well_purposes = ( |
287 | | - # [] if isna(row.CurrentUse) else self._extract_well_purposes(row) |
288 | | - # ) |
289 | | - # well_casing_materials = ( |
290 | | - # [] if isna(row.CasingDescription) else extract_casing_materials(row) |
291 | | - # ) |
292 | | - # well_pump_type = extract_well_pump_type(row) |
293 | | - # |
294 | | - # wcm = None |
295 | | - # if notna(row.ConstructionMethod): |
296 | | - # wcm = self._get_lexicon_value( |
297 | | - # row, f"LU_ConstructionMethod:{row.ConstructionMethod}", "Unknown" |
298 | | - # ) |
299 | | - # |
300 | | - # mpheight = row.MPHeight |
301 | | - # mpheight_description = row.MeasuringPoint |
302 | | - # if mpheight is None: |
303 | | - # mphs = self._measuring_point_estimator.estimate_measuring_point_height( |
304 | | - # row |
305 | | - # ) |
306 | | - # if mphs: |
307 | | - # try: |
308 | | - # mpheight = mphs[0][0] |
309 | | - # mpheight_description = mphs[1][0] |
310 | | - # except IndexError: |
311 | | - # if self.verbose: |
312 | | - # logger.warning( |
313 | | - # f"Measuring point height estimation failed for well {row.PointID}, {mphs}" |
314 | | - # ) |
315 | | - # |
316 | | - # data = CreateWell( |
317 | | - # location_id=0, |
318 | | - # name=row.PointID, |
319 | | - # first_visit_date=first_visit_date, |
320 | | - # hole_depth=row.HoleDepth, |
321 | | - # well_depth=row.WellDepth, |
322 | | - # well_casing_diameter=( |
323 | | - # row.CasingDiameter * 12 if row.CasingDiameter else None |
324 | | - # ), |
325 | | - # well_casing_depth=row.CasingDepth, |
326 | | - # release_status="public" if row.PublicRelease else "private", |
327 | | - # measuring_point_height=mpheight, |
328 | | - # measuring_point_description=mpheight_description, |
329 | | - # notes=( |
330 | | - # [{"content": row.Notes, "note_type": "General"}] |
331 | | - # if row.Notes |
332 | | - # else [] |
333 | | - # ), |
334 | | - # well_completion_date=row.CompletionDate, |
335 | | - # well_driller_name=row.DrillerName, |
336 | | - # well_construction_method=wcm, |
337 | | - # well_pump_type=well_pump_type, |
338 | | - # ) |
339 | | - # |
340 | | - # CreateWell.model_validate(data) |
341 | | - # except ValidationError as e: |
342 | | - # self._capture_validation_error(row.PointID, e) |
343 | | - # return |
344 | | - # |
345 | | - # well = None |
346 | | - # try: |
347 | | - # well_data = data.model_dump(exclude=EXCLUDED_FIELDS) |
348 | | - # well_data["thing_type"] = "water well" |
349 | | - # well_data["nma_pk_welldata"] = row.WellID |
350 | | - # well_data["nma_pk_location"] = row.LocationId |
351 | | - # |
352 | | - # well = Thing(**well_data) |
353 | | - # session.add(well) |
354 | | - # |
355 | | - # if well_purposes: |
356 | | - # for wp in well_purposes: |
357 | | - # # TODO: add validation logic here |
358 | | - # if wp in WellPurposeEnum: |
359 | | - # wp_obj = WellPurpose(thing=well, purpose=wp) |
360 | | - # session.add(wp_obj) |
361 | | - # else: |
362 | | - # logger.critical(f"{well.name}. Invalid well purpose: {wp}") |
363 | | - # |
364 | | - # if well_casing_materials: |
365 | | - # for wcm in well_casing_materials: |
366 | | - # # TODO: add validation logic here |
367 | | - # if wcm in WellCasingMaterialEnum: |
368 | | - # wcm_obj = WellCasingMaterial(thing=well, material=wcm) |
369 | | - # session.add(wcm_obj) |
370 | | - # else: |
371 | | - # logger.critical( |
372 | | - # f"{well.name}. Invalid well casing material: {wcm}" |
373 | | - # ) |
374 | | - # except Exception as e: |
375 | | - # if well is not None: |
376 | | - # session.expunge(well) |
377 | | - # |
378 | | - # self._capture_error(row.PointID, str(e), "UnknownField") |
379 | | - # |
380 | | - # logger.critical(f"Error creating well for {row.PointID}: {e}") |
381 | | - # return |
382 | | - # |
383 | | - # try: |
384 | | - # location, elevation_method, notes = make_location( |
385 | | - # row, self._cached_elevations |
386 | | - # ) |
387 | | - # session.add(location) |
388 | | - # # session.flush() |
389 | | - # self._added_locations[row.PointID] = (elevation_method, notes) |
390 | | - # except Exception as e: |
391 | | - # import traceback |
392 | | - # |
393 | | - # traceback.print_exc() |
394 | | - # self._capture_error(row.PointID, str(e), str(e), "Location") |
395 | | - # logger.critical(f"Error making location for {row.PointID}: {e}") |
396 | | - # |
397 | | - # return |
398 | | - # |
399 | 303 | def _extract_well_purposes(self, row) -> list[str]: |
400 | 304 | cu = row.CurrentUse |
401 | 305 |
|
@@ -659,7 +563,7 @@ def _build_well_payload(self, row) -> CreateWell | None: |
659 | 563 | if row.Notes |
660 | 564 | else [] |
661 | 565 | ), |
662 | | - well_completion_date=row.CompletionDate, |
| 566 | + well_completion_date=_normalize_completion_date(row.CompletionDate), |
663 | 567 | well_driller_name=row.DrillerName, |
664 | 568 | well_construction_method=wcm, |
665 | 569 | well_pump_type=well_pump_type, |
|
0 commit comments