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") specfemspec_path = os.path.join(param_path, "SpecfemParams/SpecfemParams.yml") # Load Parameters DB_params = smart_read_yaml(databaseparam_path, mpi_mode=is_mpi_env()) specfemspecs = smart_read_yaml(specfemspec_path, mpi_mode=is_mpi_env()) # Database Setup. DB = DataBaseSkeleton(basedir=DB_params["databasedir"], cmt_fn=cmt_filename, specfem_dir=specfemspecs["SPECFEM_DIR"], verbose=DB_params['verbose'], overwrite=DB_params['overwrite']) # Database Create entry DB.create_all() # Return new earthquake location. cmt_in_database = os.path.join(DB.eq_dirs[0], "eq_" + DB.eq_ids[0]) return cmt_in_database
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)
def Create_Entry(cmt_filename): """ This function creates a database entry for the inversion. :param cmt_filename: path to cmt file that an Entry is supposed to be created for :return: no returns since it is simply a copy and create dirs function """ # 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") specfemspec_path = os.path.join(param_path, "SpecfemParams/SpecfemParams.yml") # Load Parameters DB_params = smart_read_yaml(databaseparam_path, mpi_mode=is_mpi_env()) specfemspecs = smart_read_yaml(specfemspec_path, mpi_mode=is_mpi_env()) # Database Setup. DB = DataBaseSkeleton(basedir=DB_params["databasedir"], cmt_fn=cmt_filename, specfem_dir=specfemspecs["SPECFEM_DIR"], verbose=DB_params['verbose'], overwrite=DB_params['overwrite']) # Database Create entry DB.create_all() # Return new earthquake location. cmt_in_database = os.path.join(DB.Cdirs[0], "C" + DB.Cids[0]) return cmt_in_database
def create_entry(cmt_filename, param_path): # Define parameter directory databaseparam_path = os.path.join(param_path, "Database/DatabaseParameters.yml") specfemspec_path = os.path.join(param_path, "SpecfemParams/SpecfemParams.yml") stations_path = os.path.join(param_path, "RequestParams/STATIONS") # Load Parameters DB_params = smart_read_yaml(databaseparam_path, mpi_mode=is_mpi_env()) specfemspecs = smart_read_yaml(specfemspec_path, mpi_mode=is_mpi_env()) # Check whether stationsfile in Parampath if os.path.exists(stations_path): stations_file = stations_path else: # if no stations file in the parameter directory, # the standard stations file is going to be used stations_file = None # Database Setup. DB = DataBaseSkeleton(basedir=DB_params["databasedir"], cmt_fn=cmt_filename, specfem_dir=specfemspecs["SPECFEM_DIR"], stations_file=stations_file, overwrite=DB_params['overwrite']) # Database Create entry DB.create_all() # Return new earthquake location. cmt_in_database = os.path.join(DB.Cdirs[0], "C" + DB.Cids[0]) return cmt_in_database
def test__create_process_path_obs(self): """Testing the creation of the observed processing yaml 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 database DB.create_all() # Read the yaml_file which should be created in the CMT directory process_dir = os.path.join(DATA_DIR, "ProcessObserved") # CMT filename in database cmt_filename = os.path.join(DB.Cdirs[0], "C" + DB.Cids[0]) # Create Processing path files create_process_path_obs(cmt_filename, process_dir) # Solution output path: process_paths = os.path.join(DB.Cdirs[0], "seismograms", "process_paths") # One solution path file: process_file = os.path.join(process_paths, "process_observed.040_100.yml") # Solution should be: input_asdf = os.path.join(DB.Cdirs[0], "seismograms", "obs", "raw_observed.h5") input_tag = "obs" output_asdf = os.path.join(DB.Cdirs[0], "seismograms", "processed_seismograms", "processed_observed.040_100.h5") output_tag = "040_100_obsd" process_param_file = os.path.join(process_dir, "proc_obsd.40_100.param.yml") d = smart_read_yaml(process_file, mpi_mode=False) # Assessing correctness of yaml file self.assertTrue(d["input_asdf"] == input_asdf) self.assertTrue(d["input_tag"] == input_tag) self.assertTrue(d["output_asdf"] == output_asdf) self.assertTrue(d["output_tag"] == output_tag) self.assertTrue(d["process_param_file"] == process_param_file)
def test_create_inversion_dicts(self): """Test the inversion dictionary creator.""" 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 database DB.create_all() # Outputdir cmt_dir = DB.Cdirs[0] cmt_file_db = os.path.join(cmt_dir, "C" + DB.Cids[0] + ".cmt") # Read the yaml_file which should be created in the CMT directory window_process_dir = os.path.join(DATA_DIR, "CreateWindows") # Create Window path files create_window_path(cmt_file_db, window_process_dir, figure_mode=True) # Read the yaml_file which should be created in the CMT directory process_syn_dir = os.path.join(DATA_DIR, "ProcessSynthetic") # CMT filename in database cmt_filename = os.path.join(DB.Cdirs[0], "C" + DB.Cids[0]) # Create Processing path files create_process_path_syn(cmt_filename, process_syn_dir, npar=9) # Read the yaml_file which should be created in the CMT directory process_obs_dir = os.path.join(DATA_DIR, "ProcessObserved") # Create Processing path files create_process_path_obs(cmt_filename, process_obs_dir) # Create dict list inv_dict_list, filenames = create_full_inversion_dict_list( cmt_filename, process_obs_dir, process_syn_dir, window_process_dir) write_inversion_dicts(inv_dict_list, filenames) for inv_dict, filename in zip(inv_dict_list, filenames): self.assertTrue(os.path.exists(filename))
def test_get_processing_list(self): """Tests the get_processing_list function in the create_process_paths modules. """ 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 database DB.create_all() # Outputdir cmt_dir = DB.Cdirs[0] cmt_file_db = os.path.join(cmt_dir, "C" + DB.Cids[0] + ".cmt") # Read the yaml_file which should be created in the CMT directory process_syn_dir = os.path.join(DATA_DIR, "ProcessSynthetic") # CMT filename in database cmt_filename = os.path.join(DB.Cdirs[0], "C" + DB.Cids[0]) # Create Processing path files create_process_path_syn(cmt_filename, process_syn_dir, npar=9) # Read the yaml_file which should be created in the CMT directory process_obs_dir = os.path.join(DATA_DIR, "ProcessObserved") # Create Processing path files create_process_path_obs(cmt_filename, process_obs_dir) # Solution output path: process_paths = os.path.join(DB.Cdirs[0], "seismograms", "process_paths") # Get processing list processing_list, obs_list, syn_list = get_processing_list( cmt_file_db, process_obs_dir, process_syn_dir) # Files on disk solution_list = glob.glob(os.path.join(process_paths, "*")) # Check if processing list equals process dir files for process_path in processing_list: self.assertTrue(process_path in solution_list)
def test_create_db_dir(self): """This test the creation of the EQ directory.""" # Test tmp_dir exist already with tempfile.TemporaryDirectory() as tmp_dir: # Initialize database skeleton class DB = DataBaseSkeleton(basedir=tmp_dir, cmt_fn=os.path.join(DATA_DIR, "CMTSOLUTION"), specfem_dir=self.specfem_dir, verbose=True) DB.create_base() self.assertTrue(os.path.exists(DB.basedir)) # Test tmp_dir exist already with tempfile.TemporaryDirectory() as tmp_dir: # Initialize database skeleton class DB = DataBaseSkeleton(basedir=os.path.join(tmp_dir, "db"), cmt_fn=os.path.join(DATA_DIR, "CMTSOLUTION"), specfem_dir=self.specfem_dir, verbose=True) DB.create_base() self.assertTrue(os.path.exists(os.path.join(tmp_dir, "db")))
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))
def test__create_dir(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, verbose=True) # create new directory test_dir = os.path.join(tmp_dir, "test") DB._create_dir(test_dir, False) self.assertTrue( os.path.exists(test_dir) and os.path.isdir(test_dir))
def test_get_windowing_list(self): """Tests the get_processing_list function in the create_process_paths modules. """ 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 database DB.create_all() # Outputdir cmt_dir = DB.Cdirs[0] cmt_file_db = os.path.join(cmt_dir, "C" + DB.Cids[0] + ".cmt") # Read the yaml_file which should be created in the CMT directory window_process_dir = os.path.join(DATA_DIR, "CreateWindows") # Create Window path files create_window_path(cmt_file_db, window_process_dir, figure_mode=True) # Get windowing list windowing_list, outputfile_list = get_windowing_list( cmt_file_db, window_process_dir) # Solution output path: window_paths = os.path.join(DB.Cdirs[0], "window_data", "window_paths") # Files on disk solution_list = glob.glob(os.path.join(window_paths, "*")) # Check if processing list equals process dir files for window_path, outfile in zip(windowing_list, outputfile_list): self.assertTrue(window_path in solution_list)
def test_IO(self): """Testing if strings are set correctly.""" with tempfile.TemporaryDirectory() as tmp_dir: # Initialize database skeleton class DB = DataBaseSkeleton(basedir=tmp_dir, cmt_fn=os.path.join(DATA_DIR, "CMTSOLUTION"), specfem_dir=self.specfem_dir) self.assertEqual(DB.specfem_dir, self.specfem_dir) self.assertEqual(DB.cmt_fn, os.path.join(DATA_DIR, "CMTSOLUTION")) self.assertEqual(DB.basedir, tmp_dir)
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")))
def test__create_syn_path_yaml(self): """Testing the creation of the yaml 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, verbose=True) # Create database DB.create_all() # Read the yaml_file which should be created in the CMT directory yaml_file = os.path.join(DB.eq_dirs[0], "CMT_SIMs", "CMT_rr", "CMT_rr.yml") # Solution should be: waveform_dir = os.path.join(DB.eq_dirs[0], "CMT_SIMs", "CMT_rr", "OUTPUT_FILES") tag = 'syn' filetype = 'sac' output_file = os.path.join(DB.eq_dirs[0], "seismograms", "syn", "CMT_rr.h5") quakeml_file = os.path.join(DB.eq_dirs[0], "CMT_SIMs", "CMT_rr", "OUTPUT_FILES", "Quake.xml") d = smart_read_yaml(yaml_file, mpi_mode=False) # Assessing correctness of yaml file self.assertTrue(d["quakeml_file"] == quakeml_file) self.assertTrue(d["tag"] == tag) self.assertTrue(d["output_file"] == output_file) self.assertTrue(d["filetype"] == filetype) self.assertTrue(d["waveform_dir"] == waveform_dir)
def test__copy_dir(self): """Tests the copy dir function in skeleton.""" with tempfile.TemporaryDirectory() as tmp_dir: # Create on directory in temporary directory test_dir1 = os.path.join(tmp_dir, "test1") test_dir2 = os.path.join(tmp_dir, "test2") os.makedirs(test_dir1) # 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, verbose=True) DB._copy_dir(test_dir1, test_dir2, False) self.assertTrue(os.path.isdir(test_dir2))
def test__create_obs_path_yaml(self): """Testing the creation of the yaml 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, verbose=True) # Create database DB.create_all() # Read the yaml_file which should be created in the CMT directory yaml_file = os.path.join(DB.eq_dirs[0], "seismograms", "obs", "observed.yml") # Solution should be: waveform_files = os.path.join(DB.eq_dirs[0], "seismograms", "obs", DB.eq_ids[0] + ".mseed") staxml = os.path.join(DB.eq_dirs[0], "station_data", "station.xml") tag = 'obs' output_file = os.path.join(DB.eq_dirs[0], "seismograms", "obs", "raw_observed.h5") quakeml_file = os.path.join(DB.eq_dirs[0], "eq_" + DB.eq_ids[0] + ".xml") d = smart_read_yaml(yaml_file, mpi_mode=False) # Assessing correctness of yaml file self.assertTrue(d["quakeml_file"] == quakeml_file) self.assertTrue(d["tag"] == tag) self.assertTrue(d["output_file"] == output_file) self.assertTrue(d["waveform_files"] == waveform_files) self.assertTrue(d["staxml_files"] == staxml)
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, verbose=True) # 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))
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, verbose=True) # 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))
def test__create_window_path(self): """Testing the creation of the window path yaml 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 database DB.create_all() # Read the yaml_file which should be created in the CMT directory window_process_dir = os.path.join(DATA_DIR, "CreateWindows") # CMT filename in database cmt_filename = os.path.join(DB.Cdirs[0], "C" + DB.Cids[0]) # Create Processing path files create_window_path(cmt_filename, window_process_dir, figure_mode=True) # Outputdir cmt_dir = DB.Cdirs[0] # Define observed ASDF obsd_asdf = os.path.join(cmt_dir, "seismograms", "processed_seismograms", "processed_observed.040_100.h5") obsd_tag = "040_100_obsd" # Synthetic ASDF synt_asdf = os.path.join(cmt_dir, "seismograms", "processed_seismograms", "processed_synthetic_CMT.040_100.h5") synt_tag = "040_100_synt" # Output file parameters output_file = os.path.join(cmt_dir, "window_data", "windows.040_100#surface_wave.json") figure_dir = os.path.join(cmt_dir, "window_data", "window_plots.040_100#surface_wave") # window paramfile window_param_file = os.path.join(window_process_dir, "window.40_100#surface_wave." "param.yml") # Output path file path_file = os.path.join(cmt_dir, "window_data", "window_paths", "windows.040_100#surface_wave.yml") # Read written dictionary d = smart_read_yaml(path_file, mpi_mode=False) print("Solution: ", synt_asdf) print("Loaded: ", d["synt_asdf"]) # Assessing correctness of yaml file self.assertTrue(d["obsd_asdf"] == obsd_asdf) self.assertTrue(d["obsd_tag"] == obsd_tag) self.assertTrue(d["synt_asdf"] == synt_asdf) self.assertTrue(d["synt_tag"] == synt_tag) self.assertTrue(d["figure_mode"]) self.assertTrue(d["figure_dir"] == figure_dir) self.assertTrue(d["output_file"] == output_file) self.assertTrue(d["window_param_file"] == window_param_file)