Ejemplo n.º 1
0
Archivo: mfhfb.py Proyecto: ksl0/flopy
    def load(f, model, ext_unit_dict=None):
        """
        Load an existing package.

        Parameters
        ----------
        f : filename or file handle
            File to load.
        model : model object
            The model object (of type: class:`flopy.modflow.mf.Modflow`) 
            to which this package will be added.
        ext_unit_dict : dictionary, optional
            If the arrays in the file are specified using EXTERNAL,
            or older style array control records, then `f` should be a file
            handle.  In this case ext_unit_dict is required, which can be
            constructed using the function
            :class:`flopy.utils.mfreadnam.parsenamefile`.

        Returns
        -------
        hfb : ModflowHfb object
            ModflowHfb object (of type :class:`flopy.modflow.mfbas.ModflowHfb`)

        Examples
        --------

        >>> import flopy
        >>> m = flopy.modflow.Modflow()
        >>> hfb = flopy.modflow.ModflowHfb.load('test.hfb', m)

        """

        if model.verbose:
            sys.stdout.write("loading hfb6 package file...\n")

        if not hasattr(f, "read"):
            filename = f
            f = open(filename, "r")
        # dataset 0 -- header
        while True:
            line = f.readline()
            if line[0] != "#":
                break
        # dataset 1
        t = line.strip().split()
        nphfb = int(t[0])
        mxfb = int(t[1])
        nhfbnp = int(t[2])
        # --check for no-print suppressor
        options = []
        aux_names = []
        if len(t) > 2:
            it = 2
            while it < len(t):
                toption = t[it]
                # print it, t[it]
                if toption.lower() is "noprint":
                    options.append(toption)
                elif "aux" in toption.lower():
                    options.append(" ".join(t[it : it + 2]))
                    aux_names.append(t[it + 1].lower())
                    it += 1
                it += 1
        # --data set 2 and 3
        if nphfb > 0:
            dt = ModflowHfb.get_empty(1).dtype
            pak_parms = mfparbc.load(f, nphfb, dt, model.verbose)
        # --data set 4
        bnd_output = None
        if nhfbnp > 0:
            specified = ModflowHfb.get_empty(nhfbnp)
            for ibnd in range(nhfbnp):
                line = f.readline()
                if "open/close" in line.lower():
                    raise NotImplementedError("load() method does not support 'open/close'")
                t = line.strip().split()
                specified[ibnd] = tuple(t[: len(specified.dtype.names)])

            # --convert indices to zero-based
            specified["k"] -= 1
            specified["irow1"] -= 1
            specified["icol1"] -= 1
            specified["irow2"] -= 1
            specified["icol2"] -= 1

            bnd_output = np.recarray.copy(specified)

        if nphfb > 0:
            partype = ["hydchr"]
            line = f.readline()
            t = line.strip().split()
            nacthfb = int(t[0])
            for iparm in range(nacthfb):
                line = f.readline()
                t = line.strip().split()
                pname = t[0].lower()
                iname = "static"
                par_dict, current_dict = pak_parms.get(pname)
                data_dict = current_dict[iname]
                # print par_dict
                # print data_dict

                par_current = ModflowHfb.get_empty(par_dict["nlst"])

                # --
                if model.mfpar.pval is None:
                    parval = np.float(par_dict["parval"])
                else:
                    try:
                        parval = np.float(model.mfpar.pval.pval_dict[pname])
                    except:
                        parval = np.float(par_dict["parval"])

                # --fill current parameter data (par_current)
                for ibnd, t in enumerate(data_dict):
                    par_current[ibnd] = tuple(t[: len(par_current.dtype.names)])

                # --convert indices to zero-based
                par_current["k"] -= 1
                par_current["irow1"] -= 1
                par_current["icol1"] -= 1
                par_current["irow2"] -= 1
                par_current["icol2"] -= 1

                for ptype in partype:
                    par_current[ptype] *= parval

                if bnd_output is None:
                    bnd_output = np.recarray.copy(par_current)
                else:
                    bnd_output = stack_arrays((bnd_output, par_current), asrecarray=True, usemask=False)

        hfb = ModflowHfb(
            model, nphfb=0, mxfb=0, nhfbnp=len(bnd_output), hfb_data=bnd_output, nacthfb=0, options=options
        )
        return hfb
Ejemplo n.º 2
0
    def load(f, model, ext_unit_dict=None):
        """
        Load an existing package.

        Parameters
        ----------
        f : filename or file handle
            File to load.
        model : model object
            The model object (of type: class:`flopy.modflow.mf.Modflow`) 
            to which this package will be added.
        ext_unit_dict : dictionary, optional
            If the arrays in the file are specified using EXTERNAL,
            or older style array control records, then `f` should be a file
            handle.  In this case ext_unit_dict is required, which can be
            constructed using the function
            :class:`flopy.utils.mfreadnam.parsenamefile`.

        Returns
        -------
        hfb : ModflowHfb object
            ModflowHfb object (of type :class:`flopy.modflow.mfbas.ModflowHfb`)

        Examples
        --------

        >>> import flopy
        >>> m = flopy.modflow.Modflow()
        >>> hfb = flopy.modflow.ModflowHfb.load('test.hfb', m)

        """

        if model.verbose:
            sys.stdout.write('loading hfb6 package file...\n')

        if type(f) is not file:
            filename = f
            f = open(filename, 'r')
        #dataset 0 -- header
        while True:
            line = f.readline()
            if line[0] != '#':
                break
        # dataset 1
        t = line.strip().split()
        nphfb = int(t[0])
        mxfb = int(t[1])
        nhfbnp = int(t[2])
        #--check for no-print suppressor
        options = []
        aux_names = []
        if len(t) > 2:
            it = 2
            while it < len(t):
                toption = t[it]
                #print it, t[it]
                if toption.lower() is 'noprint':
                    options.append(toption)
                elif 'aux' in toption.lower():
                    options.append(' '.join(t[it:it + 2]))
                    aux_names.append(t[it + 1].lower())
                    it += 1
                it += 1
        #--data set 2 and 3
        if nphfb > 0:
            dt = ModflowHfb.get_empty(1).dtype
            pak_parms = mfparbc.load(f, nphfb, dt, model.verbose)
        #--data set 4
        bnd_output = None
        if nhfbnp > 0:
            specified = ModflowHfb.get_empty(nhfbnp)
            for ibnd in xrange(nhfbnp):
                line = f.readline()
                if "open/close" in line.lower():
                    raise NotImplementedError("load() method does not support \'open/close\'")
                t = line.strip().split()
                specified[ibnd] = tuple(t[:len(specified.dtype.names)])

            #--convert indices to zero-based
            specified['k'] -= 1
            specified['irow1'] -= 1
            specified['icol1'] -= 1
            specified['irow2'] -= 1
            specified['icol2'] -= 1

            bnd_output = np.recarray.copy(specified)

        if nphfb > 0:
            partype = ['hydchr']
            line = f.readline()
            t = line.strip().split()
            nacthfb = int(t[0])
            for iparm in xrange(nacthfb):
                line = f.readline()
                t = line.strip().split()
                pname = t[0].lower()
                iname = 'static'
                par_dict, current_dict = pak_parms.get(pname)
                data_dict = current_dict[iname]
                #print par_dict
                #print data_dict

                par_current = ModflowHfb.get_empty(par_dict['nlst'])

                #--
                if model.mfpar.pval is None:
                    parval = np.float(par_dict['parval'])
                else:
                    try:
                        parval = np.float(model.mfpar.pval.pval_dict[pname])
                    except:
                        parval = np.float(par_dict['parval'])

                #--fill current parameter data (par_current)
                for ibnd, t in enumerate(data_dict):
                    par_current[ibnd] = tuple(t[:len(par_current.dtype.names)])

                #--convert indices to zero-based
                par_current['k'] -= 1
                par_current['irow1'] -= 1
                par_current['icol1'] -= 1
                par_current['irow2'] -= 1
                par_current['icol2'] -= 1

                for ptype in partype:
                    par_current[ptype] *= parval

                if bnd_output is None:
                    bnd_output = np.recarray.copy(par_current)
                else:
                    bnd_output = stack_arrays((bnd_output, par_current),
                                              asrecarray=True, usemask=False)


        hfb = ModflowHfb(model, nphfb=0, mxfb=0, nhfbnp=len(bnd_output),
                         hfb_data=bnd_output,
                         nacthfb=0, options=options)
        return hfb