@@ -156,52 +156,6 @@ def _extract_aquifer_type_codes(aquifer_code: str) -> list[str]:
156156 return individual_codes
157157
158158
159- # Get or create aquifer system
160- def get_or_create_aquifer_system (
161- session : Session , aquifers : list , aquifer_name : str , primary_type : str
162- ) -> AquiferSystem | None :
163- """
164- Get existing aquifer or create new one if it doesn't exist.
165-
166- With the new AquiferType model, we create ONE aquifer record per named
167- aquifer (e.g., one "Santa Fe Group"), not multiple variants.
168-
169- Args:
170- session: Database session
171- aquifer_name: Name of the aquifer (from AqClass or type name)
172- primary_type: Primary aquifer type for the aquifer_type field
173- """
174- # Try to find existing aquifer by name
175- # aquifer = (
176- # session.query(AquiferSystem).filter(AquiferSystem.name == aquifer_name).first()
177- # )
178- if aquifer_name is None :
179- return None , False
180-
181- aquifer = next ((a for a in aquifers if a .name == aquifer_name ), None )
182- if aquifer :
183- return aquifer , False
184-
185- # Create new aquifer
186- try :
187- logger .info (
188- f"Creating new aquifer system: { aquifer_name } (primary type: { primary_type } )"
189- )
190-
191- aquifer = AquiferSystem (
192- name = aquifer_name ,
193- primary_aquifer_type = primary_type , # Primary type
194- geographic_scale = None , # Default
195- )
196- session .add (aquifer )
197- session .flush () # Get the ID
198- return aquifer , True
199- except DatabaseError as e :
200- session .rollback ()
201- logger .critical (f"Error creating aquifer { aquifer_name } : { e } " )
202- return None , False
203-
204-
205159def get_or_create_geologic_formation (
206160 session : Session , formation_code : str
207161) -> GeologicFormation | None :
@@ -541,8 +495,8 @@ def _add_aquifers(self, session, row, well):
541495 self ._aquifers = session .query (AquiferSystem ).all ()
542496
543497 # Get or create the aquifer
544- aquifer = get_or_create_aquifer_system (
545- session , self . _aquifers , aquifer_name , primary_type
498+ aquifer = self . _get_or_create_aquifer_system (
499+ session , row , aquifer_name , primary_type
546500 )
547501 if aquifer :
548502
@@ -554,62 +508,102 @@ def _add_aquifers(self, session, row, well):
554508 self ._aquifers .append (aquifer )
555509
556510 # Check if association already exists
557- existing_assoc = (
558- session .query (ThingAquiferAssociation )
559- .filter (
560- ThingAquiferAssociation .thing_id == well .id ,
561- ThingAquiferAssociation .aquifer_system_id == aquifer .id ,
562- )
563- .first ()
564- )
511+ # existing_assoc = (
512+ # session.query(ThingAquiferAssociation)
513+ # .filter(
514+ # ThingAquiferAssociation.thing_id == well.id,
515+ # ThingAquiferAssociation.aquifer_system_id == aquifer.id,
516+ # )
517+ # .first()
518+ # )
519+ # if not existing_assoc:
520+ # Create the association
521+ if self .verbose :
522+ logger .info (f"Associating well { well .name } with aquifer { aquifer .name } " )
523+ aquifer_assoc = ThingAquiferAssociation (thing = well , aquifer_system = aquifer )
524+ session .add (aquifer_assoc )
525+ # session.flush()
565526
566- if not existing_assoc :
567- # Create the association
568- if self .verbose :
569- logger .info (
570- f"Associating well { well .name } with aquifer { aquifer .name } "
527+ # Create AquiferType records for EACH characteristic
528+ aquifer_type_names = []
529+ for aquifer_code in aquifer_codes :
530+ try :
531+ type_name = lexicon_mapper .map_value (
532+ f"LU_AquiferType:{ aquifer_code } "
533+ )
534+ aquifer_type = AquiferType (
535+ thing_aquifer_association = aquifer_assoc ,
536+ aquifer_type = type_name ,
537+ )
538+ session .add (aquifer_type )
539+ aquifer_type_names .append (type_name )
540+ except KeyError :
541+ logger .critical (
542+ f"Unknown aquifer code '{ aquifer_code } ' from AquiferType='{ row .AquiferType } ' "
543+ f"for well { well .name } . Skipping this code."
544+ )
545+ self ._capture_error (
546+ row .PointID ,
547+ f"Unknown aquifer code: { aquifer_code } " ,
548+ "AquiferType" ,
571549 )
572- aquifer_assoc = ThingAquiferAssociation (
573- thing = well , aquifer_system = aquifer
574- )
575- session .add (aquifer_assoc )
576- # session.flush()
577-
578- # Create AquiferType records for EACH characteristic
579- aquifer_type_names = []
580- for aquifer_code in aquifer_codes :
581- try :
582- type_name = lexicon_mapper .map_value (
583- f"LU_AquiferType:{ aquifer_code } "
584- )
585- aquifer_type = AquiferType (
586- thing_aquifer_association = aquifer_assoc ,
587- aquifer_type = type_name ,
588- )
589- session .add (aquifer_type )
590- aquifer_type_names .append (type_name )
591- except KeyError :
592- logger .critical (
593- f"Unknown aquifer code '{ aquifer_code } ' from AquiferType='{ row .AquiferType } ' "
594- f"for well { well .name } . Skipping this code."
595- )
596- self ._capture_error (
597- row .PointID ,
598- f"Unknown aquifer code: { aquifer_code } " ,
599- "AquiferType" ,
600- )
601550
602- logger .info (
603- f"Associated well { well .name } with aquifer { aquifer .name } "
604- f"(types: { ', ' .join (aquifer_type_names )} )"
605- )
606- else :
607- logger .info (
608- f"Well { well .name } already associated with aquifer { aquifer .name } "
609- )
551+ logger .info (
552+ f"Associated well { well .name } with aquifer { aquifer .name } "
553+ f"(types: { ', ' .join (aquifer_type_names )} )"
554+ )
555+ # else:
556+ # logger.info(
557+ # f"Well {well.name} already associated with aquifer {aquifer.name}"
558+ # )
610559 else :
611560 logger .info (f"Failed to create aquifer for well { well .name } " )
612561
562+ # Get or create aquifer system
563+ def _get_or_create_aquifer_system (
564+ self , session : Session , row , aquifer_name : str , primary_type : str
565+ ) -> AquiferSystem | None :
566+ """
567+ Get existing aquifer or create new one if it doesn't exist.
568+
569+ With the new AquiferType model, we create ONE aquifer record per named
570+ aquifer (e.g., one "Santa Fe Group"), not multiple variants.
571+
572+ Args:
573+ session: Database session
574+ aquifer_name: Name of the aquifer (from AqClass or type name)
575+ primary_type: Primary aquifer type for the aquifer_type field
576+ """
577+ # Try to find existing aquifer by name
578+ # aquifer = (
579+ # session.query(AquiferSystem).filter(AquiferSystem.name == aquifer_name).first()
580+ # )
581+ if aquifer_name is None :
582+ return None , False
583+
584+ aquifer = next ((a for a in self ._aquifers if a .name == aquifer_name ), None )
585+ if aquifer :
586+ return aquifer , False
587+
588+ # Create new aquifer
589+ try :
590+ logger .info (
591+ f"Creating new aquifer system: { aquifer_name } (primary type: { primary_type } )"
592+ )
593+
594+ aquifer = AquiferSystem (
595+ name = aquifer_name ,
596+ primary_aquifer_type = primary_type , # Primary type
597+ geographic_scale = None , # Default
598+ )
599+ session .add (aquifer )
600+ session .flush () # Get the ID
601+ return aquifer , True
602+ except DatabaseError as e :
603+ session .rollback ()
604+ self ._capture_database_error (row .PointID , e )
605+ return None , False
606+
613607 def _after_hook (self , session ):
614608 dump_cached_elevations (self ._cached_elevations )
615609
0 commit comments