def test_configuration_via_JSON(): """ A JSON document can also be used. """ gen = InputFileGenerator() gen.config.test = "1" assert gen.config == {"test": "1"} gen.add_configuration(json.dumps({ "something_else": 2, "and_more": 3.0})) assert gen.config == { "test": "1", "something_else": 2, "and_more": 3.0} # Adding the something that already exists overwrites. gen.add_configuration(json.dumps({ "test": "4"})) assert gen.config == { "test": "4", "something_else": 2, "and_more": 3.0}
def test_configuration_via_a_dictionary(): """ Tests that a dictionary can be used to update the configuration. """ gen = InputFileGenerator() gen.config.test = "1" assert gen.config == {"test": "1"} gen.add_configuration({ "something_else": 2, "and_more": 3.0}) assert gen.config == { "test": "1", "something_else": 2, "and_more": 3.0} # Adding the something that already exists overwrites. gen.add_configuration({ "test": "4"}) assert gen.config == { "test": "4", "something_else": 2, "and_more": 3.0}
def test_config_raises_error_if_wrong_type(): """ The configuration method should raise in case a wrong type is added. """ gen = InputFileGenerator() with pytest.raises(ValueError): gen.add_configuration("something") # Same with JSON if it is not a JSON object but a list. with pytest.raises(ValueError): gen.add_configuration(json.dumps([{"something": "new"}]))
def compute(self): gen = InputFileGenerator() userconf = json.load(open(self.parameters["solver_conf_file"])) fields = userconf["fields"] for x in fields: gen.add_configuration({x["name"]:self.strToBool(x["value"])}) with open (self.parameters["quakeml"], "r") as events: quakeml=events.read() #unicode_qml=quakeml.decode('utf-8') #data = unicode_qml.encode('ascii','ignore') ## cat=readQuakeML(quakeml) events = [] #cat = obspy.readEvents(data) #Remove all events with no moment tensor. for event in cat: for fm in event.focal_mechanisms: if fm.moment_tensor and fm.moment_tensor.tensor: events.append(event) break cat.events = events gen.add_events(cat) evn=0 outputdir="" for x in userconf["events"]: gen.event_filter=[x] if self.parameters["station_format"]=="stationXML": gen.add_stations(self.parameters["stations_file"]) if self.parameters["station_format"]=="points": stlist = [] with open(self.parameters["stations_file"]) as f: k=False for line in f: if (k==False): k=True else: station={} l=line.strip().split(" ") station.update({"id":l[1]+"."+l[0]}) station.update({"latitude":float(l[3])}) station.update({"longitude":float(l[2])}) station.update({"elevation_in_m":float(l[4])}) station.update({"local_depth_in_m":float(l[5])}) stlist.append(station) gen.add_stations(stlist) gen.station_filter = userconf["stations"] outputdir=self.outputdest+userconf["runId"]+"/"+userconf["runId"]+"_"+str(evn)+"/DATA" output_files = gen.write(format=userconf["solver"], output_dir=outputdir) locations = [] for x in output_files.keys(): locations.append("file://"+socket.gethostname()+outputdir+"/"+x) self.addOutput(gen._filtered_events,location=locations,metadata=self.extractEventMetadata(outputdir,gen._filtered_events),control={"con:immediateAccess":"true"}) evn+=1 self.addOutput(outputdir,location=locations,metadata={"to_xdecompose":str(outputdir)},control={"con:immediateAccess":"true"})
def compute(self): gen = InputFileGenerator() userconf = json.load(open(self.parameters["solver_conf_file"])) fields = userconf["fields"] for x in fields: gen.add_configuration({x["name"]: self.strToBool(x["value"])}) with open(self.parameters["quakeml"], "r") as events: quakeml = events.read() #unicode_qml=quakeml.decode('utf-8') #data = unicode_qml.encode('ascii','ignore') ## cat = readQuakeML(quakeml) events = [] #cat = obspy.readEvents(data) #Remove all events with no moment tensor. for event in cat: for fm in event.focal_mechanisms: if fm.moment_tensor and fm.moment_tensor.tensor: events.append(event) break cat.events = events gen.add_events(cat) evn = 0 outputdir = "" for x in userconf["events"]: gen.event_filter = [x] if self.parameters["station_format"] == "stationXML": gen.add_stations(self.parameters["stations_file"]) if self.parameters["station_format"] == "points": stlist = [] with open(self.parameters["stations_file"]) as f: k = False for line in f: if (k == False): k = True else: station = {} l = line.strip().split(" ") station.update({"id": l[1] + "." + l[0]}) station.update({"latitude": float(l[3])}) station.update({"longitude": float(l[2])}) station.update({"elevation_in_m": float(l[4])}) station.update({"local_depth_in_m": float(l[5])}) stlist.append(station) gen.add_stations(stlist) gen.station_filter = userconf["stations"] outputdir = self.outputdest + userconf["runId"] + "/" + userconf[ "runId"] + "_" + str(evn) + "/DATA" output_files = gen.write(format=userconf["solver"], output_dir=outputdir) locations = [] for x in output_files.keys(): locations.append("file://" + socket.gethostname() + outputdir + "/" + x) self.addOutput(gen._filtered_events, location=locations, metadata=self.extractEventMetadata( outputdir, gen._filtered_events), control={"con:immediateAccess": "true"}) evn += 1 self.addOutput(outputdir, location=locations, metadata={"to_xdecompose": str(outputdir)}, control={"con:immediateAccess": "true"})