55
66import matplotlib .pyplot as plt
77import numpy as np
8+ from cloudnet_api_client .client import APIClient
89from cloudnetpy .plotting .plotting import Dimensions
910from doppy .raw import HaloSysParams
1011from doppy .raw .halo_bg import HaloBg
2526from monitoring .product import MonitoringProduct , MonitoringVariable
2627from monitoring .utils import (
2728 RawFilesDatePayload ,
28- get_api_client ,
2929 get_storage_api ,
3030 instrument_uuid_to_pid ,
3131)
32+ from processing .storage_api import StorageApi
3233
3334
3435def monitor (
35- period : PeriodType , product : MonitoringProduct , site : str , instrument_uuid : str
36+ period : PeriodType ,
37+ product : MonitoringProduct ,
38+ site : str ,
39+ instrument_uuid : str ,
40+ api_client : APIClient ,
41+ storage_api : StorageApi ,
3642) -> None :
3743 match product .id :
3844 case "halo-doppler-lidar_housekeeping" :
39- monitor_housekeeping (period , product , site , instrument_uuid )
45+ monitor_housekeeping (
46+ period , product , site , instrument_uuid , api_client , storage_api
47+ )
4048 case "halo-doppler-lidar_background" :
41- monitor_background (period , product , site , instrument_uuid )
49+ monitor_background (
50+ period , product , site , instrument_uuid , api_client , storage_api
51+ )
4252 case "halo-doppler-lidar_signal" :
43- monitor_signal (period , product , site , instrument_uuid )
53+ monitor_signal (
54+ period , product , site , instrument_uuid , api_client , storage_api
55+ )
4456
4557
4658def monitor_housekeeping (
47- period : PeriodType , product : MonitoringProduct , site : str , instrument_uuid : str
59+ period : PeriodType ,
60+ product : MonitoringProduct ,
61+ site : str ,
62+ instrument_uuid : str ,
63+ api_client : APIClient ,
64+ storage_api : StorageApi ,
4865) -> None :
49- client = get_api_client ()
50- storage_api = get_storage_api ()
51- pid = instrument_uuid_to_pid (client , instrument_uuid )
66+ pid = instrument_uuid_to_pid (api_client , instrument_uuid )
5267 date_opts : RawFilesDatePayload = {}
5368 if not isinstance (period , All ):
5469 start , stop = period .to_interval_padded (days = 31 )
5570 date_opts = {"date_from" : start , "date_to" : stop }
5671
57- records = client .raw_files (
72+ records = api_client .raw_files (
5873 site_id = site ,
5974 instrument_pid = pid ,
6075 filename_prefix = "system_parameters_" ,
@@ -117,22 +132,28 @@ def plot_housekeeping_variable(
117132 format_time_axis (ax )
118133 pretty_ax (ax , grid = "y" )
119134 fig_ = save_fig (fig )
120- return MonitoringVisualization (fig_ .bytes , variable , Dimensions (fig , [ax ]))
135+ vis = MonitoringVisualization (fig_ .bytes , variable , Dimensions (fig , [ax ]))
136+ plt .close (fig )
137+ return vis
121138
122139
123140def monitor_background (
124- period : PeriodType , product : MonitoringProduct , site : str , instrument_uuid : str
141+ period : PeriodType ,
142+ product : MonitoringProduct ,
143+ site : str ,
144+ instrument_uuid : str ,
145+ api_client : APIClient ,
146+ storage_api : StorageApi ,
125147) -> None :
126- client = get_api_client ()
127148 storage_api = get_storage_api ()
128- pid = instrument_uuid_to_pid (client , instrument_uuid )
149+ pid = instrument_uuid_to_pid (api_client , instrument_uuid )
129150
130151 date_opts : RawFilesDatePayload = {}
131152 if not isinstance (period , All ):
132153 start , stop = period .to_interval_padded (days = 1 )
133154 date_opts = {"date_from" : start , "date_to" : stop }
134155
135- records = client .raw_files (
156+ records = api_client .raw_files (
136157 site_id = site ,
137158 instrument_pid = pid ,
138159 filename_prefix = "Background_" ,
@@ -211,7 +232,9 @@ def plot_background_profile(
211232 format_time_axis (ax )
212233 pretty_ax_2d (ax )
213234 fig_ = save_fig (fig )
214- return MonitoringVisualization (fig_ .bytes , variable , Dimensions (fig , [ax ]))
235+ vis = MonitoringVisualization (fig_ .bytes , variable , Dimensions (fig , [ax ]))
236+ plt .close (fig )
237+ return vis
215238
216239
217240def plot_background_variance (
@@ -224,7 +247,9 @@ def plot_background_variance(
224247 format_time_axis (ax )
225248 pretty_ax (ax , grid = "y" )
226249 fig_ = save_fig (fig )
227- return MonitoringVisualization (fig_ .bytes , variable , Dimensions (fig , [ax ]))
250+ vis = MonitoringVisualization (fig_ .bytes , variable , Dimensions (fig , [ax ]))
251+ plt .close (fig )
252+ return vis
228253
229254
230255def plot_time_averaged_background_profile (
@@ -244,22 +269,27 @@ def plot_time_averaged_background_profile(
244269
245270 pretty_ax (ax , grid = "y" )
246271 fig_ = save_fig (fig )
247- return MonitoringVisualization (fig_ .bytes , variable , Dimensions (fig , [ax ]))
272+ vis = MonitoringVisualization (fig_ .bytes , variable , Dimensions (fig , [ax ]))
273+ plt .close (fig )
274+ return vis
248275
249276
250277def monitor_signal (
251- period : PeriodType , product : MonitoringProduct , site : str , instrument_uuid : str
278+ period : PeriodType ,
279+ product : MonitoringProduct ,
280+ site : str ,
281+ instrument_uuid : str ,
282+ api_client : APIClient ,
283+ storage_api : StorageApi ,
252284) -> None :
253- client = get_api_client ()
254- storage_api = get_storage_api ()
255- pid = instrument_uuid_to_pid (client , instrument_uuid )
285+ pid = instrument_uuid_to_pid (api_client , instrument_uuid )
256286
257287 date_opts : RawFilesDatePayload = {}
258288 if not isinstance (period , All ):
259289 start , stop = period .to_interval ()
260290 date_opts = {"date_from" : start , "date_to" : stop }
261291
262- records = client .raw_files (
292+ records = api_client .raw_files (
263293 site_id = site ,
264294 instrument_pid = pid ,
265295 filename_suffix = ".hpl" ,
@@ -353,7 +383,9 @@ def plot_radial_velocity_histogram(
353383 ax .set_ylabel ("Count" )
354384 pretty_ax (ax , grid = "both" )
355385 fig_ = save_fig (fig )
356- return MonitoringVisualization (fig_ .bytes , variable , Dimensions (fig , [ax ]))
386+ vis = MonitoringVisualization (fig_ .bytes , variable , Dimensions (fig , [ax ]))
387+ plt .close (fig )
388+ return vis
357389
358390
359391def _compute_radial_velocity_bins (raw : HaloHpl ) -> list [float ] | int :
@@ -382,4 +414,6 @@ def plot_signal_radial_velocity(
382414 ax .set_ylabel ("Radial velocity" )
383415 pretty_ax (ax )
384416 fig_ = save_fig (fig )
385- return MonitoringVisualization (fig_ .bytes , variable , Dimensions (fig , [ax ]))
417+ vis = MonitoringVisualization (fig_ .bytes , variable , Dimensions (fig , [ax ]))
418+ plt .close (fig )
419+ return vis
0 commit comments