Ejemplo n.º 1
0
    def _read_att_mat(self, data, file_name, var_names):
        """ Read MATLAB hazard's attributes. """
        self.frequency = np.squeeze(data[var_names['var_name']['freq']])
        self.orig = np.squeeze(
            data[var_names['var_name']['orig']]).astype(bool)
        self.event_id = np.squeeze(data[var_names['var_name']['even_id']]. \
            astype(np.int, copy=False))
        try:
            self.units = hdf5.get_string(data[var_names['var_name']['unit']])
        except KeyError:
            pass

        n_cen = self.centroids.size
        n_event = len(self.event_id)
        try:
            self.intensity = hdf5.get_sparse_csr_mat( \
                data[var_names['var_name']['inten']], (n_event, n_cen))
        except ValueError as err:
            LOGGER.error('Size missmatch in intensity matrix.')
            raise err
        try:
            self.fraction = hdf5.get_sparse_csr_mat( \
                data[var_names['var_name']['frac']], (n_event, n_cen))
        except ValueError as err:
            LOGGER.error('Size missmatch in fraction matrix.')
            raise err
        except KeyError:
            self.fraction = sparse.csr_matrix(
                np.ones(self.intensity.shape, dtype=np.float))
        # Event names: set as event_id if no provided
        try:
            self.event_name = hdf5.get_list_str_from_ref(
                file_name, data[var_names['var_name']['ev_name']])
        except KeyError:
            self.event_name = list(self.event_id)
        try:
            comment = hdf5.get_string(data[var_names['var_name']['comment']])
            self.tag.description += ' ' + comment
        except KeyError:
            pass

        try:
            datenum = data[var_names['var_name']['datenum']].squeeze()
            self.date = np.array([(dt.datetime.fromordinal(int(date)) + \
                dt.timedelta(days=date%1)- \
                dt.timedelta(days=366)).toordinal() for date in datenum])
        except KeyError:
            pass
Ejemplo n.º 2
0
    def read_mat(self, file_name, description='', centroids=None,
                 var_names=DEF_VAR_MAT):
        """Read climada hazard generate with the MATLAB code.

        Parameters:
            file_name (str): absolute file name
            description (str, optional): description of the data
            centroids (Centroids, optional): provide centroids if not contained
                in the file
            var_names (dict, default): name of the variables in the file,
                default: DEF_VAR_MAT constant

        Raises:
            KeyError
        """
        self.clear()
        self.tag.file_name = file_name
        self.tag.description = description
        try:
            data = hdf5.read(file_name)
            try:
                data = data[var_names['field_name']]
            except KeyError:
                pass

            haz_type = hdf5.get_string(data[var_names['var_name']['per_id']])
            self.tag.haz_type = haz_type
            self._read_centroids(centroids=centroids, var_names=var_names['var_cent'])
            self._read_att_mat(data, file_name, var_names)
        except KeyError as var_err:
            LOGGER.error("Not existing variable: %s", str(var_err))
            raise var_err
Ejemplo n.º 3
0
    def test_get_string_pass(self):
        '''Check function to get a string from input integer array'''

        # Load input
        contents = hdf5.read(HAZ_TEST_MAT)

        # Convert several strings
        str_date = hdf5.get_string(contents['hazard']['date'])
        str_comment = hdf5.get_string(contents['hazard']['comment'])
        str_wf = hdf5.get_string(contents['hazard']['windfield_comment'])
        str_fn = hdf5.get_string(contents['hazard']['filename'])

        # Check results
        self.assertEqual('14-Nov-2017 10:09:05', str_date)
        self.assertEqual(
            'TC hazard event set, generated 14-Nov-2017 10:09:05', \
                str_comment)
        self.assertEqual(
            'generating 14450 windfields took 0.25 min ' + \
            '(0.0010 sec/event)', str_wf)
        self.assertEqual('/Users/aznarsig/Documents/MATLAB/climada_data/' + \
                         'hazards/atl_prob.mat', str_fn)