Exemplo n.º 1
0
    def test_create_eq_dir(self):
        """Tests creation of earthquake directory and copying of the cmt
        solution"""

        # Check one cmt file
        with tempfile.TemporaryDirectory() as tmp_dir:

            # Cmtfile path
            cmtfile = os.path.join(DATA_DIR, "CMTSOLUTION")
            # create CMT
            cmt = CMTSource.from_CMTSOLUTION_file(cmtfile)

            # Create CMTSource to extract the file name
            eq_id = cmt.eventname

            # Earthquake directory
            eq_dir = os.path.join(tmp_dir, "eq_" + eq_id)

            # Initialize database skeleton class
            DB = DataBaseSkeleton(basedir=tmp_dir,
                                  cmt_fn=cmtfile,
                                  specfem_dir=self.specfem_dir,
                                  verbose=True)

            # Create eq directory
            DB.create_eq_dirs()

            # check if new path exists
            new_cmt_path = os.path.join(eq_dir, "eq_" + eq_id + ".cmt")
            self.assertTrue(
                os.path.exists(new_cmt_path) and os.path.isfile(new_cmt_path))
            self.assertTrue(
                CMTSource.from_CMTSOLUTION_file(new_cmt_path) == cmt)
Exemplo n.º 2
0
    def test_create_SIM_dir(self):
        """Tests the function that creates the Simulation directories and the
        copies the necessary files from the specfem directory."""

        with tempfile.TemporaryDirectory() as tmp_dir:

            # Cmtfile path
            cmtfile = os.path.join(DATA_DIR, "CMTSOLUTION")

            # create CMT
            cmt = CMTSource.from_CMTSOLUTION_file(cmtfile)

            # Create CMTSource to extract the file name
            eq_id = cmt.eventname

            # Earthquake directory
            eq_dir = os.path.join(tmp_dir, "eq_" + eq_id)

            # Initialize database skeleton class
            DB = DataBaseSkeleton(basedir=tmp_dir,
                                  cmt_fn=cmtfile,
                                  specfem_dir=self.specfem_dir,
                                  overwrite=0,
                                  verbose=True)

            # Create earthquake solution directory
            DB.create_eq_dirs()

            # Create CMT simulation directory
            DB.create_CMT_SIM_dir()

            # Parameters
            attr = [
                "CMT", "CMT_rr", "CMT_tt", "CMT_pp", "CMT_rt", "CMT_rp",
                "CMT_tp", "CMT_depth", "CMT_lat", "CMT_lon"
            ]

            # Subdirectories
            subdirs = ["DATA", "DATABASES_MPI", "OUTPUT_FILES"]

            # Check for all directories
            for at in attr:

                # Attribute path
                test_dir = os.path.join(eq_dir, "CMT_SIMs", at)

                self.assertTrue(os.path.isdir(test_dir))

                # Check if yaml path file exist.
                self.assertTrue(
                    os.path.isfile(os.path.join(test_dir, at + ".yml")))

                # Now check if subdirectories are created
                for _k, _subdir in enumerate(subdirs):
                    test_dir2 = os.path.join(test_dir, _subdir)

                    self.assertTrue(os.path.isdir(test_dir2))

                # Check if link is created
                self.assertTrue(os.path.islink(os.path.join(test_dir, "bin")))
Exemplo n.º 3
0
    def test_create_eq_dir_mult(self):
        """Tests creation of earthquake directory and copying of the cmt
                solution for multiple cmt solution files."""

        # Check multiple cmt files
        with tempfile.TemporaryDirectory() as tmp_dir:

            # Cmtfile path
            cmtfile1 = os.path.join(DATA_DIR, "CMTSOLUTION_TRUE")
            cmtfile2 = os.path.join(DATA_DIR, "CMTSOLUTION_VAR")

            # create CMT
            cmt1 = CMTSource.from_CMTSOLUTION_file(cmtfile1)
            cmt2 = CMTSource.from_CMTSOLUTION_file(cmtfile2)

            # Create CMTSource to extract the file name
            eq_id1 = cmt1.eventname
            eq_id2 = cmt2.eventname

            # Earthquake directory
            eq_dir1 = os.path.join(tmp_dir, "eq_" + eq_id1)
            eq_dir2 = os.path.join(tmp_dir, "eq_" + eq_id2)

            # Initialize database skeleton class
            DB = DataBaseSkeleton(basedir=tmp_dir,
                                  cmt_fn=os.path.join(DATA_DIR,
                                                      "CMTSOLUTION_*"),
                                  specfem_dir=self.specfem_dir,
                                  verbose=True)

            # Create eq directory
            DB.create_eq_dirs()

            # check if new path exists
            new_cmt_path1 = os.path.join(eq_dir1, "eq_" + eq_id1 + ".cmt")
            new_cmt_path2 = os.path.join(eq_dir2, "eq_" + eq_id2 + ".cmt")

            self.assertTrue(
                os.path.exists(new_cmt_path1)
                and os.path.isfile(new_cmt_path1))
            self.assertTrue(
                os.path.exists(new_cmt_path2)
                and os.path.isfile(new_cmt_path2))
Exemplo n.º 4
0
def test_load_quakeML():
    """Test the create directory method"""
    # Check one cmt file
    with tempfile.TemporaryDirectory() as tmp_dir:

        # Cmtfile path
        cmtfile = os.path.join(DATA_DIR, "testCMT")

        # create new directory
        new_xml_path = os.path.join(tmp_dir, "tests.xml")
        xml = read_events(cmtfile)
        xml.write(new_xml_path, format="QUAKEML")

        assert(os.path.exists(new_xml_path)
               and os.path.isfile(new_xml_path))

        print("QuakeML\n", CMTSource.from_quakeml_file(new_xml_path))
        print("CMT\n", CMTSource.from_CMTSOLUTION_file(cmtfile))
        assertDictAlmostEqual(CMTSource.from_quakeml_file(new_xml_path),
                              CMTSource.from_CMTSOLUTION_file(cmtfile))
Exemplo n.º 5
0
    def test__copy_file(self):
        """Test the create directory method"""
        # Check one cmt file
        with tempfile.TemporaryDirectory() as tmp_dir:
            # Cmtfile path
            cmtfile = os.path.join(DATA_DIR, "CMTSOLUTION")

            # Initialize database skeleton class
            DB = DataBaseSkeleton(basedir=tmp_dir,
                                  cmt_fn=cmtfile,
                                  specfem_dir=self.specfem_dir)

            # create new directory
            new_cmt_path = os.path.join(tmp_dir, "blub.cmt")
            DB._copy_file(cmtfile, new_cmt_path, False)

            self.assertTrue(os.path.exists(new_cmt_path)
                            and os.path.isfile(new_cmt_path))

            self.assertTrue(CMTSource.from_CMTSOLUTION_file(new_cmt_path)
                            == CMTSource.from_CMTSOLUTION_file(cmtfile))
Exemplo n.º 6
0
def test_from_CMTSOLUTION_file(cmt):
    origin_time = obspy.UTCDateTime(2001, 9, 9, 23, 59, 17.78)
    cmt_time = origin_time + 2.0
    cmt_true = \
        CMTSource(origin_time=origin_time,
                  pde_latitude=34.0745, pde_longitude=-118.3792,
                  pde_depth_in_m=6400, mb=4.2, ms=4.2, region_tag="Hollywood",
                  eventname="9703873", cmt_time=cmt_time, half_duration=1.0,
                  latitude=34.1745, longitude=-118.4792, depth_in_m=5400.0,
                  m_rr=1.0e22, m_tt=-1.0e22)

    assert cmt == cmt_true
Exemplo n.º 7
0
    def test_write_9par(self):
        """Testing the writing to file
        """

        # Basic standard parameters
        cmt = CMTSource.from_CMTSOLUTION_file(CMTFILE)
        cmt_dir = os.path.dirname(CMTFILE)
        npar = 9
        dm = 10.0**24
        dx = 2000.
        ddeg = 0.02
        outputdir = self.tmpdir

        sfsource = SpecfemSources(cmt,
                                  cmt_dir,
                                  npar=npar,
                                  dm=dm,
                                  dx=dx,
                                  ddeg=ddeg,
                                  outdir=outputdir)

        sfsource.write_sources()

        # Get files and names of the written files
        written_files = glob.glob(outputdir + "CMT*/DATA/CMTSOLUTION")

        # Get files and names of the tests files
        test_files = glob.glob(DATA_DIR + "/*")
        test_names = [os.path.basename(x) for x in test_files]

        # Assert each file is equal to the written one
        for file in written_files:

            # Get filename
            name = os.path.basename(file)

            # Find corresponding file in tests directory
            index = test_names.index(name)

            # Opening both files and notebooks them string for string
            # somehow a simple file compare was not do-able.
            with open(file, 'r') as written_file:
                with open(test_files[index], 'r') as test_file:

                    # Getting the lines
                    for line in written_file:
                        written_line = line.split()
                        test_line = test_file.readline().split()

                        # Getting the strings
                        for index, teststring in enumerate(test_line):
                            assert teststring == str(written_line[index])
Exemplo n.º 8
0
    def test__copy_quake(self):
        """Test the create directory method"""
        # Check one cmt file
        with tempfile.TemporaryDirectory() as tmp_dir:
            # Cmtfile path
            cmtfile = os.path.join(DATA_DIR, "testCMT")

            # Initialize database skeleton class
            DB = DataBaseSkeleton(basedir=tmp_dir,
                                  cmt_fn=cmtfile,
                                  specfem_dir=self.specfem_dir)

            # create new directory
            new_cmt_path = os.path.join(tmp_dir, "blub.xml")
            DB._write_quakeml(cmtfile, new_cmt_path, True)

            self.assertTrue(os.path.exists(new_cmt_path)
                            and os.path.isfile(new_cmt_path))

            print("QuakeML\n", CMTSource.from_quakeml_file(new_cmt_path))
            print("CMT\n", CMTSource.from_CMTSOLUTION_file(cmtfile))
            assertDictAlmostEqual(CMTSource.from_quakeml_file(new_cmt_path),
                                  CMTSource.from_CMTSOLUTION_file(cmtfile))
Exemplo n.º 9
0
    def from_file(cls, cmtfname,
                  stationlistfname,
                  duration=0.,
                  channels=["BHZ"],
                  locations=["00"],
                  starttime_offset=0,
                  outputdir="",
                  verbose=1):
        """Creates a downloader class from an input file

        This downloader class also needs to contain the output directory for
        the traces.
        """

        # Name of the output directory:
        # Can't be also parsed input since it's dependent on the provided
        # filename
        if outputdir == "":
            outputdir = os.path.abspath(os.path.join(cmtfname, os.pardir))

        # First Create CMT Solution
        cmt = CMTSource.from_CMTSOLUTION_file(cmtfname)

        # Read station list file with two columns and whitespace separation
        statfile = open(stationlistfname, 'r')

        # For loop to add all stations to the station list
        stationlist = []
        for line in statfile:
            # Read stations into list of stations
            line = line.split()

            if line[0] == "NETWORK":
                continue
            else:
                # Append the [network station latitude longitude elevation]
                # to the station list
                stationlist.append(line)

        return cls(cmt=cmt,
                   stationlist=stationlist,
                   channels=channels,
                   locations=locations,
                   duration=duration,
                   starttime_offset=starttime_offset,
                   outputdir=outputdir,
                   verbose=verbose)
Exemplo n.º 10
0
def main(cmt_filename):

    # Define parameter directory
    param_path = os.path.join(
        os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "params")
    databaseparam_path = os.path.join(param_path,
                                      "Database/DatabaseParameters.yml")
    inversionparam_path = os.path.join(param_path,
                                       "CMTInversion/InversionParams.yml")

    # Load Parameters
    DB_params = smart_read_yaml(databaseparam_path, mpi_mode=is_mpi_env())

    # Inversion Params
    INV_params = smart_read_yaml(inversionparam_path, mpi_mode=is_mpi_env())

    # File and directory
    cmt_dir = os.path.dirname(cmt_filename)
    cmt = CMTSource.from_CMTSOLUTION_file(cmt_filename)
    outdir = os.path.join(cmt_dir, "CMT_SIMs")

    # Basic parameters
    dm = float(INV_params["config"]["dmoment"])  # 10**22 dyne*cm
    dz = float(INV_params["config"]["ddepth"])  # 1000 m
    ddeg = float(INV_params["config"]["dlocation"])  # 0.001 deg

    if DB_params["verbose"]:
        print("\n")
        print("  Perturbation parameters")
        print("  " + 50 * "*")
        print("  𝚫M: %g" % dm)
        print("  𝚫z: %g" % dz)
        print("  𝚫deg: %g" % ddeg)
        print("  " + 50 * "*" + "\n")

    # Create source creation class
    sfsource = SpecfemSources(cmt,
                              npar=DB_params['npar'],
                              dm=dm,
                              dx=dz,
                              ddeg=ddeg,
                              verbose=DB_params['verbose'],
                              outdir=outdir)

    # Write sources
    sfsource.write_sources()
Exemplo n.º 11
0
    def setUp(self):
        # Create a temporary directory
        self.test_dir = tempfile.TemporaryDirectory()
        self.tmpdir = self.test_dir.name

        # Create subdirectories for each simulation
        self.attr = [
            "CMT", "CMT_rr", "CMT_tt", "CMT_pp", "CMT_rt", "CMT_rp", "CMT_tp",
            "CMT_depth", "CMT_lat", "CMT_lon"
        ]
        for at in self.attr:
            # create each sim dir
            os.makedirs(os.path.join(self.tmpdir, at))
            os.makedirs(os.path.join(self.tmpdir, at, "DATA"))
            os.makedirs(os.path.join(self.tmpdir, at, "OUTPUT_FILES"))

        self.cmt = CMTSource.from_CMTSOLUTION_file(CMTFILE)
Exemplo n.º 12
0
def data_request(cmt_filename):
    # Set directories of the parameter files
    param_path = os.path.join(
        os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "params")

    request_param_path = os.path.join(param_path,
                                      "RequestParams/RequestParams.yml")

    # Read the parameter file
    rCparams = smart_read_yaml(request_param_path, mpi_mode=is_mpi_env())

    # Earthquake and Station parameters
    cmt_dir = os.path.dirname(cmt_filename)
    station_dir = os.path.join(cmt_dir, "station_data")

    # Get STATIONS file from CMT directory
    stationsfile = os.path.join(station_dir, "STATIONS")

    # Observed output dir
    obsd_dir = os.path.join(cmt_dir, "seismograms", "obs")

    # CMT parameter input
    cmt = CMTSource.from_CMTSOLUTION_file(cmt_filename)
    duration = rCparams['duration']
    starttime_offset = rCparams['starttime_offset']

    starttime = cmt.origin_time + starttime_offset
    endtime = starttime + duration

    # Get station_list from station_file in database entry
    stations = read_station_file(stationsfile)
    station_ids = [station[0] + "_" + station[1] for station in stations]

    # Download Station Data
    _, _, filtered_station_ids = \
        download_stationxml(station_ids, starttime, endtime,
                            outputdir=station_dir, client=None,
                            level="response")

    # Download waveform
    download_waveform(filtered_station_ids,
                      starttime,
                      endtime,
                      outputdir=obsd_dir,
                      client=None)
Exemplo n.º 13
0
def test_write_CMTSOLUTION_File(tmpdir, cmt):
    fn = os.path.join(str(tmpdir), "CMTSOLUTION.temp")
    cmt.write_CMTSOLUTION_file(fn)
    new_cmt = CMTSource.from_CMTSOLUTION_file(fn)
    assert new_cmt == cmt
Exemplo n.º 14
0
def main(cmt_filename):

    # Define parameter directory
    param_path = os.path.join(
        os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "params")

    # Load Database Parameters
    databaseparam_path = os.path.join(param_path,
                                      "Database/DatabaseParameters.yml")
    DB_params = smart_read_yaml(databaseparam_path, mpi_mode=is_mpi_env())

    # Inversion Params
    inversionparam_path = os.path.join(param_path,
                                       "CMTInversion/InversionParams.yml")
    INV_params = smart_read_yaml(inversionparam_path, mpi_mode=is_mpi_env())

    # Get processing path from cmt_filename in database
    cmt_dir = os.path.dirname(os.path.abspath(cmt_filename))

    # Create cmt source:
    cmtsource = CMTSource.from_CMTSOLUTION_file(cmt_filename)

    # Window directory
    window_dir = os.path.join(cmt_dir, "window_data")

    # Inversion dictionary directory
    inv_dict_dir = os.path.join(cmt_dir, "inversion", "inversion_dicts")

    # Inversion dictionaries
    inv_dicts = glob.glob(os.path.join(inv_dict_dir, "*"))

    # Inversion output directory
    inv_out_dir = os.path.join(cmt_dir, "inversion", "inversion_output")

    if DB_params["verbose"]:
        print("\n#######################################################")
        print("#                                                     #")
        print("#      Starting inversion ...                         #")
        print("#                                                     #")
        print("#######################################################\n")

    # Creating Data container
    dcon = DataContainer(parlist=PARLIST[:DB_params["npar"]])

    for _i, inv_dict in enumerate(inv_dicts):

        # Get processing band
        bandstring = str(os.path.basename(inv_dict)).split(".")[1]
        band = [float(x) for x in bandstring.split("_")]

        if DB_params["verbose"]:
            print("\n")
            print("  " + 54 * "*")
            print("  Getting data for inversion from period band:")
            print("  Low: %d s || High: %d s" % tuple(band))
            print("  " + 54 * "*" + "\n")

        # Load inversion file dictionary
        asdf_dict = smart_read_yaml(inv_dict, mpi_mode=is_mpi_env())
        window_files = glob.glob(
            os.path.join(window_dir,
                         "windows." + bandstring + "*[!stats].json"))

        # Adding measurements

        for _j, window_file in enumerate(window_files):
            # Print Inversion parameters:
            if DB_params["verbose"]:
                print("  Adding measurements to data container:")
                print(
                    "  _____________________________________________________\n"
                )

            # Add measurements from ASDF file and windowfile
            # if _j == 0:
            if DB_params["verbose"]:
                print("  Window file:\n", "  ", window_file)
                print("\n  ASDF files:")
                for key, value in asdf_dict.items():
                    print("    ", key + ":", value)
            dcon.add_measurements_from_asdf(window_file, asdf_dict)
            # else:
            #     if DB_params["verbose"]:
            #         print("  Window file:\n", "  ", window_file)
            #     dcon.add_measurements_from_sac(window_file)

            if DB_params["verbose"]:
                print(
                    "  _____________________________________________________\n"
                )
                print("   ... \n\n")

    if DB_params["verbose"]:
        print("  Inverting for a new moment tensor .... ")
        print("  " + 54 * "*" + "\n\n")

    # Setting up weight config
    weight_config = DefaultWeightConfig(normalize_by_energy=False,
                                        normalize_by_category=False,
                                        comp_weight={
                                            "Z": 1.0,
                                            "R": 1.0,
                                            "T": 1.0
                                        },
                                        love_dist_weight=1.0,
                                        pnl_dist_weight=1.0,
                                        rayleigh_dist_weight=1.0,
                                        azi_exp_idx=0.5)

    # Setting up general inversion config
    config = Config(DB_params["npar"],
                    dlocation=float(INV_params["config"]["dlocation"]),
                    ddepth=float(INV_params["config"]["ddepth"]),
                    dmoment=float(INV_params["config"]["dmoment"]),
                    weight_data=True,
                    station_correction=True,
                    zero_trace=True,
                    double_couple=False,
                    bootstrap=True,
                    bootstrap_repeat=100,
                    weight_config=weight_config)

    srcinv = Cmt3D(cmtsource, dcon, config)
    srcinv.source_inversion()

    # plot result
    srcinv.plot_summary(inv_out_dir, figure_format="pdf")

    if DB_params["verbose"]:
        print("  DONE inversion for period band: %d - %d s.\n" % tuple(band))

    if DB_params["verbose"]:
        print("\n#######################################################")
        print("#                                                     #")
        print("#      Inversion DONE.                                #")
        print("#                                                     #")
        print("#######################################################\n")
Exemplo n.º 15
0
 def setUp(self):
     # Create a temporary directory
     self.test_dir = tempfile.TemporaryDirectory()
     self.tmpdir = self.test_dir.name
     self.cmt = CMTSource.from_CMTSOLUTION_file(CMTFILE)
Exemplo n.º 16
0
def data_request(cmt_filename, param_path):

    # Request config_file
    request_param_path = os.path.join(param_path,
                                      "RequestParams/RequestParams.yml")

    # Read the parameter file
    rCparams = smart_read_yaml(request_param_path, mpi_mode=is_mpi_env())

    # Earthquake and Station parameters
    cmt_dir = os.path.dirname(cmt_filename)
    station_dir = os.path.join(cmt_dir, "station_data")

    # Get STATIONS file from CMT directory
    stationsfile = os.path.join(station_dir, "STATIONS")

    # Observed output dir
    obsd_dir = os.path.join(cmt_dir, "seismograms", "obs")

    # CMT parameter input
    cmt = CMTSource.from_CMTSOLUTION_file(cmt_filename)
    duration = rCparams['duration']
    starttime_offset = rCparams['starttime_offset']

    starttime = cmt.origin_time + starttime_offset
    endtime = starttime + duration

    # Get station_list from station_file in database entry
    stations = read_station_file(stationsfile)

    # Create list of networks to download from
    networks = list(set([station[0] for station in stations]))
    network_string = ",".join(networks)

    # Set domain containing all locations
    # Rectangular domain containing parts of southern Germany.
    domain = RectangularDomain(minlatitude=-90,
                               maxlatitude=90,
                               minlongitude=-180,
                               maxlongitude=180)

    # Set download restrictions
    restrictions = Restrictions(
        starttime=starttime,
        endtime=endtime,
        reject_channels_with_gaps=False,
        minimum_length=float(rCparams['minimum_length']),
        # Trace needs to be almost full length
        network=network_string,  # Only certain networks
        channel=",".join(rCparams['channels']),
        location=",".join(rCparams['locations']))

    # No specified providers will result in all known ones being queried.
    providers = ["IRIS"]
    mdl = MassDownloader(providers=providers)
    # The data will be downloaded to the ``./waveforms/`` and ``./stations/``
    # folders with automatically chosen file n
    stationxml_storage = os.path.join(station_dir)
    waveform_storage = os.path.join(obsd_dir)
    logger.info("MSEEDs: %s" % waveform_storage)
    logger.info("XMLs: %s" % stationxml_storage)

    mdl.download(domain,
                 restrictions,
                 mseed_storage=waveform_storage,
                 stationxml_storage=stationxml_storage)
Exemplo n.º 17
0
def cmt():
    return CMTSource.from_CMTSOLUTION_file(CMTFILE)