Example #1
0
def write_topo_file(topo_file,topo_type=1,factor=4,bathy_type=None,plot=False,
                        force=False,verbose=False):
    """Creates topography file needed by the simulation"""
    
    if os.path.exists(topo_file):
        if not force:
            ans = raw_input("The file %s already exists, replace: [Y/n] " % topo_file)
            if ans[0].capitalize() == 'N':
                print "File %s already exists, use force if you want to replace the existing file." % topo_file
                return
    print "Creating topography file ",topo_file
    
    # Look at parameters of domain
    data = Data('./amr2ez.data')
    dx = abs(data.xupper-data.xlower) / (data.mx)
    dy = abs(data.yupper-data.ylower) / (data.my)
    d = min(dx,dy)
    mx = int((data.xupper-data.xlower) / d) + 8
    my = int((data.yupper-data.ylower) / d) + 8

    xlower = data.xlower-d*4.0
    ylower = data.ylower-d*4.0
    xupper = data.xupper+d*4.0
    yupper = data.yupper+d*4.0
    
    bathy_profiles = generate_profiles()

    # Pick out bathymetry profile
    if bathy_type is None:
        bathy_profile = 'flat'
    if bathy_type in bathy_profiles.keys():
        bathy_profile = bathy_profiles[bathy_type]
    else:
        err_msg = "Bathy profile %s not found, available options are" % bathy_type
        for name in bathy_profiles.keys():
            err_msg = '\n   '.join((err_msg,name))
        raise Exception(err_msg)
    if verbose:
        print "%s profile: %s" % (bathy_type,bathy_profile)

    # Create and write topography
    bathy_func = util.create_topo_func(bathy_profile,verbose=verbose)
    N = len(bathy_profile)
    if topo_type == 1:
        tt.topo1writer(topo_file,bathy_func,xlower,xupper,ylower,yupper,
            factor*mx,factor*my)
    elif topo_type == 2:
        print (xlower - xupper) / (factor*mx)
        print (ylower - yupper) / (factor*my)
        tt.topo2writer(topo_file,bathy_func,xlower,xupper,ylower,yupper,
            factor*mx,factor*my)
    else:
        raise Exception("Unknown topo type %s requested" % topo_type)

    # Plotting
    if plot:
        plot_profiles({bathy_type:bathy_profile},factor=factor)
Example #2
0
def makeqinit():
    """
    Create qinit data file
    """
    nxpoints=100
    nypoints=100
    xlower=-84.e0
    xupper=-80.e0
    ylower=-49.e0
    yupper=-45.e0
    outfile= "hump.xyz"
    topotools.topo1writer(outfile,qinit,xlower,xupper,ylower,yupper,nxpoints,nypoints)
Example #3
0
def makeqinit():
    """
    Create qinit data file
    """
    nxpoints = 101
    nypoints = 101
    xlower = -50.e0
    xupper = 50.e0
    yupper = 50.e0
    ylower = -50.e0
    outfile = "hump.xyz"
    topotools.topo1writer(outfile, qinit, xlower, xupper, ylower, yupper,
                          nxpoints, nypoints)
Example #4
0
def plot_profiles(profiles,topo_type=1,factor=4,verbose=True):
    
    import tempfile
    import shutil
    import matplotlib.pyplot as plt
    
    # Assume an amr2ez.data file is present
    data = Data('./amr2ez.data')
    dx = abs(data.xupper-data.xlower) / (data.mx)
    dy = abs(data.yupper-data.ylower) / (data.my)
    d = min(dx,dy)
    mx = int((data.xupper-data.xlower) / d) + 8
    my = int((data.yupper-data.ylower) / d) + 8

    xlower = data.xlower-d*4.0
    ylower = data.ylower-d*4.0
    xupper = data.xupper+d*4.0
    yupper = data.yupper+d*4.0
    
    bathy_path = tempfile.mkdtemp()
    grid_size = int(np.ceil(np.sqrt(len(profiles.keys()))))
    index = 1
    plt.figure(1)
    
    for (name,profile) in profiles.iteritems():
        topo_file = os.path.join(bathy_path,name)
        
        # Write out temporary file
        bathy_func = util.create_topo_func(profile,verbose=verbose)
        N = len(profile)
        factor = 4
        if topo_type == 1:
            tt.topo1writer(topo_file,bathy_func,xlower,xupper,ylower,yupper,
                        factor*mx,factor*my)
        elif topo_type == 2:
            tt.topo2writer(topo_file,bathy_func,xlower,xupper,ylower,yupper,
                        factor*mx,factor*my)
        else:
            raise Exception("Invalid topo_type %s." % topo_type)
                        
        # Read it back in
        (X,Y,Z) = tt.topofile2griddata(topo_file,topotype=topo_type)
        slice_index = int(X.shape[0] / 2.0)
        x = X[slice_index,:]
        b = Z[slice_index,:]
        
        limits = [np.min(x),np.max(x),np.min(b)*1.05,np.max(b)*1.05]
        wet_index = np.nonzero(b < 0.0)
        
        plt.subplot(grid_size,grid_size,index)
        plt.hold(True)
        plt.plot(x,b,'k',linewidth=2)
        plt.plot(x[wet_index],np.zeros(x[wet_index].shape),'b')
        plt.axis(limits)
        plt.title(name)
        plt.xlabel('km')
        plt.ylabel('m')
        locs,labels = plt.xticks()
        labels = locs/1e3
        plt.xticks(locs,labels)
        plt.hold(False)
        index = index + 1
        
    plt.show()
        
    shutil.rmtree(bathy_path)