예제 #1
0
def csv_flight_details(hdf_path,
                       kti_list,
                       kpv_list,
                       phase_list,
                       dest_path=None,
                       append_to_file=True):
    """
    Currently writes to csv and prints to a table.
    
    Phase types have a 'duration' column
    
    :param dest_path: Outputs CSV to dest_path (removing if exists). If None,
      collates results by appending to a single file: 'combined_test_output.csv'
    """
    rows = []
    params = ['Airspeed', 'Altitude AAL']
    attrs = ['value', 'datetime', 'latitude', 'longitude']
    header = ['path', 'type', 'index', 'duration', 'name'] + attrs + params
    if not dest_path:
        header.append('Path')
    formats = {
        'index': '%.3f',
        'value': '%.3f',
        'duration': '%.2f',
        'latitude': '%.4f',
        'longitude': '%.4f',
        'Airspeed': '%d kts',
        'Altitude AAL': '%d ft',
    }
    for value in kti_list:
        vals = value.todict()  # recordtype
        vals['path'] = hdf_path
        vals['type'] = 'Key Time Instance'
        rows.append(vals)

    for value in kpv_list:
        vals = value.todict()  # recordtype
        vals['path'] = hdf_path
        vals['type'] = 'Key Point Value'
        rows.append(vals)

    for value in phase_list:
        vals = value._asdict()  # namedtuple
        vals['name'] = value.name + ' [START]'
        vals['path'] = hdf_path
        vals['type'] = 'Phase'
        vals['index'] = value.start_edge
        vals['duration'] = value.stop_edge - value.start_edge  # (secs)
        rows.append(vals)
        # create another at the stop of the phase
        end = copy(vals)
        end['name'] = value.name + ' [END]'
        end['index'] = value.stop_edge
        rows.append(end)

    # Append values of useful parameters at this time
    with hdf_file(hdf_path) as hdf:
        for param in params:
            # Create DerivedParameterNode to utilise the .at() method
            if param not in hdf:
                continue
            p = hdf[param]
            dp = Parameter(name=p.name,
                           array=p.array,
                           frequency=p.frequency,
                           offset=p.offset)
            for row in rows:
                row[param] = dp.at(row['index'])

    # sort rows
    rows = sorted(rows, key=lambda x: x['index'])

    skip_header = False

    # print to CSV
    if not dest_path:
        dest_path = 'combined_test_output.csv'
    elif os.path.isfile(dest_path):
        if not append_to_file:
            logger.info("Deleting existing copy of: %s", dest_path)
            os.remove(dest_path)
        else:
            # If we are appending and the file exista, we don't want to output
            # the header again
            skip_header = True

    with open(dest_path, 'ab') as dest:
        writer = TypedWriter(dest,
                             fieldnames=header,
                             fieldformats=formats,
                             skip_header=skip_header,
                             extrasaction='ignore')
        writer.writerows(rows)
        # print to Debug I/O
        logger.info(
            indent([header] + writer.rowlist(rows),
                   hasHeader=True,
                   wrapfunc=lambda x: str(x)))
    return rows
def csv_flight_details(hdf_path, kti_list, kpv_list, phase_list,
                       dest_path=None, append_to_file=True):
    """
    Currently writes to csv and prints to a table.

    Phase types have a 'duration' column

    :param dest_path: Outputs CSV to dest_path (removing if exists). If None,
      collates results by appending to a single file: 'combined_test_output.csv'
    """
    rows = []
    params = ['Airspeed', 'Altitude AAL']
    attrs = ['value', 'datetime', 'latitude', 'longitude']
    header = ['path', 'type', 'index', 'duration', 'name'] + attrs + params
    if not dest_path:
        header.append('Path')
    formats = {'index': '%.3f',
               'value': '%.3f',
               'duration': '%.2f',
               'latitude': '%.4f',
               'longitude': '%.4f',
               'Airspeed': '%d kts',
               'Altitude AAL': '%d ft',
               }
    for value in kti_list:
        vals = value._asdict()  # recordtype
        vals['path'] = hdf_path
        vals['type'] = 'Key Time Instance'
        rows.append( vals )

    for value in kpv_list:
        vals = value._asdict()  # recordtype
        vals['path'] = hdf_path
        vals['type'] = 'Key Point Value'
        rows.append( vals )

    for value in phase_list:
        vals = value._asdict()  # namedtuple
        vals['name'] = value.name + ' [START]'
        vals['path'] = hdf_path
        vals['type'] = 'Phase'
        vals['index'] = value.start_edge
        vals['duration'] = value.stop_edge - value.start_edge  # (secs)
        rows.append(vals)
        # create another at the stop of the phase
        end = copy(vals)
        end['name'] = value.name + ' [END]'
        end['index'] = value.stop_edge
        rows.append(end)

    # Append values of useful parameters at this time
    with hdf_file(hdf_path) as hdf:
        for param in params:
            # Create DerivedParameterNode to utilise the .at() method
            if param not in hdf:
                continue
            p = hdf[param]
            dp = Parameter(name=p.name, array=p.array,
                           frequency=p.frequency, offset=p.offset)
            for row in rows:
                row[param] = dp.at(row['index'])

    # sort rows
    rows = sorted(rows, key=lambda x: x['index'])

    skip_header = False

    # print to CSV
    if not dest_path:
        dest_path = 'combined_test_output.csv'
    elif os.path.isfile(dest_path):
        if not append_to_file:
            logger.info("Deleting existing copy of: %s", dest_path)
            os.remove(dest_path)
        else:
            # If we are appending and the file exista, we don't want to output
            # the header again
            skip_header = True

    with open(dest_path, 'a') as dest:
        writer = TypedWriter(dest, fieldnames=header, fieldformats=formats,
                             skip_header=skip_header, extrasaction='ignore')
        writer.writerows(rows)
        # print to Debug I/O
        logger.debug(indent([header] + writer.rowlist(rows), hasHeader=True, wrapfunc=lambda x:str(x)))
    return rows