Skip to content

Commit 7697d7f

Browse files
authored
Merge pull request #2 from iml-gddaiss/biometrie_hack
Biometrie hack
2 parents 6cc2587 + 5136c41 commit 7697d7f

File tree

4 files changed

+367
-22
lines changed

4 files changed

+367
-22
lines changed

andes_migrate/biometrie_petoncle.py

+176
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
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+

docs/source/contraintes.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ Doit corréspondre à une entrée de la table `PROJET_MOLLUSQUE` ayant une valeu
3030
Pour les mission pétoncle, un de ces choix:
3131
- `Évaluation de stocks IML - Pétoncle I de M`
3232
- `Évaluation de stocks IML - Pétoncle Minganie`
33-
3433
Pour les missions buccin:
3534
- `Relevé buccin Haute Côte-Nord`
3635

@@ -43,6 +42,8 @@ Doit corréspondre à une entrée de la table `TRAIT_MOLLUSQUE` ayant une valeur
4342
Pour les mission pétoncle, un de ces choix:
4443
- `Îles-de-la-Madeleine`
4544
- `Côte-Nord`
45+
Pour les missions buccin:
46+
- `Haute Côte-Nord`
4647

4748
Pour les missions buccin:
4849
- `Haute Côte-Nord`

run.py

+51-21
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
import csv
12
import shutil
23
import pyodbc
3-
import logging
4+
import logging
5+
from andes_migrate.biometrie_petoncle import BiometriePetoncle
46

57
from andes_migrate.capture_mollusque import CaptureMollusque
68
from andes_migrate.oracle_helper import OracleHelper
@@ -20,10 +22,10 @@
2022

2123

2224
# INPUT VALUES
23-
no_notification = "IML-2023-011"
24-
zone = "20"
25+
no_notification = "IML-2024-008E"
26+
zone = "16E"
2527
espece = "pétoncle"
26-
SEQ_peche = 151
28+
SEQ_peche = 0
2729

2830
output_fname = f'./{no_notification}.mdb'
2931
shutil.copyfile('andes_migrate/ref_data/access_template.mdb', output_fname)
@@ -38,28 +40,56 @@
3840
proj = ProjetMollusque(andes_db, output_cur, ref=ref, zone=zone, no_notif=no_notification, espece=espece)
3941

4042

43+
4144
for p in proj:
4245
print(f"Projet: ", p)
43-
trait = TraitMollusque(andes_db, proj, output_cur)
44-
for t in trait:
45-
no_moll = 1
46-
print(f"Trait: ", t)
47-
engin = EnginMollusque(trait, output_cur)
48-
for e in engin:
49-
# print(f"Engin: ", e)
50-
capture = CaptureMollusque(engin, output_cur)
51-
for c in capture:
52-
# print(f"Capture: ", c)
53-
54-
freq = FreqLongMollusque(capture, output_cur, no_moll_init=no_moll)
55-
for f in freq:
56-
# print(f"FreqLong: ", f)
57-
# if (c['COD_ESP_GEN'] == 48 or c['COD_ESP_GEN'] == 50) :
58-
no_moll += 1
46+
47+
collection_name = 'Conserver un specimen'
48+
# observationgroup_name = 'Biometrie 16E extérieur'
49+
biometrie = BiometriePetoncle(andes_db, proj, collection_name, output_cur)
50+
with open('Biometrie_16E.csv','w') as fp:
51+
writer = csv.DictWriter(fp, lineterminator="\n", fieldnames=["id_specimen",
52+
"secteur",
53+
"trait",
54+
"no",
55+
"taille",
56+
"taille (old)",
57+
"poids_vif",
58+
"poids_muscle",
59+
"poids_gonade",
60+
"poids_visceres",
61+
"poids_gonade",
62+
"sexe",
63+
"comment"])
64+
writer.writeheader()
65+
for b in biometrie:
66+
# print(b)
67+
if b is not None:
68+
writer.writerow(b)
69+
70+
71+
72+
exit()
73+
# trait = TraitMollusque(andes_db, proj, output_cur)
74+
# for t in trait:
75+
# no_moll = 1
76+
# print(f"Trait: ", t)
77+
# engin = EnginMollusque(trait, output_cur)
78+
# for e in engin:
79+
# # print(f"Engin: ", e)
80+
# capture = CaptureMollusque(engin, output_cur)
81+
# for c in capture:
82+
# # print(f"Capture: ", c)
83+
84+
# freq = FreqLongMollusque(capture, output_cur, no_moll_init=no_moll)
85+
# for f in freq:
86+
# # print(f"FreqLong: ", f)
87+
# # if (c['COD_ESP_GEN'] == 48 or c['COD_ESP_GEN'] == 50) :
88+
# no_moll += 1
5989

6090

6191
# monolithic commit if no errors are found
62-
output_cur.commit()
92+
# output_cur.commit()
6393

6494

6595

0 commit comments

Comments
 (0)