From e2afc80e1d66348b1254e6f4c560716c029f57f0 Mon Sep 17 00:00:00 2001 From: pietersmit Date: Mon, 28 Mar 2022 15:21:11 -0700 Subject: [PATCH 1/2] Included the option to ask for: - Barometer Data - Pressure Data --- src/pysofar/sofar.py | 86 +++++++++++++++++++++++++++++------------- src/pysofar/spotter.py | 7 +++- 2 files changed, 66 insertions(+), 27 deletions(-) diff --git a/src/pysofar/sofar.py b/src/pysofar/sofar.py index dc17f97..a45a0a1 100644 --- a/src/pysofar/sofar.py +++ b/src/pysofar/sofar.py @@ -20,6 +20,7 @@ class SofarApi(SofarConnection): """ Class for interfacing with the Sofar Wavefleet API """ + def __init__(self, custom_token=None): if custom_token is not None: super().__init__(custom_token) @@ -74,11 +75,13 @@ def grab_datafile(self, spotter_id: str, start_date: str, end_date: str): file_url = response['fileUrl'] if status != "complete": - raise CouldNotRetrieveFile(f"File creation not yet complete. Try {file_url} in a little bit") + raise CouldNotRetrieveFile( + f"File creation not yet complete. Try {file_url} in a little bit") # downloading the file file_name = f"{spotter_id}_{start_date}_{end_date}" - with urllib.request.urlopen(file_url) as response, open(file_name, 'wb') as out_file: + with urllib.request.urlopen(file_url) as response, open(file_name, + 'wb') as out_file: shutil.copyfileobj(response, out_file) return f"{file_name} downloaded successfully" @@ -121,7 +124,7 @@ def get_sensor_data(self, spotter_id: str, start_date: str, end_date: str): :return: Data as a json from the requested spotter """ - + params = { "spotterId": spotter_id, "startDate": start_date, @@ -163,7 +166,8 @@ def update_spotter_name(self, spotter_id, new_spotter_name): return new_spotter_name # ---------------------------------- Multi Spotter Endpoints -------------------------------------- # - def get_wave_data(self, start_date: str = None, end_date: str = None, params: dict = None): + def get_wave_data(self, start_date: str = None, end_date: str = None, + params: dict = None): """ Get all wave data for related spotters @@ -175,7 +179,8 @@ def get_wave_data(self, start_date: str = None, end_date: str = None, params: di """ return self._get_all_data(['waves'], start_date, end_date, params) - def get_wind_data(self, start_date: str = None, end_date: str = None, params: dict = None): + def get_wind_data(self, start_date: str = None, end_date: str = None, + params: dict = None): """ Get all wind data for related spotters @@ -187,7 +192,8 @@ def get_wind_data(self, start_date: str = None, end_date: str = None, params: di """ return self._get_all_data(['wind'], start_date, end_date, params) - def get_frequency_data(self, start_date: str = None, end_date: str = None, params: dict = None): + def get_frequency_data(self, start_date: str = None, end_date: str = None, + params: dict = None): """ Get all Frequency data for related spotters @@ -199,7 +205,8 @@ def get_frequency_data(self, start_date: str = None, end_date: str = None, param """ return self._get_all_data(['frequency'], start_date, end_date, params) - def get_track_data(self, start_date: str = None, end_date: str = None, params: dict = None): + def get_track_data(self, start_date: str = None, end_date: str = None, + params: dict = None): """ Get all track data for related spotters @@ -211,7 +218,8 @@ def get_track_data(self, start_date: str = None, end_date: str = None, params: d """ return self._get_all_data(['track'], start_date, end_date, params) - def get_all_data(self, start_date: str = None, end_date: str = None, params: dict = None): + def get_all_data(self, start_date: str = None, end_date: str = None, + params: dict = None): """ Get all data for related spotters @@ -221,9 +229,11 @@ def get_all_data(self, start_date: str = None, end_date: str = None, params: dic :return: Data as a list """ - return self._get_all_data(['waves', 'wind', 'frequency', 'track'], start_date, end_date, params) + return self._get_all_data(['waves', 'wind', 'frequency', 'track'], + start_date, end_date, params) - def get_spotters(self): return get_and_update_spotters(_api=self) + def get_spotters(self): + return get_and_update_spotters(_api=self) # ---------------------------------- Helper Functions -------------------------------------- # @property @@ -268,7 +278,8 @@ def _device_radius(self): return spot_data - def _get_all_data(self, worker_names: list, start_date: str = None, end_date: str = None, params: dict = None): + def _get_all_data(self, worker_names: list, start_date: str = None, + end_date: str = None, params: dict = None): # helper function to return another function used for grabbing all data from spotters in a period def helper(_name): _ids = self.device_ids @@ -299,7 +310,8 @@ class WaveDataQuery(SofarConnection): """ _MISSING = object() - def __init__(self, spotter_id: str, limit: int = 20, start_date=_MISSING, end_date=_MISSING, params=None): + def __init__(self, spotter_id: str, limit: int = 20, start_date=_MISSING, + end_date=_MISSING, params=None): """ Query the Sofar api for spotter data @@ -333,7 +345,9 @@ def __init__(self, spotter_id: str, limit: int = 20, start_date=_MISSING, end_da 'includeFrequencyData': 'false', 'includeDirectionalMoments': 'false', 'includeSurfaceTempData': 'false', - 'includeNonObs': 'false' + 'includeNonObs': 'false', + 'includeMicrophoneData': 'false', + 'includeBarometerData': 'false' } if params is not None: self._params.update(params) @@ -368,6 +382,20 @@ def limit(self, value: int): self._limit = value self._params.update({'limit': value}) + def barometer(self, include: bool): + """ + + :param include: True if you want the query to include waves + """ + self._params.update({'includeBarometerData': str(include).lower()}) + + def microphone(self, include: bool): + """ + + :param include: True if you want the query to include waves + """ + self._params.update({'includeMicrophoneData': str(include).lower()}) + def waves(self, include: bool): """ @@ -408,8 +436,8 @@ def directional_moments(self, include: bool): Since the query does not include frequency data (of which directional moments are a subset) the data you have requested will not be included. \n Please set includeFrequencyData to true with .frequency(True) if desired. \n""") - self._params.update({'includeDirectionalMoments': str(include).lower()}) - + self._params.update( + {'includeDirectionalMoments': str(include).lower()}) def surface_temp(self, include: bool): """ @@ -471,16 +499,19 @@ def clear_end_date(self): del self._params['endDate'] def __str__(self): - s = f"Query for {self.spotter_id} \n" +\ - f" Start: {self.start_date or 'From Beginning'} \n" +\ - f" End: {self.end_date or 'Til Present'} \n" +\ - " Params:\n" +\ - f" id: {self._params['spotterId']}\n" +\ - f" limit: {self._params['limit']} \n" +\ - f" waves: {self._params['includeWaves']} \n" +\ - f" wind: {self._params['includeWindData']} \n" +\ - f" track: {self._params['includeTrack']} \n" +\ - f" frequency: {self._params['includeFrequencyData']} \n" +\ + s = f"Query for {self.spotter_id} \n" + \ + f" Start: {self.start_date or 'From Beginning'} \n" + \ + f" End: {self.end_date or 'Til Present'} \n" + \ + " Params:\n" + \ + f" id: {self._params['spotterId']}\n" + \ + f" limit: {self._params['limit']} \n" + \ + f" waves: {self._params['includeWaves']} \n" + \ + f" wind: {self._params['includeWindData']} \n" + \ + f" barometer: {self._params['includeBarometerData']} \n" + \ + f" sst: {self._params['includeSurfaceTempData']} \n" + \ + f" microphone: {self._params['includeMicrophoneData']} \n" + \ + f" track: {self._params['includeTrack']} \n" + \ + f" frequency: {self._params['includeFrequencyData']} \n" + \ f" directional_moments: {self._params['includeDirectionalMoments']} \n" return s @@ -539,7 +570,9 @@ def worker_wrapper(args): :return: All data for that type for all spotters in the queried period """ worker_type, _ids, st_date, end_date, params = args - queries = [WaveDataQuery(_id, limit=500, start_date=st_date, end_date=end_date, params=params) for _id in _ids] + queries = [ + WaveDataQuery(_id, limit=500, start_date=st_date, end_date=end_date, + params=params) for _id in _ids] # grabbing data from all of the spotters in parallel pool = ThreadPool(processes=16) @@ -564,6 +597,7 @@ def _worker(data_type): :return: A helper function able to process a query for that specific data type """ + def _helper(data_query): st = data_query.start_date end = data_query.end_date diff --git a/src/pysofar/spotter.py b/src/pysofar/spotter.py index 62e45e7..c4e89e5 100644 --- a/src/pysofar/spotter.py +++ b/src/pysofar/spotter.py @@ -238,11 +238,14 @@ def grab_data(self, limit: int = 20, include_track: bool = False, include_frequency_data: bool = False, include_directional_moments: bool = False, include_surface_temp_data: bool = False, + include_barometer_data=False, + include_microphone_data=False, smooth_wave_data: bool = False, smooth_sg_window: int = 135, smooth_sg_order: int = 4, interpolate_utc: bool = False, - interpolate_period_seconds: int = 3600): + interpolate_period_seconds: int = 3600 + ): """ Grabs the requested data for this spotter based on the given keyword arguments @@ -270,6 +273,8 @@ def grab_data(self, limit: int = 20, _query.frequency(include_frequency_data) _query.directional_moments(include_directional_moments) _query.surface_temp(include_surface_temp_data) + _query.barometer(include_barometer_data) + _query.microphone(include_microphone_data) _query.smooth_wave_data(smooth_wave_data) _query.smooth_sg_window(smooth_sg_window) _query.smooth_sg_order(smooth_sg_order) From aaeaa832e6095864f9c98dc4f1e80480652bf2fc Mon Sep 17 00:00:00 2001 From: pietersmit Date: Mon, 28 Mar 2022 15:57:59 -0700 Subject: [PATCH 2/2] Bumped the version number --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 242b9a2..2a25510 100644 --- a/setup.py +++ b/setup.py @@ -6,7 +6,7 @@ setuptools.setup( name='pysofar', - version='0.1.10', + version='0.1.11', license='Apache 2 Licesnse', install_requires=[ 'requests',