Ejemplo n.º 1
0
    def test_microsecs_to_sec(self):
        """
        test microsecs_to_sec
        """

        # bad
        with self.assertRaises(ValueError):
            ph5utils.microsecs_to_sec("1234")
        ret = ph5utils.microsecs_to_sec(12345)
        self.assertTrue(ret)
        self.assertAlmostEqual(ret, 0.012345, 6)
Ejemplo n.º 2
0
    def Parse_Networks(self, path):
        network_list = self.args.get('network_list')
        if isinstance(network_list, collections.Iterable):
            network_patterns = network_list
        else:
            network_patterns = self.args.get('network_list').split(',')

        reportnum_list = self.args.get('reportnum_list')
        if isinstance(reportnum_list, collections.Iterable):
            reportnum_patterns = reportnum_list
        else:
            reportnum_patterns = self.args.get('reportnum_list').split(',')

        self.ph5 = ph5api.PH5(path=path, nickname=self.args.get('nickname'))
        self.ph5.read_experiment_t()
        self.experiment_t = self.ph5.Experiment_t['rows']
        self.ph5.read_event_t_names()
        self.ph5.read_array_t_names()
        test = self.read_events(None)
        shot_lines = sorted(self.ph5.Event_t_names)
        if test == -1:
            self.ph5.close()
            return None
        if network_patterns and reportnum_patterns:
            if not ph5utils.does_pattern_exists(
                network_patterns, self.experiment_t[0]['net_code_s']) and \
               not ph5utils.does_pattern_exists(
                   reportnum_patterns,
                   self.experiment_t[0]['experiment_id_s']):
                self.ph5.close()
                return None
        elif network_patterns:
            # read network code and compare to network list
            if not ph5utils.does_pattern_exists(
                    network_patterns, self.experiment_t[0]['net_code_s']):
                self.ph5.close()
                return None
        elif reportnum_patterns:
            # read reportnum and compare to reportnum list
            if not ph5utils.does_pattern_exists(
                    reportnum_patterns,
                    self.experiment_t[0]['experiment_id_s']):
                self.ph5.close()
                return None

        self.read_arrays(None)
        array_names = self.ph5.Array_t_names
        array_names.sort()

        # get the earliest deploy and latest pickup dates from the arrays table
        earliest_deploy = None
        latest_pickup = None
        for array_name in array_names:
            arraybyid = self.ph5.Array_t[array_name]['byid']
            arrayorder = self.ph5.Array_t[array_name]['order']
            for ph5_station in arrayorder:
                station_list = arraybyid.get(ph5_station)
                for deployment in station_list:
                    station_len = len(station_list[deployment])
                    for st_num in range(0, station_len):
                        micro = ph5utils.microsecs_to_sec(
                            station_list[deployment][st_num]
                            ['deploy_time/micro_seconds_i'])
                        deploy_time = (station_list[deployment][st_num]
                                       ['deploy_time/epoch_l'] + micro)
                        micro = ph5utils.microsecs_to_sec(
                            station_list[deployment][st_num]
                            ['pickup_time/micro_seconds_i'])
                        pickup_time = (station_list[deployment][st_num]
                                       ['pickup_time/epoch_l'] + micro)
                        if earliest_deploy is None or \
                                earliest_deploy > deploy_time:
                            earliest_deploy = deploy_time
                        if latest_pickup is None or \
                                latest_pickup < pickup_time:
                            latest_pickup = pickup_time

        if self.args.get('start_time') and self.args.get(
                'start_time') < datetime.fromtimestamp(earliest_deploy):
            self.args['start_time'] = datetime.fromtimestamp(earliest_deploy)

        if self.args.get('stop_time') and self.args.get(
                'stop_time') > datetime.fromtimestamp(latest_pickup):
            self.args['stop_time'] = datetime.fromtimestamp(latest_pickup)

        network = Network(self.experiment_t[0]['net_code_s'],
                          self.experiment_t[0]['experiment_id_s'],
                          self.experiment_t[0]['longname_s'])

        shot_lines_ = []
        shots = []

        for shot_line in shot_lines:
            sl = Shotline(shot_line)
            event_t = self.ph5.Event_t[shot_line]['byid']
            if self.args.get('shotline') and \
               not ph5utils.does_pattern_exists(self.args.get('shotline'),
                                                str(shot_line[-3:])):
                continue
            for key, value in event_t.iteritems():
                if self.args.get('shotid') and \
                        not ph5utils.does_pattern_exists(
                            self.args.get('shotid'), key):
                    continue
                cha_longitude = float(value['location/X/value_d'])
                cha_latitude = float(value['location/Y/value_d'])
                if not self.is_lat_lon_match(cha_latitude, cha_longitude):
                    continue

                if self.args.get('start_time') and (datetime.fromtimestamp(
                        value['time/epoch_l']) < self.args.get('start_time')):
                    continue

                if self.args.get('stop_time') and (datetime.fromtimestamp(
                        value['time/epoch_l']) > self.args.get('stop_time')):
                    continue

                restricted = self.args.get('restricted')
                if restricted:
                    is_restricted = False
                    for r in restricted:
                        if r.network == network.code and \
                           value['time/epoch_l'] >= r.starttime and \
                           value['time/epoch_l'] <= r.endtime:
                            is_restricted = True
                            break
                    if is_restricted:
                        continue

                shot = Shot(
                    key, value['size/value_d'], value['size/units_s'],
                    self.get_fdsn_time(value['time/epoch_l'],
                                       value['time/micro_seconds_i']),
                    value['location/Y/value_d'], value['location/X/value_d'],
                    value['location/Z/value_d'], value['location/X/units_s'],
                    value['location/Z/units_s'], value['description_s'])
                shot.depth = value['depth/value_d']
                shots.append(shot)

            sl.shots = shots
            shot_lines_.append(sl)

        network.shot_lines = shot_lines_

        self.ph5.close()

        return network
Ejemplo n.º 3
0
 def get_fdsn_time(self, epoch, microseconds):
     seconds = ph5utils.microsecs_to_sec(microseconds)
     fdsn_time = datetime.utcfromtimestamp(epoch + seconds).strftime(
         "%Y-%m-%dT%H:%M:%S.%fZ")
     return fdsn_time