예제 #1
0
    def _get_file_suffix(self):
        """Return a string to be used for the file suffix."""

        # Note: the self.timestamp variable exists for debugging, and
        # should be left as None in actual use, which tells the time_str
        # method to use current system time.
        return time_str(timestamp=self.timestamp,
                        time_zone=self.time_zone,
                        time_format=self.time_format)
예제 #2
0
    def _get_file_suffix(self):
        """Return a string to be used for the file suffix."""

        # Note: the self.timestamp variable exists for debugging, and
        # should be left as None in actual use, which tells the time_str
        # method to use current system time.

        # if there is no split interval
        if self.split_interval is None:
            return time_str(timestamp=self.timestamp,
                            time_zone=self.time_zone,
                            time_format=self.time_format)

        # if the data is being split by N hours
        elif self.split_interval[1] == 'H':  # hour
            timestamp_raw = datetime.now(self.time_zone)
            timestamp_proc = timestamp_raw.replace(
                hour=self.split_interval[0] *
                math.floor(timestamp_raw.hour / self.split_interval[0]),
                minute=0,
                second=0)
            self.next_file_split = timestamp_proc + timedelta(
                seconds=self.split_interval_in_seconds)

            return time_str(timestamp=timestamp_proc.timestamp(),
                            time_zone=self.time_zone,
                            time_format=self.time_format)

        # if the data is being split by N minutes
        elif self.split_interval[1] == 'M':  # minute
            timestamp_raw = datetime.now(self.time_zone)
            timestamp_proc = timestamp_raw.replace(
                minute=self.split_interval[0] *
                math.floor(timestamp_raw.minute / self.split_interval[0]),
                second=0)
            self.next_file_split = timestamp_proc + timedelta(
                seconds=self.split_interval_in_seconds)

            return time_str(timestamp=timestamp_proc.timestamp(),
                            time_zone=self.time_zone,
                            time_format=self.time_format)
예제 #3
0
    def transform(self, record, ts=None):
        """Prepend a timestamp"""
        if record is None:
            return None

        # First off, grab a current timestamp
        ts = ts or timestamp.time_str(time_format=self.time_format)

        # If we've got a list, hope it's a list of records. Recurse,
        # calling transform() on each of the list elements in order and
        # return the resulting list. Give each element the same timestamp.
        if type(record) is list:
            results = []
            for single_record in record:
                results.append(self.transform(single_record, ts=ts))
            return results

        return ts + self.sep + record
 def transform(self, record):
     """Prepend a timestamp"""
     if record is None:
         return None
     return timestamp.time_str(time_format=self.time_format) + ' ' + record
예제 #5
0
 def test_time_str(self):
     self.assertEqual(timestamp.time_str(1507810403.33),
                      '2017-10-12T12:13:23.330000Z')
     self.assertEqual(
         timestamp.time_str(1507810403.33, time_format='%H/%M'), '12/13')
예제 #6
0
 def test_timestamp(self):
     self.assertAlmostEqual(timestamp.timestamp(timestamp.time_str()),
                            timestamp.timestamp(),
                            places=1)
     self.assertEqual(timestamp.timestamp('1970-01-01T00:00:10.0Z'), 10.0)
예제 #7
0
    def transform(self, record):
        """Incorporate any useable fields in this record, and if it gives 
    us a new true wind value, return it."""
        if not record:
            return None

        if not type(record) is DASRecord:
            logging.warning('Improper format record: %s', record)
            return None

        update = False
        for field_name in record.fields:
            if field_name in self.course_fields:
                self.course_val = record.fields[field_name]
            elif field_name in self.speed_fields:
                self.speed_val = record.fields[
                    field_name] * self.convert_speed_factor
            elif field_name in self.heading_fields:
                self.heading_val = record.fields[field_name]
            elif field_name in self.wind_dir_fields:
                self.wind_dir_val = record.fields[field_name]
            elif field_name in self.wind_speed_fields:
                self.wind_speed_val = record.fields[
                    field_name] * self.convert_wind_factor

            if field_name in self.update_on_fields:
                update = True

        # If we've not seen anything that updates fields that would
        # trigger a new true winds value, return None.
        if not update:
            return None

        if self.course_val is None:
            logging.info('Still missing course_val')
            return None
        if self.speed_val is None:
            logging.info('Still missing speed_val')
            return None
        if self.heading_val is None:
            logging.info('Still missing heading_val')
            return None
        if self.wind_dir_val is None:
            logging.info('Still missing wind_dir_val')
            return None
        if self.wind_speed_val is None:
            logging.info('Still missing wind_speed_val')
            return None

        logging.info('Computing new true winds')
        (true_dir, true_speed, app_dir) = truew(crse=self.course_val,
                                                cspd=self.speed_val,
                                                hd=self.heading_val,
                                                wdir=self.wind_dir_val,
                                                zlr=self.zero_line_reference,
                                                wspd=self.wind_speed_val)

        logging.info('Got true winds: dir: %s, speed: %s, app_dir: %s',
                     true_dir, true_speed, app_dir)
        if true_dir is None or true_speed is None or app_dir is None:
            logging.info('Got invalid true winds')
            return None

        # If here, we've got a valid new true wind result
        if self.output_nmea:
            new_record = '%s %s %g,%g,%g' % (self.data_id,
                                             time_str(record.timestamp),
                                             true_dir, true_speed, app_dir)
        else:
            new_record = DASRecord(data_id=self.data_id,
                                   timestamp=record.timestamp,
                                   fields={
                                       'TrueWindDir': true_dir,
                                       'TrueWindSpeed': true_speed,
                                       'ApparentWindDir': app_dir
                                   })
        return new_record