Skip to content

Instantly share code, notes, and snippets.

@calum-chamberlain
Last active October 19, 2016 21:11
Show Gist options
  • Save calum-chamberlain/ca84185a324dc42d631808d94e4ad0c6 to your computer and use it in GitHub Desktop.
Save calum-chamberlain/ca84185a324dc42d631808d94e4ad0c6 to your computer and use it in GitHub Desktop.
Simple code to take obspy Inventory objects and write out station locations formatted for seisan STATION0.HYP files.
def inventory_to_seisan(inventory, filename):
"""
Write station co-ordinates from an inventory object to STATION0.HYP format.
:type inventory: :class:`~obspy.core.inventory.Inventory`
:param inventory: Inventory to write co-ordinated from.
:type filename: str
:param filename: Filename to output to, will overwrite file is present
.. note::
Currently Only works to the low-precision level (see seisan
manual for explanation).
"""
written_stations = []
with open(filename, 'w') as f:
for network in inventory:
for station in network:
if station.code in written_stations:
warnings.warn('Not writing duplicate stations for %s'
% station.code)
else:
f.write(station_to_seisan(station=station) + '\n')
written_stations.append(station.code)
def station_to_seisan(station):
"""
Convert obspy inventory to string formatted for Seisan STATION0.HYP file.
:type station: :class:`~obspy.core.inventory.station.Station`
:param station:
A single station to be written in seisan STATION0.HYP format.
:returns: str
.. note::
Currently only works to the low-precision level (see seisan
manual for explanation).
"""
if station.latitude < 0:
lat_str = 'S'
else:
lat_str = 'N'
if station.longitude < 0: # Stored in =/- 180, not 0-360
lon_str = 'W'
else:
lon_str = 'E'
if len(station.code) > 4:
sta_str = station.code[0:4]
else:
sta_str = station.code.ljust(4)
if len(station.channels) > 0:
depth = station.channels[0].depth
else:
msg = 'No depth found in station.channels, have you set the level ' +\
'of stationXML download to channel if using obspy.get_stations?'
raise IOError(msg)
elev = str(int(round(station.elevation - depth))).rjust(4)
# lat and long are written in STATION0.HYP in deg,decimal mins
lat = abs(station.latitude)
lat_degree = int(lat)
lat_decimal_minute = (lat - lat_degree) * 60
lon = abs(station.longitude)
lon_degree = int(lon)
lon_decimal_minute = (lon - lon_degree) * 60
lat = ''.join([str(abs(lat_degree)).rjust(2),
'{0:.2f}'.format(lat_decimal_minute).rjust(5)])
lon = ''.join([str(abs(lon_degree)).rjust(3),
'{0:.2f}'.format(lon_decimal_minute).rjust(5)])
station_str = ''.join([' ', sta_str, lat, lat_str, lon, lon_str, elev])
return station_str
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment