# see if station is already in the station inv dictionary if new_station in station_inventory_dict.keys(): # station inventory is already in dict get the station inventory object and append the channel info sta_inv = station_inventory_dict[new_station] # add channel inventory sta_inv.channels.append(select_inv[0][0][0]) else: # create the station inventory sta_inv = Station(code=new_station, creation_date=select_inv[0][0].creation_date, start_date=select_inv[0][0].start_date, end_date=select_inv[0][0].end_date, latitude=select_inv[0][0].latitude, longitude=select_inv[0][0].longitude, elevation=select_inv[0][0].elevation, site=Site(new_station)) sta_inv.channels.append(select_inv[0][0][0]) # append it to the station inventory dict station_inventory_dict[new_station] = sta_inv # # # lookup table for channel info: # channel_lookup_dict = {"HZ": (), # "HN":,}
def getInventory(self): """Extract an ObsPy inventory object from a StationStream.""" networks = [trace.stats.network for trace in self] if len(set(networks)) > 1: raise Exception( "Input stream has stations from multiple networks.") # We'll first create all the various objects. These strongly follow the # hierarchy of StationXML files. source = "" if "standard" in self[0].stats and "source" in self[0].stats.standard: source = self[0].stats.standard.source inv = Inventory( # We'll add networks later. networks=[], # The source should be the id whoever create the file. source=source, ) net = Network( # This is the network code according to the SEED standard. code=networks[0], # A list of stations. We'll add one later. stations=[], description="source", # Start-and end dates are optional. ) channels = [] for trace in self: logging.debug(f"trace: {trace}") channel = _channel_from_stats(trace.stats) channels.append(channel) subdict = {} for k in UNUSED_STANDARD_PARAMS: if k in self[0].stats.standard: subdict[k] = self[0].stats.standard[k] format_specific = {} if "format_specific" in self[0].stats: format_specific = dict(self[0].stats.format_specific) big_dict = {"standard": subdict, "format_specific": format_specific} jsonstr = json.dumps(big_dict) sta = Station( # This is the station code according to the SEED standard. code=self[0].stats.station, latitude=self[0].stats.coordinates.latitude, elevation=self[0].stats.coordinates.elevation, longitude=self[0].stats.coordinates.longitude, channels=channels, site=Site(name=self[0].stats.standard.station_name), description=jsonstr, creation_date=UTCDateTime(1970, 1, 1), # this is bogus total_number_of_channels=len(self), ) net.stations.append(sta) inv.networks.append(net) return inv
def get_inventory(): # We'll first create all the various objects. These strongly follow the # hierarchy of StationXML files. inv = Inventory( # We'll add networks later. networks=[], # The source should be the id whoever create the file. source="ObsPy-Tutorial", ) net = Network( # This is the network code according to the SEED standard. code="US", # A list of stations. We'll add one later. stations=[], description="A test stations.", # Start-and end dates are optional. start_date=UTCDateTime(2009, 1, 2), end_date=UTCDateTime(2016, 1, 2), ) sta = Station( # This is the station code according to the SEED standard. code="ABCD", latitude=1.0, longitude=2.0, elevation=345.0, creation_date=UTCDateTime(2016, 1, 2), site=Site(name="First station"), ) cha1 = Channel( # This is the channel code according to the SEED standard. code="HN1", # This is the location code according to the SEED standard. location_code="11", # Note that these coordinates can differ from the station coordinates. latitude=1.0, longitude=2.0, elevation=345.0, depth=10.0, azimuth=0.0, dip=-90.0, sample_rate=1, ) cha2 = Channel( # This is the channel code according to the SEED standard. code="HN2", # This is the location code according to the SEED standard. location_code="11", # Note that these coordinates can differ from the station coordinates. latitude=1.0, longitude=2.0, elevation=345.0, depth=10.0, azimuth=90.0, dip=-90.0, sample_rate=1, ) cha3 = Channel( # This is the channel code according to the SEED standard. code="HNZ", # This is the location code according to the SEED standard. location_code="11", # Note that these coordinates can differ from the station coordinates. latitude=1.0, longitude=2.0, elevation=345.0, depth=10.0, azimuth=0.0, dip=-90.0, sample_rate=1, ) # Now tie it all together. sta.channels.append(cha1) sta.channels.append(cha2) sta.channels.append(cha3) net.stations.append(sta) inv.networks.append(net) return inv
def test_reading_unicode_file(self): """ Tests reading a file with non ASCII characters. """ # Manually create an expected Inventory object. expected_inv = Inventory( source=None, networks=[ Network( code="PR", stations=[ Station( code="CTN1", latitude=18.43718, longitude=-67.1303, elevation=10.0, site=Site(name="CATA¿O DEFENSA CIVIL"), start_date=obspy.UTCDateTime( "2004-01-27T00:00:00"), end_date=obspy.UTCDateTime("2599-12-31T23:59:59")) ]) ]) # Read from a filename. filename = os.path.join(self.data_dir, "unicode_example_fdsn.txt") inv = read_fdsn_station_text_file(filename) inv_obs = obspy.read_inventory(filename) # Copy creation date as it will be slightly different otherwise. inv.created = expected_inv.created inv_obs.created = expected_inv.created self.assertEqual(inv, expected_inv) self.assertEqual(inv_obs, expected_inv) # Read from open file in text mode. with open(filename, "rt", encoding="utf8") as fh: inv = read_fdsn_station_text_file(fh) fh.seek(0, 0) inv_obs = obspy.read_inventory(fh) inv.created = expected_inv.created inv_obs.created = expected_inv.created self.assertEqual(inv, expected_inv) self.assertEqual(inv_obs, expected_inv) # Read from open file in binary mode. with open(filename, "rb") as fh: inv = read_fdsn_station_text_file(fh) fh.seek(0, 0) inv_obs = obspy.read_inventory(fh) inv.created = expected_inv.created inv_obs.created = expected_inv.created self.assertEqual(inv, expected_inv) self.assertEqual(inv_obs, expected_inv) # Read from StringIO. with open(filename, "rt", encoding="utf8") as fh: with io.StringIO(fh.read()) as buf: buf.seek(0, 0) inv = read_fdsn_station_text_file(buf) buf.seek(0, 0) inv_obs = obspy.read_inventory(buf) inv.created = expected_inv.created inv_obs.created = expected_inv.created self.assertEqual(inv, expected_inv) self.assertEqual(inv_obs, expected_inv) # Read from BytesIO. with open(filename, "rb") as fh: with io.BytesIO(fh.read()) as buf: buf.seek(0, 0) inv = read_fdsn_station_text_file(buf) buf.seek(0, 0) inv_obs = obspy.read_inventory(buf) inv.created = expected_inv.created inv_obs.created = expected_inv.created self.assertEqual(inv, expected_inv) self.assertEqual(inv_obs, expected_inv)
def test_reading_station_file(self): """ Test reading a file at the station level. """ # Manually create an expected Inventory object. expected_inv = Inventory( source=None, networks=[ Network( code="TA", stations=[ Station( code="A04A", latitude=48.7197, longitude=-122.707, elevation=23.0, site=Site(name="Legoe Bay, Lummi Island, WA, USA"), start_date=obspy.UTCDateTime( "2004-09-19T00:00:00"), end_date=obspy.UTCDateTime("2008-02-19T23:59:59")), Station( code="A04D", latitude=48.7201, longitude=-122.7063, elevation=13.0, site=Site(name="Lummi Island, WA, USA"), start_date=obspy.UTCDateTime( "2010-08-18T00:00:00"), end_date=obspy.UTCDateTime("2599-12-31T23:59:59")) ]), Network( code="TR", stations=[ Station( code="ALNG", latitude=10.1814, longitude=-61.6883, elevation=10.0, site=Site(name="Trinidad, Point Fortin"), start_date=obspy.UTCDateTime( "2000-01-01T00:00:00"), end_date=obspy.UTCDateTime("2599-12-31T23:59:59")) ]) ]) # Read from a filename. filename = os.path.join(self.data_dir, "station_level_fdsn.txt") inv = read_fdsn_station_text_file(filename) inv_obs = obspy.read_inventory(filename) # Copy creation date as it will be slightly different otherwise. inv.created = expected_inv.created inv_obs.created = expected_inv.created self.assertEqual(inv, expected_inv) self.assertEqual(inv_obs, expected_inv) # Read from open file in text mode. with open(filename, "rt", encoding="utf8") as fh: inv = read_fdsn_station_text_file(fh) fh.seek(0, 0) inv_obs = obspy.read_inventory(fh) inv.created = expected_inv.created inv_obs.created = expected_inv.created self.assertEqual(inv, expected_inv) self.assertEqual(inv_obs, expected_inv) # Read from open file in binary mode. with open(filename, "rb") as fh: inv = read_fdsn_station_text_file(fh) fh.seek(0, 0) inv_obs = obspy.read_inventory(fh) inv.created = expected_inv.created inv_obs.created = expected_inv.created self.assertEqual(inv, expected_inv) self.assertEqual(inv_obs, expected_inv) # Read from StringIO. with open(filename, "rt", encoding="utf8") as fh: with io.StringIO(fh.read()) as buf: buf.seek(0, 0) inv = read_fdsn_station_text_file(buf) buf.seek(0, 0) inv_obs = obspy.read_inventory(buf) inv.created = expected_inv.created inv_obs.created = expected_inv.created self.assertEqual(inv, expected_inv) self.assertEqual(inv_obs, expected_inv) # Read from BytesIO. with open(filename, "rb") as fh: with io.BytesIO(fh.read()) as buf: buf.seek(0, 0) inv = read_fdsn_station_text_file(buf) buf.seek(0, 0) inv_obs = obspy.read_inventory(buf) inv.created = expected_inv.created inv_obs.created = expected_inv.created self.assertEqual(inv, expected_inv) self.assertEqual(inv_obs, expected_inv)
def main(): chans = "EHZ,EHN,EHE" # Get StationXML file print(f"Interactive StaXML builder") print(f"Work in progress...some things hardwired\n\n") inv_name = input(f"Enter StationXML file name: ") if (os.path.isfile(inv_name)): inv = read_inventory(inv_name) else: print(f"Making new inventory: {inv_name}\n") inv = Inventory(networks=[], source="Weston") # Net code ques = f"Enter Network Code ({str(netc)}) :" net_code = str(input(ques) or netc) net = Network(code=net_code, stations=[]) print(f"\n") # connect to NRL nrl = NRL() # Datalogger info ret = 0 digi = f"REF TEK|RT 130S & 130-SMHR|1|200" print(f"Input NRL Digi info ( | separated, careful with spaces)....") print(f"E.g manufacturer| model| gain| sps\n") while ret == 0: ques = f"Enter DIGI info ({digi}) :" digi = str(input(ques) or digi) print(f"\n") try: nrl.get_datalogger_response(digi.split('|')) ret = 1 print("!!!!! DATA LOGGER SUCCESS!!!\n") except Exception as e: print(f"Try again ... {e}") # Sensor info ret = 0 sensor = f"Streckeisen,STS-1,360 seconds" print(f"Input NRL Sensor info ....\n") print(f"E.g Manufact|model|Sensitivy\n") print(f"Guralp|CMG-40T,30s - 100Hz|800") print(f"Sercel/Mark Products|L-22D|5470 Ohms|20000 Ohms") print(f"Streckeisen|STS-1|360 seconds") print(f"Nanometrics|Trillium Compact 120 (Vault, Posthole, OBS)|754 V/m/s") while ret == 0: ques = f"Enter sensor info {str(sensor)} :" sensor = str(input(ques) or sensor) try: nrl.get_sensor_response(sensor.split('|')) ret = 1 inst_info = f"{sensor.split('|')[0]} {sensor.split('|')[1]}" print("Sensor success!!!!") except Exception as e: print(f"Try again ... {e}") print("Getting full response...") try: response = nrl.get_response(sensor_keys=sensor.split('|'), datalogger_keys=digi.split('|')) print("Full response success \n\n") except Exception as e: print(f"Oops .. {e}") # nstas = int( input( "Enter number of stations to add with same sensor/digitizer (default 1):" ) or 1) for i in range(0, nstas): ques = "Station code (" + str(scode) + ") :" sta_code = str(input(ques) or scode) ques = "Station latitude (" + str(geolat) + ") :" sta_lat = float(input(ques) or geolat) ques = "Station longitude (" + str(geolon) + ") :" sta_lon = float(input(ques) or geolat) ques = "Station elev(" + str(geoelev) + ") :" sta_elev = float(input(ques) or geoelev) ques = "Station ondate (" + str(date) + ") :" sta_ondate = str(input(ques) or date) ques = "Station offdate (" + str(date) + ") :" sta_offdate = str(input(ques) or date) ques = "Station long name (" + str(longname) + ") :" sta_sitename = str(input(ques) or longname) sta = Station(code=sta_code, latitude=sta_lat, longitude=sta_lon, elevation=sta_elev, creation_date=UTCDateTime(sta_ondate), site=Site(name=sta_sitename)) # add station to network net.stations.append(sta) # Default chan info coords = { 'latitude': sta_lat, 'longitude': sta_lon, 'elevation': sta_elev, 'depth': 0.0, 'sample_rate': sps } n = -1 ques = f"Enter channel names, comma separated ({chans}) :" chans = str(input(ques) or chans) for j in chans.split(','): n += 1 chantmp = j print("Doing channel ", chantmp) aztmp = azims[n] diptmp = dips[n] loc = locs[n] for k in coords.keys(): ques = str(chantmp) + " enter " + k + "(" + str( coords[k]) + "):" coords[k] = float(input(ques) or coords[k]) chan = Channel(code=chantmp, location_code=loc, latitude=coords['latitude'], longitude=coords['longitude'], elevation=coords['elevation'], depth=coords['depth'], azimuth=aztmp, dip=diptmp, sample_rate=coords['sample_rate'], sensor=Equipment(description=inst_info)) chan.response = response sta.channels.append(chan) inv.networks.append(net) inv.write(inv_name, format="STATIONXML")
def main(argv): '''@package isc2stnxml It gathers station information from all STN files provided in ISC and Engdahl catalogues assigning correct network code. When proper network code can not be identified the program just guess it, sorry... ''' inv = read_inventory("IRIS-ALL.xml") # unknown stations in Indonesia are usually installed by Potsdam and we assume they have network name GE default_net = 'GE' ehb1 = read_eng('BMG.STN') ehb2 = read_eng('ISC.STN') ehb = np.unique(np.vstack((ehb1, ehb2)), axis=0) isc1 = read_isc('ehb.stn') isc2 = read_isc('iscehb.stn') isc = np.unique(np.vstack((isc1, isc2)), axis=0) catalogue = [] our_xml = Inventory(networks=[], source='EHB') for i in range(ehb.shape[0]): filed = False xml = False stn_found = isc[isc[:, 0] == ehb[i, 0], :] min_dist = 10e10 if stn_found.shape[0] > 0: if stn_found.shape[0] > 1: for j in range(stn_found.shape[0]): dist = locations2degrees(np.float(stn_found[j, 2]), np.float(stn_found[j, 3]), np.float(ehb[i, 1]), np.float(ehb[i, 2])) if dist < min_dist: min_dist = dist record = stn_found[j, :] else: min_dist = locations2degrees(np.float(stn_found[0, 2]), np.float(stn_found[0, 3]), np.float(ehb[i, 1]), np.float(ehb[i, 2])) record = stn_found[0, :] # Now we try to find the same station in XML file # if min_dist > 1. or stn_found.shape[0]==0: xstn_found = inv.select(station=ehb[i, 0], channel="*HZ") if len(stn_found) == 0 and len(xstn_found) == 0: # we filed to find station anywhere and assign dummy values record = [ ehb[i, 0], default_net, ehb[i, 1], ehb[i, 2], ehb[i, 3], 'Z', '1964-1-1 00:00:00', '2599-12-31 23:59:59' ] min_dist = 0. filed = True else: # if station is found somehwere we try to iterate and see if XML has data giving it preference through adding extra value to min_dist found in ISC if len(xstn_found) > 0: # print "----------",len(xstn_found) # print xstn_found[0][0].latitude min_dist = min_dist + 0.1 for j in range(len(xstn_found)): dist = locations2degrees(xstn_found[j][0].latitude, xstn_found[j][0].longitude, np.float(ehb[i, 1]), np.float(ehb[i, 2])) if min_dist > dist: min_dist = dist record = xstn_found[j] # print record xml = True # last defence if stations have been done but distance between declared and found locations are more than 1 degree if min_dist > 1: record = [ ehb[i, 0], default_net, ehb[i, 1], ehb[i, 2], ehb[i, 3], 'Z', '1964-1-1 00:00:00', '2599-12-31 23:59:59' ] filed = True if xml: #our_xml.networks.append(record) xml = False else: if filed: if len(record[7]) < 5: record[7] = '2599-12-31 23:59:59' catalogue.append(record) else: stn_found = isc[(isc[:, 0] == record[0]) & (isc[:, 1] == record[1]), :] for k in range(stn_found.shape[0]): net = Network(code=stn_found[k, 1], stations=[], description=' ') if len(stn_found[k, 7]) < 5: stn_found[k, 7] = '2599-12-31 23:59:59' catalogue.append(stn_found[k, :]) stn_found = np.unique(np.array(catalogue), axis=0) if len(stn_found[stn_found == '']) > 0 or len( stn_found[stn_found == ' ']) > 0: print("Some elements are empty, check the list") # we composed our inventory. However some stations from ISC list can be left behind. We check if some stations in ISC are forgotten lost = [] for j in range(isc.shape[0]): # is there any common station name? common_st = stn_found[isc[j, 0] == stn_found[:, 0]] if common_st.shape[0] > 0: # is network code the same? common_net = common_st[common_st[:, 1] == isc[j, 1]] if common_net.shape[0] < 1: # ok we found forgotten one, check the XML if len(inv.select(station=isc[j, 0], network=isc[j, 1])) <= 0: # Bingo... lost.append(isc[j, :]) else: if len(inv.select(station=isc[j, 0], network=isc[j, 1])) <= 0: # Bingo... lost.append(isc[j, :]) stn_found = np.vstack((stn_found, np.array(lost))) for k in range(stn_found.shape[0]): net = Network(code=stn_found[k, 1], stations=[], description=' ') if len(stn_found[k, 7]) < 5: stn_found[k, 7] = '2599-12-31 23:59:59' catalogue.append(stn_found[k, :]) sta=Station(code=stn_found[k,0],creation_date=utcdatetime.UTCDateTime(stn_found[k,6]), \ termination_date=utcdatetime.UTCDateTime(stn_found[k,7]), \ site=Site(name=' '), \ latitude=np.float(stn_found[k,2]), \ longitude=np.float(stn_found[k,3]), \ elevation=np.float(stn_found[k,4])) cha=Channel(code=stn_found[k,5], \ depth=0., \ azimuth=0., \ dip=-90., \ location_code='', \ latitude=np.float(stn_found[k,2]), \ longitude=np.float(stn_found[k,3]), \ elevation=np.float(stn_found[k,4])) sta.channels.append(cha) net.stations.append(sta) our_xml.networks.append(net) # print 'np',stn_found[k,:] our_xml.write("station.xml", format="stationxml", validate=True) our_xml.write("station.txt", format="stationtxt")