def convert_version(self, d):
        ''' Convert a Version dictionary.

        Parameters
        ----------
        d: dict
            The dictionary to convert.

        Returns
        -------
        inst: :class:`mss_dataserver.core.util.Version`
            The instance of the converted dictionary.
        '''
        inst = util.Version(d['version'])
        return inst
Exemplo n.º 2
0
class GeneralFileDecoder(json.JSONDecoder):
    version = util.Version('1.0.0')

    def __init__(self, **kwarg):
        json.JSONDecoder.__init__(self, object_hook=self.convert_dict)

    def convert_dict(self, d):

        if '__class__' in d:
            class_name = d.pop('__class__')
            module_name = d.pop('__module__')
            base_class = d.pop('__baseclass__')

            if class_name == 'Version':
                inst = self.convert_version(d)
            elif class_name == 'UTCDateTime':
                inst = self.convert_utcdatetime(d)
            elif class_name == 'ndarray':
                inst = self.convert_np_array(d)
            else:
                inst = {'ERROR': 'MISSING CONVERTER'}

        else:
            inst = d

        return inst

    def decode_hinted_tuple(self, item):
        if isinstance(item, dict):
            if '__tuple__' in item:
                return tuple(item['items'])
        elif isinstance(item, list):
            return [self.decode_hinted_tuple(x) for x in item]
        else:
            return item

    def convert_version(self, d):
        inst = util.Version(d['version'])
        return inst

    def convert_utcdatetime(self, d):
        inst = obspy.UTCDateTime(d['utcdatetime'])
        return inst

    def convert_np_array(self, d):
        inst = np.array(d['data'])
        return inst
Exemplo n.º 3
0
class GeneralFileEncoder(json.JSONEncoder):
    ''' A JSON encoder for the serialization of general data.
    '''
    version = util.Version('1.0.0')

    def __init__(self, **kwarg):
        json.JSONEncoder.__init__(self, **kwarg)

        # The logger.
        loggerName = __name__ + "." + self.__class__.__name__
        self.logger = logging.getLogger(loggerName)

        # File format settings.
        self.indent = 4
        self.sort_keys = True

    def default(self, obj):
        ''' Convert pSysmon project objects to a dictionary.
        '''
        obj_class = obj.__class__.__name__
        base_class = [x.__name__ for x in obj.__class__.__bases__]

        #self.logger.debug('obj_class: %s.', obj_class)
        if obj_class == 'FileContainer':
            d = self.convert_filecontainer(obj)
        elif obj_class == 'Version':
            d = self.convert_version(obj)
        elif obj_class == 'UTCDateTime':
            d = self.convert_utcdatetime(obj)
        elif obj_class == 'ndarray':
            d = self.convert_np_ndarray(obj)
        elif obj_class == 'type':
            d = {}
        else:
            d = {
                'ERROR':
                'MISSING CONVERTER for obj_class {obj_class} with base_class {base_class}'
                .format(obj_class=str(obj_class), base_class=str(base_class))
            }

        # Add the class and module information to the dictionary.
        if obj_class != 'FileContainer':
            try:
                module = obj.__module__
            except Exception:
                module = obj.__class__.__module__
            tmp = {
                '__baseclass__': base_class,
                '__class__': obj.__class__.__name__,
                '__module__': module
            }
            d.update(tmp)

        self.logger.debug('d: %s', d)
        return d

    def convert_filecontainer(self, obj):
        d = obj.data
        file_meta = {
            'file_version': self.version,
            'save_date': obspy.UTCDateTime(),
            'agency_uri': obj.agency_uri,
            'author_uri': obj.author_uri
        }
        d['file_meta'] = file_meta
        return d

    def convert_utcdatetime(self, obj):
        return {'utcdatetime': obj.isoformat()}

    def convert_version(self, obj):
        return {'version': str(obj)}

    def convert_np_ndarray(self, obj):
        return {'data': obj.tolist()}
Exemplo n.º 4
0
 def convert_version(self, d):
     inst = util.Version(d['version'])
     return inst
Exemplo n.º 5
0
class SupplementDetectionDataDecoder(json.JSONDecoder):
    version = util.Version('1.0.0')

    def __init__(self, **kwarg):
        json.JSONDecoder.__init__(self, object_hook=self.convert_dict)

    def convert_dict(self, d):
        #print "Converting dict: %s." % str(d)

        if '__class__' in d:
            class_name = d.pop('__class__')
            module_name = d.pop('__module__')
            base_class = d.pop('__baseclass__')

            if class_name == 'Version':
                inst = self.convert_version(d)
            elif class_name == 'UTCDateTime':
                inst = self.convert_utcdatetime(d)
            elif class_name == 'ndarray':
                inst = self.convert_np_array(d)
            elif 'Station' in base_class:
                inst = self.convert_station(d)
            else:
                inst = {'ERROR': 'MISSING CONVERTER'}

        else:
            inst = d

        return inst

    def decode_hinted_tuple(self, item):
        if isinstance(item, dict):
            if '__tuple__' in item:
                return tuple(item['items'])
        elif isinstance(item, list):
            return [self.decode_hinted_tuple(x) for x in item]
        else:
            return item

    def convert_version(self, d):
        inst = util.Version(d['version'])
        return inst

    def convert_utcdatetime(self, d):
        inst = obspy.UTCDateTime(d['utcdatetime'])
        return inst

    def convert_np_array(self, d):
        inst = np.array(d['data'])
        return inst

    def convert_station(self, d):
        inst = geom.inventory.Station(name=d['name'],
                                      location=d['location'],
                                      x=d['x'],
                                      y=d['y'],
                                      z=d['z'],
                                      coord_system=d['coord_system'],
                                      description=d['description'],
                                      author_uri=d['author_uri'],
                                      agency_uri=d['agency_uri'],
                                      creation_time=d['creation_time'])
        return inst
Exemplo n.º 6
0
class SupplementDetectionDataEncoder(json.JSONEncoder):
    ''' A JSON encoder for the event supplement detection data.
    '''

    version = util.Version('1.0.0')

    def __init__(self, **kwarg):
        json.JSONEncoder.__init__(self, **kwarg)

        # The logger.
        loggerName = __name__ + "." + self.__class__.__name__
        self.logger = logging.getLogger(loggerName)

        # File format settings.
        self.indent = 4
        self.sort_keys = True

    def default(self, obj):
        ''' Convert the detection data instances to dictionaries.
        '''
        obj_class = obj.__class__.__name__
        base_class = [x.__name__ for x in obj.__class__.__bases__]
        #print 'Converting %s' % obj_class

        if obj_class == 'FileContainer':
            d = self.convert_filecontainer(obj)
        elif obj_class == 'UTCDateTime':
            d = self.convert_utcdatetime(obj)
        elif obj_class == 'Version':
            d = self.convert_version(obj)
        elif obj_class == 'ndarray':
            d = self.convert_np_ndarray(obj)
        elif 'Station' in base_class:
            d = self.convert_station(obj)
        else:
            d = {
                'ERROR':
                'MISSING CONVERTER for obj_class {obj_class} with base_class {base_class}'
                .format(obj_class=str(obj_class), base_class=str(base_class))
            }

        # Add the class and module information to the dictionary.
        if obj_class != 'FileContainer':
            try:
                module = obj.__module__
            except Exception:
                module = obj.__class__.__module__
            tmp = {
                '__baseclass__': base_class,
                '__class__': obj.__class__.__name__,
                '__module__': module
            }
            d.update(tmp)

        return d

    def convert_filecontainer(self, obj):
        d = obj.data
        file_meta = {
            'file_version': self.version,
            'save_date': obspy.UTCDateTime(),
            'agency_uri': obj.agency_uri,
            'author_uri': obj.author_uri
        }
        d['file_meta'] = file_meta
        return d

    def convert_utcdatetime(self, obj):
        return {'utcdatetime': obj.isoformat()}

    def convert_version(self, obj):
        return {'version': str(obj)}

    def convert_np_ndarray(self, obj):
        return {'data': obj.tolist()}

    def convert_station(self, obj):
        attr = [
            'name', 'location', 'network', 'x', 'y', 'z', 'coord_system',
            'description', 'author_uri', 'agency_uri', 'creation_time'
        ]
        d = object_to_dict(obj, attr)
        return d
class SupplementDetectionDataDecoder(json.JSONDecoder):
    ''' A JSON decoder for the deserialization of detection supplement data.

    Parameters
    ----------
    **kwargs: keyword argument
        Keyword arguments passed to :class:`json.encoder.JSONDecoder`.

    Attributes
    ----------
    version: :class:`mss_dataserver.core.util`
        The version of the file decoder.
    '''
    version = util.Version('1.0.0')

    def __init__(self, **kwarg):
        ''' Initialize the instance.
        '''
        json.JSONDecoder.__init__(self, object_hook=self.convert_dict)

        self.inventory = geom.inventory.Inventory(name='detection_data_import')

    def convert_dict(self, d):
        ''' Convert a dictionary to objects.

        The dictionary to convert should have been with an mss_dataserver
        JSON file encoder class. In this case, the dictionaries contains
        hints of the original class and module name in the __class__, 
        __module__ and __base_class__ keys. These are used to convert 
        the dictionary to instances of the given classes.

        Parameters
        ----------
        d: dict
            The dictionary to convert.

        Returns
        -------
        inst: object
            The object representation of dict d.
        '''
        #print "Converting dict: %s." % str(d)
        if '__class__' in d:
            class_name = d.pop('__class__')
            module_name = d.pop('__module__')
            base_class = d.pop('__baseclass__')

            if class_name == 'Version':
                inst = self.convert_version(d)
            elif class_name == 'UTCDateTime':
                inst = self.convert_utcdatetime(d)
            elif class_name == 'ndarray':
                inst = self.convert_np_array(d)
            elif 'Station' in base_class:
                inst = self.convert_station(d)
            else:
                inst = {'ERROR': 'MISSING CONVERTER'}

        else:
            inst = d

        return inst

    def decode_hinted_tuple(self, item):
        ''' Decode a tuple.

        JSON doesn't support tuples. Use a custom dictionary
        to decode. If the dictionary contains the __tuple__
        attribute, the dictionary is converted to a tuple.

        Parameters
        ----------
        item: dict
            The dictionary to decode.

        Returns
        ------
        item: object
            The object representation of the item dictionary.
        '''
        if isinstance(item, dict):
            if '__tuple__' in item:
                return tuple(item['items'])
        elif isinstance(item, list):
            return [self.decode_hinted_tuple(x) for x in item]
        else:
            return item

    def convert_version(self, d):
        ''' Convert a Version dictionary.

        Parameters
        ----------
        d: dict
            The dictionary to convert.

        Returns
        -------
        inst: :class:`mss_dataserver.core.util.Version`
            The instance of the converted dictionary.
        '''
        inst = util.Version(d['version'])
        return inst

    def convert_utcdatetime(self, d):
        ''' Convert a UTCDateTime dictionary.

        Parameters
        ----------
        d: dict
            The dictionary to convert.

        Returns
        -------
        d: obspy.UTCDateTime
            The instance of the converted dictionary.
        '''
        inst = obspy.UTCDateTime(d['utcdatetime'])
        return inst

    def convert_np_array(self, d):
        ''' Convert a numpy ndarray dictionary.

        Parameters
        ----------
        d: dict
            The dictionary to convert.

        Returns
        -------
        inst: numpy.ndarray
            The instance of the converted dictionary.
        '''
        inst = np.array(d['data'])
        return inst

    def convert_station(self, d):
        ''' Convert a Station dictionary.

        Parameters
        ----------
        d: dict
            The dictionary to convert.

        Returns
        -------
        inst: mss_dataserver.geometry.inventory.Station
            The instance of the converted dictionary.
        '''
        cur_station = self.inventory.get_station(name=d['name'],
                                                 location=d['location'],
                                                 network=d['network'])
        if len(cur_station) == 1:
            # Use the found station.
            inst = cur_station[0]
        else:
            # Create a new station and add it to the inventory.
            inst = geom.inventory.Station(name=d['name'],
                                          location=d['location'],
                                          x=d['x'],
                                          y=d['y'],
                                          z=d['z'],
                                          coord_system=d['coord_system'],
                                          description=d['description'],
                                          author_uri=d['author_uri'],
                                          agency_uri=d['agency_uri'],
                                          creation_time=d['creation_time'])
            network_name = d['network']
            cur_net = self.inventory.get_network(name=network_name)
            if len(cur_net) == 1:
                cur_net = cur_net[0]
            else:
                cur_net = geom.inventory.Network(name=network_name,
                                                 author_uri=d['author_uri'],
                                                 agency_uri=d['agency_uri'])
            self.inventory.add_network(cur_net)
            self.inventory.add_station(network_name=d['network'],
                                       station_to_add=inst)
        return inst
class SupplementDetectionDataEncoder(json.JSONEncoder):
    ''' A JSON encoder for the event supplement detection data.

    Parameters
    ----------
    **kwargs: keyword argument
        Keyword arguments passed to :class:`json.encoder.JSONEncoder`.

    Attributes
    ----------
    version: :class:`mss_dataserver.core.util`
        The version of the file encoder.
    '''

    version = util.Version('1.0.0')

    def __init__(self, **kwarg):
        ''' Initialization of the instance.
        '''
        json.JSONEncoder.__init__(self, **kwarg)

        # The logger.
        loggerName = __name__ + "." + self.__class__.__name__
        self.logger = logging.getLogger(loggerName)

        # File format settings.
        self.indent = 4
        self.sort_keys = True

    def default(self, obj):
        ''' Convert objects to a dictionary.

        The instance class, module and base_class relations are stored in 
        the __class__, __module__ and __base_class__ keys. These are used 
        by the related file decoder to restore the correct class instances.

        Parameters
        ----------
        obj: object
            The instance to convert to a dictionary.

        Returns
        -------
        d: dict
           The dictionary representation of the instance obj.
        ''' ''' Convert the detection data instances to dictionaries.
        '''
        obj_class = obj.__class__.__name__
        base_class = [x.__name__ for x in obj.__class__.__bases__]
        #print 'Converting %s' % obj_class

        if obj_class == 'FileContainer':
            d = self.convert_filecontainer(obj)
        elif obj_class == 'UTCDateTime':
            d = self.convert_utcdatetime(obj)
        elif obj_class == 'Version':
            d = self.convert_version(obj)
        elif obj_class == 'ndarray':
            d = self.convert_np_ndarray(obj)
        elif 'Station' in base_class:
            d = self.convert_station(obj)
        else:
            d = {
                'ERROR':
                'MISSING CONVERTER for obj_class {obj_class} with base_class {base_class}'
                .format(obj_class=str(obj_class), base_class=str(base_class))
            }

        # Add the class and module information to the dictionary.
        if obj_class != 'FileContainer':
            try:
                module = obj.__module__
            except Exception:
                module = obj.__class__.__module__
            tmp = {
                '__baseclass__': base_class,
                '__class__': obj.__class__.__name__,
                '__module__': module
            }
            d.update(tmp)

        return d

    def convert_filecontainer(self, obj):
        ''' Convert a filecontainer instance.

        Parameters
        ----------
        obj: FileContainer
            The filecontainer to convert.

        Returns
        -------
        d: dict
           The dictionary representation of the instance obj.
        '''
        d = obj.data
        file_meta = {
            'file_version': self.version,
            'save_date': obspy.UTCDateTime(),
            'agency_uri': obj.agency_uri,
            'author_uri': obj.author_uri
        }
        d['file_meta'] = file_meta
        return d

    def convert_utcdatetime(self, obj):
        ''' Convert a UTCDateTime instance.

        Parameters
        ----------
        obj: obspy.utcdatetime.UTCDateTime
            The UTCDateTime instance to convert.

        Returns
        -------
        d: dict
            The dictionary representation of obj.
        '''
        return {'utcdatetime': obj.isoformat()}

    def convert_version(self, obj):
        ''' Convert a Version dictionary.

        Parameters
        ----------
        d: dict
            The dictionary to convert.

        Returns
        -------
        inst: :class:`mss_dataserver.core.util.Version`
            The instance of the converted dictionary..
        '''
        return {'version': str(obj)}

    def convert_np_ndarray(self, obj):
        ''' Convert a numpy ndarray dictionary.

        Parameters
        ----------
        d: dict
            The dictionary to convert.

        Returns
        -------
        inst: numpy.ndarray
            The instance of the converted dictionary..
        '''
        return {'data': obj.tolist()}

    def convert_station(self, obj):
        ''' Convert a Station dictionary.

        Parameters
        ----------
        d: dict
            The dictionary to convert.

        Returns
        -------
        inst: :class:`mss_dataserver.geometry.inventory.Station`
            The instance of the converted dictionary.
        '''
        attr = [
            'name', 'location', 'network', 'x', 'y', 'z', 'coord_system',
            'description', 'author_uri', 'agency_uri', 'creation_time'
        ]
        d = object_to_dict(obj, attr)
        return d
class GeneralFileDecoder(json.JSONDecoder):
    ''' A JSON decoder for the deserialization of general data.

    Parameters
    ----------
    **kwargs: keyword argument
        Keyword arguments passed to :class:`json.encoder.JSONDecoder`.

    Attributes
    ----------
    version: :class:`mss_dataserver.core.util`
        The version of the file decoder.
    '''

    version = util.Version('1.0.0')

    def __init__(self, **kwarg):
        json.JSONDecoder.__init__(self, object_hook=self.convert_dict)

    def convert_dict(self, d):
        ''' Convert a dictionary to objects.

        The dictionary to convert should have been with an mss_dataserver
        JSON file encoder class. In this case, the dictionaries contains
        hints of the original class and module name in the __class__, 
        __module__ and __base_class__ keys. These are used to convert 
        the dictionary to instances of the given classes.

        Parameters
        ----------
        d: dict
            The dictionary to convert.

        Returns
        -------
        inst: object
            The object representation of dict d.
        '''
        if '__class__' in d:
            class_name = d.pop('__class__')
            module_name = d.pop('__module__')
            base_class = d.pop('__baseclass__')

            if class_name == 'Version':
                inst = self.convert_version(d)
            elif class_name == 'UTCDateTime':
                inst = self.convert_utcdatetime(d)
            elif class_name == 'ndarray':
                inst = self.convert_np_array(d)
            else:
                inst = {'ERROR': 'MISSING CONVERTER'}

        else:
            inst = d

        return inst

    def decode_hinted_tuple(self, item):
        ''' Decode a tuple.

        JSON doesn't support tuples. Use a custom dictionary
        to decode. If the dictionary contains the __tuple__
        attribute, the dictionary is converted to a tuple.

        Parameters
        ----------
        item: dict
            The dictionary to decode.

        Returns
        ------
        item: object
            The object representation of the item dictionary.
        '''
        if isinstance(item, dict):
            if '__tuple__' in item:
                return tuple(item['items'])
        elif isinstance(item, list):
            return [self.decode_hinted_tuple(x) for x in item]
        else:
            return item

    def convert_version(self, d):
        ''' Convert a Version dictionary.

        Parameters
        ----------
        d: dict
            The dictionary to convert.

        Returns
        -------
        inst: :class:`mss_dataserver.core.util.Version`
            The instance of the converted dictionary.
        '''
        inst = util.Version(d['version'])
        return inst

    def convert_utcdatetime(self, d):
        ''' Convert a UTCDateTime dictionary.

        Parameters
        ----------
        d: dict
            The dictionary to convert.

        Returns
        -------
        d: obspy.UTCDateTime
            The instance of the converted dictionary.
        '''
        inst = obspy.UTCDateTime(d['utcdatetime'])
        return inst

    def convert_np_array(self, d):
        ''' Convert a numpy ndarray dictionary.

        Parameters
        ----------
        d: dict
            The dictionary to convert.

        Returns
        -------
        inst: numpy.ndarray
            The instance of the converted dictionary.
        '''
        inst = np.array(d['data'])
        return inst
class GeneralFileEncoder(json.JSONEncoder):
    ''' A JSON encoder for the serialization of general data.

    Parameters
    ----------
    **kwargs: keyword argument
        Keyword arguments passed to :class:`json.encoder.JSONEncoder`.

    Attributes
    ----------
    version: :class:`mss_dataserver.core.util`
        The version of the file encoder.

    logger: logging.Logger
        The logging instance.
    '''
    version = util.Version('1.0.0')

    def __init__(self, **kwarg):
        ''' Initialization of the instance.
        '''
        json.JSONEncoder.__init__(self, **kwarg)

        # The logger.
        loggerName = __name__ + "." + self.__class__.__name__
        self.logger = logging.getLogger(loggerName)

        # File format settings.
        self.indent = 4
        self.sort_keys = True

    def default(self, obj):
        ''' Convert objects to a dictionary.

        The instance class, module and base_class relations are stored in 
        the __class__, __module__ and __base_class__ keys. These are used 
        by the related file decoder to restore the correct class instances.

        Parameters
        ----------
        obj: object
            The instance to convert to a dictionary.

        Returns
        -------
        d: dict
           The dictionary representation of the instance obj.
        '''
        obj_class = obj.__class__.__name__
        base_class = [x.__name__ for x in obj.__class__.__bases__]

        #self.logger.debug('obj_class: %s.', obj_class)
        if obj_class == 'FileContainer':
            d = self.convert_filecontainer(obj)
        elif obj_class == 'Version':
            d = self.convert_version(obj)
        elif obj_class == 'UTCDateTime':
            d = self.convert_utcdatetime(obj)
        elif obj_class == 'ndarray':
            d = self.convert_np_ndarray(obj)
        elif obj_class == 'type':
            d = {}
        else:
            d = {
                'ERROR':
                'MISSING CONVERTER for obj_class {obj_class} with base_class {base_class}'
                .format(obj_class=str(obj_class), base_class=str(base_class))
            }

        # Add the class and module information to the dictionary.
        if obj_class != 'FileContainer':
            try:
                module = obj.__module__
            except Exception:
                module = obj.__class__.__module__
            tmp = {
                '__baseclass__': base_class,
                '__class__': obj.__class__.__name__,
                '__module__': module
            }
            d.update(tmp)

        self.logger.debug('d: %s', d)
        return d

    def convert_filecontainer(self, obj):
        ''' Convert a filecontainer instance.

        Parameters
        ----------
        obj: FileContainer
            The filecontainer to convert.

        Returns
        -------
        d: dict
           The dictionary representation of the instance obj.
        '''
        d = obj.data
        file_meta = {
            'file_version': self.version,
            'save_date': obspy.UTCDateTime(),
            'agency_uri': obj.agency_uri,
            'author_uri': obj.author_uri
        }
        d['file_meta'] = file_meta
        return d

    def convert_utcdatetime(self, obj):
        ''' Convert a UTCDateTime instance.

        Parameters
        ----------
        obj: obspy.utcdatetime.UTCDateTime
            The UTCDateTime instance to convert.

        Returns
        -------
        d: dict
            The dictionary representation of obj.
        '''
        return {'utcdatetime': obj.isoformat()}

    def convert_version(self, obj):
        ''' Convert a UTCDateTime instance.

        Parameters
        ----------
        obj: obspy.utcdatetime.UTCDateTime
            The UTCDateTime instance to convert.

        Returns
        -------
        d: dict
            The dictionary representation of obj.
        ''' ''' Convert a Version instance.

        Parameters
        ----------
        obj: mss_dataserver.core.util.Version
            The instance to convert.

        Returns
        -------
        d: dict
            The dictionary representation of obj.
        '''
        return {'version': str(obj)}

    def convert_np_ndarray(self, obj):
        ''' Convert a numpy array instance.

        Parameters
        ----------
        obj: numpy.ndarray
            The instance to convert.

        Returns
        -------
        d: dict
            The dictionary representation of obj.
        '''
        return {'data': obj.tolist()}