Esempio n. 1
0
    def search(self,
               locations,
               species=None,
               inlet=None,
               instrument=None,
               start_datetime=None,
               end_datetime=None):
        if self._service is None:
            raise PermissionError("Cannot use a null service")

        args = {}
        args["species"] = species
        args["locations"] = locations

        if inlet is not None:
            args["inlet"] = inlet

        if instrument is not None:
            args["instrument"] = instrument

        if start_datetime:
            args["start_datetime"] = datetime_to_string(start_datetime)
        if end_datetime:
            args["end_datetime"] = datetime_to_string(end_datetime)

        response = self._service.call_function(function="search",
                                               args=args)["results"]

        self._results = response

        return response
Esempio n. 2
0
    def daterange_str(self):
        """ Get the daterange this Datasource covers as a string in
            the form start_end

            Returns:
                str: Daterange covered by this Datasource
        """
        from Acquire.ObjectStore import datetime_to_string

        start, end = self.daterange()
        return "".join([datetime_to_string(start), "_", datetime_to_string(end)])
Esempio n. 3
0
    def to_data(self):
        """ Return a JSON-serialisable dictionary of object
            for storage in object store

            Storing of the data within the Datasource is done in
            the save function

            Args:
                store (bool, default=False): True if we are storing this
                in the object store
            Returns:
                dict: Dictionary version of object
        """
        from Acquire.ObjectStore import datetime_to_string

        data = {}
        data["UUID"] = self._uuid
        data["name"] = self._name
        data["creation_datetime"] = datetime_to_string(self._creation_datetime)
        data["metadata"] = self._metadata
        data["stored"] = self._stored
        data["data_keys"] = self._data_keys
        data["data_type"] = self._data_type
        data["latest_version"] = self._latest_version
        data["rank"] = self._rank

        return data
Esempio n. 4
0
    def create_daterange(self, start, end):
        """ Create a JSON serialisable daterange string for use in ranking dict

            Args:
                start (datetime): Start of daterange
                end (datetime): End of daterange
            Returns:
                str: Serialisable daterange string
        """
        from Acquire.ObjectStore import datetime_to_string
        from pandas import Timestamp

        if isinstance(start, str) and isinstance(end, str):
            start = Timestamp(start).to_pydatetime()
            end = Timestamp(end).to_pydatetime()

        return "".join(
            [datetime_to_string(start), "_",
             datetime_to_string(end)])
Esempio n. 5
0
def retrieve(args):
    """ Calls the HUGS function to retrieve data stored at the given key
        and combine them into a single Pandas DataFrame for download / visualization

        Args:
            args (dict): Dictionary of arguments
        Returns:
            dict: Dictionary of results

    """
    from HUGS.Processing import recombine_sections
    from Acquire.ObjectStore import datetime_to_string
    from json import dumps as json_dumps
    from collections import defaultdict

    try:
        key_dict = args["keys"]
    except KeyError:
        raise KeyError("Keys required for data retrieval")
    
    return_type = args.get("return_type", "json")

    # if not isinstance(key_dict, dict):
    #     raise TypeError("Keys must be passed in dictionary format. For example {bsd_co2: [key_list]}")

    combined_data = defaultdict(dict)
    for key, dateranges in key_dict.items():
        for daterange in dateranges:
            # Create a key for this range
            data_keys = key_dict[key][daterange]
            # Retrieve the data from the object store and combine into a NetCDF
            combined = recombine_sections(data_keys)

            if return_type == "json":
                dataset_dict = combined.to_dict()

                # Need to convert the time data to string and then back again on the other side
                # See https://github.com/pydata/xarray/issues/2656
                datetime_data = dataset_dict["coords"]["time"]["data"]
                # Convert the datetime object to string
                for i, _ in enumerate(datetime_data):
                    datetime_data[i] = datetime_to_string(datetime_data[i])

                json_data = json_dumps(dataset_dict, indent=4)
                combined_data[key][daterange] = json_data
            else:
                raise NotImplementedError("Not yet implemented")

    return {"results": combined_data}
Esempio n. 6
0
def test_datetime():
    dt = get_datetime_now()

    assert (dt.tzinfo == datetime.timezone.utc)

    d = dt.date()
    t = dt.timetz()

    sdt = datetime_to_string(dt)
    assert (dt == string_to_datetime(sdt))

    sd = date_to_string(d)
    assert (d == string_to_date(sd))
    assert (d == string_to_date(date_to_string(dt)))

    st = time_to_string(t)
    assert (t == string_to_time(st))
    assert (t == string_to_time(time_to_string(dt)))
Esempio n. 7
0
    def to_data(self):
        """ Return a JSON-serialisable dictionary of object
            for storage in object store

            Returns:
                dict: Dictionary version of object
        """
        from Acquire.ObjectStore import datetime_to_string

        data = {}
        data["creation_datetime"] = datetime_to_string(self._creation_datetime)
        data["stored"] = self._stored
        data["datasource_uuids"] = self._datasource_uuids
        data["datasource_names"] = self._datasource_names
        data["file_hashes"] = self._file_hashes
        data["rank_data"] = self._rank_data

        return data
def run(args):
    """This function will allow anyone to obtain the public
       keys for the passed login session
    """
    try:
        session_uid = args["session_uid"]
    except:
        session_uid = None

    try:
        short_uid = args["short_uid"]
    except:
        short_uid = None

    try:
        scope = args["scope"]
    except:
        scope = None

    try:
        permissions = args["permissions"]
    except:
        permissions = None

    if session_uid:
        login_session = LoginSession.load(uid=session_uid,
                                          scope=scope,
                                          permissions=permissions)
    else:
        if short_uid is None:
            raise PermissionError(
                "You must specify either the session_uid or the short_uid "
                "of the login session")

        try:
            status = args["status"]
        except:
            raise PermissionError(
                "You must specify the status of the short_uid session you "
                "wish to query...")

        login_session = LoginSession.load(short_uid=short_uid,
                                          status=status,
                                          scope=scope,
                                          permissions=permissions)

    return_value = {}

    # only send information if the user had logged in!
    should_return_data = False

    if login_session.is_approved():
        should_return_data = True
        return_value["public_key"] = login_session.public_key().to_data()

    elif login_session.is_logged_out():
        should_return_data = True
        return_value["logout_datetime"] = \
            datetime_to_string(login_session.logout_time())

    if should_return_data:
        return_value["public_cert"] = \
            login_session.public_certificate().to_data()
        return_value["scope"] = login_session.scope()
        return_value["permissions"] = login_session.permissions()
        return_value["user_uid"] = login_session.user_uid()

    return_value["session_status"] = login_session.status()
    return_value["login_message"] = login_session.login_message()

    return return_value