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