Пример #1
0
    def create_dump_file(self, workingdirypath: PathLike,
                         filepath: PathLike) -> bool:
        if not isinstance(filepath, Path):
            return False
        ext = filepath.suffix
        if ext not in ['.sql', '.txt', '.dump', '.zip']:
            return False
        if not isinstance(workingdirypath, Path) or not workingdirypath.is_dir:
            return False

        try:
            tmpfilepath = workingdirypath.joinpath(filepath.stem + '.sql')
            cmd = 'mysqldump -h {} -P {} -u {} -p{} --skip-comments {} > {}'.format(
                self.dbparams.db_host, self.dbparams.db_port,
                self.dbparams.db_user, self.dbparams.db_passwd,
                self.dbparams.db_name, str(tmpfilepath.absolute()))
            subprocess.check_call(cmd, shell=True)
            if ext == '.zip':
                with ZipFile(filepath, 'w', ZIP_DEFLATED) as outfile:
                    outfile.write(tmpfilepath, tmpfilepath.name)
                tmpfilepath.unlink()
            else:
                shutil.move(str(tmpfilepath.absolute()),
                            str(filepath.absolute()))
        except Exception:
            return False
        return True
Пример #2
0
def save_preprocessor_to_dir(
    preprocessor: Preprocessor,
    parent_dir: os.PathLike,
) -> os.PathLike:
    """Save preprocessor to file. Returns path saved to."""
    parent_dir = Path(parent_dir)
    with open(parent_dir.joinpath(PREPROCESSOR_KEY), "wb") as f:
        cpickle.dump(preprocessor, f)
Пример #3
0
def load_preprocessor_from_dir(
    parent_dir: os.PathLike, ) -> Optional["Preprocessor"]:
    """Loads preprocessor from directory, if file exists."""
    parent_dir = Path(parent_dir)
    preprocessor_path = parent_dir.joinpath(PREPROCESSOR_KEY)
    if preprocessor_path.exists():
        with open(preprocessor_path, "rb") as f:
            preprocessor = cpickle.load(f)
    else:
        preprocessor = None
    return preprocessor
Пример #4
0
def get_topo_max(soln_dir: os.PathLike, frame_bg: int, frame_ed: int,
                 level: int):
    """Get the maximum elevation during runtime among the time frames at a specific AMR level.

    Arguments
    ---------
    soln_dir : pathlike
        Path to where the solution files are.
    frame_bg, frame_ed : int
        Begining and end frame numbers.
    level : int
        The level of AMR to provess.

    Returns
    -------
    vmax : float
    """

    soln_dir = pathlib.Path(soln_dir).expanduser().resolve()
    vmax = -float("inf")

    for fno in range(frame_bg, frame_ed):

        # aux and solution file of this time frame
        aux = soln_dir.joinpath("fort.a" + "{}".format(fno).zfill(4)).is_file()

        if not aux:  # this time frame does not contain runtime topo data
            continue

        soln = pyclaw.Solution()
        soln.read(fno, str(soln_dir), file_format="binary", read_aux=True)

        # search through AMR grid patches in this solution
        for state in soln.states:
            if state.patch.level != level:
                continue

            vmax = max(vmax, state.aux[0].max())

    if vmax == -float("inf"):
        raise _misc.NoFrameDataError("No AUX found in frames {} to {}.".format(
            frame_bg, frame_ed))

    return vmax
Пример #5
0
def get_soln_extent(soln_dir: os.PathLike, frame_bg: int, frame_ed: int,
                    level: int):
    """Get the bounding box of the results of all time frames at a specific AMR level.

    Arguments
    ---------
    soln_dir : pathlike
        Path to where the solution files are.
    frame_bg, frame_ed : int
        Begining and end frame numbers.
    level : int
        The level of AMR to provess.

    Returns
    -------
    extent : tuple/list
        [xmin, ymin, xmax, ymax] (i.e., [west, south, east, north])
    """

    soln_dir = pathlib.Path(soln_dir).expanduser().resolve()
    extent = [float("inf"), float("inf"), -float("inf"), -float("inf")]

    for fno in range(frame_bg, frame_ed):

        # aux and solution file of this time frame
        aux = soln_dir.joinpath("fort.a" + "{}".format(fno).zfill(4))
        soln = pyclaw.Solution()
        soln.read(fno,
                  str(soln_dir),
                  file_format="binary",
                  read_aux=aux.is_file())

        # search through AMR grid patches in this solution
        for state in soln.states:
            if state.patch.level != level:
                continue

            extent[0] = min(extent[0], state.patch.lower_global[0])
            extent[1] = min(extent[1], state.patch.lower_global[1])
            extent[2] = max(extent[2], state.patch.upper_global[0])
            extent[3] = max(extent[3], state.patch.upper_global[1])

    return extent
Пример #6
0
def get_soln_max(soln_dir: os.PathLike, frame_bg: int, frame_ed: int,
                 level: int):
    """Get the maximum depth of the results of all time frames at a specific AMR level.

    Arguments
    ---------
    soln_dir : pathlike
        Path to where the solution files are.
    frame_bg, frame_ed : int
        Begining and end frame numbers.
    level : int
        The level of AMR to provess.

    Returns
    -------
    vmax : float
    """

    soln_dir = pathlib.Path(soln_dir).expanduser().resolve()
    vmax = -float("inf")

    for fno in range(frame_bg, frame_ed):

        # aux and solution file of this time frame
        aux = soln_dir.joinpath("fort.a" + "{}".format(fno).zfill(4))
        soln = pyclaw.Solution()
        soln.read(fno,
                  str(soln_dir),
                  file_format="binary",
                  read_aux=aux.is_file())

        # search through AMR grid patches in this solution
        for state in soln.states:
            if state.patch.level != level:
                continue

            vmax = max(vmax, state.q[0].max())

    return vmax
Пример #7
0
def get_soln_res(soln_dir: os.PathLike, frame_bg: int, frame_ed: int,
                 level: int):
    """Get the resolution of the grid at a specific AMR level.

    Arguments
    ---------
    soln_dir : pathlike
        Path to where the solution files are.
    frame_bg, frame_ed : int
        Begining and end frame numbers.
    level : int
        The level of AMR to provess.

    Returns
    -------
    dx, dy : float
        Cell size at x and y direction.
    """

    soln_dir = pathlib.Path(soln_dir).expanduser().resolve()

    for fno in range(frame_bg, frame_ed):

        # aux and solution file of this time frame
        aux = soln_dir.joinpath("fort.a" + "{}".format(fno).zfill(4))
        soln = pyclaw.Solution()
        soln.read(fno,
                  str(soln_dir),
                  file_format="binary",
                  read_aux=aux.is_file())

        # search through AMR grid patches, if found desired dx & dy at the level, quit
        for state in soln.states:
            if state.patch.level == level:
                return state.patch.delta

    raise _misc.AMRLevelError("No solutions has AMR level {}".format(level))
Пример #8
0
def write_soln_to_nc(
        nc_file: os.PathLike, soln_dir: os.PathLike, frame_bg: int, frame_ed: int,
        level: int, dry_tol: float, extent: Tuple[float, float, float, float],
        res: float, nodata: int
):
    """Write solutions of time frames to band data of an existing NetCDF raster file.

    This function will first interpolate the simulation results onto a new uniform grid/raster with
    the giiven `extent` and `res` (resolution), and then it writes the solutions on this uniform
    grid to the NetCDF raster file.

    Arguments
    ---------
    nc_file : os.PathLike
        The path to the target NetCDF raster file.
    soln_dir : os.PathLike
        The folder where Clawutil's solution files are.
    frame_bg, frame_ed : int
        The beginning and end frame numbers. The end frame number should be one larger than the real
        end because it will be used in `range` funtion directly.
    level : int
        The target AMR level.
    dry_tol : float
        Depth below `dry_tol` will be treated as dry cells and have value `nodata`.
    extent : Tuple[float, float, float, float]
        The extent/bound of solution raster. The format is [xmin, ymin, xmax, ymax].
    res : float
        The resolution of the output
    nodata : int
        The value indicating a cell being masked.
    """  # pylint: disable=too-many-arguments

    # open the provided NC file and get the root group
    root = netCDF4.Dataset(  # pylint: disable=no-member
        filename=nc_file, mode="r+", encoding="utf-8", format="NETCDF4")

    print("Frame No. ", end="")
    for band, fno in enumerate(range(frame_bg, frame_ed)):

        print("..{}".format(fno), end="")
        sys.stdout.flush()

        # determine whether to read aux
        aux = soln_dir.joinpath("fort.a"+"{}".format(fno).zfill(4)).is_file()

        # read in solution data
        soln = pyclaw.Solution()
        soln.read(fno, str(soln_dir), file_format="binary", read_aux=aux)

        # write the time
        root["time"][band] = soln.state.t

        try:
            # write the depth values
            root["depth"][band, :, :] = _postprocessing.calc.interpolate(
                soln, level, dry_tol, extent, res, nodata)[0]
        except _misc.NoWetCellError:
            root["depth"][band, :, :] = nodata

    print()
    root.close()