Mercurial > repos > ecology > iabiodiv_smartbiodiv_med_environ
comparison med_environ.py @ 0:f82f266381be draft
planemo upload for repository https://github.com/jeremyfix/medenv commit 6068531bc50297a9c8f2c24e748d7d791fe5f2ba
author | ecology |
---|---|
date | Thu, 15 Feb 2024 20:01:29 +0000 |
parents | |
children | b79faea33a8a |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:f82f266381be |
---|---|
1 # coding: utf-8 | |
2 | |
3 # Standard imports | |
4 import argparse | |
5 import os | |
6 import pathlib | |
7 from datetime import datetime | |
8 | |
9 # External imports | |
10 import medenv | |
11 | |
12 import pandas as pd | |
13 | |
14 | |
15 def environment_observation( | |
16 row, | |
17 fetcher, | |
18 lat_key, | |
19 long_key, | |
20 depth_key, | |
21 date_key, | |
22 tol_spatial_coordinates, | |
23 verbose, | |
24 ): | |
25 lat0 = ( | |
26 row[lat_key] - tol_spatial_coordinates / 2.0, | |
27 row[lat_key] + tol_spatial_coordinates / 2.0, | |
28 ) | |
29 long0 = ( | |
30 row[long_key] - tol_spatial_coordinates / 2.0, | |
31 row[long_key] + tol_spatial_coordinates / 2.0, | |
32 ) | |
33 depth = row[depth_key] | |
34 date = datetime.strptime(row[date_key], "%Y-%m-%dT%H:%M:%SZ") | |
35 | |
36 values, info_values = fetcher.get_values(date, (long0, lat0), depth) | |
37 | |
38 if verbose: | |
39 print( | |
40 f"Was fetching for date={date}, long={long0}, lat={lat0}," | |
41 f"depth={depth} and got {values} at the coordinates {info_values}" | |
42 ) | |
43 for fname, fvalue in values.items(): | |
44 row[fname] = fvalue | |
45 return row | |
46 | |
47 | |
48 def environment_dataset(args): | |
49 # Note: galaxy is given us, for boolean parameters, the string | |
50 # "true" or the string "false" that we need to convert to a bool | |
51 verbose = True if args.verbose == "true" else False | |
52 | |
53 if args.keyfile is not None and os.path.exists(args.keyfile): | |
54 with open(args.keyfile, "r") as fh: | |
55 key_lines = fh.readlines() | |
56 cmems_username = key_lines[0].split(":")[1].strip() | |
57 cmems_password = key_lines[1].split(":")[1].strip() | |
58 os.environ["CMEMS_USERNAME"] = cmems_username | |
59 os.environ["CMEMS_PASSWORD"] = cmems_password | |
60 | |
61 features = args.variables.split(",") | |
62 fetcher = medenv.Fetcher(features, reduction="mean") | |
63 | |
64 # Loads the input dataset | |
65 df = pd.read_csv(args.datafile, sep="\t") | |
66 lat_key = df.columns[args.lat_key] | |
67 long_key = df.columns[args.long_key] | |
68 depth_key = df.columns[args.depth_key] | |
69 date_key = df.columns[args.date_key] | |
70 print(lat_key, long_key, depth_key, date_key) | |
71 | |
72 df = df.apply( | |
73 environment_observation, | |
74 args=( | |
75 fetcher, | |
76 lat_key, | |
77 long_key, | |
78 depth_key, | |
79 date_key, | |
80 args.tol_spatial_coordinates, | |
81 verbose, | |
82 ), | |
83 axis=1, | |
84 ) | |
85 | |
86 df.to_csv(args.out_file, header=True, index=False, sep="\t") | |
87 | |
88 | |
89 def __main__(): | |
90 parser = argparse.ArgumentParser() | |
91 parser.add_argument("--datafile", type=pathlib.Path, required=True) | |
92 parser.add_argument("--out_file", type=pathlib.Path, required=True) | |
93 parser.add_argument("--lat_key", type=int, required=True) | |
94 parser.add_argument("--long_key", type=int, required=True) | |
95 parser.add_argument("--date_key", type=int, required=True) | |
96 parser.add_argument("--depth_key", type=int, required=True) | |
97 parser.add_argument("--variables", type=str, required=True) | |
98 parser.add_argument("--tol_spatial_coordinates", type=float, required=True) | |
99 parser.add_argument("--keyfile", type=pathlib.Path, default=None) | |
100 parser.add_argument("--verbose", type=str, default=False) | |
101 args = parser.parse_args() | |
102 | |
103 environment_dataset(args) | |
104 | |
105 | |
106 if __name__ == "__main__": | |
107 __main__() |