Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
nick-falco committed Jul 26, 2019
1 parent 1592d22 commit cd7fdf3
Showing 1 changed file with 132 additions and 144 deletions.
276 changes: 132 additions & 144 deletions ph5/clients/ph5tostationxml.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,8 +343,9 @@ def add_ph5_stationids(self):
arrayorder = self.manager.ph5.Array_t[array_name]['order']
for station in arrayorder:
station_list = arraybyid.get(station)
for deployment in station_list:
station_entry = station_list[deployment][0]
for deployment, station_entry in \
((dk, se) for dk, dv in station_list.items()
for se in dv):
for sta_pattern in sta_xml_obj.station_list:
if not station_entry['seed_station_name_s'] and \
fnmatch.fnmatch(str(station),
Expand Down Expand Up @@ -648,111 +649,100 @@ def read_stations(self):
if not ph5utils.does_pattern_exists(array_patterns,
array_code):
continue

arraybyid = self.manager.ph5.Array_t[array_name]['byid']
arraybyid = self.manager.ph5.Array_t[array_name]['byid']
arrayorder = self.manager.ph5.Array_t[array_name]['order']
for sta_id in arrayorder:
station_list = arraybyid.get(sta_id)
obs_channels = []
if sta_id not in sta_xml_obj.ph5_station_id_list:
continue
for deployment in station_list:
station_epochs = station_list[deployment]
for station_entry in station_epochs:
sta_longitude = station_entry['location/X/value_d']
sta_latitude = station_entry['location/Y/value_d']
sta_elevation = \
station_entry['location/Z/value_d']

if not self.is_lat_lon_match(sta_xml_obj,
sta_latitude,
sta_longitude):
for deployment, station_epoch, station_entry in \
((dk, dv, se) for dk, dv in station_list.items()
for se in dv):
if not self.is_lat_lon_match(
sta_xml_obj,
station_entry['location/Y/value_d'],
station_entry['location/X/value_d']):
continue

if station_entry['seed_station_name_s']:
station_code = station_entry['seed_station_name_s']
else:
station_code = sta_id

start_date = UTCDateTime(
station_entry['deploy_time/epoch_l'])
end_date = UTCDateTime(
station_entry['pickup_time/epoch_l'])

if (sta_xml_obj.start_time and
sta_xml_obj.start_time > end_date):
# chosen start time after pickup
continue
elif (sta_xml_obj.end_time and
sta_xml_obj.end_time < start_date):
# chosen end time before pickup
continue

obs_channels = []
# run channel filters if necessary. we do this
# first to avoid creating a station that has no
# channels
if (self.manager.level.upper() == "RESPONSE" or
self.manager.level.upper() == "CHANNEL" or
sta_xml_obj.location_list != ['*'] or
sta_xml_obj.channel_list != ['*'] or
sta_xml_obj.component_list != ['*'] or
sta_xml_obj.receiver_list != ['*']):
obs_channels = self.read_channels(sta_xml_obj,
station_entry,
deployment,
station_code,
array_code)
# go to the next station if no channels were
# returned
if len(obs_channels) == 0:
continue

if station_entry['seed_station_name_s']:
station_code = \
station_entry['seed_station_name_s']
else:
station_code = sta_id

start_date = station_entry['deploy_time/epoch_l']
start_date = UTCDateTime(start_date)
end_date = station_entry['pickup_time/epoch_l']
end_date = UTCDateTime(end_date)
if sta_xml_obj.start_time and \
sta_xml_obj.start_time > end_date:
# chosen start time after pickup
continue
elif sta_xml_obj.end_time and \
sta_xml_obj.end_time < start_date:
# chosen end time before pickup
continue
creation_date = start_date
termination_date = end_date
site_name = station_entry['location/description_s']

obs_channels = []
# run channel filters if necessary. we do this
# first to avoid creating a station that has no
# channels
if self.manager.level.upper() == "RESPONSE" or \
self.manager.level.upper() == "CHANNEL" or \
sta_xml_obj.location_list != ['*'] or \
sta_xml_obj.channel_list != ['*'] or \
sta_xml_obj.component_list != ['*'] or \
sta_xml_obj.receiver_list != ['*']:
obs_channels = \
self.read_channels(sta_xml_obj,
station_entry,
deployment,
station_code,
array_code)
# go to the next station if no channels were
# returned
if len(obs_channels) == 0:
continue

sta_key = \
self.manager.get_station_key(station_code,
start_date,
end_date,
sta_longitude,
sta_latitude,
sta_elevation,
site_name)
if self.manager.get_obs_station(sta_key):
# station already created and added to metadata
obs_station = \
self.manager.get_obs_station(sta_key)
else:
# create and add a new station
obs_station = self.create_obs_station(
station_code,
start_date,
end_date,
sta_longitude,
sta_latitude,
sta_elevation,
creation_date,
termination_date,
site_name)

# Add matching channels to station if necessary
if obs_channels:
obs_station.channels.extend(obs_channels)
obs_station.selected_number_of_channels = \
len(obs_station.channels)
else:
obs_station.selected_number_of_channels = 0

obs_station.total_number_of_channels += \
len(station_list)

if self.manager.get_obs_station(sta_key) is None:
all_stations.append(obs_station)
self.manager.set_obs_station(sta_key,
obs_station)
sta_key = self.manager.get_station_key(
station_code,
start_date,
end_date,
station_entry['location/X/value_d'], # lng
station_entry['location/Y/value_d'], # lat
station_entry['location/Z/value_d'], # elv
station_entry['location/description_s'])
if self.manager.get_obs_station(sta_key):
# station already created and added to metadata
obs_station = self.manager.get_obs_station(sta_key)
else:
# create and add a new station
obs_station = self.create_obs_station(
station_code,
start_date,
end_date,
station_entry['location/X/value_d'], # lng
station_entry['location/Y/value_d'], # lat
station_entry['location/Z/value_d'], # elv
start_date, # creation_date
end_date, # termination date
station_entry['location/description_s'])

# Add matching channels to station if necessary
if obs_channels:
obs_station.channels.extend(obs_channels)
obs_station.selected_number_of_channels = \
len(obs_station.channels)
else:
obs_station.selected_number_of_channels = 0

obs_station.total_number_of_channels += \
len(station_list)

if self.manager.get_obs_station(sta_key) is None:
all_stations.append(obs_station)
self.manager.set_obs_station(sta_key,
obs_station)
return all_stations

def read_channels(self, sta_xml_obj, station_entry, deployment,
Expand All @@ -772,10 +762,9 @@ def read_channels(self, sta_xml_obj, station_entry, deployment,
if not ph5utils.does_pattern_exists(component_list_patterns, c_id):
return

cha_code = \
station_entry['seed_band_code_s'] + \
station_entry['seed_instrument_code_s'] + \
station_entry['seed_orientation_code_s']
cha_code = (station_entry['seed_band_code_s'] +
station_entry['seed_instrument_code_s'] +
station_entry['seed_orientation_code_s'])

for pattern in cha_list_patterns:
if fnmatch.fnmatch(cha_code, pattern):
Expand All @@ -788,13 +777,10 @@ def read_channels(self, sta_xml_obj, station_entry, deployment,
loc_code):
continue

cha_longitude = station_entry['location/X/value_d']
cha_latitude = station_entry['location/Y/value_d']
cha_elevation = station_entry['location/Z/value_d']

if not self.is_lat_lon_match(sta_xml_obj,
cha_latitude,
cha_longitude):
if not self.is_lat_lon_match(
sta_xml_obj,
station_entry['location/Y/value_d'],
station_entry['location/X/value_d']):
continue
start_date = UTCDateTime(station_entry['deploy_time/epoch_l'])
end_date = UTCDateTime(station_entry['pickup_time/epoch_l'])
Expand All @@ -812,23 +798,24 @@ def read_channels(self, sta_xml_obj, station_entry, deployment,
receiver_table_n_i = station_entry['receiver_table_n_i']
Receiver_t = self.manager.ph5.get_receiver_t_by_n_i(
receiver_table_n_i)
azimuth = Receiver_t['orientation/azimuth/value_f']
dip = Receiver_t['orientation/dip/value_f']

sensor_manufacturer = station_entry['sensor/manufacturer_s']
sensor_model = station_entry['sensor/model_s']
sensor_serial = station_entry['sensor/serial_number_s']
das_manufacturer = station_entry['das/manufacturer_s']
das_model = station_entry['das/model_s']
das_serial = station_entry['das/serial_number_s']
cha_component = station_entry['channel_number_i']

cha_key = self.manager.get_channel_key(
sta_code, loc_code, cha_code, start_date, end_date,
cha_longitude, cha_latitude, cha_elevation, cha_component,
receiver_id, sample_rate, sample_rate_ration, azimuth,
dip, sensor_manufacturer, sensor_model, sensor_serial,
das_manufacturer, das_model, das_serial)
station_entry['location/X/value_d'], # lng
station_entry['location/Y/value_d'], # lat
station_entry['location/Z/value_d'], # elv
station_entry['channel_number_i'], # component
receiver_id,
sample_rate,
sample_rate_ration,
Receiver_t['orientation/azimuth/value_f'],
Receiver_t['orientation/dip/value_f'],
station_entry['sensor/manufacturer_s'],
station_entry['sensor/model_s'],
station_entry['sensor/serial_number_s'],
station_entry['das/manufacturer_s'],
station_entry['das/model_s'],
station_entry['das/serial_number_s'])

if self.manager.get_obs_channel(cha_key):
# update existing channe entry
Expand All @@ -840,27 +827,28 @@ def read_channels(self, sta_xml_obj, station_entry, deployment,
obs_cha.extra.PH5Array.value = ",".join(arrays_list)
else:
# create new channel entry
obs_channel = self.create_obs_channel(sta_code,
loc_code,
cha_code,
start_date,
end_date,
cha_longitude,
cha_latitude,
cha_elevation,
cha_component,
receiver_id,
array_code,
sample_rate,
sample_rate_ration,
azimuth,
dip,
sensor_manufacturer,
sensor_model,
sensor_serial,
das_manufacturer,
das_model,
das_serial)
obs_channel = self.create_obs_channel(
sta_code,
loc_code,
cha_code,
start_date,
end_date,
station_entry['location/X/value_d'], # lng
station_entry['location/Y/value_d'], # lat
station_entry['location/Z/value_d'], # elv
station_entry['channel_number_i'], # component
receiver_id,
array_code,
sample_rate,
sample_rate_ration,
Receiver_t['orientation/azimuth/value_f'],
Receiver_t['orientation/dip/value_f'],
station_entry['sensor/manufacturer_s'],
station_entry['sensor/model_s'],
station_entry['sensor/serial_number_s'],
station_entry['das/manufacturer_s'],
station_entry['das/model_s'],
station_entry['das/serial_number_s'])
self.manager.set_obs_channel(cha_key, obs_channel)

# read response and add it to obspy channel inventory
Expand Down

0 comments on commit cd7fdf3

Please sign in to comment.