示例#1
0
    def __init__(self, pkg, num_dim):
        super(ClawRunData, self).__init__()
        self.add_attribute('pkg', pkg)
        self.add_attribute('num_dim', num_dim)
        self.add_attribute('data_list', [])
        self.add_attribute('xclawcmd', None)

        # Always need the basic clawpack data object
        self.add_data(ClawInputData(num_dim), 'clawdata')

        # Add package specific data objects
        if pkg.lower() in ['classic', 'classicclaw']:
            self.xclawcmd = 'xclaw'

        elif pkg.lower() in ['amrclaw', 'amr']:

            import clawpack.amrclaw.data as amrclaw

            self.xclawcmd = 'xamr'
            self.add_data(ClawInputData(num_dim), 'clawdata')
            self.add_data(amrclaw.AmrclawInputData(self.clawdata), 'amrdata')
            self.add_data(amrclaw.RegionData(num_dim=num_dim), 'regiondata')
            self.add_data(amrclaw.GaugeData(num_dim=num_dim), 'gaugedata')

        elif pkg.lower() in ['geoclaw']:

            import clawpack.amrclaw.data as amrclaw
            import clawpack.geoclaw.data as geoclaw

            self.xclawcmd = 'xgeoclaw'

            # Required data set for basic run parameters:
            self.add_data(amrclaw.AmrclawInputData(self.clawdata), 'amrdata')
            self.add_data(amrclaw.RegionData(num_dim=num_dim), 'regiondata')
            self.add_data(amrclaw.GaugeData(num_dim=num_dim), 'gaugedata')
            self.add_data(geoclaw.GeoClawData(), ('geo_data'))
            self.add_data(geoclaw.TopographyData(), 'topo_data')
            self.add_data(geoclaw.DTopoData(), 'dtopo_data')
            self.add_data(geoclaw.RefinementData(), 'refinement_data')
            self.add_data(geoclaw.FixedGridData(), 'fixed_grid_data')
            self.add_data(geoclaw.QinitData(), 'qinit_data')
            self.add_data(geoclaw.FGmaxData(), 'fgmax_data')

            # can remove try-except after PR clawpack/geoclaw#80 is merged
            try:
                self.add_data(geoclaw.SurgeData(), 'surge_data')
                self.add_data(geoclaw.FrictionData(), 'friction_data')
            except:
                pass

        else:
            raise AttributeError("Unrecognized Clawpack pkg = %s" % pkg)
示例#2
0
    def do_plotgauge(self, rest):
        from clawpack.visclaw import gaugetools
        import os
        outdir = os.path.abspath(self.plotdata.outdir)

        # Construct gaugeno list
        if rest in ['', 'all']:
            try:
                import clawpack.amrclaw.data as amrclaw
            except ImportError as e:
                print("You must have AMRClaw installed to plot gauges.")
                print("continuing...")
                return

            gaugedata = amrclaw.GaugeData()
            gaugedata.read(self.plotdata.outdir)
            gaugenos = gaugedata.gauge_numbers
        else:
            gaugenos = [int(rest)]

        # Loop through requested gauges and read in if not in gaugesoln_dict
        for (n, gaugeno) in enumerate(gaugenos):
            # Is the next line necessary or can we delete it?
            gauge = self.plotdata.getgauge(gaugeno, outdir)

            gaugetools.plotgauge(gaugeno, self.plotdata)

            if n < len(gaugenos) - 1:
                ans = input("      Hit return for next gauge or q to quit ")
                if ans == "q":
                    break
示例#3
0
def load_geoclaw_gauge_data(only_gauges=None,
                            base_path="_output",
                            verbose=True):
    r"""Load all gauge data in gauge file at base_path/fort.gauge

    Returns a dictionary of GaugeSolution objects keyed by their gauge numbers.
    """

    gauges = {}

    # Read in gauge.data file
    gauge_info_file = amrclaw.GaugeData()
    gauge_info_file.read(data_path=base_path, file_name='gauges.data')

    if only_gauges is None:
        gauge_list = gauge_info_file.gauge_numbers
    else:
        gauge_list = only_gauges

    locations = {}
    for (n, gauge) in enumerate(gauge_info_file.gauges):
        locations[gauge[0]] = gauge[1:3]

    # Read in each gauge solution
    import time
    import sys
    start = time.clock()
    for (i, gauge_no) in enumerate(gauge_list):
        gauge = gaugetools.GaugeSolution(
            gauge_no, location=gauge_info_file.gauges[i][1:3])
        gauge.read(output_path=base_path, file_name='fort.gauge')
        gauges[gauge_no] = gauge
        if verbose:
            print "Read in GeoClaw gauge %s" % gauge_no
    elapsed = (time.clock() - start)
    print "Single gauge reading elapsed time = %s" % elapsed

    start = time.clock()
    raw_data = np.loadtxt(os.path.join(base_path, 'fort.gauge'))
    raw_numbers = np.array([int(value) for value in raw_data[:, 0]])
    for n in gauge_list:
        gauge = gaugetools.GaugeSolution(n, location=locations[n])
        gauge_indices = np.nonzero(n == raw_numbers)[0]

        gauge.level = [int(value) for value in raw_data[gauge_indices, 1]]
        gauge.t = raw_data[gauge_indices, 2]
        gauge.q = raw_data[gauge_indices, 3:].transpose()
        # gauge_read_string = " ".join((gauge_read_string,str(n)))

        # self.gaugesoln_dict[(n, outdir)] = gauge

    elapsed = (time.clock() - start)
    print "All gauges reading elapsed time = %s" % elapsed
    return gauges
示例#4
0
def read_setgauges(datadir):
    """
    Read the info from setgauges.data.
    """

    try:
        import clawpack.amrclaw.data as amrclaw
    except ImportError as e:
        print "You must have AMRClaw installed to plot gauges."
        print "continuing..."
        return None

    setgauges = amrclaw.GaugeData()
    try:
        setgauges.read(datadir)
    except IOError as e:
        # No gauges.data file was found, ignore this exception
        pass

    return setgauges
示例#5
0
    def getgauge(self, gaugeno, outdir=None, verbose=True):
        """
        ClawPlotData.getgauge:
        Return an object of class clawdata.Gauge containing the solution
        for gauge number gaugeno.

        If self.refresh_gauges == True then this gauge is read from the
        fort.gauge file, otherwise it is read only if the
        the dictionary self.gaugesoln_dict has no key gaugeno.  If it does, the
        gauge has previously been read and the dictionary value is returned.
        """
        # Construct path to file
        if outdir is None:
            outdir = self.outdir
        outdir = os.path.abspath(outdir)

        key = (gaugeno, outdir)

        # Reread gauge data file
        if self.refresh_gauges or (not self.gaugesoln_dict.has_key(key)):
            # Attempt to fetch location and time data for checking
            location = None
            try:
                try:
                    import clawpack.amrclaw.data as amrclaw
                except ImportError as e:
                    print "You must have AMRClaw installed to plot gauges."
                    print "continuing..."
                    return None

                gauge_data = amrclaw.GaugeData()
                gauge_data.read(outdir)

                # Check to make sure the gauge requested is in the data file
                if gaugeno not in gauge_data.gauge_numbers:
                    raise Exception(
                        "Could not find guage %s in gauges data file.")
                # Extract locations from gauge data file to be used with the
                # solutions below
                locations = {}
                for gauge in gauge_data.gauges:
                    locations[gauge[0]] = gauge[1:-2]

            except:
                if verbose:
                    print "*** WARNING *** Could not read gauges.data file from"
                    print "     %s" % outdir
                    print "*** Unable to determine gauge locations"
                    # raise Warning()

            # Read in all gauges
            try:
                file_path = os.path.join(outdir, 'fort.gauge')
                if not os.path.exists(file_path):
                    print '*** Warning: cannot find gauge data file %s' % file_path
                    pass
                else:
                    if verbose:
                        print "Reading gauge data from %s" % file_path
                    raw_data = np.loadtxt(file_path)

                    gauge_read_string = ""
                    if len(raw_data) == 0:
                        print '*** Warning: fort.gauge is empty'
                        gauge_numbers = []
                    else:
                        # Convert type for equality comparison:
                        raw_numbers = np.array(raw_data[:, 0], dtype=int)

                        gauge_numbers = list(set(raw_numbers))
                        gauge_numbers.sort()
                        if verbose:
                            print "In fort.gauge file, found gauge numbers %s" \
                                   % gauge_numbers

                    for n in gauge_numbers:
                        try:
                            loc = locations[n]
                        except:
                            loc = None
                        gauge = gaugetools.GaugeSolution(gaugeno, location=loc)
                        gauge_indices = np.nonzero(n == raw_numbers)[0]

                        gauge.level = [
                            int(value) for value in raw_data[gauge_indices, 1]
                        ]
                        gauge.t = raw_data[gauge_indices, 2]
                        gauge.q = raw_data[gauge_indices, 3:].transpose()
                        gauge.number = n
                        gauge_read_string = " ".join(
                            (gauge_read_string, str(n)))

                        self.gaugesoln_dict[(n, outdir)] = gauge

                    if verbose:
                        print "Read in gauges [%s]" % gauge_read_string[1:]

            except Exception as e:
                print '*** Error reading gauges in ClawPlotData.getgauge'
                print '*** outdir = ', outdir
                import pdb
                pdb.set_trace()
                raise e

        # Attempt to fetch gauge requested
        try:
            return self.gaugesoln_dict[key]
        except Exception as e:
            print '*** Unable to find gauge %d in solution dictionary' % gaugeno
            print '*** Lookup key was %s' % str(key)
            raise e
def load_geoclaw_gauge_data(only_gauges=None,
                            base_path="_output",
                            verbose=True):
    r"""Load all gauge data in gauge file at base_path/fort.gauge

    Returns a dictionary of GaugeSolution objects keyed by their gauge numbers.
    """

    gauges = {}

    # Read in gauge.data file
    gauge_info_file = amrclaw.GaugeData()
    gauge_info_file.read(data_path=base_path, file_name='gauges.data')

    if only_gauges is None:
        gauge_list = gauge_info_file.gauge_numbers
    else:
        gauge_list = only_gauges

    locations = {}
    for (n, gauge) in enumerate(gauge_info_file.gauges):
        locations[gauge[0]] = gauge[1:3]

    # Read in each gauge solution
    import time
    import sys
    start = time.clock()

    # Read in all gauges
    try:
        file_path = os.path.join(base_path, 'fort.gauge')
        if not os.path.exists(file_path):
            print '*** Warning: cannot find gauge data file %s' % file_path
            pass
        else:
            print "Reading gauge data from %s" % file_path
            raw_data = np.loadtxt(file_path)

            gauge_read_string = ""
            raw_numbers = np.array(
                raw_data[:,
                         0], dtype=int)  # Convert type for equality comparison
            for n in gauge_list:
                gauge = gaugetools.GaugeSolution(n, location=locations[n])
                gauge_indices = np.nonzero(n == raw_numbers)[0]

                gauge.level = [
                    int(value) for value in raw_data[gauge_indices, 1]
                ]
                gauge.t = raw_data[gauge_indices, 2]
                gauge.q = raw_data[gauge_indices, 3:].transpose()
                gauge.number = n
                gauge_read_string = " ".join((gauge_read_string, str(n)))

                gauges[n] = gauge

            if verbose:
                print "Read in GeoClaw gauge [%s]" % gauge_read_string[1:]

    except Exception as e:
        print '*** Error reading gauges in ClawPlotData.getgauge'
        print '*** outdir = ', base_path
        raise e

    # for (i,gauge_no) in enumerate(gauge_list):
    #     gauge = gaugetools.GaugeSolution(gauge_no, location=gauge_info_file.gauges[i][1:3])
    #     gauge.read(output_path=base_path, file_name='fort.gauge')
    #     gauges[gauge_no] = gauge

    elapsed = (time.clock() - start)
    print "Single gauge reading elapsed time = %s" % elapsed

    start = time.clock()
    raw_data = np.loadtxt(os.path.join(base_path, 'fort.gauge'))
    raw_numbers = np.array([int(value) for value in raw_data[:, 0]])
    for n in gauge_list:
        gauge = gaugetools.GaugeSolution(n, location=locations[n])
        gauge_indices = np.nonzero(n == raw_numbers)[0]

        gauge.level = [int(value) for value in raw_data[gauge_indices, 1]]
        gauge.t = raw_data[gauge_indices, 2]
        gauge.q = raw_data[gauge_indices, 3:].transpose()
        # gauge_read_string = " ".join((gauge_read_string,str(n)))

        # self.gaugesoln_dict[(n, outdir)] = gauge

    elapsed = (time.clock() - start)
    print "All gauges reading elapsed time = %s" % elapsed
    return gauges