-
Notifications
You must be signed in to change notification settings - Fork 41
/
citests_httpdata_manager
executable file
·1204 lines (1024 loc) · 41.2 KB
/
citests_httpdata_manager
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
"""
CI Tests data manager
Use this script to download all possible tests data to be used by the local http server fixture
>>> citests_httpdata_manager --help
>>> citests_httpdata_manager --destination
>>> citests_httpdata_manager -a check
>>> citests_httpdata_manager -a download
>>> citests_httpdata_manager -a download --force
>>> citests_httpdata_manager -a download --force --refresh
>>> citests_httpdata_manager -a dry -t erddap --refresh
>>> citests_httpdata_manager -a download -t erddap
>>> citests_httpdata_manager -a download -t erddap_bgc
>>> citests_httpdata_manager -a download -t ifremer_gdac
>>> citests_httpdata_manager -a download -t ifremer_gdac_bgc
>>> citests_httpdata_manager -a download -t argovis
>>> citests_httpdata_manager -a download -t topo
>>> citests_httpdata_manager -a download -t oops
>>> citests_httpdata_manager -a download -t nvs
>>> citests_httpdata_manager -a download -t docs
>>> citests_httpdata_manager -a download -t ea_dataselection
>>> citests_httpdata_manager -a download -t ifremer_api
>>> citests_httpdata_manager -a download -t github
>>> citests_httpdata_manager -a download -t altim
If some downloads (eg erddap) are difficult:
>>> citests_httpdata_manager -t erddap_bgc -a check # Delete files raising errors
>>> citests_httpdata_manager -t erddap_bgc -a download # Try another download
>>> citests_httpdata_manager -t erddap_bgc -a check # Delete files raising errors, then trying again download
Clean up all data for a fresh start:
>>> citests_httpdata_manager -a clear --refresh
"""
import os
import sys
import argparse
import aiohttp
import asyncio
import aiofiles
import json
from collections import UserList
import pandas as pd
import xarray as xr
from urllib.parse import urlparse, parse_qs, unquote
import logging
import hashlib
import numpy as np
from pathlib import Path
import glob
sys.path.append(os.path.join(os.path.dirname(__file__), ".."))
import argopy
from argopy import DataFetcher, clear_cache
from argopy.related import (
TopoFetcher,
OceanOPSDeployments,
ArgoNVSReferenceTables,
ArgoDocs,
)
log = logging.getLogger("argopy.cli.test_data")
# Where to save remote data to:
DATA_FOLDER = os.path.dirname(os.path.realpath(__file__)).replace(
"cli", "argopy/tests/test_data"
)
# DB file to be used by the http server fixture to load content:
DB_FILE = os.path.join(DATA_FOLDER, "httpmocked_uri_index.json")
# Dictionary mapping of file extension with http data content type
CONTENT_TYPE = {
"js": "application/json",
"json": "application/json",
"yaml": "text/yaml",
"ld+json": "application/ld+json",
"rdf+xml": "application/rdf+xml",
"xml": "application/rdf+xml",
"text/turtle": "text/turtle",
"turtle": "text/turtle",
"nc": "application/x-netcdf",
"ncHeader": "text/plain",
"txt": "text/plain",
"html": "text/html",
"png": "image/png",
"gz": "application/x-gzip",
}
DEFAULT_TARGETS = [
"erddap",
"erddap_bgc",
"ifremer_gdac",
"ifremer_gdac_bgc",
"argovis",
"topo",
"oops",
"nvs",
"docs",
"altim",
"ea_dataselection",
"ifremer_api",
"github",
"altim",
]
start_with = lambda f, x: (
f[0 : len(x)] == x if len(x) <= len(f) else False
) # noqa: E731
def setup_args():
icons_help_string = """CI Tests data manager for the http mocked server"""
parser = argparse.ArgumentParser(
description="argopy http mocked server test data manager",
formatter_class=argparse.RawTextHelpFormatter,
epilog="%s\n(c) Argo-France/Ifremer/LOPS, 2023-2024" % icons_help_string,
)
def action_choicesDescriptions():
return """
download - download all test data
list - list available test data
clear - delete all test data and associated files
dry - list & store URI to be accessed
check - consolidate test data (check file content and possibly remove file if content not expected)
search - search record data for a given url
"""
def action_getChoices():
return ["download", "list", "clear", "dry", "check", "search"]
parser.add_argument(
"-a",
"--action",
choices=action_getChoices(),
help="action to perform among: %s" % action_choicesDescriptions(),
metavar="",
)
def target_choicesDescriptions():
return """
erddap - URIs accessed by ErddapArgoDataFetcher with ds='phy'/'ref' from test_fetchers_data_erddap/Test_Backend and test_xarray_accessor/*
erddap_bgc - URIs accessed by ErddapArgoDataFetcher with ds='bgc'/'bgc-s' ifromn test_fetchers_data_erddap_bgc/Test_Backend
ifremer_gdac - URIs accessed by FTPArgoDataFetcher with ds='phy' from test_fetchers_data_gdac.py/Test_Backend
ifremer_gdac_bgc - URIs accessed by FTPArgoDataFetcher with ds='bgc'/'bgc-s'/'bgc-b'
argovis - URIs accessed by ArgovisDataFetcher from test_fetchers_data_argovis/Test_Backend
topo - URIs accessed by TopoFetcher with ds='gebco' from test_related/Test_TopoFetcher
oops - URIs accessed by OceanOPSDeployments from test_related/Test_OceanOPSDeployments
nvs - URIs accessed by ArgoNVSReferenceTables from test_related/Test_ArgoNVSReferenceTables
docs - URIs accessed by ArgoDocs from test_related/Test_ArgoDocs
altim - URIs accessed by open_sat_altim_report from test_related/Test_open_sat_altim_report
ea_dataselection - URIs from https://dataselection.euro-argo.eu/api/trajectory accessed by test_plot_dashboards and test_related
ifremer_api - URIs from https://api.ifremer.fr
github - URIs from https://github.com/euroargodev/argopy-data/raw/master
"""
def target_getChoices():
return [
"erddap",
"erddap_bgc",
"ifremer_gdac",
"ifremer_gdac_bgc",
"argovis",
"topo",
"oops",
"nvs",
"docs",
"altim",
"ea_dataselection",
"ifremer_api",
"github",
]
parser.add_argument(
"-t",
"--target",
choices=target_getChoices(),
help="HTTP target server among: %s" % target_choicesDescriptions(),
metavar="",
)
parser.add_argument(
"-r",
"--refresh",
help="Refresh URI list if necessary",
action="store_true",
)
parser.add_argument(
"-f",
"--force",
help="Force write on data files even if they already exist",
action="store_true",
)
parser.add_argument(
"-d",
"--destination",
help="Return absolute path to test data storage folder",
action="store_true",
)
parser.add_argument("--url", nargs="?", help="Url to search in database")
return parser
def can_xr_be_opened(src, file):
try:
xr.open_dataset(file)
return src
except:
# print("This source can't be opened with xarray: %s" % src)
return src
def log_file_desc(file, data, src):
size = float(os.stat(file).st_size / (1024 * 1024))
prt_size = lambda x: "Size < 1Mb" if x < 1 else "Size = %0.2dMb" % x
msg = []
# msg.append("- %s, %s" % (file.replace(DATA_FOLDER, '<DATA_FOLDER>'), data[0:3]))
if "erddap.ifremer.fr" in src["uri"]:
if src["ext"] != "json":
msg.append("🤖 ERDDAP: %s" % parse_qs(src["uri"]))
else:
msg.append("🤖 ERDDAP: %s" % src["uri"])
elif "coastwatch.pfeg.noaa.gov/erddap" in src["uri"]:
# msg.append("🤖 NOAA ERDDAP: %s" % urlparse(unquote(src["uri"])).query)
msg.append("🤖 NOAA ERDDAP: %s" % unquote(src["uri"]))
# elif "github.com/euroargodev/argopy-data" in src['uri']:
# msg.append("🤖 GITHUB ARGOPY-DATA: %s" % parse_qs(src["uri"]))
elif start_with(src["uri"], "https://www.ocean-ops.org/api/1"):
# msg.append("🤖 Ocean-OPS API: %s" % unquote(src["uri"]))
msg.append("🤖 Ocean-OPS API: %s" % parse_qs(src["uri"]))
elif start_with(src["uri"], "https://argovis-api.colorado.edu"):
msg.append("🤖 Argovis API: %s" % unquote(src["uri"]))
else:
msg.append("🔸 %s" % src["uri"])
msg.append(
"%s*, %s, %s" % (file.replace(DATA_FOLDER, "")[1:13], data[0:3], prt_size(size))
)
return ", ".join(msg)
# return " 🔸 ".join(msg)
def ls_files(load=True, target=None):
if target is None:
targets = DEFAULT_TARGETS
else:
targets = [target]
L = Lister()
for t in targets:
if Path(DB_FILE).exists():
print("Listing data for '%s':" % t)
with open(DB_FILE, "r") as f:
URIs = json.load(f)
for item in L.load(t, refresh=False):
test_data_file = Path(DATA_FOLDER).joinpath(
"%s.%s" % (item["sha"], item["ext"])
)
for db_item in URIs:
if db_item["uri"] == item["uri"]:
if load:
with open(test_data_file, mode="rb") as file:
data = file.read()
else:
data = "---"
print(log_file_desc(str(test_data_file), data, db_item))
break
class UriRegistry(UserList):
"""
>>> R = UriRegistry(name='My_Registry')
>>> R.exists(uri)
>>> R.commit(uri)
>>> R.delete(uri)
>>> R.save() # to 'My_Registry.json'
>>> R.load() # from 'My_Registry.json'
>>> UriRegistry().commit({'uri': 'a'}) + UriRegistry().commit({'uri': 'b'})
>>> UriRegistry().commit({'uri': 'a'}).commit({'uri': 'b'}) - UriRegistry().commit({'uri': 'a'})
"""
def __init__(
self,
name: str = "UriRegistry",
checking_key: str = "uri",
overwrite: bool = False,
):
self.checking_key = checking_key
self.overwrite = overwrite
self.name = name
super().__init__([])
def exists(self, entry):
for item in self.data:
if item.get(self.checking_key) == entry.get(self.checking_key):
return True
return False
def commit(self, entry, overwrite: bool = None):
overwrite = self.overwrite if overwrite is None else overwrite
if not self.exists(entry):
super().append(entry)
elif overwrite:
self.delete(entry)
super().append(entry)
log.debug("commit: %s" % entry["uri"])
return self
def delete(self, entry):
for item in self.data:
if item.get(self.checking_key) == entry.get(self.checking_key):
super().remove(item)
return self
def save(self, file=None):
file = "%s.json" % self.name if file is None else file
with open(file, "w") as f:
json.dump(self.data, f, indent=4)
return self
def load(self, file=None):
file = "%s.json" % self.name if file is None else file
with open(file, "r") as f:
data = json.load(f)
super().__init__(data)
return self
def __add__(self, obj):
if isinstance(obj, UriRegistry):
for item in obj.data:
self += item
super().append(item)
else:
raise TypeError("Can only add a UriRegistry with another UriRegistry")
return self
def __sub__(self, obj):
if isinstance(obj, UriRegistry):
for item in obj.data:
self.delete(item)
else:
raise TypeError("Can only subtract a UriRegistry from another UriRegistry")
return self
async def place_file(session: aiohttp.ClientSession, source: dict) -> None:
"""Download remote file and save it locally"""
test_data_file = os.path.join(DATA_FOLDER, "%s.%s" % (source["sha"], source["ext"]))
if OVERWRITE or not os.path.exists(test_data_file):
async with session.get(source["uri"], ssl=False, timeout=60 * 10) as r:
if r.content_type not in CONTENT_TYPE.values():
print(
"Unexpected content type (%s) with this GET request: %s (%s extension)"
% (
r.content_type,
source["uri"],
os.path.splitext(urlparse(source["uri"]).path)[1],
)
)
async with aiofiles.open(test_data_file, "wb") as f:
data = await r.content.read(n=-1) # load all read bytes !
# if data[0:3] in [b'Err', b'<!D']:
# raise ValueError(source)
await f.write(data)
print(log_file_desc(f.name, data, source))
return can_xr_be_opened(source, test_data_file)
else:
# print("%s already exists !" % test_data_file)
return can_xr_be_opened(source, test_data_file)
class Lister:
def __init__(self, session: aiohttp.ClientSession = None):
self.session = session
def dfile(self, url: str, fmt: str) -> dict:
"""Return a dictionary to store one URI asset into a UriRegistry instance
For gzip files we should also modify the header: Content-Encoding: gzip
"""
return {
"uri": url,
"ext": fmt,
"sha": hashlib.sha256(url.encode()).hexdigest(),
"type": CONTENT_TYPE[fmt],
"ts": pd.to_datetime("now", utc=True).strftime("%F %T"),
}
def respond(self, URIs: UriRegistry) -> list:
R = UriRegistry(overwrite=False, name="citests_httpdata_manager_%s" % URIs.name)
[R.commit(x) for x in URIs]
R.save()
return R.data
def src(self, target: str = None):
return "citests_httpdata_manager_%s.json" % target
def load(self, target, refresh=False):
if os.path.exists("citests_httpdata_manager_%s.json" % target) and not refresh:
log.debug("Found a URI list for target='%s'" % target)
return UriRegistry(name="citests_httpdata_manager_%s" % target).load().data
else:
log.debug("Refreshing URI list for target='%s'" % target)
return getattr(self, target)
@property
def erddap(self):
"""From test_fetchers_data_erddap.py"""
this_URI = UriRegistry(name="erddap")
# This should correspond to all the possible erddap requests made during CI tests.
# And because of the erddap fetcher N_POINT attribute, we also need to fetch ".ncHeader" on top of ".nc" files
requests_phy = {
"float": [[1901393], [1901393, 6902746]],
"profile": [
[6902746, 34],
[6902746, [1, 12]],
],
"region": [
[-20, -16.0, 0, 1, 0, 100.0],
[-20, -16.0, 0, 1, 0, 100.0, "2004-01-01", "2004-01-31"],
],
}
requests_ref = {
"region": [
[-25, -10, 36, 40, 0, 10.0],
[-25, -10, 36, 40, 0, 10.0, "20180101", "20190101"],
]
}
requests_seq = {"phy": requests_phy, "ref": requests_ref}
requests_par = {
"float": [[1900468, 1900117, 1900386]],
"region": [
[-60, -55, 40.0, 45.0, 0.0, 20.0],
[-60, -55, 40.0, 45.0, 0.0, 20.0, "2007-08-01", "2007-09-01"],
],
}
def add_to_URI(facade):
uri = facade.uri
for ii, url in enumerate(uri):
this_URI.commit(self.dfile(url, "nc"))
this_URI.commit(
self.dfile(
url.replace("." + facade.fetcher.erddap.response, ".ncHeader"),
"ncHeader",
)
)
for ds in requests_seq:
for mode in ["expert", "standard", "research"]:
fetcher = DataFetcher(src="erddap", ds=ds, mode=mode)
for access_point in requests_seq[ds]:
[
add_to_URI(fetcher.profile(*cfg))
for cfg in requests_seq[ds][access_point]
if access_point == "profile"
]
[
add_to_URI(fetcher.float(cfg))
for cfg in requests_seq[ds][access_point]
if access_point == "float"
]
[
add_to_URI(fetcher.region(cfg))
for cfg in requests_seq[ds][access_point]
if access_point == "region"
]
for mode in ["expert", "standard", "research"]:
fetcher = DataFetcher(
src="erddap",
ds="phy",
mode=mode,
parallel=True,
parallel_method="thread",
chunks_maxsize={"lon": 2.5, "lat": 2.5, "wmo": 1},
)
for access_point in requests_par:
[
add_to_URI(fetcher.profile(*cfg))
for cfg in requests_par[access_point]
if access_point == "profile"
]
[
add_to_URI(fetcher.float(cfg))
for cfg in requests_par[access_point]
if access_point == "float"
]
[
add_to_URI(fetcher.region(cfg))
for cfg in requests_par[access_point]
if access_point == "region"
]
# Add more URI accessed by the erddap data fetcher:
this_URI.commit(
self.dfile(
"https://erddap.ifremer.fr/erddap/info/ArgoFloats/index.json", "json"
)
)
this_URI.commit(
self.dfile(
"https://erddap.ifremer.fr/erddap/info/ArgoFloats-reference/index.json",
"json",
)
)
this_URI.commit(
self.dfile("https://erddap.ifremer.fr/erddap/info/index.json", "json")
)
return self.respond(this_URI)
@property
def erddap_bgc(self):
"""From test_fetchers_data_erddap_bgc.py"""
this_URI = UriRegistry(name="erddap_bgc")
# This should correspond to all the possible erddap requests made during CI tests.
# And because of the erddap fetcher N_POINT attribute, we also need to fetch ".ncHeader" on top of ".nc" files
requests_seq = {
# "float": [[6904240]],
"float": [[5903248], [6904240], [5903248, 6904241]],
"profile": [
[5903248, 34],
[5903248, np.arange(12, 14)],
[5903248, [1, 12]],
],
"region": [
[-55, -47, 55, 57, 0, 10],
[-55, -47, 55, 57, 0, 10, "2022-05-1", "2023-07-01"],
],
}
requests_par = {
"float": [[5903248, 6904241]],
"region": [
[-55, -47, 55, 57, 0, 10, "2022-05-1", "2023-07-01"],
],
}
def add_to_URI(facade):
uri = facade.uri
for ii, url in enumerate(uri):
this_URI.commit(self.dfile(url, "nc"))
this_URI.commit(
self.dfile(
url.replace("." + facade.fetcher.erddap.response, ".ncHeader"),
"ncHeader",
)
)
uri = facade.fetcher.fs.urls_registry
for ii, url in enumerate(uri):
suffix = Path(urlparse(url).path).suffix
if suffix == ".gz":
this_URI.commit(self.dfile(url, "gz"))
clear_cache()
indexfs = argopy.ArgoIndex(
index_file="argo_synthetic-profile_index.txt", # corresponds to dataset in the erddap
cache=True,
)
for mode in ["expert", "standard", "research"]:
for measured in [None, "all", "DOXY"]:
for params in ["all", "DOXY"]:
fetcher = DataFetcher(
src="erddap",
ds="bgc",
cache=True,
mode=mode,
params=params,
measured=measured,
indexfs=indexfs,
)
for access_point in requests_seq:
[
add_to_URI(fetcher.profile(*cfg))
for cfg in requests_seq[access_point]
if access_point == "profile"
]
[
add_to_URI(fetcher.float(cfg))
for cfg in requests_seq[access_point]
if access_point == "float"
]
[
add_to_URI(fetcher.region(cfg))
for cfg in requests_seq[access_point]
if access_point == "region"
]
measured = None
for mode in ["expert", "standard", "research"]:
for params in ["all", "DOXY"]:
fetcher = DataFetcher(
src="erddap",
ds="bgc",
cache=True,
parallel=True,
parallel_method="thread",
chunks_maxsize={"lon": 2.5, "lat": 2.5, "wmo": 1},
mode=mode,
params=params,
measured=measured,
indexfs=indexfs,
)
for access_point in requests_par:
[
add_to_URI(fetcher.profile(*cfg))
for cfg in requests_par[access_point]
if access_point == "profile"
]
[
add_to_URI(fetcher.float(cfg))
for cfg in requests_par[access_point]
if access_point == "float"
]
[
add_to_URI(fetcher.region(cfg))
for cfg in requests_par[access_point]
if access_point == "region"
]
# Add more URI from the erddap:
this_URI.commit(
self.dfile(
"https://erddap.ifremer.fr/erddap/info/ArgoFloats-synthetic-BGC/index.json",
"json",
)
)
return self.respond(this_URI)
@property
def ifremer_gdac(self):
"""From test_fetchers_data_gdac.py"""
this_URI = UriRegistry(name="ifremer_gdac")
server = "https://data-argo.ifremer.fr"
requests_seq = {
"float": [[13857]],
"profile": [[13857, 90]],
"region": [
[-20, -16.0, 0, 1, 0, 100.0],
[-20, -16.0, 0, 1, 0, 100.0, "1997-07-01", "1997-09-01"],
],
}
def add_to_URI(facade):
[this_URI.commit(self.dfile(uri, "nc")) for uri in facade.uri]
fetcher = DataFetcher(src="gdac", gdac=server, ds="phy", MAX_FILES=100)
for access_point in requests_seq:
[
add_to_URI(fetcher.profile(*cfg))
for cfg in requests_seq[access_point]
if access_point == "profile"
]
[
add_to_URI(fetcher.float(cfg))
for cfg in requests_seq[access_point]
if access_point == "float"
]
[
add_to_URI(fetcher.region(cfg))
for cfg in requests_seq[access_point]
if access_point == "region"
]
# Add more URI from the IFREMER GDAC:
this_URI.commit(self.dfile("%s/ar_index_global_prof.txt.gz" % server, "gz"))
this_URI.commit(self.dfile("%s/dac" % server, "html"))
return self.respond(this_URI)
@property
def ifremer_gdac_bgc(self):
this_URI = UriRegistry(name="ifremer_gdac_bgc")
server = "https://data-argo.ifremer.fr"
requests_seq = {
"float": [[5904989], [3902131], [6903247]],
"profile": [[5904989, 12]],
}
def add_to_URI(facade):
[this_URI.commit(self.dfile(uri, "nc")) for uri in facade.uri]
fetcher = DataFetcher(
src="gdac", gdac=server, ds="bgc", mode="expert", MAX_FILES=100
)
for access_point in requests_seq:
[
add_to_URI(fetcher.profile(*cfg))
for cfg in requests_seq[access_point]
if access_point == "profile"
]
[
add_to_URI(fetcher.float(cfg))
for cfg in requests_seq[access_point]
if access_point == "float"
]
[
add_to_URI(fetcher.region(cfg))
for cfg in requests_seq[access_point]
if access_point == "region"
]
# Add more URI from the IFREMER GDAC:
this_URI.commit(self.dfile("%s/argo_bio-profile_index.txt.gz" % server, "gz"))
this_URI.commit(
self.dfile("%s/argo_synthetic-profile_index.txt.gz" % server, "gz")
)
return self.respond(this_URI)
@property
def argovis(self):
this_URI = UriRegistry(name="argovis")
requests_phy = {
"float": [[1901393], [1901393, 6902746]],
"profile": [
[6902746, 34],
[6902746, [1, 12]],
],
"region": [
[-70, -65, 35.0, 40.0, 0, 10.0, "2012-01", "2012-03"],
[-70, -65, 35.0, 40.0, 0, 10.0, "2012-01", "2012-06"],
],
}
requests_seq = {"phy": requests_phy}
requests_par = {
"float": [[1900468, 1900117, 1900386]],
"region": [
[-70, -65, 35.0, 40.0, 0.0, 10.0, "2012-01", "2012-06"],
[-60, -55, 40.0, 45.0, 0.0, 20.0, "2007-08-01", "2007-09-01"],
],
}
def add_to_URI(facade):
encode = facade.fetcher.url_encode
[this_URI.commit(self.dfile(uri, "js")) for uri in encode(facade.uri)]
for ds in requests_seq:
fetcher = DataFetcher(src="argovis", ds=ds)
for access_point in requests_seq[ds]:
[
add_to_URI(fetcher.profile(*cfg))
for cfg in requests_seq[ds][access_point]
if access_point == "profile"
]
[
add_to_URI(fetcher.float(cfg))
for cfg in requests_seq[ds][access_point]
if access_point == "float"
]
[
add_to_URI(fetcher.region(cfg))
for cfg in requests_seq[ds][access_point]
if access_point == "region"
]
fetcher = DataFetcher(
src="argovis", parallel=True, chunks_maxsize={"lon": 2.5, "lat": 2.5}
)
for access_point in requests_par:
[
add_to_URI(fetcher.profile(*cfg))
for cfg in requests_par[access_point]
if access_point == "profile"
]
[
add_to_URI(fetcher.float(cfg))
for cfg in requests_par[access_point]
if access_point == "float"
]
[
add_to_URI(fetcher.region(cfg))
for cfg in requests_par[access_point]
if access_point == "region"
]
# Add more URI from the ARGOVIS server:
this_URI.commit(self.dfile("https://argovis-api.colorado.edu/ping", "json"))
return self.respond(this_URI)
@property
def topo(self):
"""https://coastwatch.pfeg.noaa.gov/erddap"""
this_URI = UriRegistry(name="topo")
box = [81, 123, -67, -54]
fetcher = TopoFetcher(box, ds="gebco", stride=[10, 10], cache=True)
this_URI.commit(self.dfile(unquote(fetcher.uri[0]), "nc"))
return self.respond(this_URI)
@property
def oops(self):
this_URI = UriRegistry(name="oops")
scenarios = [
# (None, False), # Can't be handled by the mocked server (test date is surely different from the test data date)
# ([-90, 0, 0, 90], False), # Can't be handled by the mocked server (test date is surely different from the test data date)
([-90, 0, 0, 90, "2022-01"], True),
([-90, 0, 0, 90, "2022-01"], False),
([None, 0, 0, 90, "2022-01-01", "2023-01-01"], True),
([None, 0, 0, 90, "2022-01-01", "2023-01-01"], False),
([-90, None, 0, 90, "2022-01-01", "2023-01-01"], True),
([-90, None, 0, 90, "2022-01-01", "2023-01-01"], False),
([-90, 0, None, 90, "2022-01-01", "2023-01-01"], True),
([-90, 0, None, 90, "2022-01-01", "2023-01-01"], False),
([-90, 0, 0, None, "2022-01-01", "2023-01-01"], True),
([-90, 0, 0, None, "2022-01-01", "2023-01-01"], False),
([-90, 0, 0, 90, None, "2023-01-01"], True),
([-90, 0, 0, 90, None, "2023-01-01"], False),
([-90, 0, 0, 90, "2022-01-01", None], True),
([-90, 0, 0, 90, "2022-01-01", None], False),
]
for sc in scenarios:
box, deployed_only = sc
oops = OceanOPSDeployments(box, deployed_only=deployed_only)
this_URI.commit(self.dfile(unquote(oops.uri), "json"))
this_URI.commit(
self.dfile("https://www.ocean-ops.org/api/1/oceanops-api.yaml", "yaml")
)
return self.respond(this_URI)
@property
def nvs(self):
this_URI = UriRegistry(name="nvs")
nvs = ArgoNVSReferenceTables()
for rtid in nvs.valid_ref:
url = nvs.get_url(rtid)
this_URI.commit(self.dfile(unquote(url), "json"))
fmts = {"ld+json": "json", "rdf+xml": "xml", "text/turtle": "txt"}
for fmt in fmts.keys():
url = nvs.get_url(3, fmt=fmt)
this_URI.commit(self.dfile(unquote(url), fmts[fmt]))
return self.respond(this_URI)
@property
def docs(self):
this_URI = UriRegistry(name="docs")
docs = ArgoDocs().list
for ii, doc in docs.iterrows():
Ad = ArgoDocs(doc["id"])
doi = "https://dx.doi.org/%s" % Ad.js["doi"]
this_URI.commit(self.dfile(unquote(doi), "html"))
return self.respond(this_URI)
@property
def altim(self):
# Used by Test_open_sat_altim_report
this_URI = UriRegistry(name="altim")
server = "https://data-argo.ifremer.fr"
for scenario in [[2901623], [2901623, 6901929]]:
for wmo in scenario:
url = "%s/etc/argo-ast9-item13-AltimeterComparison/figures/%i.png" % (
server,
wmo,
)
this_URI.commit(self.dfile(url, "png"))
return self.respond(this_URI)
@property
def ea_dataselection(self):
this_URI = UriRegistry(name="ea_dataselection")
WMO_list = []
WMO_list.append(6901929) # Used by test_related
WMO_list.append(5904797) # Used by test_plot_dashboards.py
WMO_list.append(6902755) # Used by test_plot_dashboards.py
WMO_list.append(
2901623
) # Used by test_fetchers_facade_data.py::Test_Facade::test_to_index_coriolis
for uri in [
"https://dataselection.euro-argo.eu/api/trajectory/%i" % wmo
for wmo in WMO_list
]:
this_URI.commit(self.dfile(uri, "json"))
return self.respond(this_URI)
@property
def ifremer_api(self):
this_URI = UriRegistry(name="ifremer_api")
repo = "https://api.ifremer.fr"
uris = ["argopy/data/ARGO-FULL.json", "argopy/data/ARGO-BGC.json"]
for uri in uris:
file_extension = uri.split(".")[-1]
this_URI.commit(self.dfile("%s/%s" % (repo, uri), file_extension))
return self.respond(this_URI)
@property
def github(self):
this_URI = UriRegistry(name="github")
repo = "https://github.com/euroargodev/argopy-data/raw/master"
uris = [
"ftp/dac/csiro/5900865/5900865_prof.nc",
"ftp/ar_index_global_prof.txt",
"ftp/dac/csiro/5900865/profiles/D5900865_001.nc",
"ftp/dac/csiro/5900865/profiles/D5900865_002.nc",
]
for uri in uris:
file_extension = uri.split(".")[-1]
this_URI.commit(self.dfile("%s/%s" % (repo, uri), file_extension))
return self.respond(this_URI)
async def fetch_download_links(session: aiohttp.ClientSession, target=None):
"""Gather the list of URIs to download
The return list is a list of dictionaries with all the necessary keys to save and
retrieve requests offline using the fixture of the local HTTP server
Lister(session).target
"""
URI = []
if target is None:
targets = DEFAULT_TARGETS
else:
targets = [target]
L = Lister(session)
for t in targets:
[URI.append(link) for link in L.load(t, refresh=REFRESH)]
# Return the list of dictionaries
log.debug("Listed %i URIs for %i targets" % (len(URI), len(targets)))
return URI
async def download(target=None):
connector = aiohttp.TCPConnector(limit=1, force_close=True)
async with aiohttp.ClientSession(
connector=connector, timeout=aiohttp.ClientTimeout(total=60 * 10)
) as session:
urls = await fetch_download_links(session, target=target)
return await asyncio.gather(*[place_file(session, url) for url in urls])
async def dry_download(target=None):
connector = aiohttp.TCPConnector(limit=1, force_close=True)
async with aiohttp.ClientSession(
connector=connector, timeout=aiohttp.ClientTimeout(total=60 * 10)
) as session:
return await fetch_download_links(session, target=target)
def clear_data(target=None):