apply the SFR segement multiplier parameters.  Expected to be run in the same dir
    as the model exists

    Parameters
    ----------
        reach_pars : bool
            if reach paramters need to be applied

    Returns
    -------
        sfr : flopy.modflow.ModflowSfr instance

    Note
    ----
        expects "sfr_seg_pars.config" to exist
        expects <nam_file>+"_backup_.sfr" to exist

def apply_sfr_seg_parameters(seg_pars=True, reach_pars=False):
    """apply the SFR segement multiplier parameters.  Expected to be run in the same dir
    as the model exists

    Parameters
    ----------
        reach_pars : bool
            if reach paramters need to be applied

    Returns
    -------
        sfr : flopy.modflow.ModflowSfr instance

    Note
    ----
        expects "sfr_seg_pars.config" to exist
        expects <nam_file>+"_backup_.sfr" to exist


    """

    if not seg_pars and not reach_pars:
        raise Exception("gw_utils.apply_sfr_pars() error: both seg_pars and reach_pars are False")
    #if seg_pars and reach_pars:
    #    raise Exception("gw_utils.apply_sfr_pars() error: both seg_pars and reach_pars are True")

    import flopy
    bak_sfr_file,pars = None,None

    # if seg_pars:
    #     config_file = "sfr_seg_pars.config"
    #     idx_cols = ['nseg', 'icalc', 'outseg', 'iupseg', 'iprior', 'nstrpts']
    # else:
    #     config_file = "sfr_reach_pars.config"
    #     idx_cols = ["node", "k", "i", "j", "iseg", "ireach", "reachID", "outreach"]
    #
    # assert os.path.exists(config_file),"gw_utils.apply_sfr_pars() error: config file {0} missing".format(config_file)
    # with open(config_file, 'r') as f:
    #     pars = {}
    #     for line in f:
    #         line = line.strip().split()
    #         pars[line[0]] = line[1]
    #
    # m = flopy.modflow.Modflow.load(pars["nam_file"], load_only=[], check=False)
    # bak_sfr_file = pars["nam_file"] + "_backup_.sfr"
    # sfr = flopy.modflow.ModflowSfr2.load(os.path.join(bak_sfr_file), m)
    # sfrfile = pars["sfr_filename"]
    #
    #
    # mlt_df = pd.read_csv(pars["mult_file"], delim_whitespace=False, index_col=0)
    # present_cols = [c for c in idx_cols if c in mlt_df.columns]
    # mlt_cols = mlt_df.columns.drop(present_cols)
    #
    # if seg_pars:
    #     for key, val in m.sfr.segment_data.items():
    #         df = pd.DataFrame.from_records(val)
    #         df.loc[:, mlt_cols] *= mlt_df.loc[:, mlt_cols]
    #         val = df.to_records(index=False)
    #         sfr.segment_data[key] = val
    # else:
    #     df = pd.DataFrame.from_records(m.sfr.reach_data)
    #     df.loc[:, mlt_cols] *= mlt_df.loc[:, mlt_cols]
    #     sfr.reach_data = df.to_records(index=False)
    #
    #
    # if "time_mult_file" in pars:
    #     time_mult_file = pars["time_mult_file"]
    #     time_mlt_df = pd.read_csv(pars["time_mult_file"], delim_whitespace=False, index_col=0)
    #     for kper,sdata in m.sfr.segment_data.items():
    #         assert kper in time_mlt_df.index,"gw_utils.apply_sfr_seg_parameters() error: kper "+\
    #                                          "{0} not in time_mlt_df index".format(kper)
    #         for col in time_mlt_df.columns:
    #             sdata[col] *= time_mlt_df.loc[kper,col]
    #
    #
    # sfr.write_file(filename=sfrfile)
    # return sfr

    if seg_pars:
        assert os.path.exists("sfr_seg_pars.config")

        with open("sfr_seg_pars.config",'r') as f:
            pars = {}
            for line in f:
                line = line.strip().split()
                pars[line[0]] = line[1]
        bak_sfr_file = pars["nam_file"]+"_backup_.sfr"
        #m = flopy.modflow.Modflow.load(pars["nam_file"],model_ws=pars["model_ws"],load_only=["sfr"],check=False)
        m = flopy.modflow.Modflow.load(pars["nam_file"], load_only=[], check=False)
        sfr = flopy.modflow.ModflowSfr2.load(os.path.join(bak_sfr_file), m)
        sfrfile = pars["sfr_filename"]
        mlt_df = pd.read_csv(pars["mult_file"], delim_whitespace=False, index_col=0)
        time_mlt_df = None
        if "time_mult_file" in pars:
            time_mult_file = pars["time_mult_file"]
            time_mlt_df = pd.read_csv(pars["time_mult_file"], delim_whitespace=False,index_col=0)

        idx_cols = ['nseg', 'icalc', 'outseg', 'iupseg', 'iprior', 'nstrpts']
        present_cols = [c for c in idx_cols if c in mlt_df.columns]
        mlt_cols = mlt_df.columns.drop(present_cols)
        for key, val in m.sfr.segment_data.items():
            df = pd.DataFrame.from_records(val)
            df.loc[:, mlt_cols] *= mlt_df.loc[:, mlt_cols]
            val = df.to_records(index=False)
            sfr.segment_data[key] = val
    if reach_pars:
        assert os.path.exists("sfr_reach_pars.config")
        with open("sfr_reach_pars.config", 'r') as f:
            r_pars = {}
            for line in f:
                line = line.strip().split()
                r_pars[line[0]] = line[1]
        if bak_sfr_file is None:  # will be the case is seg_pars is false
            bak_sfr_file = r_pars["nam_file"]+"_backup_.sfr"
            #m = flopy.modflow.Modflow.load(pars["nam_file"],model_ws=pars["model_ws"],load_only=["sfr"],check=False)
            m = flopy.modflow.Modflow.load(r_pars["nam_file"], load_only=[], check=False)
            sfr = flopy.modflow.ModflowSfr2.load(os.path.join(bak_sfr_file), m)
            sfrfile = r_pars["sfr_filename"]
        r_mlt_df = pd.read_csv(r_pars["mult_file"],sep=',',index_col=0)
        r_idx_cols = ["node", "k", "i", "j", "iseg", "ireach", "reachID", "outreach"]
        r_mlt_cols = r_mlt_df.columns.drop(r_idx_cols)
        r_df = pd.DataFrame.from_records(m.sfr.reach_data)
        r_df.loc[:, r_mlt_cols] *= r_mlt_df.loc[:, r_mlt_cols]
        sfr.reach_data = r_df.to_records(index=False)


    #m.remove_package("sfr")
    if pars is not None and "time_mult_file" in pars:
        time_mult_file = pars["time_mult_file"]
        time_mlt_df = pd.read_csv(time_mult_file, delim_whitespace=False, index_col=0)
        for kper, sdata in m.sfr.segment_data.items():
            assert kper in time_mlt_df.index, "gw_utils.apply_sfr_seg_parameters() error: kper " + \
                                              "{0} not in time_mlt_df index".format(kper)
            for col in time_mlt_df.columns:
                sdata[col] *= time_mlt_df.loc[kper, col]

    sfr.write_file(filename=sfrfile)
    return sfr