11import datetime
22import logging
3- import re
43import uuid
54from dataclasses import dataclass
65from pathlib import Path
76from typing import Literal
87
8+ import cloudnet_api_client as cac
99from cloudnetpy .exceptions import PlottingError
1010from cloudnetpy .model_evaluation .plotting .plotting import generate_L3_day_plots
1111from cloudnetpy .plotting import Dimensions , PlotParameters , generate_figure
@@ -87,11 +87,13 @@ def __init__(
8787 storage_api : StorageApi ,
8888 pid_utils : PidUtils ,
8989 dvas : Dvas ,
90+ client : cac .APIClient ,
9091 ):
9192 self .md_api = md_api
9293 self .storage_api = storage_api
9394 self .pid_utils = pid_utils
9495 self .dvas = dvas
96+ self .client = client
9597
9698 def get_site (self , site_id : str ) -> Site :
9799 site = self .md_api .get (f"api/sites/{ site_id } " )
@@ -214,6 +216,7 @@ def download_instrument(
214216 else :
215217 start_date , end_date = date
216218 start_date_ext , end_date_ext = start_date , end_date
219+
217220 if time_offset is not None :
218221 if largest_only :
219222 raise ValueError ("Cannot use both time_offset and largest_only" )
@@ -223,54 +226,49 @@ def download_instrument(
223226 start_date_ext -= datetime .timedelta (days = 1 )
224227 elif time_offset > TIMEDELTA_ZERO :
225228 end_date_ext += datetime .timedelta (days = 1 )
226- payload = self ._get_payload (
227- site = site_id ,
228- date = (start_date_ext , end_date_ext ),
229- instrument = instrument_id ,
229+
230+ metadata = self .client .raw_metadata (
231+ site_id ,
232+ date_from = start_date_ext ,
233+ date_to = end_date_ext ,
234+ instrument_id = instrument_id ,
230235 instrument_pid = instrument_pid ,
231- skip_created = True ,
232236 filename_prefix = filename_prefix ,
233237 filename_suffix = filename_suffix ,
238+ status = ["uploaded" , "processed" ],
234239 )
235- upload_metadata = self .md_api .get ("api/raw-files" , payload )
236- if include_pattern is not None :
237- upload_metadata = _include_records_with_pattern_in_filename (
238- upload_metadata , include_pattern
240+ if include_pattern :
241+ metadata = self .client .filter (metadata , include_pattern = include_pattern )
242+ if exclude_pattern :
243+ metadata = self .client .filter (metadata , exclude_pattern = exclude_pattern )
244+ if include_tag_subset :
245+ metadata = self .client .filter (
246+ metadata , include_tag_subset = include_tag_subset
239247 )
240- if exclude_pattern is not None :
241- upload_metadata = _exclude_records_with_pattern_in_filename (
242- upload_metadata , exclude_pattern
248+ if exclude_tag_subset :
249+ metadata = self . client . filter (
250+ metadata , exclude_tag_subset = exclude_tag_subset
243251 )
244- if include_tag_subset is not None :
245- upload_metadata = [
246- record
247- for record in upload_metadata
248- if include_tag_subset .issubset (set (record ["tags" ]))
249- ]
250- if exclude_tag_subset is not None :
251- upload_metadata = [
252- record
253- for record in upload_metadata
254- if not exclude_tag_subset .issubset (set (record ["tags" ]))
255- ]
256- if not upload_metadata :
252+
253+ if not metadata :
257254 if allow_empty :
258255 return [], []
259256 else :
260257 raise utils .RawDataMissingError
258+
261259 if largest_only :
262- upload_metadata = [max (upload_metadata , key = lambda item : int (item ["size" ]))]
263- full_paths , uuids = self .storage_api .download_raw_data (
264- upload_metadata , directory
265- )
260+ metadata = [max (metadata , key = lambda item : int (item .size ))]
261+
262+ full_paths = self .client .download (metadata , directory , progress = False )
263+ uuids = [f .uuid for f in metadata ]
264+
266265 if time_offset is not None :
267266 uuids = [
268- meta ["uuid" ]
269- for meta in upload_metadata
270- if start_date
271- <= datetime .date .fromisoformat (meta ["measurementDate" ])
272- <= end_date
267+ meta .uuid
268+ for meta in metadata
269+ if start_date <= meta .measurement_date <= end_date
273270 ]
271+
274272 if largest_only :
275273 return full_paths [0 ], uuids
276274 return full_paths , uuids
@@ -563,24 +561,6 @@ def _dimensions2dict(dimensions: Dimensions) -> dict:
563561 }
564562
565563
566- def _include_records_with_pattern_in_filename (metadata : list , pattern : str ) -> list :
567- """Includes only records with certain pattern."""
568- return [
569- row
570- for row in metadata
571- if re .search (pattern , row ["filename" ], flags = re .IGNORECASE )
572- ]
573-
574-
575- def _exclude_records_with_pattern_in_filename (metadata : list , pattern : str ) -> list :
576- """Excludes records with certain pattern."""
577- return [
578- row
579- for row in metadata
580- if not re .search (pattern , row ["filename" ], flags = re .IGNORECASE )
581- ]
582-
583-
584564def _full_product_to_l3_product (full_product : str ):
585565 """Returns l3 product name."""
586566 return full_product .split ("-" )[1 ]
0 commit comments