Skip to content

Commit

Permalink
sizeclass and aphia filters for Capture
Browse files Browse the repository at this point in the history
  • Loading branch information
davidsean committed Sep 11, 2024
1 parent 10fe7e1 commit 6cc2587
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 30 deletions.
6 changes: 3 additions & 3 deletions andes_migrate/biometrie_mollusque.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,14 @@ def populate_data(self):
self.data['VOLUME_GONADE_P'] = self.get_volume_gonade_p()
self.data['NO_CHARGEMENT'] = self.get_no_chargement()

@tag(HardCoded)
def get_cod_esp_gen(self) -> int:
"""COD_ESP_GEN INTEGER / NUMBER(5,0)
Identification de l'espèce capturée tel que défini dans la table ESPECE_GENERAL
Extrait de la capture ::func:`~andes_migrate.capture_mollusque.CapturenMollusque.get_cod_esp_gen`
hard-coded to whelk eggs: 2151
"""
return self.capture.get_cod_esp_gen()
return self._hard_coded_result(2151)

def get_cod_eng_gen(self) -> int:
"""COD_ENG_GEN INTEGER / NUMBER(5,0)
Expand Down
60 changes: 34 additions & 26 deletions andes_migrate/capture_mollusque.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,17 @@ class CaptureMollusque(TablePecheSentinelle):
Object model representing the CAPTURE_MOLLUSQUE table
"""

def __init__(self, engin: EnginMollusque, *args, **kwargs):
def __init__(self, engin: EnginMollusque, *args, aphia_id_filter=None, size_class_filter=None, **kwargs):

super().__init__(*args, ref=engin.reference_data, **kwargs)

self.engin: EnginMollusque = engin
self.table_name = "CAPTURE_MOLLUSQUE"

self.andes_db = engin.andes_db
self.data = {}

self.aphia_id_filter = aphia_id_filter
self.size_class_filter = size_class_filter
self._init_rows()

def _init_rows(self):
Expand All @@ -53,26 +55,28 @@ def _init_rows(self):
# "ORDER BY ecosystem_survey_catch.id ASC;"
# )

# HACK! only choose aphia IDs for commercial target species
placopecten_magellanicus = 156972
chlamys_islandica = 140692
buccinum_undatum = 138878
if 'pétoncle'==self.engin.trait.proj.espece:
species_filter = f"AND (shared_models_species.aphia_id = {placopecten_magellanicus} OR shared_models_species.aphia_id = {chlamys_islandica}) "
elif 'buccin'==self.engin.trait.proj.espece:
# NEED other bucinums, glacial, teranovae etc
species_filter = f"AND (shared_models_species.aphia_id = {buccinum_undatum}) "
else:
print("Problem filtering species, need aphia-ids for extraction")
raise ValueError


# DOUBLE HACK! only choose catches that contain baskets that are NOT an NA size-class (shared_models_sizeclass.code=0)
# some NA baskets can still exist in a catch that has a non-NA basket
# The NA basket filter is probably not needed...
# also do not consider basket class 9 (biodiversity basket)
size_class_code_NA = 0
size_class_code_biodiversity = 9
# create a SQL query to filter by aphia ids
aphia_id_filter_query = None
if self.aphia_id_filter:
aphia_id_filter_query='AND ('
# the first one
aphia_id_filter_query += f"shared_models_species.aphia_id = {self.aphia_id_filter[0]}"
# the rest
for aphia_id in self.aphia_id_filter[1:]:
aphia_id_filter_query += f" OR shared_models_species.aphia_id = {aphia_id}"
aphia_id_filter_query+=') '

# create a SQL query to filter by size class codes
size_class_filter_query = None
if self.size_class_filter:
size_class_filter_query='AND ('
# the first one
size_class_filter_query += f"shared_models_sizeclass.code={self.size_class_filter[0]}"
# the rest
for size_class_code in self.size_class_filter[1:]:
size_class_filter_query += f" OR shared_models_sizeclass.code={size_class_code}"
size_class_filter_query+=') '

query = (
"SELECT DISTINCT ecosystem_survey_catch.id "
Expand All @@ -87,17 +91,15 @@ def _init_rows(self):
"LEFT JOIN shared_models_cruise "
"ON shared_models_cruise.sampling_protocol_id = shared_models_sizeclass.sampling_protocol_id "
f"WHERE shared_models_cruise.id={self.engin.trait.proj._get_current_row_pk()} "
f"AND NOT shared_models_sizeclass.code={size_class_code_NA} "
f"AND NOT shared_models_sizeclass.code={size_class_code_biodiversity} "
f"AND ecosystem_survey_catch.set_id={self.engin.trait._get_current_row_pk()} "
f"{species_filter} "
f"{size_class_filter_query if size_class_filter_query else ''} "
f"{aphia_id_filter_query if aphia_id_filter_query else ''} "
"ORDER BY ecosystem_survey_catch.id ASC "
)

result = self.andes_db.execute_query(query)
# a set could be empty,
# self._assert_not_empty(result)

# a list of all the catch pk's (need to unpack a bit)
self._row_list = [catch[0] for catch in result]
self._row_idx = 0
Expand Down Expand Up @@ -186,6 +188,13 @@ def get_cod_esp_gen(self) -> int:
"""
oeufs_buccin_size_class = 3
espece = "buccin"
# HACK for whelk eggs,
if self.engin.trait.proj.espece=="buccin" and self.size_class_filter and oeufs_buccin_size_class in self.size_class_filter:
self.logger.info("cod_es_gen hack for whelk eggs")
return self._hard_coded_result(2151)

query = (
"SELECT shared_models_species.id, shared_models_species.aphia_id, shared_models_species.code "
"FROM ecosystem_survey_catch "
Expand All @@ -199,7 +208,6 @@ def get_cod_esp_gen(self) -> int:
andes_id = result[0][0]
andes_aphia_id = result[0][1]
andes_code = result[0][2]
# print( andes_id, andes_aphia_id, andes_code)

# try to match an aphia_id
self.logger.info(
Expand Down
14 changes: 14 additions & 0 deletions make_access_IML-2024008.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,26 @@
access_file = 'andes_migrate/ref_data/access_template.mdb'
ref = OracleHelper(access_file=access_file)

placopecten_magellanicus = 156972
chlamys_islandica = 140692
buccinum_undatum = 138878

na_size_class = 0
claquette_ouverte=2
vivant_intact_size_class = 1
vivant_brisé_size_class = 2
oeufs_Buccin_size_class = 3
predateur_size_class = 4
biodiversite_size_class = 9


# INPUT VALUES
no_notification = "IML-2024-008F"
zone = "16F"
espece = "pétoncle"
SEQ_peche = 151
aphia_id_filter = [placopecten_magellanicus, chlamys_islandica]
size_class_filter = [vivant_intact_size_class, claquette_ouverte]

output_fname = f'./{no_notification}.mdb'
shutil.copyfile('andes_migrate/ref_data/access_template.mdb', output_fname)
Expand Down
15 changes: 15 additions & 0 deletions make_access_IML-2024009.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,27 @@
access_file = 'andes_migrate/ref_data/access_template.mdb'
ref = OracleHelper(access_file=access_file)

placopecten_magellanicus = 156972
chlamys_islandica = 140692
buccinum_undatum = 138878

na_size_class = 0
claquette_ouverte=2
vivant_intact_size_class = 1
vivant_brisé_size_class = 2
oeufs_Buccin_size_class = 3
predateur_size_class = 4
biodiversite_size_class = 9


# INPUT VALUES
no_notification = "IML-2024-009"
zone = "20"
espece = "pétoncle"
SEQ_peche = 151
aphia_id_filter = [placopecten_magellanicus, chlamys_islandica]
size_class_filter = [vivant_intact_size_class, claquette_ouverte]


output_fname = f'./{no_notification}.mdb'
shutil.copyfile('andes_migrate/ref_data/access_template.mdb', output_fname)
Expand Down
25 changes: 24 additions & 1 deletion make_access_IML-2024022.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,27 @@
access_file = 'andes_migrate/ref_data/access_template.mdb'
ref = OracleHelper(access_file=access_file)

# HACK! only choose aphia IDs for commercial target species
placopecten_magellanicus = 156972
chlamys_islandica = 140692
buccinum_undatum = 138878

na_size_class = 0
claquette_ouverte=2
vivant_intact_size_class = 1
vivant_brisé_size_class = 2
oeufs_buccin_size_class = 3
predateur_size_class = 4
biodiversite_size_class = 9

# INPUT VALUES
no_notification = "IML-2024-022"
zone = None
espece = "buccin"
SEQ_peche = 151
aphia_id_filter = [buccinum_undatum]
size_class_filter = [vivant_intact_size_class, vivant_brisé_size_class]


output_fname = f'./{no_notification}.mdb'
shutil.copyfile('andes_migrate/ref_data/access_template.mdb', output_fname)
Expand Down Expand Up @@ -55,7 +70,10 @@
engin = EnginMollusque(trait, output_cur)
for e in engin:
# print(f"Engin: ", e)
capture = CaptureMollusque(engin, output_cur)
# capture for freq-long
capture = CaptureMollusque(engin, output_cur,
aphia_id_filter=aphia_id_filter,
size_class_filter=[vivant_intact_size_class, vivant_brisé_size_class])
for c in capture:
# print(f"Capture: ", c)

Expand All @@ -65,6 +83,11 @@
# if (c['COD_ESP_GEN'] == 48 or c['COD_ESP_GEN'] == 50) :
no_moll_freq_long += 1

# capture for biometrie (whelk eggs)
capture = CaptureMollusque(engin, output_cur,
aphia_id_filter=aphia_id_filter,
size_class_filter=[oeufs_buccin_size_class])
for c in capture:
biometrie = BiometrieMollusque(capture, output_cur, no_moll_init=no_moll_biometrie)
for b in biometrie:
print(f"biometrie: ", b)
Expand Down

0 comments on commit 6cc2587

Please sign in to comment.