예제 #1
0
def main(name_file, config_file, action, output=None, verbose=None):

    if verbose is True:
        logger.level = logging.DEBUG
    else:
        verbose = False
        logger.level = logging.INFO

    # Try to load files for immediate warnings back to the user
    try:
        with open(name_file) as f:
            f.read()
    except IOError:
        logger.info("Error opening namefile: {!s}".format(name_file))
        return

    try:
        with open(config_file) as f:
            f.read()
    except IOError:
        logger.info("Error opening configuration file: {!s}".format(config_file))
        return

    if action not in ['plot', 'netcdf']:
        logger.error("The 'action' paramter must be 'plot' or 'netcdf'")
        return

    mo = ModflowOutput(name_file, config_file=config_file)
    if action == "plot":
        mo.to_plot()
    else:
        action == "netcdf"
        mo.to_netcdf(output)

    return 0
예제 #2
0
def main(name_file, config_file, action, output=None, verbose=None):

    if verbose is True:
        logger.level = logging.DEBUG
    else:
        verbose = False
        logger.level = logging.INFO

    # Try to load files for immediate warnings back to the user
    try:
        with open(name_file) as f:
            f.read()
    except IOError:
        logger.info("Error opening namefile: {!s}".format(name_file))
        return

    try:
        with open(config_file) as f:
            f.read()
    except IOError:
        logger.info(
            "Error opening configuration file: {!s}".format(config_file))
        return

    if action not in ['plot', 'netcdf']:
        logger.error("The 'action' paramter must be 'plot' or 'netcdf'")
        return

    mo = ModflowOutput(name_file, config_file=config_file)
    if action == "plot":
        mo.to_plot()
    else:
        action == "netcdf"
        mo.to_netcdf(output)

    return 0
예제 #3
0
    def to_plot(self, variable=None, level=None, time=None, colormap=None):

        import matplotlib.cm as cm
        import matplotlib.pyplot as plt
        from mpl_toolkits.mplot3d.axes3d import Axes3D

        if level is not None:
            try:
                level = int(level)
            except ValueError:
                logger.error("Level must be an integer, defaulting to 0.")
                level = 0
        else:
            level = 0

        if time is not None:
            try:
                time = int(time)
            except ValueError:
                logger.error("Time must be an integer, defaulting to 0.")
                time = 0
        else:
            time = 0

        try:
            _, tmp_file = tempfile.mkstemp(suffix=".nc")
            self.to_netcdf(output_file=tmp_file)
            nc = netCDF4.Dataset(tmp_file)

            if variable is not None and variable not in nc.variables:
                raise ValueError("Variable {0} was not found in NetCDF file.  Available variables are: {1}".format(variable, ", ".join([v for v in nc.variables])))

            # Common variables
            x = nc.variables.get("longitude")[:]
            y = nc.variables.get("latitude")[:]
            z = nc.variables.get("elevation")[level, :]

            fig = plt.figure(figsize=(20, 10))

            if colormap is None:
                colormap = cm.Reds

            def plot_thing(rows, columns, spot, camera_height, camera_azimuth):
                ax = fig.add_subplot(rows, columns, spot, projection='3d')
                ax.set_xticks([])
                ax.set_yticks([])
                ax.view_init(camera_height, camera_azimuth)
                if variable is None:
                    ax.set_title('Time: {0} Level: {1}'.format(time, level))
                    p = ax.plot_surface(x, y, z, rstride=1, cstride=1, linewidth=0, cmap=colormap, alpha=0.80)
                    fig.colorbar(p, shrink=0.7)
                else:
                    data = nc.variables.get(variable)
                    data = data[time, level, :, :]
                    m = cm.ScalarMappable(cmap=colormap)
                    m.set_array(data)
                    colors = m.to_rgba(data)[:, :, 0]
                    ax.set_title('Time: {0} Level: {1} Variable: {2}'.format(time, level, variable))
                    p = ax.plot_surface(x, y, z, rstride=1, cstride=1, linewidth=0, alpha=0.80, facecolors=colormap(colors))
                    fig.colorbar(m, shrink=0.7)

            plot_thing(2, 2, 1, 40, 30)
            plot_thing(2, 2, 2, 40, 120)
            plot_thing(2, 2, 3, 40, 210)
            plot_thing(2, 2, 4, 40, 300)

            return plt

        except:
            raise
        finally:
            os.remove(tmp_file)