diff --git a/andes_migrate/biometrie_petoncle.py b/andes_migrate/biometrie_petoncle.py new file mode 100644 index 0000000..44b6306 --- /dev/null +++ b/andes_migrate/biometrie_petoncle.py @@ -0,0 +1,176 @@ + + +from andes_migrate.andes_helper import AndesHelper +from andes_migrate.projet_mollusque import ProjetMollusque +from andes_migrate.table_peche_sentinelle import TablePecheSentinelle + + +class BiometriePetoncle(TablePecheSentinelle): + + def __init__(self, andes_db: AndesHelper, proj: ProjetMollusque, collection_name:str, *args, **kwargs): + # super().__init__(*args, **kwargs) + super().__init__(*args, ref=proj.reference_data, **kwargs) + + self.andes_db = andes_db + self.proj: ProjetMollusque = proj + self.collection_name:str = collection_name + # self.table_name = "TRAIT_MOLLUSQUE" + + # call this form outside + self._init_rows() + + def populate_data(self): + """Populate data: run all getters""" + # secteur trait no taille poids_vif poids_muscle poids_gonade poids_visceres sexe espece comment + if self.collection_name == 'Conserver le spécimen (Biométrie Centre)': + secteur = "Centre" + if self.collection_name == 'Conserver le spécimen (Biométrie Ouest)': + secteur = "Ouest" + # if self.collection_name == 'Conserver un specimen': + # secteur = "16E" + # secteur = "16E" + + # print(self.collection_name) + sexe_dict={'0':"I", + '1':'M', + '2':'F', + } + self.data["id_specimen"] = self._get_current_row_pk() + self.data["secteur"] = secteur + self.data["trait"] = self.get_ident_no_trait() + self.data["no"] = self.get_observation("Code Collection coquille").strip().split("-")[-1] + self.data["taille"] = self.get_observation("Longuer (biométrie)").replace(".",",") + self.data["taille (old)"] = self.get_observation("Longueur").replace(".",",") + self.data["poids_vif"] = self.get_observation("Poids vif").replace(".",",") + self.data["poids_muscle"] = self.get_observation("Poids du muscle").replace(".",",") + self.data["poids_gonade"] = self.get_observation("Poids des gonades").replace(".",",") + self.data["poids_visceres"] = self.get_observation("Poids des viscères").replace(".",",") + self.data["poids_gonade"] = self.get_observation("Poids des gonades").replace(".",",") + self.data["sexe"] = self.get_observation("Sexe") + self.data["comment"] = self.get_comment() + + def __next__(self): + """ + Increment to focus on next row + """ + # print(self.table_name) + # print(f"{self._row_idx} of {len(self._row_list)}") + # print() + if self._row_idx is not None and self._row_list is not None: + if self._row_idx < len(self._row_list): + # increment first, it'l be adjusted in _get_current_row_pk() + self._row_idx += 1 + self.populate_data() + + # self.write_row() + return self.data + else: + raise StopIteration + else: + self.logger.error("Row data not initialise, did you run _init_rows()?") + raise ValueError + + + def _init_rows(self): + """Initialisation method + This queries the Andes DB and creates a list of row entries to be added to the current table + + After running this methods initialises the following attribute: + self._row_list + self._row_idx (hopefully to self._row_idx=0) + + self._row_list will be populated with the specimen ids belonging in the collection + self._row_idx will start at 0 + + """ + query = ( + "SELECT specimen_id " + "FROM ecosystem_survey_observation " + "LEFT JOIN ecosystem_survey_specimen " + "ON ecosystem_survey_observation.specimen_id = ecosystem_survey_specimen.id " + "LEFT JOIN ecosystem_survey_basket " + "ON ecosystem_survey_specimen.basket_id = ecosystem_survey_basket.id " + "LEFT JOIN ecosystem_survey_catch " + "ON ecosystem_survey_basket.catch_id=ecosystem_survey_catch.id " + "LEFT JOIN shared_models_set " + "ON shared_models_set.id=ecosystem_survey_catch.set_id " + "LEFT JOIN shared_models_observationtype " + "ON shared_models_observationtype.id=observation_type_id " + f"WHERE shared_models_set.cruise_id = {self.proj._get_current_row_pk()} " + f"AND (shared_models_observationtype.nom ='{self.collection_name}' AND observation_value=1) " + ) + result = self.andes_db.execute_query(query) + self._assert_not_empty(result) + self._row_list = [specimen[0] for specimen in result] + self._row_idx = 0 + + # @validate_int() + # @log_results + def get_ident_no_trait(self) -> int: + """IDENT_NO_TRAIT INTEGER / NUMBER(5,0) + Numéro séquentiel d'identification du trait + + Andes + ----- + shared_models.set.set_number + """ + specimen_pk = self._get_current_row_pk() + query = ( + "SELECT shared_models_set.set_number " + "FROM ecosystem_survey_specimen " + "LEFT JOIN ecosystem_survey_basket " + "ON ecosystem_survey_specimen.basket_id=ecosystem_survey_basket.id " + "LEFT JOIN ecosystem_survey_catch " + "ON ecosystem_survey_basket.catch_id=ecosystem_survey_catch.id " + "LEFT JOIN shared_models_set " + "ON shared_models_set.id=ecosystem_survey_catch.set_id " + f"WHERE ecosystem_survey_specimen.id={specimen_pk} " + ) + result = self.andes_db.execute_query(query) + self._assert_one(result) + to_return = result[0][0] + return to_return + + + def get_observation(self, name_fr): + specimen_pk = self._get_current_row_pk() + query = ( + "SELECT observation_value " + "FROM ecosystem_survey_observation " + "LEFT JOIN shared_models_observationtype " + "ON ecosystem_survey_observation.observation_type_id=shared_models_observationtype.id " + f"WHERE ecosystem_survey_observation.specimen_id={specimen_pk} " + f"AND shared_models_observationtype.nom='{name_fr}' " + ) + result = self.andes_db.execute_query(query) + try: + self._assert_one(result) + to_return = result[0][0] + except ValueError: + # print('error:', specimen_pk, name_fr,result) + to_return = None + if to_return is None: + to_return="" + return to_return + + def get_comment(self): + specimen_pk = self._get_current_row_pk() + query = ( + "SELECT ecosystem_survey_specimen.comment " + "FROM ecosystem_survey_specimen " + f"WHERE ecosystem_survey_specimen.id={specimen_pk} " + ) + result = self.andes_db.execute_query(query) + self._assert_one(result) + to_return = result[0][0] + if to_return is None: + to_return="" + to_return.replace("\n", " ") + to_return.replace("\r", " ") + return to_return + + + + + + diff --git a/docs/source/contraintes.md b/docs/source/contraintes.md index 99cbc2d..d2f2479 100644 --- a/docs/source/contraintes.md +++ b/docs/source/contraintes.md @@ -30,7 +30,6 @@ Doit corréspondre à une entrée de la table `PROJET_MOLLUSQUE` ayant une valeu Pour les mission pétoncle, un de ces choix: - `Évaluation de stocks IML - Pétoncle I de M` - `Évaluation de stocks IML - Pétoncle Minganie` - Pour les missions buccin: - `Relevé buccin Haute Côte-Nord` @@ -43,6 +42,8 @@ Doit corréspondre à une entrée de la table `TRAIT_MOLLUSQUE` ayant une valeur Pour les mission pétoncle, un de ces choix: - `Îles-de-la-Madeleine` - `Côte-Nord` +Pour les missions buccin: +- `Haute Côte-Nord` Pour les missions buccin: - `Haute Côte-Nord` diff --git a/run.py b/run.py index 91c8df1..8860f56 100644 --- a/run.py +++ b/run.py @@ -1,6 +1,8 @@ +import csv import shutil import pyodbc -import logging +import logging +from andes_migrate.biometrie_petoncle import BiometriePetoncle from andes_migrate.capture_mollusque import CaptureMollusque from andes_migrate.oracle_helper import OracleHelper @@ -20,10 +22,10 @@ # INPUT VALUES -no_notification = "IML-2023-011" -zone = "20" +no_notification = "IML-2024-008E" +zone = "16E" espece = "pétoncle" -SEQ_peche = 151 +SEQ_peche = 0 output_fname = f'./{no_notification}.mdb' shutil.copyfile('andes_migrate/ref_data/access_template.mdb', output_fname) @@ -38,28 +40,56 @@ proj = ProjetMollusque(andes_db, output_cur, ref=ref, zone=zone, no_notif=no_notification, espece=espece) + for p in proj: print(f"Projet: ", p) - trait = TraitMollusque(andes_db, proj, output_cur) - for t in trait: - no_moll = 1 - print(f"Trait: ", t) - engin = EnginMollusque(trait, output_cur) - for e in engin: - # print(f"Engin: ", e) - capture = CaptureMollusque(engin, output_cur) - for c in capture: - # print(f"Capture: ", c) - - freq = FreqLongMollusque(capture, output_cur, no_moll_init=no_moll) - for f in freq: - # print(f"FreqLong: ", f) - # if (c['COD_ESP_GEN'] == 48 or c['COD_ESP_GEN'] == 50) : - no_moll += 1 + + collection_name = 'Conserver un specimen' + # observationgroup_name = 'Biometrie 16E extérieur' + biometrie = BiometriePetoncle(andes_db, proj, collection_name, output_cur) + with open('Biometrie_16E.csv','w') as fp: + writer = csv.DictWriter(fp, lineterminator="\n", fieldnames=["id_specimen", + "secteur", + "trait", + "no", + "taille", + "taille (old)", + "poids_vif", + "poids_muscle", + "poids_gonade", + "poids_visceres", + "poids_gonade", + "sexe", + "comment"]) + writer.writeheader() + for b in biometrie: + # print(b) + if b is not None: + writer.writerow(b) + + + + exit() +# trait = TraitMollusque(andes_db, proj, output_cur) +# for t in trait: +# no_moll = 1 +# print(f"Trait: ", t) +# engin = EnginMollusque(trait, output_cur) +# for e in engin: +# # print(f"Engin: ", e) +# capture = CaptureMollusque(engin, output_cur) +# for c in capture: +# # print(f"Capture: ", c) + +# freq = FreqLongMollusque(capture, output_cur, no_moll_init=no_moll) +# for f in freq: +# # print(f"FreqLong: ", f) +# # if (c['COD_ESP_GEN'] == 48 or c['COD_ESP_GEN'] == 50) : +# no_moll += 1 # monolithic commit if no errors are found -output_cur.commit() +# output_cur.commit() diff --git a/run_IML-2023-011.py b/run_IML-2023-011.py new file mode 100644 index 0000000..a1f6f26 --- /dev/null +++ b/run_IML-2023-011.py @@ -0,0 +1,138 @@ +import csv +import shutil +import pyodbc +import logging +from andes_migrate.biometrie_petoncle import BiometriePetoncle + +from andes_migrate.capture_mollusque import CaptureMollusque +from andes_migrate.oracle_helper import OracleHelper +from andes_migrate.projet_mollusque import ProjetMollusque +from andes_migrate.trait_mollusque import TraitMollusque +from andes_migrate.engin_mollusque import EnginMollusque +from andes_migrate.freq_long_mollusque import FreqLongMollusque +from andes_migrate.andes_helper import AndesHelper + + +logging.basicConfig(level=logging.ERROR) + + +andes_db = AndesHelper() +access_file = 'andes_migrate/ref_data/access_template.mdb' +ref = OracleHelper(access_file=access_file) + + +# INPUT VALUES +no_notification = "IML-2024-008" +zone = "16E" +espece = "pétoncle" +SEQ_peche = 0 + +output_fname = f'./{no_notification}.mdb' +shutil.copyfile('andes_migrate/ref_data/access_template.mdb', output_fname) +con = pyodbc.connect( + f"Driver={{Microsoft Access Driver (*.mdb, *.accdb)}};DBQ={output_fname};" +) +output_cur = con.cursor() + + +# proj = ProjetMollusque(andes_db, output_cur, ref=ref) +# proj.init_input(zone="20", no_releve=34, no_notif=no_notification, espece="pétoncle") +proj = ProjetMollusque(andes_db, output_cur, ref=ref, zone=zone, no_notif=no_notification, espece=espece) + + + +for p in proj: + print(f"Projet: ", p) + + collection_name='Conserver un specimen' + biometrie = BiometriePetoncle(andes_db, proj, collection_name, output_cur) + with open('16E.csv','w') as fp: + writer = csv.DictWriter(fp, lineterminator="\n", fieldnames=["id_specimen", + "secteur", + "trait", + "no", + "taille", + "taille (old)", + "poids_vif", + "poids_muscle", + "poids_gonade", + "poids_visceres", + "poids_gonade", + "sexe", + "comment"]) + writer.writeheader() + for b in biometrie: + if b is not None: + writer.writerow(b) + + + # collection_name='Conserver le spécimen (Biométrie Centre)' + # biometrie = BiometriePetoncle(andes_db, proj, collection_name, output_cur) + # with open('centre.csv','w') as fp: + # writer = csv.DictWriter(fp, lineterminator="\n", fieldnames=["id_specimen", + # "secteur", + # "trait", + # "no", + # "taille", + # "poids_vif", + # "poids_muscle", + # "poids_gonade", + # "poids_visceres", + # "poids_gonade", + # "sexe", + # "comment"]) + # writer.writeheader() + # for b in biometrie: + # writer.writerow(b) + + exit() +# trait = TraitMollusque(andes_db, proj, output_cur) +# for t in trait: +# no_moll = 1 +# print(f"Trait: ", t) +# engin = EnginMollusque(trait, output_cur) +# for e in engin: +# # print(f"Engin: ", e) +# capture = CaptureMollusque(engin, output_cur) +# for c in capture: +# # print(f"Capture: ", c) + +# freq = FreqLongMollusque(capture, output_cur, no_moll_init=no_moll) +# for f in freq: +# # print(f"FreqLong: ", f) +# # if (c['COD_ESP_GEN'] == 48 or c['COD_ESP_GEN'] == 50) : +# no_moll += 1 + + +# monolithic commit if no errors are found +# output_cur.commit() + + + + + +# for i in range(12): +# trait._increment_row() +# print("trait: ", trait.get_ident_no_trait()) + +# statement = trait.get_insert_statement() +# cur.execute(statement) +# cur.commit() + + +# statement = engin.get_insert_statement() +# cur.execute(statement) +# cur.commit() + +# capture.populate_data() +# statement = capture.get_insert_statement() +# cur.execute(statement) +# cur.commit() + +# freq_long = FreqLongMollusque(capture) +# print("iterating...") +# for i in FreqLongMollusque(capture, output_cur): +# print(i) +# # statement = i.get_insert_statement() +# # cur.execute(statement) +# # cur.commit()