Exemplo n.º 1
0
def _read_block_21_25_or_29(fobj, nrec, recarray, block):
    """
    Method to read blocks 21, 25, and 29 from the AG package

    Parameters
    ----------
    fobj : File object
    nrec : int
        number of records
    recarray : np.recarray
        recarray to add data to
    block : int
        valid options are 6, 10, or 14

    Returns
    -------
        recarray : np.recarray
    """
    t = []

    hrus = False
    if "hru_id0" in recarray.dtype.names and \
            "segid" in recarray.dtype.names:
        hrus = True

    for _ in range(nrec):
        t1 = []
        ll = multi_line_strip(fobj).split()
        if block in (21, ):
            # do not zero adjust segid
            ll[0] = int(ll[0])
        else:
            ll[0] = int(ll[0]) - 1

        if block in (21, 25):
            # correct list length if not using trigger factor
            if len(ll) == 2:
                ll += [-1e+10, 1e+10]
            elif len(ll) == 3:
                ll += [1e-10]

            t1 += ll[:4]

        elif block == 29:
            t1 += ll[:2]

        else:
            raise AssertionError("block number must be 6, 10, or 14")

        for numcell in range(int(ll[1])):
            if block == 29:
                if len(ll) == 2:
                    ll += [1e-10]

            if hrus or block == 29:
                tmp = multi_line_strip(fobj).split()[:3]
                if block == 29:
                    tmp[0] = int(tmp[0])
                else:
                    tmp[0] = int(tmp[0]) - 1
            else:
                tmp = multi_line_strip(fobj).split()[:4]
                tmp[0:2] = [int(tmp[0]) - 1, int(tmp[1]) - 1]

            t1 += tmp

        t.append(t1)

    if len(t) > 0:
        for ix, rec in enumerate(t):
            for ix2, name in enumerate(recarray.dtype.names):
                if ix2 >= len(rec):
                    pass
                else:
                    recarray[name][ix] = rec[ix2]
    return recarray
Exemplo n.º 2
0
    def load(f, model, nper=0, ext_unit_dict=None):
        """
        Method to load the AG package from file

        Parameters
        ----------
        f : str
            filename
        model : gsflow.modflow.Modflow object
            model to attach the ag pacakge to
        nper : int
            number of stress periods in model
        ext_unit_dict : dict, optional

        Returns
        -------
            ModflowAwu object
        """
        if nper == 0:
            nper = model.nper

        with open(f) as mfag:

            # strip the file header if it exists
            while True:
                line = multi_line_strip(mfag)
                if line:
                    break

            # read the options block
            options = OptionBlock.load_options(mfag, ModflowAg)

            line = multi_line_strip(mfag)

            time_series = None
            if "time series" in line:
                # read time_series
                t = []
                while True:
                    line = multi_line_strip(mfag)
                    if line == "end":
                        line = multi_line_strip(mfag)
                        break

                    else:
                        t.append(line.split())

                if len(t) > 0:
                    nrec = len(t)
                    time_series = ModflowAg.get_empty(nrec,
                                                      block="time series")

                    for ix, rec in enumerate(t):
                        if rec[0] in ("welletall", "wellall"):
                            time_series[ix] = (rec[0], -999, rec[-1])
                        else:
                            time_series[ix] = tuple(rec[:3])

            # read item 12-14
            segments = None
            if "segment list" in line:
                # read item 13
                t = []
                while True:
                    line = multi_line_strip(mfag)
                    if line == "end":
                        line = multi_line_strip(mfag)
                        break
                    else:
                        t.append(line.split())

                if len(t) > 0:
                    segments = []
                    for rec in t:
                        iseg = int(rec[0])
                        segments.append(iseg)

            # read item 15-17 well_list
            well = None
            if "well list" in line:
                # read item 16
                t = []
                while True:
                    line = multi_line_strip(mfag)
                    if line == "end":
                        line = multi_line_strip(mfag)
                        break

                    else:
                        t.append(line.split())

                if len(t) > 0:
                    nrec = len(t)

                    # check if this is block 16a
                    if isinstance(options.tabfiles, np.recarray):
                        tf = True
                        well = ModflowAg.get_empty(nrec, block="tabfile_well")
                    else:
                        tf = False
                        well = ModflowAg.get_empty(nrec, block="well")

                    for ix, rec in enumerate(t):
                        if not tf:
                            k = int(rec[0]) - 1
                            i = int(rec[1]) - 1
                            j = int(rec[2]) - 1
                            well[ix] = (k, i, j, rec[3])
                        else:
                            k = int(rec[2]) - 1
                            i = int(rec[3]) - 1
                            j = int(rec[4]) - 1
                            well[ix] = (rec[0], rec[1], k, i, j)

            maxcellsdiversion = 0
            if options.maxcellsdiversion is not None:
                maxcellsdiversion = options.maxcellsdiversion

            maxcellswell = 0
            if options.maxcellswell is not None:
                maxcellswell = options.maxcellswell

            maxdiversions = 0
            if options.maxdiversions is not None:
                maxdiversions = options.maxdiversions

            irr_diversion = {}
            irr_well = {}
            sup_well = {}
            # get the stress period data from blocks 18 - 29
            for per in range(nper):
                while True:
                    if "stress period" in line:
                        line = multi_line_strip(mfag)

                    # block 18
                    elif "irrdiversion" in line:
                        # read block 19
                        nrec = int(multi_line_strip(mfag).split()[0])
                        if nrec == -1:
                            irr = np.copy(irr_diversion[per - 1])
                        else:
                            if model.version2 == "gsflow":
                                irr = ModflowAg.get_empty(
                                    nrec,
                                    maxells=maxcellsdiversion,
                                    block="irrdiversion",
                                )
                            else:
                                irr = flopy.modflow.ModflowAg.get_empty(
                                    nrec,
                                    maxells=maxcellsdiversion,
                                    block="irrdiversion",
                                )

                            # read blocks 20 & 21
                            irr = _read_block_21_25_or_29(mfag, nrec, irr, 21)
                        irr_diversion[per] = irr
                        line = multi_line_strip(mfag)

                    # block 22
                    elif "irrwell" in line:
                        # read block 23
                        nrec = int(multi_line_strip(mfag).split()[0])
                        if nrec == -1:
                            irr = np.copy(irr_well[per - 1])
                        else:
                            if model.version2 == "gsflow":
                                irr = ModflowAg.get_empty(nrec,
                                                          maxells=maxcellswell,
                                                          block="irrwell")
                            else:
                                irr = flopy.modflow.ModflowAg.get_empty(
                                    nrec,
                                    maxells=maxcellswell,
                                    block="irrwell")

                            # read blocks 24 & 25
                            irr = _read_block_21_25_or_29(mfag, nrec, irr, 25)

                        irr_well[per] = irr
                        line = multi_line_strip(mfag)

                    # block 26
                    elif "supwel" in line:
                        # read block 27
                        nrec = int(multi_line_strip(mfag).split()[0])
                        if nrec == -1:
                            sup = np.copy(sup_well[per - 1])

                        else:
                            sup = ModflowAg.get_empty(nrec,
                                                      maxells=maxdiversions,
                                                      block="supwell")
                            # read blocks 28 & 29
                            sup = _read_block_21_25_or_29(mfag, nrec, sup, 29)

                        sup_well[per] = sup
                        line = multi_line_strip(mfag)

                    # block 30?
                    elif "end" in line:
                        if per == nper - 1:
                            break

                        line = multi_line_strip(mfag)
                        break

                    else:
                        raise ValueError(
                            "Something went wrong at: {}".format(line))

        return ModflowAg(
            model,
            options=options,
            time_series=time_series,
            well_list=well,
            irrwell=irr_well,
            irrdiversion=irr_diversion,
            supwell=sup_well,
            nper=nper,
        )
Exemplo n.º 3
0
    def load(f, model, nper=0, method="gsflow", ext_unit_dict=None):
        """
        Method to load the AG package from file

        Parameters
        ----------
        f : str
            filename
        model : gsflow.modflow.Modflow object
            model to attach the ag pacakge to
        nper : int
            number of stress periods in model
        method : str
            "gsflow" or "modflow"
        ext_unit_dict : dict, optional

        Returns
        -------
            ModflowAwu object
        """
        if nper == 0:
            nper = model.nper

        with open(f) as mfag:

            # strip the file header if it exists
            while True:
                line = multi_line_strip(mfag)
                if line:
                    break

            # read the options block
            options = OptionBlock.load_options(mfag, ModflowAwu)

            line = multi_line_strip(mfag)

            time_series = None
            if "time series" in line:
                # read time_series
                t = []
                while True:
                    line = multi_line_strip(mfag)
                    if line == "end":
                        line = multi_line_strip(mfag)
                        break

                    else:
                        t.append(line.split())

                if len(t) > 0:
                    nrec = len(t)
                    time_series = ModflowAwu.get_empty(nrec,
                                                       block="time series")

                    for ix, rec in enumerate(t):
                        if rec[0] in ('welletall', 'wellall'):
                            time_series[ix] = (rec[0], -999, rec[-1])
                        else:
                            time_series[ix] = tuple(rec[:3])

            # read item 1-2 well_list
            well = None
            if "well list" in line:
                # read item 2
                t = []
                while True:
                    line = multi_line_strip(mfag)
                    if line == "end":
                        line = multi_line_strip(mfag)
                        break

                    else:
                        t.append(line.split())

                if len(t) > 0:
                    nrec = len(t)

                    # check if this is block 2a
                    if isinstance(options.tabfiles, np.recarray):
                        tf = True
                        well = ModflowAwu.get_empty(nrec, block='tabfile_well')
                    else:
                        tf = False
                        well = ModflowAwu.get_empty(nrec, block="well")

                    for ix, rec in enumerate(t):
                        if not tf:
                            k = int(rec[0]) - 1
                            i = int(rec[1]) - 1
                            j = int(rec[2]) - 1
                            well[ix] = (k, i, j, rec[3])
                        else:
                            k = int(rec[2]) - 1
                            i = int(rec[3]) - 1
                            j = int(rec[4]) - 1
                            well[ix] = (rec[0], rec[1], k, i, j)

            maxcellsdiversion = 0
            if options.maxcellsdiversion is not None:
                maxcellsdiversion = options.maxcellsdiversion

            maxcellswell = 0
            if options.maxcellswell is not None:
                maxcellswell = options.maxcellswell

            maxdiversions = 0
            if options.maxdiversions is not None:
                maxdiversions = options.maxdiversions

            irr_diversion = {}
            irr_well = {}
            sup_well = {}
            # get the stress period data from blocks 3 - 14
            for per in range(nper):
                while True:
                    if 'stress period' in line:
                        line = multi_line_strip(mfag)

                    # block 3
                    elif 'irrdiversion' in line:
                        # read block 4
                        nrec = int(multi_line_strip(mfag).split()[0])
                        if nrec == -1:
                            irr = np.copy(irr_diversion[per - 1])
                        else:
                            # model.version2 will need to be changed
                            # for pure flopy compatibility if migrated
                            if model.version2 == "gsflow":
                                irr = ModflowAwu.get_empty(
                                    nrec,
                                    maxells=maxcellsdiversion,
                                    block="irrdiversion_gsflow")
                            else:
                                irr = ModflowAwu.get_empty(
                                    nrec,
                                    maxells=maxcellsdiversion,
                                    block="irrdiversion_modflow")

                            # read blocks 5 & 6
                            irr = _read_block_6_10_or_14(mfag, nrec, irr, 6)
                        irr_diversion[per] = irr
                        line = multi_line_strip(mfag)

                    # block 7
                    elif 'irrwell' in line:
                        # read block 8
                        nrec = int(multi_line_strip(mfag).split()[0])
                        if nrec == -1:
                            irr = np.copy(irr_well[per - 1])
                        else:
                            # model.version2 will need to be changed
                            # for pure flopy compatibility if migrated
                            if model.version2 == "gsflow":
                                irr = ModflowAwu.get_empty(
                                    nrec,
                                    maxells=maxcellswell,
                                    block="irrwell_gsflow")
                            else:
                                irr = ModflowAwu.get_empty(
                                    nrec,
                                    maxells=maxcellswell,
                                    block="irrwell_modflow")

                            # read blocks 9 & 10
                            irr = _read_block_6_10_or_14(mfag, nrec, irr, 10)

                        irr_well[per] = irr
                        line = multi_line_strip(mfag)

                    # block 11
                    elif 'supwel' in line:
                        # read block 12
                        nrec = int(multi_line_strip(mfag).split()[0])
                        if nrec == -1:
                            sup = np.copy(sup_well[per - 1])

                        else:
                            sup = ModflowAwu.get_empty(nrec,
                                                       maxells=maxdiversions,
                                                       block="supwell")
                            # read blocks 13 & 14
                            sup = _read_block_6_10_or_14(mfag, nrec, sup, 14)

                        sup_well[per] = sup
                        line = multi_line_strip(mfag)

                    # block 15?
                    elif "end" in line:
                        if per == nper - 1:
                            break

                        line = multi_line_strip(mfag)
                        break

                    else:
                        raise ValueError(
                            "Something went wrong at: {}".format(line))

        return ModflowAwu(model,
                          options=options,
                          time_series=time_series,
                          well_list=well,
                          irrwell=irr_well,
                          irrdiversion=irr_diversion,
                          supwell=sup_well,
                          nper=nper)