|
| 1 | + |
| 2 | + |
| 3 | +from andes_migrate.andes_helper import AndesHelper |
| 4 | +from andes_migrate.projet_mollusque import ProjetMollusque |
| 5 | +from andes_migrate.table_peche_sentinelle import TablePecheSentinelle |
| 6 | + |
| 7 | + |
| 8 | +class BiometriePetoncle(TablePecheSentinelle): |
| 9 | + |
| 10 | + def __init__(self, andes_db: AndesHelper, proj: ProjetMollusque, collection_name:str, *args, **kwargs): |
| 11 | + # super().__init__(*args, **kwargs) |
| 12 | + super().__init__(*args, ref=proj.reference_data, **kwargs) |
| 13 | + |
| 14 | + self.andes_db = andes_db |
| 15 | + self.proj: ProjetMollusque = proj |
| 16 | + self.collection_name:str = collection_name |
| 17 | + # self.table_name = "TRAIT_MOLLUSQUE" |
| 18 | + |
| 19 | + # call this form outside |
| 20 | + self._init_rows() |
| 21 | + |
| 22 | + def populate_data(self): |
| 23 | + """Populate data: run all getters""" |
| 24 | + # secteur trait no taille poids_vif poids_muscle poids_gonade poids_visceres sexe espece comment |
| 25 | + if self.collection_name == 'Conserver le spécimen (Biométrie Centre)': |
| 26 | + secteur = "Centre" |
| 27 | + if self.collection_name == 'Conserver le spécimen (Biométrie Ouest)': |
| 28 | + secteur = "Ouest" |
| 29 | + # if self.collection_name == 'Conserver un specimen': |
| 30 | + # secteur = "16E" |
| 31 | + # secteur = "16E" |
| 32 | + |
| 33 | + # print(self.collection_name) |
| 34 | + sexe_dict={'0':"I", |
| 35 | + '1':'M', |
| 36 | + '2':'F', |
| 37 | + } |
| 38 | + self.data["id_specimen"] = self._get_current_row_pk() |
| 39 | + self.data["secteur"] = secteur |
| 40 | + self.data["trait"] = self.get_ident_no_trait() |
| 41 | + self.data["no"] = self.get_observation("Code Collection coquille").strip().split("-")[-1] |
| 42 | + self.data["taille"] = self.get_observation("Longuer (biométrie)").replace(".",",") |
| 43 | + self.data["taille (old)"] = self.get_observation("Longueur").replace(".",",") |
| 44 | + self.data["poids_vif"] = self.get_observation("Poids vif").replace(".",",") |
| 45 | + self.data["poids_muscle"] = self.get_observation("Poids du muscle").replace(".",",") |
| 46 | + self.data["poids_gonade"] = self.get_observation("Poids des gonades").replace(".",",") |
| 47 | + self.data["poids_visceres"] = self.get_observation("Poids des viscères").replace(".",",") |
| 48 | + self.data["poids_gonade"] = self.get_observation("Poids des gonades").replace(".",",") |
| 49 | + self.data["sexe"] = self.get_observation("Sexe") |
| 50 | + self.data["comment"] = self.get_comment() |
| 51 | + |
| 52 | + def __next__(self): |
| 53 | + """ |
| 54 | + Increment to focus on next row |
| 55 | + """ |
| 56 | + # print(self.table_name) |
| 57 | + # print(f"{self._row_idx} of {len(self._row_list)}") |
| 58 | + # print() |
| 59 | + if self._row_idx is not None and self._row_list is not None: |
| 60 | + if self._row_idx < len(self._row_list): |
| 61 | + # increment first, it'l be adjusted in _get_current_row_pk() |
| 62 | + self._row_idx += 1 |
| 63 | + self.populate_data() |
| 64 | + |
| 65 | + # self.write_row() |
| 66 | + return self.data |
| 67 | + else: |
| 68 | + raise StopIteration |
| 69 | + else: |
| 70 | + self.logger.error("Row data not initialise, did you run _init_rows()?") |
| 71 | + raise ValueError |
| 72 | + |
| 73 | + |
| 74 | + def _init_rows(self): |
| 75 | + """Initialisation method |
| 76 | + This queries the Andes DB and creates a list of row entries to be added to the current table |
| 77 | +
|
| 78 | + After running this methods initialises the following attribute: |
| 79 | + self._row_list |
| 80 | + self._row_idx (hopefully to self._row_idx=0) |
| 81 | +
|
| 82 | + self._row_list will be populated with the specimen ids belonging in the collection |
| 83 | + self._row_idx will start at 0 |
| 84 | +
|
| 85 | + """ |
| 86 | + query = ( |
| 87 | + "SELECT specimen_id " |
| 88 | + "FROM ecosystem_survey_observation " |
| 89 | + "LEFT JOIN ecosystem_survey_specimen " |
| 90 | + "ON ecosystem_survey_observation.specimen_id = ecosystem_survey_specimen.id " |
| 91 | + "LEFT JOIN ecosystem_survey_basket " |
| 92 | + "ON ecosystem_survey_specimen.basket_id = ecosystem_survey_basket.id " |
| 93 | + "LEFT JOIN ecosystem_survey_catch " |
| 94 | + "ON ecosystem_survey_basket.catch_id=ecosystem_survey_catch.id " |
| 95 | + "LEFT JOIN shared_models_set " |
| 96 | + "ON shared_models_set.id=ecosystem_survey_catch.set_id " |
| 97 | + "LEFT JOIN shared_models_observationtype " |
| 98 | + "ON shared_models_observationtype.id=observation_type_id " |
| 99 | + f"WHERE shared_models_set.cruise_id = {self.proj._get_current_row_pk()} " |
| 100 | + f"AND (shared_models_observationtype.nom ='{self.collection_name}' AND observation_value=1) " |
| 101 | + ) |
| 102 | + result = self.andes_db.execute_query(query) |
| 103 | + self._assert_not_empty(result) |
| 104 | + self._row_list = [specimen[0] for specimen in result] |
| 105 | + self._row_idx = 0 |
| 106 | + |
| 107 | + # @validate_int() |
| 108 | + # @log_results |
| 109 | + def get_ident_no_trait(self) -> int: |
| 110 | + """IDENT_NO_TRAIT INTEGER / NUMBER(5,0) |
| 111 | + Numéro séquentiel d'identification du trait |
| 112 | +
|
| 113 | + Andes |
| 114 | + ----- |
| 115 | + shared_models.set.set_number |
| 116 | + """ |
| 117 | + specimen_pk = self._get_current_row_pk() |
| 118 | + query = ( |
| 119 | + "SELECT shared_models_set.set_number " |
| 120 | + "FROM ecosystem_survey_specimen " |
| 121 | + "LEFT JOIN ecosystem_survey_basket " |
| 122 | + "ON ecosystem_survey_specimen.basket_id=ecosystem_survey_basket.id " |
| 123 | + "LEFT JOIN ecosystem_survey_catch " |
| 124 | + "ON ecosystem_survey_basket.catch_id=ecosystem_survey_catch.id " |
| 125 | + "LEFT JOIN shared_models_set " |
| 126 | + "ON shared_models_set.id=ecosystem_survey_catch.set_id " |
| 127 | + f"WHERE ecosystem_survey_specimen.id={specimen_pk} " |
| 128 | + ) |
| 129 | + result = self.andes_db.execute_query(query) |
| 130 | + self._assert_one(result) |
| 131 | + to_return = result[0][0] |
| 132 | + return to_return |
| 133 | + |
| 134 | + |
| 135 | + def get_observation(self, name_fr): |
| 136 | + specimen_pk = self._get_current_row_pk() |
| 137 | + query = ( |
| 138 | + "SELECT observation_value " |
| 139 | + "FROM ecosystem_survey_observation " |
| 140 | + "LEFT JOIN shared_models_observationtype " |
| 141 | + "ON ecosystem_survey_observation.observation_type_id=shared_models_observationtype.id " |
| 142 | + f"WHERE ecosystem_survey_observation.specimen_id={specimen_pk} " |
| 143 | + f"AND shared_models_observationtype.nom='{name_fr}' " |
| 144 | + ) |
| 145 | + result = self.andes_db.execute_query(query) |
| 146 | + try: |
| 147 | + self._assert_one(result) |
| 148 | + to_return = result[0][0] |
| 149 | + except ValueError: |
| 150 | + # print('error:', specimen_pk, name_fr,result) |
| 151 | + to_return = None |
| 152 | + if to_return is None: |
| 153 | + to_return="" |
| 154 | + return to_return |
| 155 | + |
| 156 | + def get_comment(self): |
| 157 | + specimen_pk = self._get_current_row_pk() |
| 158 | + query = ( |
| 159 | + "SELECT ecosystem_survey_specimen.comment " |
| 160 | + "FROM ecosystem_survey_specimen " |
| 161 | + f"WHERE ecosystem_survey_specimen.id={specimen_pk} " |
| 162 | + ) |
| 163 | + result = self.andes_db.execute_query(query) |
| 164 | + self._assert_one(result) |
| 165 | + to_return = result[0][0] |
| 166 | + if to_return is None: |
| 167 | + to_return="" |
| 168 | + to_return.replace("\n", " ") |
| 169 | + to_return.replace("\r", " ") |
| 170 | + return to_return |
| 171 | + |
| 172 | + |
| 173 | + |
| 174 | + |
| 175 | + |
| 176 | + |
0 commit comments