Example #1
0
    def test_multi_agg(self):
        expected_cols = [
            'medDry_B', 'aveDry_B', 'maxDry_B',
            'minWet_B', 'aveWet_M', 'maxWet_M',
        ]
        wq_cols = [
            ('Dry_B', 'medIAN'),
            ('Dry_B',),
            ('Dry_B', 'MAXIMUM'),
            ('Wet_B', 'minIMUM'),
            ('Wet_M',),
            ('Wet_M', 'MAXIMUM'),
        ]

        with utils.OverwriteState(True), utils.WorkSpace(self.ws):
            wq, cols = analysis.preprocess_wq(
                monitoring_locations=self.ml,
                subcatchments=self.sc,
                id_col='CID',
                ds_col='DS_CID',
                output_path=self.results,
                value_columns=wq_cols
            )
        expected = 'expected_multi_agg.shp'
        pptest.assert_shapefiles_are_close(
            os.path.join(self.ws, expected),
            os.path.join(self.ws, self.results),
        )
        nt.assert_true(isinstance(wq, numpy.ndarray))
        nt.assert_list_equal(cols, expected_cols)
Example #2
0
 def test_no_wq_col_error(self):
     with utils.OverwriteState(True), utils.WorkSpace(self.ws):
         wq, cols = analysis.preprocess_wq(
             monitoring_locations=self.ml,
             subcatchments=self.sc,
             id_col='CID',
             ds_col='DS_CID',
             output_path=self.results,
         )
Example #3
0
 def test_baseline(self):
     with utils.OverwriteState(True), utils.WorkSpace(self.ws):
         wq, cols = analysis.preprocess_wq(
             monitoring_locations=self.ml,
             subcatchments=self.sc,
             id_col='CID',
             ds_col='DS_CID',
             output_path=self.results,
             value_columns=self.wq_cols
         )
     expected = 'expected.shp'
     pptest.assert_shapefiles_are_close(
         os.path.join(self.ws, expected),
         os.path.join(self.ws, self.results),
     )
     nt.assert_true(isinstance(wq, numpy.ndarray))
     nt.assert_list_equal(cols, self.expected_cols)
Example #4
0
def propagate(subcatchments=None,
              id_col=None,
              ds_col=None,
              monitoring_locations=None,
              ml_filter=None,
              ml_filter_cols=None,
              value_columns=None,
              streams=None,
              output_path=None,
              verbose=False,
              asMessage=False):
    """
    Propagate water quality scores upstream from the subcatchments of
    a watershed.

    Parameters
    ----------
    subcatchments : str
        Path to the feature class containing the subcatchments.
        Attribute table must contain fields for the subcatchment ID
        and the ID of the downstream subcatchment.
    id_col, ds_col : str
        Names of the fields in the ``subcatchments`` feature class that
        specifies the subcatchment ID and the ID of the downstream
        subcatchment, respectively.
    monitoring_locations : str
        Path to the feature class containing the monitoring locations
        and water quality scores.
    value_columns : list of str
        List of the fields in ``monitoring_locations`` that contains
        water quality score that should be propagated.
    ml_filter : callable, optional
        Function used to exclude (remove) monitoring locations from
        from aggregation/propagation.
    ml_filter_cols : str, optional
        Name of any additional columns in ``monitoring_locations`` that
        are required to use ``ml_filter``.
    streams : str
        Path to the feature class containing the streams.
    output_path : str
        Path to where the the new subcatchments feature class with the
        propagated water quality scores should be saved.

    Returns
    -------
    output_path : str

    Examples
    --------
    >>> import propagator
    >>> from propagator import utils
    >>> with utils.WorkSpace('C:/gis/SOC.gdb'):
    ...     propagator.propagate(
    ...         subcatchments='subbasins',
    ...         id_col='Catch_ID',
    ...         ds_col='DS_ID',
    ...         monitoring_locations='wq_data',
    ...         value_columns=['Dry_Metals', 'Wet_Metals', 'Wet_TSS'],
    ...         ml_filter=lambda row: row['StationType'] != 'Coastal',
    ...         ml_filter_cols=['StationType'],
    ...         streams='SOC_streams',
    ...         output_path='propagated_metals'
    ...     )

    See also
    --------
    propagator.analysis.preprocess_wq
    propagator.analysis.mark_edges
    propagator.analysis.propagate_scores
    propagator.analysis.aggregate_streams_by_subcatchment
    propagator.utils.update_attribute_table

    """

    subcatchment_output = utils.add_suffix_to_filename(output_path,
                                                       'subcatchments')
    stream_output = utils.add_suffix_to_filename(output_path, 'streams')

    wq, result_columns = analysis.preprocess_wq(
        monitoring_locations=monitoring_locations,
        ml_filter=ml_filter,
        ml_filter_cols=ml_filter_cols,
        subcatchments=subcatchments,
        value_columns=value_columns,
        id_col=id_col,
        ds_col=ds_col,
        output_path=subcatchment_output,
        verbose=verbose,
        asMessage=asMessage,
        msg="Aggregating water quality data in subcatchments")

    wq = analysis.mark_edges(
        wq,
        id_col=id_col,
        ds_col=ds_col,
        edge_ID='EDGE',
        verbose=verbose,
        asMessage=asMessage,
        msg="Marking all subcatchments that flow out of the watershed")

    for n, res_col in enumerate(result_columns, 1):
        wq = analysis.propagate_scores(
            subcatchment_array=wq,
            id_col=id_col,
            ds_col=ds_col,
            value_column=res_col,
            edge_ID='EDGE',
            verbose=verbose,
            asMessage=asMessage,
            msg="{} of {}: Propagating {} scores".format(
                n, len(result_columns), res_col))

    utils.update_attribute_table(subcatchment_output, wq, id_col,
                                 result_columns)

    stream_output = analysis.aggregate_streams_by_subcatchment(
        stream_layer=streams,
        subcatchment_layer=subcatchment_output,
        id_col=id_col,
        ds_col=ds_col,
        other_cols=result_columns,
        agg_method='first',
        output_layer=stream_output,
        verbose=verbose,
        asMessage=asMessage,
        msg='Aggregating and associating scores with streams.',
    )

    return subcatchment_output, stream_output