Skip to content

Commit

Permalink
Changed some inputs to kwargs and other formatting changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
smithara committed Aug 21, 2018
1 parent 9c381a2 commit 42e9617
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 10 deletions.
1 change: 1 addition & 0 deletions viresclient/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,6 @@
from ._client_swarm import SwarmRequest
from ._client_aeolus import AeolusRequest
from ._data_handling import ReturnedData
from ._data_handling import ReturnedDataGroup

__version__ = "0.2.0"
20 changes: 12 additions & 8 deletions viresclient/_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def get_log_level(level):


def wps_xml_request(templatefile, inputs):
"""Creates a WPS request object that can later be executed
"""Renders a WPS request template (xml) that can later be executed
Args:
templatefile (str): Name of the xml template file
Expand Down Expand Up @@ -151,7 +151,7 @@ def write(self, message):
This isn't working right in notebooks.
Could be fixed by using tqdm_notebook, but this doesn't work with
regular terminals and also messes up the bar_format
regular terminals and also messes up the bar_format at present.
"""
self.tqdm_pbar.write(message)

Expand Down Expand Up @@ -278,7 +278,7 @@ def _copy_progress(copied, total):
return pbar.update(100*copied/total)
return _copy_progress

def _write_response(file_obj):
def write_response(file_obj):
"""Acts on a file object to copy it to another file
https://stackoverflow.com/a/7244263
file_obj is what is returned from urllib.urlopen()
Expand All @@ -290,13 +290,13 @@ def _write_response(file_obj):
total=size
)

def _write_response_without_reporting(file_obj):
def write_response_without_reporting(file_obj):
copyfileobj(file_obj, out_file)

if show_progress:
return _write_response
return write_response
else:
return _write_response_without_reporting
return write_response_without_reporting

@staticmethod
def _chunkify_request(start_time, end_time, sampling_step):
Expand Down Expand Up @@ -347,10 +347,11 @@ def _get(self, request=None, asynchronous=None, response_handler=None,
"""Make a request and handle response according to response_handler
Args:
request: the rendered xml request
request: the rendered xml request, from wps_xml_request
asynchronous (bool): True for asynchronous processing,
False for synchronous
response_handler: a function that handles the server response
message (str): Message to be added to the progress bar
"""
try:
if asynchronous:
Expand All @@ -371,7 +372,7 @@ def _get(self, request=None, asynchronous=None, response_handler=None,
"Server error - may be outside of product availability?"
)

def get_between(self, start_time, end_time,
def get_between(self, start_time=None, end_time=None,
filetype="cdf", asynchronous=True):
"""Make the server request and download the data.
Expand All @@ -386,6 +387,9 @@ def get_between(self, start_time, end_time,
ReturnedData object
"""
if not (isinstance(start_time, datetime) &
isinstance(end_time, datetime)):
raise TypeError("start_time and end_time must be datetime objects")

if asynchronous not in [True, False]:
raise TypeError("asynchronous must be set to either True or False")
Expand Down
4 changes: 3 additions & 1 deletion viresclient/_client_swarm.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ def set_products(self, measurements=None, models=None, auxiliaries=None,
self._request_inputs.variables = variables
self._request_inputs.sampling_step = sampling_step

def set_range_filter(self, parameter, minimum, maximum):
def set_range_filter(self, parameter=None, minimum=None, maximum=None):
"""Set a filter to apply.
Note:
Expand All @@ -362,6 +362,8 @@ def set_range_filter(self, parameter, minimum, maximum):
maximum (float)
"""
if not isinstance(parameter, str):
raise TypeError("parameter must be a str")
# Update the list that contains the separate filters
self._filterlist += [parameter+":"+str(minimum)+","+str(maximum)]
# Convert the list into the string that gets passed to the xml template
Expand Down
13 changes: 12 additions & 1 deletion viresclient/_data_handling.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ def _fix_Dataset_column(da):
If column is 2D:
Convert the DataArray from a stack of lists to a ND array
Attach 'time' and 'dim':(0,1,2,..) as the coordinates
"""
data = numpy.stack(da.values)
times = da.coords['Timestamp'].values
Expand All @@ -78,6 +79,7 @@ def _fix_Dataset_column(da):

def _DataFrame_to_xarrayDataset(df):
"""Convert pandas DataFrame to xarray Dataset
"""
# Transfer to Dataset
ds = xarray.Dataset.from_dataframe(df)
Expand All @@ -93,8 +95,9 @@ class ReturnedData(object):
Holds the data returned from the server and the data type.
Data is held in a NamedTemporaryFile. It is automatically closed & destroyed
when it goes out of scope.
when it goes out of scope.
Provides output to different file types and data objects.
"""

def __init__(self, filetype=None):
Expand Down Expand Up @@ -125,6 +128,7 @@ def file(self):

def _write_new_data(self, data):
"""Replace the tempfile contents with 'data' (bytes)
"""
if not isinstance(data, bytes):
raise TypeError("data must be of type bytes")
Expand All @@ -133,6 +137,7 @@ def _write_new_data(self, data):

def _write_file(self, filename):
"""Write the tempfile out to a regular file
"""
self.file.seek(0)
with open(filename, 'wb') as out_file:
Expand All @@ -156,6 +161,7 @@ def filetype(self, value):
@staticmethod
def _check_outfile(path, path_extension, overwrite=False):
"""Check validity of path and extension, and if it exists already
"""
if not isinstance(path, str):
raise TypeError("path must be a string")
Expand All @@ -177,6 +183,7 @@ def to_file(self, path, overwrite=False):
Args:
path (str): path to the file to save as
overwrite (bool): Will overwrite existing file if True
"""
self._check_outfile(path, self.filetype, overwrite)
self._write_file(path)
Expand All @@ -197,6 +204,7 @@ def to_netcdf(self, path, overwrite=False):
"""Saves the data as a netCDF4 file (this is compatible with HDF5)
Extension should be .nc
"""
self._check_outfile(path, 'nc', overwrite)
# Convert to xarray Dataset
Expand Down Expand Up @@ -279,6 +287,7 @@ def as_xarray(self, group=None):

class ReturnedDataGroup(object):
"""Holds a list of ReturnedData objects
"""

def __init__(self, filetype=None, N=1):
Expand Down Expand Up @@ -315,6 +324,7 @@ def to_files(self, paths, overwrite=False):
Args:
paths (list of str): paths to the files to save as
overwrite (bool): Will overwrite existing file if True
"""
nfiles = len(self.contents)
if not isinstance(paths, list) or not isinstance(paths[0], str):
Expand All @@ -336,6 +346,7 @@ def to_file(self, path, overwrite=False):
Args:
path (str): path to the file to save as
overwrite (bool): Will overwrite existing file if True
"""
if len(self.contents) != 1:
raise Exception("Data is split into multiple files. "
Expand Down

0 comments on commit 42e9617

Please sign in to comment.