Example #1
0
def test_read_write_topo_bowl_hill():
    """
    Test writing and reading topo files.
    """
    temp_path = tempfile.mkdtemp()

    try:
        topo = topotools.Topography(topo_func=topo_bowl_hill)
        topo.x = numpy.linspace(-1.5, 2.5, 101)
        topo.y = numpy.linspace(-1.0, 2.0, 76)

        for topo_type in xrange(1, 4):
            file_path = os.path.join(temp_path, 'bowl_hill.tt%s' % topo_type)
            topo.write(file_path, topo_type=topo_type)
            topo_in = topotools.Topography(path=file_path, topo_type=topo_type)
            assert numpy.allclose(topo.Z, topo_in.Z), \
                   "Written file of topo_type=%s does not equal read in" + \
                   " file." % topo_type

    except AssertionError as e:
        # If the assertion failed then copy the contents of the directory
        shutil.copytree(
            temp_path,
            os.path.join(os.getcwd(), "test_read_write_topo_bowl_hill"))
        raise e
    finally:
        shutil.rmtree(temp_path)
Example #2
0
def test_get_remote_file():
    """Test the ability to fetch a remote file from the web."""

    temp_path = tempfile.mkdtemp()
    try:

        url = "".join(('https://raw.githubusercontent.com/rjleveque/geoclaw/',
                       '5f675256c043e59e5065f9f3b5bdd41c2901702c/src/python/',
                       'geoclaw/tests/kahului_sample_1s.tt2'))
        clawpack.clawutil.data.get_remote_file(url,
                                               output_dir=temp_path,
                                               force=True)

        local_path = os.path.join(temp_path, os.path.basename(url))
        download_topo = topotools.Topography(path=local_path)

        test_path = os.path.join(testdir, "data", os.path.basename(url))
        test_topo = topotools.Topography(path=test_path)

        assert numpy.allclose(download_topo.Z, test_topo.Z), \
               "Downloaded file does not match %s" % test_path
    except AssertionError as e:
        shutil.copy(local_path, os.path.join(os.getcwd(), "remote_file.tt2"))
        raise e
    finally:
        shutil.rmtree(temp_path)
Example #3
0
def convert_topo(location, plot=False):
    """Convert geotiff to topotype 3"""

    for loc_dict in locations[location]:
        topo = None
        if not os.path.exists(loc_dict['out_path']):

            topo = topotools.Topography(path=loc_dict['path'], topo_type=5)
            topo.read()

            if loc_dict['strip_zeros']:
                # Remove 0 values around perimiter
                topo.Z = numpy.flipud(topo.Z[:-1, :-1])

            topo.write(loc_dict['out_path'], topo_type=3)

        if plot:
            if topo is None:
                topo = topotools.Topography(path=loc_dict['out_path'])

            fig = plt.figure()
            axes = fig.add_subplot(1, 1, 1)

            topo.plot(axes=axes,
                      contour_levels=loc_dict['contours'],
                      limits=loc_dict['limits'],
                      cmap=land_cmap)
    if plot:
        plt.show()
Example #4
0
def plot_topo():
    figure(figsize=(12, 8))
    topo1 = topotools.Topography()
    topo1.read('flat.tt2', 2)
    contourf(topo1.x, topo1.y, topo1.Z, linspace(-30, 20, 51), extend='both')
    topo2 = topotools.Topography()
    topo2.read('hilo_flattened.tt2', 2)
    contourf(topo2.x, topo2.y, topo2.Z, linspace(-30, 20, 51), extend='both')
    colorbar()
    x1 = 204.9
    x2 = 204.955
    y1 = 19.715
    y2 = 19.755
    axis([x1, x2, y1, y2])
    gca().set_aspect(1. / cos(y1 * pi / 180.))
    ticklabel_format(format='plain', useOffset=False)
    contour(topo2.x, topo2.y, topo2.Z, [0.], colors='k')
    plot([204.9447], [19.7308], 'ko')  # from BM description
    plot([204.9437], [19.7307], 'ro')  # closer to pier

    # from <http://tidesandcurrents.noaa.gov/stationhome.html?id=1617760>
    # location is listed as: 19 degrees 43.8' N,  155 degrees, 3.3' W
    xg = 360 - (155 + 3.3 / 60.)
    yg = 19 + 43.8 / 60.
    plot([xg], [yg], 'bo')
Example #5
0
def plot_topo_bowl_hill():
    """
    Create topo and write out, then read in again and plot.
    Note that center of bowl should be at (0,0).
    """
    import matplotlib
    matplotlib.use("Agg")  # use image backend -- needed for Travis tests
    import matplotlib.pyplot as plt

    topo = topotools.Topography(topo_func=topo_bowl_hill)
    topo.x = numpy.linspace(-1.5, 2.5, 101)
    topo.y = numpy.linspace(-1.0, 2.0, 76)
    fname = 'bowl_hill.tt2'
    topo = topotools.Topography(fname, topo_type=2)

    topo.plot()
    fname = "bowl_hill.png"
    plt.savefig(fname)
    print "Created ", fname

    topo2 = topo.crop([0.5, 1.5, 0., 2.])
    topo2.plot()
    plt.title("Cropped topography")
    fname = "bowl_hill_crop.png"
    plt.savefig(fname)
    print "Created ", fname
Example #6
0
def test_unstructured_topo(save=False, plot=False):

    try:
        import scipy
    except:
        raise nose.SkipTest("Skipping test since scipy not found")

    # Create random test data
    def func(x, y):
        return x * (1 - x) * numpy.cos(4 * numpy.pi * x) * numpy.sin(
            4 * numpy.pi * y**2)**2

    fill_topo = topotools.Topography()
    fill_topo.x = numpy.linspace(0, 1, 100)
    fill_topo.y = numpy.linspace(0, 1, 200)
    fill_topo.Z = func(fill_topo.X, fill_topo.Y)

    points = numpy.loadtxt(
        os.path.join(testdir, "data", "unstructured_points.txt"))
    values = func(points[:, 0], points[:, 1])

    # Create topography object
    topo = topotools.Topography(unstructured=True)
    topo.x = points[:, 0]
    topo.y = points[:, 1]
    topo.z = values

    if plot:
        import matplotlib.pyplot as plt

        fig = plt.figure(figsize=(16, 6))
        axes = fig.add_subplot(1, 3, 1)
        fill_topo.plot(axes=axes)
        axes.set_title("True Field")
        axes = fig.add_subplot(1, 3, 2)
        topo.plot(axes=axes, region_extent=[0, 1, 0, 1])
        axes.set_title("Unstructured Field")

    topo.interp_unstructured(fill_topo,
                             extent=[0, 1, 0, 1],
                             delta=(1e-2, 1e-2))
    assert not topo.unstructured

    # Load (and save) test data and make the comparison
    test_data_path = os.path.join(testdir, "data",
                                  "unstructured_test_data.tt3")
    if save:
        topo.write(test_data_path)

    compare_data = topotools.Topography(path=test_data_path)

    assert numpy.allclose(compare_data.Z, topo.Z)

    if plot:
        axes = fig.add_subplot(1, 3, 3)
        topo.plot(axes=axes)
        axes.set_title("Interpolated Field")

        plt.show()
Example #7
0
def extract_bathy(fill_paths,
                  region,
                  topo_path='./bathymix.xyz',
                  out_bathy="new_acapulco_bathy.tt2"):

    # Read in concatenated data and transform it
    print "Reading fine data..."
    orig_data = topotools.Topography(topo_path, unstructured=True)
    orig_data.x, orig_data.y = UTM2longlat(orig_data.x, orig_data.y)
    orig_data.z = -orig_data.z
    print "Done."

    # Project data into new grid and write it out
    # Specify custom region for projection
    filter_region = list(orig_data.extent)
    buffer_degrees = topotools.dist_meters2latlong(1e3, 0.0,
                                                   numpy.mean(orig_data.y))[0]
    filter_region[0] -= buffer_degrees
    filter_region[1] += buffer_degrees
    # filter_region[2] = 16.8128
    filter_region[2] -= buffer_degrees
    filter_region[3] += buffer_degrees

    # Create fill topography objects using filters and transforming coordinates
    print "Reading fill topography..."
    fig = plt.figure()
    axes = fig.add_subplot(1, 1, 1)
    fill_topo = [
        topotools.Topography(path, unstructured=True) for path in fill_paths
    ]
    for (n, topo) in enumerate(fill_topo):
        topo.no_data_value = 0.0
        topo.read(mask=True)
        topo.x, topo.y = UTM2longlat(topo.x, topo.y)
        indices = mlab.find((numpy.abs(topo.z) > 1e-2))
        print "Throwing out %s/%s points." % (indices.shape[0],
                                              topo.x.shape[0])
        topo.x = topo.x.take(indices)
        topo.y = topo.y.take(indices)
        topo.z = topo.z.take(indices)
        if n == 0:
            topo.plot(axes=axes, region_extent=region, add_colorbar=True)
        else:
            topo.plot(axes=axes, region_extent=region, add_colorbar=False)
    plt.savefig("fill_topo.png")
    print "Done reading fill topography..."

    print "Interpolating unstructured data..."
    orig_data.interp_unstructured(fill_topo, extent=region, delta_limit=20.0)
    orig_data.write(out_bathy, topo_type=2)
    print "Done projecting data."

    # Plot final topography
    print "Plotting final topography."
    acapulco_final_topo = topotools.Topography(out_bathy)
    acapulco_final_topo.plot()

    plt.savefig("final_topo.png")
def create_fake_topo(X, Y, island, bathy_name):
    topo = topotools.Topography()
    # create topography based on the simulated shelf
    x = X[0]
    y = Y[0]
    topo = topotools.Topography()
    topo.x = x
    topo.y = y
    topo.Z = island
    # generate the 2d grid and save the file
    topo.generate_2d_coordinates()
    topo.write(path=bathy_name, topo_type=3)
Example #9
0
def test_netcdf():
    r"""Test Python NetCDF formatted topography reading"""

    temp_path = tempfile.mkdtemp()

    try:
        # Fetch comparison data
        url = "".join(('https://raw.githubusercontent.com/rjleveque/geoclaw/',
                       '5f675256c043e59e5065f9f3b5bdd41c2901702c/src/python/',
                       'geoclaw/tests/kahului_sample_1s.tt2'))
        clawpack.clawutil.data.get_remote_file(url,
                                               output_dir=temp_path,
                                               force=True)

        # Paths
        local_path = os.path.join(temp_path, os.path.basename(url))
        nc_path = os.path.join(temp_path, "test.nc")

        # Write out NetCDF version of file
        ascii_topo = topotools.Topography(path=local_path)
        ascii_topo.read()
        ascii_topo.write(nc_path, topo_type=4, Z_format="%22.15e")

        # Read back in NetCDF file
        nc_topo = topotools.Topography(path=nc_path)
        nc_topo.read()

        # Compare arrays - use tolerance based on 30 arcsecond accuracy
        assert numpy.allclose(ascii_topo.x, nc_topo.x), \
                    "Flat x-arrays did not match."
        assert numpy.allclose(ascii_topo.y, nc_topo.y), \
                    "Flat y-arrays did not match."
        assert numpy.allclose(ascii_topo.Z, nc_topo.Z), \
                    "Flat y-arrays did not match."

    except AssertionError as e:
        shutil.copytree(temp_path, os.path.join(os.getcwd()),
                        'test_read_netcdf')
        raise e

    except ImportError as e:
        raise nose.SkipTest("Skipping test since NetCDF support not found.")

    except RuntimeError as e:
        raise nose.SkipTest("NetCDF topography test skipped due to " +
                            "runtime failure.")
    except URLError:
        raise nose.SkipTest("Could not fetch remote file, skipping test.")

    finally:
        shutil.rmtree(temp_path)
Example #10
0
def test_read_write_topo_bowl():
    """
    Test writing and reading topo files with small number of points
    Note that ordering should go from NW corner.
    """

    # Base topography
    topo = topotools.Topography(topo_func=topo_bowl)
    topo.x = numpy.linspace(-1.0, 3.0, 5)
    topo.y = numpy.linspace(0.0, 3.0, 4)

    assert numpy.allclose(topo.x, numpy.array([-1.,  0.,  1.,  2., 3.])), \
           "Topography x values are incorrect."
    assert numpy.allclose(topo.X,
                          numpy.array([[-1.,  0.,  1.,  2.,  3.],
                                       [-1.,  0.,  1.,  2.,  3.],
                                       [-1.,  0.,  1.,  2.,  3.],
                                       [-1.,  0.,  1.,  2.,  3.]])), \
           "Topography X values are incorrect."
    assert numpy.allclose(topo.y, numpy.array([ 0.,  1.,  2.,  3.])), \
           "Topography y values are incorrect."
    assert numpy.allclose(topo.Y,
                          numpy.array([[ 0.,  0.,  0.,  0.,  0.],
                                       [ 1.,  1.,  1.,  1.,  1.],
                                       [ 2.,  2.,  2.,  2.,  2.],
                                       [ 3.,  3.,  3.,  3.,  3.]])), \
           "Topography Y values are incorrect."
    assert numpy.allclose(topo.Z,
                numpy.array([[     0.,  -1000.,      0.,   3000., 8000.],
                             [  1000.,      0.,   1000.,   4000.,   9000.],
                             [  4000.,   3000.,   4000.,   7000.,  12000.],
                             [  9000.,   8000.,   9000.,  12000.,  17000.]])), \
           "Topography Z values are incorrect."

    temp_path = tempfile.mkdtemp()
    try:
        for topo_type in xrange(1, 4):
            path = os.path.join(temp_path, 'bowl.tt%s' % topo_type)
            topo.write(path, topo_type=topo_type)

            topo_in = topotools.Topography(path)
            assert numpy.allclose(topo.Z, topo_in.Z), \
                   "Differnece in written and read topography found."
    except AssertionError as e:
        # If the assertion failed then copy the contents of the directory
        shutil.copytree(temp_path,
                        os.path.join(os.getcwd(), "test_read_write_topo_bowl"))
        raise e
    finally:
        shutil.rmtree(temp_path)
Example #11
0
def slice_region(topo_outfile="Tohoku/65_05_2_topo.tt3", bathy_outfile="Tohoku/65_05_2_bathy.tt3", \
    bathy_filename='./Tohoku/tohoku_topo_whitehead.tt3', topo_filename='./Tohoku/srtm_65_05.asc', shore_plots=False, \
    filter=[140,141.15,35,38.1]):
    """
    Takes in two different topography/bathymetry files, returns cutouts of those
    files at desired coordinates

    Parameters:
        topo_outfile(string): filename for writing out samller topography object
        bathy_outfile(string) : filename for writing out smaller bathymetry object
        bathy_filename(string) : filename to read in bathymetry(or lower resolution file)
        topo_filename(string) : filename to read in topography(or higher resolution file)
        shore_plots(bool) : If True, displays shore plots of the two cutout pieces
        filter(list) : Coordinates to create cutout of larger files in form [xlower,xupper,ylower,yupper]

    Note: In some distributions of Topotools the plot function has not been
    updated to new matplotlib standards and will not work. In this case you may
    either plot shores or change source code in topotools.py to the correct parameter
    """
    # Create instance of Topography class for topo file and bathymetry file
    topo_file = topo.Topography()
    bathy_file = topo.Topography()

    # read in topography and bathymetry files

    topo_file.read(topo_filename, topo_type=3)    # high res topography with -9999 values on sea
    bathy_file.read(bathy_filename, topo_type=3)  # low res bathymetry and topography
    # specify topo_type(format) for your files
    # topotools has a built in function to tell you topo_type of any file if needed

    # Crop both files to the same coordinates, [xlower,xupper,ylower,yupper]
    bathy_small = bathy_file.crop(filter_region=filter)
    topo_small = topo_file.crop(filter_region=bathy_small.extent)

    # At this point I recommend creating a shoreline and plotting to verify filter region cut
    if shore_plots == True:
        # Create shorelines in 2xn matrix
        shore_topo_small = topo_small.make_shoreline_xy()
        shore_bathy_small = bathy_small.make_shoreline_xy()
        # Plot both shorelines next to each other
        plt.subplot(221)
        plt.plot(shore_topo_small[:,0],shore_topo_small[:,1])
        plt.subplot(222)
        plt.plot(shore_bathy_small[:,0],shore_bathy_small[:,1])
        plt.show()

    # Write out both files for use in interpolation written by Adam Robertson
    bathy_small.write(bathy_outfile, topo_type=3) # low res
    topo_small.write(topo_outfile, topo_type=3)   # high res
Example #12
0
    def setUp(self):

        super(DTopoTests, self).setUp()

        # Make topography
        h0 = 1000.0
        topo_func = lambda x,y: -h0*(1 + 0.5 * numpy.cos(x - y))
        topo = topotools.Topography(topo_func=topo_func)
        topo.topo_type = 2
        topo.x = numpy.linspace(-10.0, 10.0, 201)
        topo.y = numpy.linspace(-10.0, 10.0, 201)
        topo.write(os.path.join(self.temp_path, "topo1.topotype2"))

        h0 = 1000.0
        topo_func = lambda x,y: -h0*(1. + numpy.exp(x+y))
        topo = topotools.Topography(topo_func=topo_func)
        topo.topo_type = 2
        topo.x = numpy.linspace(-0.5, -0.3, 21)
        topo.y = numpy.linspace(-0.1, 0.4, 51)
        topo.write(os.path.join(self.temp_path, "topo2.topotype2"))

        # Make dtopography
        subfault_path = os.path.join(self.test_path, "dtopo1.csv")
        input_units = {'slip': 'm', 'depth': 'km', 'length': 'km', 'width': 'km'}
        fault = dtopotools.CSVFault()
        fault.read(subfault_path, input_units=input_units, 
                coordinate_specification="top center")
        fault.rupture_type = 'dynamic'
        times = numpy.linspace(0.0, 1.0, 25)
        x = numpy.linspace(-0.4,0.6,151)
        y = numpy.linspace(-0.4,0.4,121)
        dtopo = fault.create_dtopography(x,y,times=times)
        dtopo.write(os.path.join(self.temp_path, "dtopo1.tt3"), dtopo_type=3)

        subfault_path = os.path.join(self.test_path, "dtopo2.csv")
        input_units = {'slip': 'm', 'depth': 'km', 'length': 'km', 'width': 'km'}
        fault = dtopotools.CSVFault()
        fault.read(subfault_path, input_units=input_units, 
                    coordinate_specification="top center")
        fault.rupture_type = 'dynamic'
        times = numpy.linspace(0.5, 1.2, 25)
        x = numpy.linspace(-0.9,0.1,201)
        y = numpy.linspace(-0.4,0.4,161)
        dtopo = fault.create_dtopography(x,y,times=times)
        dtopo.write(os.path.join(self.temp_path, "dtopo2.tt3"), dtopo_type=3)

        # copy existing file:
        shutil.copy(os.path.join(self.test_path, "dtopo3.tt1"),
                                 self.temp_path)
Example #13
0
def plot_topo(path, stride=[1, 1], axes=None):
    """"""

    if axes is None:
        fig, axes = plt.subplots(1, 1)

    topo = topotools.Topography(path=path)

    for i in range(len(strips)):
        topo = topotools.Topography(
            path=os.path.join(output_dir, "strip%s.nc" % i))
        topo.read(stride=stride)
        topo.plot(axes=axes)

    return axes
Example #14
0
def plot_topo_big():
    figure(figsize=(8, 12))
    topo1 = topotools.Topography()
    topo1.read('flat.tt2', 2)
    contourf(topo1.x, topo1.y, topo1.Z, linspace(-30, 20, 51), extend='both')
    topo2 = topotools.Topography()
    topo2.read('hilo_flattened.tt2', 2)
    contourf(topo2.x, topo2.y, topo2.Z, linspace(-30, 20, 51), extend='both')
    x1 = 204.90028
    x2 = 204.96509
    y1 = 19.71
    y2 = 19.95
    plot([x1, x2, x2, x1, x1], [y1, y1, y2, y2, y1], 'w')
    axis('scaled')
    colorbar()
Example #15
0
def get_topo(makeplots=False):
    """
    Retrieve the topo file from the GeoClaw repository.
    """
    from clawpack.geoclaw import topotools, etopotools

    topo_fname = 'etopo1_-140_-60_-60_10_10min.tt3'

    if os.path.exists(topo_fname):
        print("*** Not regenerating dtopo file (already exists): %s" \
                    % dtopo_fname)
    else:
        xlimits = (-140, -60)
        ylimits = (-60, 10)
        resolution = 10. / 60.  # degrees

        topo = etopotools.etopo1_download(xlimits,ylimits, dx=resolution, \
                output_dir=scratch_dir, file_name=topo_fname, return_topo=True)

    if makeplots:
        from matplotlib import pyplot as plt
        topo = topotools.Topography(os.path.join(scratch_dir, topo_fname),
                                    topo_type=2)
        topo.plot()
        fname = os.path.splitext(topo_fname)[0] + '.png'
        plt.savefig(fname)
        print("Created ", fname)
Example #16
0
def test_crop_topo_bowl():
    """
    Test cropping a topo file.
    """

    topo = topotools.Topography(topo_func=topo_bowl)
    topo.x = numpy.linspace(-1.0, 3.0, 5)
    topo.y = numpy.linspace(0.0, 3.0, 4)

    # topo.Z should be created automatically when referenced below:
    assert numpy.allclose(topo.Z,
           numpy.array([[     0.,  -1000.,      0.,   3000., 8000.],
                        [  1000.,      0.,   1000.,   4000.,   9000.],
                        [  4000.,   3000.,   4000.,   7000.,  12000.],
                        [  9000.,   8000.,   9000.,  12000.,  17000.]])), \
           "Basic topography does not match test data."

    cropped_topo = topo.crop([0, 1, 0, 2])
    assert numpy.allclose(cropped_topo.x, numpy.array([0.0, 1.0])), \
           "Cropped topography y values do not match"
    assert numpy.allclose(cropped_topo.y, numpy.array([ 0.,  1.,  2.])), \
           "Cropped topography y values do not match."
    assert numpy.allclose(cropped_topo.Z,
                          numpy.array([[-1000.,     0.],
                                       [    0.,  1000.],
                                       [ 3000.,  4000.]])), \
           "Cropped topography Z values do not match."
Example #17
0
def get_topo(output_dir=None, force=False, plot=False, verbose=False):
    """Retrieve ETOPO1 data from NOAA"""

    if output_dir is None:
        output_dir = os.getcwd()

    strips = [-45.0, -30.0, -15.0, 0.0, 15.0, 30.0]
    for (i, lower_bound) in enumerate(strips):
        file_name = "strip%s.nc" % i
        URL = form_etopo_URL([-180, lower_bound], [180, lower_bound + 15.0],
                             file_name=file_name)
        file_path = os.path.join(output_dir, file_name)
        if os.path.exists(file_path) and (not force):
            print("Skipping download... file already exists: ", file_path)

        else:
            data.get_remote_file(URL,
                                 output_dir=output_dir,
                                 file_name=file_name,
                                 verbose=verbose,
                                 force=force)

            # TODO: Check output for errors

    if plot:
        fig = plt.figure()
        axes = fig.add_subplot(1, 1, 1)
        for i in range(len(strips)):
            topo = topotools.Topography(
                path=os.path.join(output_dir, "strip%s.nc" % i))
            topo.read(stride=[100, 100])
            topo.plot(axes=axes)
        plt.show()
Example #18
0
def get_topo(makeplots=False):
    """
        Retrieve the topo file from online.
    """
    from clawpack.geoclaw import topotools
    topo_fname = 'etopo1min170E124W40N61N.asc'
    url = 'http://students.washington.edu/bndavis/misc/topo/' + topo_fname
    clawpack.clawutil.data.get_remote_file(url,
                                           output_dir=scratch_dir,
                                           file_name=topo_fname,
                                           verbose=True)

    topo_fname = 'etopo4min120E110W0N62N.asc'
    url = 'http://students.washington.edu/bndavis/misc/topo/' + topo_fname
    clawpack.clawutil.data.get_remote_file(url,
                                           output_dir=scratch_dir,
                                           file_name=topo_fname,
                                           verbose=True)

    if makeplots:
        from matplotlib import pyplot as plt
        topo = topotools.Topography(topo_fname, topo_type=2)
        topo.plot()
        fname = os.path.splitext(topo_fname)[0] + '.png'
        plt.savefig(fname)
        print "Created ", fname
Example #19
0
def maketopo_onshore():

    try:
        x = loadtxt('x_onshore.txt')
        y = loadtxt('y_onshore.txt')
        z = loadtxt('z_onshore.txt')
    except:
        raise Exception("Did you create onshore topo files?  See README.md")

    # modify x and y so that cell size is truly uniform:
    xx = arange(x.min(), x.min() + 0.01 * (len(x) - 1) + .001, 0.01)
    yy = arange(y.min(), y.min() + 0.01 * (len(y) - 1) + .001, 0.01)
    zz = z - 0.97  # shift so sea_level = 0

    topo = topotools.Topography()
    topo.x = xx
    topo.y = yy
    topo.Z = zz

    topo.write('seaside_onshore.tt2', topo_type=2)

    figure()
    contourf(xx, yy, zz.T, linspace(-1, 0.4, 15))
    axis('scaled')
    colorbar()
Example #20
0
def write_topo_file(run_data, out_file, **kwargs):
    """Create simple topography"""

    # Make topography
    topo = tt.Topography()
    topo.x = numpy.linspace(run_data.clawdata.lower[0], 
                            run_data.clawdata.upper[0], 
                            run_data.clawdata.num_cells[0] + 8)
    topo.y = numpy.linspace(run_data.clawdata.lower[1], 
                            run_data.clawdata.upper[1], 
                            run_data.clawdata.num_cells[1] + 8)

    # Create bathymetry profile
    beach_slope = 0.05
    basin_depth = -3000
    shelf_depth = -200
    x0 = 350e3
    x1 = 450e3
    x2 = 480e3
    topo_profile = [(run_data.clawdata.lower[0], basin_depth),
                    (x0, basin_depth), (x1, shelf_depth), (x2, shelf_depth),
                    (run_data.clawdata.upper[0], 
                            beach_slope * (run_data.clawdata.upper[0] - x2) 
                            + shelf_depth)]
    topo.topo_func = tt.create_topo_func(topo_profile)
    topo.write(out_file)

    return topo
def create_dtopo(location, scenario_name, N=100, estimate_mass=True, 
                 force=False, plot=False, topo_path=None):

    scenario = locations[location]["scenarios"][scenario_name]
    extent = locations[location]['extent']
    path = os.path.join("..", location, "%s.tt3" % scenario_name)

    if os.path.exists(path):
        if force:
            os.remove(path)
        else:
            print("Slide file already exists.")
            sys.exit(0)

    # Create dtopo
    dtopo = dt.DTopography()
    dtopo.x = numpy.linspace(extent[0], extent[1], N)
    dtopo.y = numpy.linspace(extent[2], extent[3], N)
    dtopo.X, dtopo.Y = numpy.meshgrid(dtopo.x, dtopo.y)
    dtopo.times = numpy.linspace(0, scenario['t_end'], 8)
    dtopo.dZ = numpy.empty((dtopo.times.shape[0], dtopo.x.shape[0], 
                                                  dtopo.y.shape[0]))

    for (i, t) in enumerate(dtopo.times):
        dtopo.dZ[i, :, :] = slide_topo(dtopo.X, dtopo.Y, t, 
                                       scenario["start"],
                                       scenario["slide_speed"],
                                       scenario["max_length"],
                                       scenario["theta"],
                                       scenario["sigma"],
                                       scenario["A"],
                                       estimate_mass=estimate_mass)

    dtopo.write(path=path)

    if plot:
      # Load topo for comparison
      topo = tt.Topography(path=locations[location]["topo_path"], topo_type=3)

      if not os.path.exists("./%s" % scenario_name):
          os.mkdir(scenario_name)

      for i in range(dtopo.times.shape[0] - 1, -1, -1):
          fig = plt.figure()
          axes = fig.add_subplot(1, 1, 1)
          extent = [numpy.min(dtopo.x), numpy.max(dtopo.x),
                    numpy.min(dtopo.y), numpy.max(dtopo.y)]
          axes.pcolor(dtopo.x, dtopo.y, dtopo.dZ_at_t(dtopo.times[i]))
          axes.contour(topo.x, topo.y, topo.Z, levels=[5000], colors='w')
          axes.set_xlim(extent[:2])
          axes.set_ylim(extent[2:])

          axes.set_title("t = %s" % str(dtopo.times[i]))
          fig.savefig('./%s/slide_%s.png' % (scenario_name, i))

      # make a movie
      cmd = r"ffmpeg -r 2 -i %s" % scenario_name + r"/slide_%1d.png " + \
            r"-q:a 0 -q:v 0 -vcodec mpeg4 -vb 20M -r 24 %s/%s.mp4" % (scenario_name, 
                                                                      scenario_name)
      subprocess.call(cmd, shell=True)
Example #22
0
def test_etopo1_topo(make_plot=False, save=False):
    
    try:
        import netCDF4
    except:
        raise nose.SkipTest("netCDF4 not installed, skipping test")
        
    topo1 = topotools.read_netcdf('etopo1', extent=extent, verbose=True)

    topo10 = topotools.read_netcdf('etopo1', extent=extent, 
                                   coarsen=10, verbose=True)

    testdata_path = os.path.join(os.path.dirname(__file__), 'data', 'etopo1_10min.asc')
    if save:
        topo10.write(testdata_path, topo_type=3, Z_format='%.0f')
        print('Created %s' % testdata_path)

    topo10input = topotools.Topography()
    topo10input.read(testdata_path, topo_type=3)
    
    assert numpy.allclose(topo10.Z, topo10input.Z), \
           "topo10.Z does not agree with archived data"
    
    if make_plot:
        import matplotlib.pyplot as plt
        plt.figure(figsize=(12,5))
        ax1 = plt.subplot(1,2,1)
        topo1.plot(axes=ax1)
        plt.title('1 minute etopo1 data')
        ax10 = plt.subplot(1,2,2)
        topo10.plot(axes=ax10)
        plt.title('10 minute etopo1 data')
        pname = 'etopo1_test_plot.png'
        plt.savefig(pname)
        print('Created %s' % pname)
Example #23
0
    def ps4_to_arrays(self, verbose=True):
        """
        for point_style==4, convert lists of fgmax values into masked arrays
        based on the topo_style==3 file self.xy_fname that was used to specify
        the fgmax points in the GeoClaw run.
        """

        from numpy import ma
        assert self.point_style == 4, '*** Requires point_style==4'

        if self.X.ndim == 2 or self.Y.ndim == 2:
            print('*** X and Y already 2d, not converting')
            return

        x_1d = self.X
        y_1d = self.Y

        if verbose:
            print('Will map fgmax points onto masked arrays defined by file:')
            print('     %s' % self.xy_fname)

        pts_chosen = topotools.Topography(path=self.xy_fname, topo_type=3)
        X = pts_chosen.X
        Y = pts_chosen.Y
        mask = numpy.logical_not(pts_chosen.Z)
        x1 = X.min()
        y1 = Y.min()

        # possible arrays from GeoClaw output to convert:
        zarrays = ['level','B','h','h_time','s','s_time','hs','hs_time',\
                   'hss','hss_time','hmin','hmin_time','arrival_time']

        dx = X[0, 1] - X[0, 0]
        dy = Y[1, 0] - Y[0, 0]
        if verbose:
            print('Deduced dx = %g, dy = %g' % (dx, dy))

        for attr in zarrays:
            z_1d = getattr(self, attr, None)
            if z_1d is None:
                if verbose: print('not converting attribute %s == None' % attr)
            else:
                Z = ma.masked_array(data=numpy.empty(X.shape), mask=True)
                for k in range(len(x_1d)):
                    i = int(round((x_1d[k] - x1) / dx))
                    j = int(round((y_1d[k] - y1) / dy))
                    Z[j, i] = z_1d[k]
                if 0:
                    if not numpy.alltrue(mask == Z.mask):
                        print(
                            '*** converting to arrays gave unexpected mask for'
                        )
                        print('    Z array =  %s' % attr)
                setattr(self, attr, Z)
                if verbose: print('converted %s to 2d array' % attr)

        self.X = X
        self.Y = Y
Example #24
0
    def setUp(self):

        self.temp_path = tempfile.mkdtemp()

        self.stdout = open(os.path.join(self.temp_path, "run_output.txt"), "w")
        self.stdout.write("Output from Test %s\n" % self.__class__.__name__)
        # TODO - Should change this to use the time module's formatting 
        # apparatus
        tm = time.localtime()
        year = str(tm[0]).zfill(4)
        month = str(tm[1]).zfill(2)
        day = str(tm[2]).zfill(2)
        hour = str(tm[3]).zfill(2)
        minute = str(tm[4]).zfill(2)
        second = str(tm[5]).zfill(2)
        date = 'Started %s/%s/%s-%s:%s.%s\n' % (year,month,day,hour,minute,second)
        self.stdout.write(date)
        self.stdout.write(("="*80 + "\n"))

        self.stderr = open(os.path.join(self.temp_path, "error_output.txt"), "w")
        self.stderr.write("Errors from Test %s\n" % self.__class__.__name__)
        self.stderr.write(date)
        self.stderr.write(("="*80 + "\n"))

        self.stdout.flush()
        self.stderr.flush()

        self.stdout.write("Paths:")
        self.stdout.write("  %s" % self.temp_path)
        self.stdout.write("  %s" % self.test_path)
        self.stdout.flush()

        # Make topography
        a = 1.
        h0 = 0.1
        topo_func = lambda x,y: h0 * (x**2 + y**2) / a**2 - h0

        topo = topotools.Topography(topo_func=topo_func)
        topo.x = numpy.linspace(-2.1, 2.1, 210)
        topo.y = numpy.linspace(-2.1, 2.1, 210)
        try:
            topo.write(os.path.join(self.temp_path, "bowl.nc"))
        
        except ImportError:
            # Assume that NetCDF is not installed and move on
            self.netcdf_passed = False
            self.success = True
            raise nose.SkipTest("NetCDF topography test skipped due to " + 
                                "failure to build test program.")

        except RuntimeError as e:
            print(e.message)
            self.netcdf_passed = False
            self.success = True
            raise nose.SkipTest("NetCDF topography test skipped due to " +
                                "runtime failure.")
        else:
            self.build_executable()
Example #25
0
def convert_netcdf_file(path, file_name="indian_ocean_conv.nc"):
    r"""Needed to rename variables in indian ocean topography"""

    indian_ocean_topo = topotools.Topography("indian_ocean.nc", topo_type=4)
    indian_ocean_topo.read(nc_params={"x_var":"lon", 
                                      "y_var":"lat", 
                                      "z_var":"Band1"})

    indian_ocean_topo.write(file_name, topo_type=4)
Example #26
0
def plot_kahului():
    r"""
    Example illustrating reading in a topo file and plotting.
    Uses the test data kahului_sample_1s.tt2, created by cropping 
    the data file obtained from the NGDC site
        http://www.ngdc.noaa.gov/dem/squareCellGrid/download/604
    In addition to using the Topography.plot function, also 
    illustrate how to do a contour data of the data directly.
    """

    try:
        import matplotlib
    except ImportError:
        raise nose.SkipTest("Skipping test since matplotlib not found.")

    matplotlib.use("Agg")  # use image backend -- needed for Travis tests
    import matplotlib.pyplot as plt

    path = os.path.join(testdir, 'kahului_sample_1s.tt2')
    K = topotools.Topography(path, topo_type=2)
    K.plot()
    plt.title("Kahului Harbor at 1 second resolution")

    plt.title("Kahului Harbor at 1 second resolution")
    fname = "kahului_imshow.png"
    plt.savefig(fname)
    print("Created ", fname)

    assert K.Z.shape == (46, 65), "*** K.Z is wrong shape"
    assert numpy.allclose(K.Z[:3,:3], \
                          numpy.array([[ 11.339,  11.339, 11.339],
                                       [ 13.339,  11.339,  11.339],
                                       [ 13.339,  11.339, 10.339]])), \
                "*** Topography K does not match"

    # Make a contour plot of topography / bathymetry:
    plt.figure()
    ax = plt.axes()
    plt.contour(K.X, K.Y, K.Z, numpy.linspace(-20,-2,10), colors='b', \
                linestyles='-')
    plt.contour(K.X, K.Y, K.Z, numpy.linspace(2, 20, 10), colors='g')
    plt.contour(K.X, K.Y, K.Z, [0.], colors='r')  # mean high water

    # fix aspect ratio based on latitude:
    mean_lat = 0.5 * (K.y.max() + K.y.min())
    ax.set_aspect(1.0 / numpy.cos(numpy.pi / 180.0 * mean_lat))

    # fix tick marks so readable:
    ax.ticklabel_format(format="plain", useOffset=False)
    plt.xticks(rotation=20)

    plt.title("2-meter contours of topo (green) and bathymetry (blue)",\
              fontsize=12)
    fname = "kahului_contour.png"
    plt.savefig(fname)
    print("Created ", fname)
Example #27
0
    def setUp(self):

        super(MultilayerTest, self).setUp()

        # Make topography
        topo_func = lambda x, y: bathy_step(
            x, y, location=0.15, angle=numpy.pi / 8.0, left=-1.0, right=-0.2)
        topo = topotools.Topography(topo_func=topo_func)
        topo.x = numpy.linspace(-1.16, 2.16, 166)
        topo.y = numpy.linspace(-1.16, 2.16, 166)
        topo.write(os.path.join(self.temp_path, "jump_topo.topotype2"))
Example #28
0
def write_qinit(A=40.0,
                num_cells=(101, 101),
                lower=(-50.0, -50.0),
                upper=(50.0, 50.0),
                file_name='hump.xyz'):
    r"""Write the initial condition"""

    qinit = lambda x, y: gaussian_hump(x, y, A=A)
    topo = topotools.Topography(topo_func=qinit)
    topo.x = numpy.linspace(lower[0], upper[0], num_cells[0])
    topo.y = numpy.linspace(lower[1], upper[1], num_cells[1])
    topo.write(os.path.join(os.getcwd(), file_name), topo_type=1)
Example #29
0
def write_topo(num_cells=(201, 201),
               lower=(-100.0, -100.0),
               upper=(100.0, 100.0),
               file_name='bowl.topotype2'):
    r"""Write the synthetic bowl topography"""

    topo = topotools.Topography(topo_func=bowl_radial_topo)
    topo.x = numpy.linspace(lower[0], upper[0], num_cells[0])
    topo.y = numpy.linspace(lower[1], upper[1], num_cells[1])
    topo.write(os.path.join(os.getcwd(), file_name),
               topo_type=2,
               Z_format="%22.15e")
Example #30
0
def write_topo_file(run_data, out_file):

    # Make topography
    topo_func = lambda x, y: bathy_step(
        x, y, location=0.15, angle=numpy.pi / 8.0, left=-1.0, right=-0.2)
    topo = tt.Topography(topo_func=topo_func)
    topo.x = numpy.linspace(-1.16, 2.16, 166)
    topo.y = numpy.linspace(-1.16, 2.16, 166)
    topo.write(out_file)

    # Write out simple bathy geometry file for communication to the plotting
    with open("./bathy_geometry.data", 'w') as bathy_geometry_file:
        bathy_geometry_file.write("%s\n%s" % (0.15, numpy.pi / 8.0))