Commit b612bd94 authored by Kelvin Rodriguez's avatar Kelvin Rodriguez Committed by GitHub
Browse files

single isd format + mro test conversion (#336)

* single isd format + mro test_conversion

* init

* isds

* we forget to add important files around here
parent 81e9de49
Loading
Loading
Loading
Loading
+5 −3
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@ from collections import OrderedDict

from ale.formatters.usgscsm_formatter import to_usgscsm
from ale.formatters.isis_formatter import to_isis
from ale.formatters.formatter import to_isd
from ale.base.data_isis import IsisSpice

from abc import ABC
@@ -32,7 +33,8 @@ __all__ = [driver for driver in __all__ if driver not in __disabled_drivers__]
__driver_modules__ = [importlib.import_module('.'+m, package='ale.drivers') for m in __all__]

__formatters__ = {'usgscsm': to_usgscsm,
                  'isis': to_isis}
                  'isis': to_isis,
                  'ale' : to_isd}

def sort_drivers(drivers=[]):
    return list(sorted(drivers, key=lambda x:IsisSpice in x.__bases__, reverse=False))
@@ -51,7 +53,7 @@ class AleJsonEncoder(json.JSONEncoder):
            return obj.isoformat()
        return json.JSONEncoder.default(self, obj)

def load(label, props={}, formatter='usgscsm', verbose=False):
def load(label, props={}, formatter='ale', verbose=False):
    """
    Attempt to load a given label from all possible drivers

@@ -86,6 +88,6 @@ def load(label, props={}, formatter='usgscsm', verbose=False):
                traceback.print_exc()
    raise Exception('No Such Driver for Label')

def loads(label, props='', formatter='usgscsm', verbose=False):
def loads(label, props='', formatter='ale', verbose=False):
    res = load(label, props, formatter, verbose=verbose)
    return json.dumps(res, cls=AleJsonEncoder)
+164 −0
Original line number Diff line number Diff line
import json
import numpy as np
from scipy.interpolate import interp1d, BPoly

from networkx.algorithms.shortest_paths.generic import shortest_path

from ale.transformation import FrameChain
from ale.base.type_sensor import LineScanner, Framer
from ale.rotation import ConstantRotation, TimeDependentRotation

def to_isd(driver):
    """
    Formatter to create sensor model meta data from a driver.

    Parameters
    ----------
    driver : Driver
        Concrete driver for the image that meta data is being generated for.

    Returns
    -------
    string
        The ISIS compatible meta data as a JSON encoded string.
    """


    meta_data = {}

    meta_data['IsisCameraVersion'] = driver.sensor_model_version

    # interiror orientation
    meta_data['NaifKeywords'] = driver.naif_keywords
    meta_data['detector_sample_summing'] = driver.sample_summing
    meta_data['detector_line_summing'] = driver.line_summing
    meta_data['focal_length_model'] = {
        'focal_length' : driver.focal_length
    }
    meta_data['detector_center'] = {
        'line' : driver.detector_center_line,
        'sample' : driver.detector_center_sample
    }

    meta_data['starting_detector_line'] = driver.detector_start_line
    meta_data['starting_detector_sample'] = driver.detector_start_sample
    meta_data['focal2pixel_lines'] = driver.focal2pixel_lines
    meta_data['focal2pixel_samples'] = driver.focal2pixel_samples
    meta_data['optical_distortion'] = driver.usgscsm_distortion_model

    # general information
    meta_data['image_lines'] = driver.image_lines
    meta_data['image_samples'] = driver.image_samples
    meta_data['name_platform'] = driver.platform_name
    meta_data['name_sensor'] = driver.sensor_name
    meta_data['reference_height'] = {
        "maxheight": 1000,
        "minheight": -1000,
        "unit": "m"
    }

    # line scan sensor model specifics
    if isinstance(driver, LineScanner):
        meta_data['name_model'] = 'USGS_ASTRO_LINE_SCANNER_SENSOR_MODEL'
        meta_data['interpolation_method'] = 'lagrange'

        start_lines, start_times, scan_rates = driver.line_scan_rate
        center_time = driver.center_ephemeris_time
        meta_data['line_scan_rate'] = [[line, time, rate] for line, time, rate in zip(start_lines, start_times, scan_rates)]
        meta_data['starting_ephemeris_time'] = driver.ephemeris_start_time
        meta_data['center_ephemeris_time'] = center_time

    # frame sensor model specifics
    if isinstance(driver, Framer):
        meta_data['name_model'] = 'USGS_ASTRO_FRAME_SENSOR_MODEL'
        meta_data['center_ephemeris_time'] = driver.center_ephemeris_time

    frame_chain = driver.frame_chain
    sensor_frame = driver.sensor_frame_id
    target_frame = driver.target_frame_id

    body_radii = driver.target_body_radii
    meta_data['radii'] = {
        'semimajor' : body_radii[0],
        'semiminor' : body_radii[2],
        'unit' : 'km'
    }

    instrument_pointing = {}
    source_frame, destination_frame, time_dependent_sensor_frame = frame_chain.last_time_dependent_frame_between(1, sensor_frame)

    # Reverse the frame order because ISIS orders frames as
    # (destination, intermediate, ..., intermediate, source)
    instrument_pointing['TimeDependentFrames'] = shortest_path(frame_chain, destination_frame, 1)
    time_dependent_rotation = frame_chain.compute_rotation(1, destination_frame)
    instrument_pointing['CkTableStartTime'] = time_dependent_rotation.times[0]
    instrument_pointing['CkTableEndTime'] = time_dependent_rotation.times[-1]
    instrument_pointing['CkTableOriginalSize'] = len(time_dependent_rotation.times)
    instrument_pointing['EphemerisTimes'] = time_dependent_rotation.times
    instrument_pointing['Quaternions'] = time_dependent_rotation.quats[:, [3, 0, 1, 2]]
    instrument_pointing['AngularVelocity'] = time_dependent_rotation.av

    # Reverse the frame order because ISIS orders frames as
    # (destination, intermediate, ..., intermediate, source)
    instrument_pointing['ConstantFrames'] = shortest_path(frame_chain, sensor_frame, destination_frame)
    constant_rotation = frame_chain.compute_rotation(destination_frame, sensor_frame)
    instrument_pointing['ConstantRotation'] = constant_rotation.rotation_matrix().flatten()
    meta_data['InstrumentPointing'] = instrument_pointing

    body_rotation = {}
    source_frame, destination_frame, time_dependent_target_frame = frame_chain.last_time_dependent_frame_between(target_frame, 1)

    if source_frame != 1:
        # Reverse the frame order because ISIS orders frames as
        # (destination, intermediate, ..., intermediate, source)
        body_rotation['TimeDependentFrames'] = shortest_path(frame_chain, source_frame, 1)
        time_dependent_rotation = frame_chain.compute_rotation(1, source_frame)
        body_rotation['CkTableStartTime'] = time_dependent_rotation.times[0]
        body_rotation['CkTableEndTime'] = time_dependent_rotation.times[-1]
        body_rotation['CkTableOriginalSize'] = len(time_dependent_rotation.times)
        body_rotation['EphemerisTimes'] = time_dependent_rotation.times
        body_rotation['Quaternions'] = time_dependent_rotation.quats[:, [3, 0, 1, 2]]
        body_rotation['AngularVelocity'] = time_dependent_rotation.av

    if source_frame != target_frame:
        # Reverse the frame order because ISIS orders frames as
        # (destination, intermediate, ..., intermediate, source)
        body_rotation['ConstantFrames'] = shortest_path(frame_chain, target_frame, source_frame)
        constant_rotation = frame_chain.compute_rotation(source_frame, target_frame)
        body_rotation['ConstantRotation'] = constant_rotation.rotation_matrix().flatten()

    meta_data['BodyRotation'] = body_rotation

    j2000_rotation = frame_chain.compute_rotation(target_frame, 1)

    instrument_position = {}
    positions, velocities, times = driver.sensor_position
    instrument_position['SpkTableStartTime'] = times[0]
    instrument_position['SpkTableEndTime'] = times[-1]
    instrument_position['SpkTableOriginalSize'] = len(times)
    instrument_position['EphemerisTimes'] = times
    # Rotate positions and velocities into J2000 then scale into kilometers
    velocities = j2000_rotation.rotate_velocity_at(positions, velocities, times)/1000
    positions = j2000_rotation.apply_at(positions, times)/1000
    instrument_position['Positions'] = positions
    instrument_position['Velocities'] = velocities
    meta_data['InstrumentPosition'] = instrument_position

    sun_position = {}
    positions, velocities, times = driver.sun_position
    sun_position['SpkTableStartTime'] = times[0]
    sun_position['SpkTableEndTime'] = times[-1]
    sun_position['SpkTableOriginalSize'] = len(times)
    sun_position['EphemerisTimes'] = times
    # Rotate positions and velocities into J2000 then scale into kilometers
    velocities = j2000_rotation.rotate_velocity_at(positions, velocities, times)/1000
    positions = j2000_rotation.apply_at(positions, times)/1000
    sun_position['Positions'] = positions
    sun_position['Velocities'] = velocities
    meta_data['SunPosition'] = sun_position

    # check that there is a valid sensor model name
    if 'name_model' not in meta_data:
        raise Exception('No CSM sensor model name found!')

    return meta_data
+13 −0
Original line number Diff line number Diff line
@@ -3,6 +3,8 @@ import os
import re
import warnings
import numpy as np
import json

import ale

from glob import glob
@@ -93,6 +95,17 @@ def get_image_label(image, label_type='pds3'):

    return label_file[0]

def get_isd(instrument):
    if not isinstance(instrument, str):
        raise KeyError('instrument name is not a string')

    label_file = glob(os.path.join(data_root, 'isds',f'{instrument}_isd.json'))
    if not label_file:
        raise Exception(f'Could not find label file for {instrument}')

    return json.load(open(label_file[0]))


def get_image_kernels(image):
    """
    Get the kernels to use with a test image.
+506 −0
Original line number Diff line number Diff line
{
    "IsisCameraVersion": 1,
    "NaifKeywords": {
        "BODY499_RADII": [
            3396.19,
            3396.19,
            3376.2
        ],
        "BODY_FRAME_CODE": 10014,
        "BODY_CODE": 499,
        "INS-74021_FOV_ANGLE_UNITS": "DEGREES",
        "TKFRAME_-74021_UNITS": "DEGREES",
        "INS-74021_FOV_ANGULAR_SIZE": [
            5.73,
            0.001146
        ],
        "INS-74021_PIXEL_LINES": 1.0,
        "TKFRAME_-74021_ANGLES": [
            0.0,
            0.0,
            0.0
        ],
        "INS-74021_IFOV": [
            2e-05,
            2e-05
        ],
        "FRAME_-74021_CENTER": -74.0,
        "INS-74021_F/RATIO": 3.25,
        "INS-74021_PLATFORM_ID": -74000.0,
        "INS-74021_CCD_CENTER": [
            2500.5,
            0.5
        ],
        "INS-74021_PIXEL_SAMPLES": 5000.0,
        "INS-74021_FOCAL_LENGTH": 352.9271664,
        "INS-74021_FOV_CROSS_ANGLE": 0.00057296,
        "INS-74021_TRANSX": [
            0.0,
            0.0,
            0.007
        ],
        "INS-74021_FOV_CLASS_SPEC": "ANGLES",
        "INS-74021_TRANSY": [
            0.0,
            0.007,
            0.0
        ],
        "INS-74021_FOV_REF_VECTOR": [
            0.0,
            1.0,
            0.0
        ],
        "INS-74021_BORESIGHT": [
            0.0,
            0.0,
            1.0
        ],
        "FRAME_-74021_NAME": "MRO_CTX",
        "INS-74021_PIXEL_PITCH": 0.007,
        "TKFRAME_-74021_AXES": [
            1.0,
            2.0,
            3.0
        ],
        "TKFRAME_-74021_SPEC": "ANGLES",
        "INS-74021_BORESIGHT_LINE": 0.430442527,
        "INS-74021_FOV_SHAPE": "RECTANGLE",
        "INS-74021_BORESIGHT_SAMPLE": 2543.46099,
        "FRAME_-74021_CLASS": 4.0,
        "INS-74021_CK_FRAME_ID": -74000.0,
        "INS-74021_ITRANSL": [
            0.0,
            142.85714285714,
            0.0
        ],
        "INS-74021_FOV_REF_ANGLE": 2.86478898,
        "INS-74021_ITRANSS": [
            0.0,
            0.0,
            142.85714285714
        ],
        "FRAME_-74021_CLASS_ID": -74021.0,
        "INS-74021_OD_K": [
            -0.0073433925920054505,
            2.8375878636241697e-05,
            1.2841989124027099e-08
        ],
        "INS-74021_FOV_FRAME": "MRO_CTX",
        "INS-74021_CK_REFERENCE_ID": -74900.0,
        "TKFRAME_-74021_RELATIVE": "MRO_CTX_BASE",
        "INS-74021_PIXEL_SIZE": [
            0.007,
            0.007
        ],
        "SCLK_PARTITION_END_74999": [
            52973626698957.0,
            56987144678331.0,
            58187590527162.99,
            60316687182323.0,
            60877152115000.0,
            61228279788693.0,
            61339176915162.99,
            61899057915627.0,
            63521451859691.0,
            65622287643263.0
        ],
        "SCLK01_N_FIELDS_74999": 2.0,
        "SCLK01_OUTPUT_DELIM_74999": 1.0,
        "BODY499_POLE_DEC": [
            52.8865,
            -0.0609,
            0.0
        ],
        "SCLK01_OFFSETS_74999": [
            0.0,
            0.0
        ],
        "SCLK_DATA_TYPE_74999": 1.0,
        "SCLK01_COEFFICIENTS_74999": [
            0.0,
            -631195148.816,
            1.0,
            3097283854336.0,
            -583934347.816,
            1.0,
            5164027215872.0,
            -552398346.816,
            1.0,
            7230770577408.0
        ],
        "SCLK01_TIME_SYSTEM_74999": 2.0,
        "SCLK_PARTITION_START_74999": [
            0.0,
            52973626982399.99,
            56987144683520.0,
            58187590533120.0,
            60316687204352.01,
            60877152124927.99,
            61228279791616.0,
            61339176927232.0,
            61899057922048.0,
            63521451868160.0
        ],
        "BODY499_POLE_RA": [
            317.68143,
            -0.1061,
            0.0
        ],
        "BODY499_PM": [
            176.63,
            350.89198226,
            0.0
        ],
        "SCLK01_MODULI_74999": [
            4294967296.0,
            65536.0
        ]
    },
    "detector_sample_summing": 1,
    "detector_line_summing": 1,
    "focal_length_model": {
        "focal_length": 352.9271664
    },
    "detector_center": {
        "line": 0.430442527,
        "sample": 2542.96099
    },
    "starting_detector_line": 0,
    "starting_detector_sample": 0,
    "focal2pixel_lines": [
        0.0,
        142.85714285714,
        0.0
    ],
    "focal2pixel_samples": [
        0.0,
        0.0,
        142.85714285714
    ],
    "optical_distortion": {
        "radial": {
            "coefficients": [
                -0.0073433925920054505,
                2.8375878636241697e-05,
                1.2841989124027099e-08
            ]
        }
    },
    "image_lines": 400,
    "image_samples": 5056,
    "name_platform": "Mars_Reconnaissance_Orbiter",
    "name_sensor": "CONTEXT CAMERA",
    "reference_height": {
        "maxheight": 1000,
        "minheight": -1000,
        "unit": "m"
    },
    "name_model": "USGS_ASTRO_LINE_SCANNER_SENSOR_MODEL",
    "interpolation_method": "lagrange",
    "line_scan_rate": [
        [
            0.5,
            -0.37540000677108765,
            0.001877
        ]
    ],
    "starting_ephemeris_time": 297088762.24158406,
    "center_ephemeris_time": 297088762.61698407,
    "radii": {
        "semimajor": 3396.19,
        "semiminor": 3376.2,
        "unit": "km"
    },
    "InstrumentPointing": {
        "TimeDependentFrames": [
            -74000,
            -74900,
            1
        ],
        "CkTableStartTime": 297088762.24158406,
        "CkTableEndTime": 297088762.9923841,
        "CkTableOriginalSize": 6,
        "EphemerisTimes": [
            297088762.24158406,
            297088762.3917441,
            297088762.5419041,
            297088762.69206405,
            297088762.84222406,
            297088762.9923841
        ],
        "Quaternions": [
            [
                0.42061124835443375,
                0.1860622266332136,
                -0.23980124331599867,
                0.8549633847610767
            ],
            [
                0.42062260840195603,
                0.186123564618581,
                -0.23976950628253949,
                0.8549533460089124
            ],
            [
                0.4206354696231763,
                0.18618437842074986,
                -0.23973759356053978,
                0.8549427262458993
            ],
            [
                0.42064763332258537,
                0.18624550650991442,
                -0.23970570189408144,
                0.8549323694681581
            ],
            [
                0.42065922954773666,
                0.1863066728084066,
                -0.23967381992970707,
                0.8549222750073063
            ],
            [
                0.4206714374933977,
                0.1863668684529047,
                -0.23964184692167076,
                0.8549121108185723
            ]
        ],
        "AngularVelocity": [
            [
                -0.0006409728984903079,
                0.0005054077299115119,
                0.0004718267948468069
            ],
            [
                -0.0006410700774431097,
                0.0005044862657976017,
                0.0004731836236807216
            ],
            [
                -0.0006408186407087456,
                0.0004992170698116158,
                0.0004802237192760833
            ],
            [
                -0.0006363961683672021,
                0.0004989647975959612,
                0.00047654664046286975
            ],
            [
                -0.0006376443791903504,
                0.0004996117504290811,
                0.00047678850931380653
            ],
            [
                -0.0006404093657132724,
                0.0005028749658176146,
                0.0004805228583087444
            ]
        ],
        "ConstantFrames": [
            -74021,
            -74020,
            -74699,
            -74690,
            -74000
        ],
        "ConstantRotation": [
            0.9999995608798441,
            -1.51960241928035e-05,
            0.0009370214510594064,
            1.5276552075356694e-05,
            0.9999999961910578,
            -8.593317911879532e-05,
            -0.000937020141647677,
            8.594745584079714e-05,
            0.9999995573030465
        ]
    },
    "BodyRotation": {
        "TimeDependentFrames": [
            10014,
            1
        ],
        "CkTableStartTime": 297088762.24158406,
        "CkTableEndTime": 297088762.9923841,
        "CkTableOriginalSize": 6,
        "EphemerisTimes": [
            297088762.24158406,
            297088762.3917441,
            297088762.5419041,
            297088762.69206405,
            297088762.84222406,
            297088762.9923841
        ],
        "Quaternions": [
            [
                -0.8371209459443085,
                0.2996928944391797,
                0.10720760458181891,
                0.4448811306448063
            ],
            [
                -0.8371185783490869,
                0.2996934649760026,
                0.1072060096645597,
                0.4448855856569007
            ],
            [
                -0.8371162107293473,
                0.2996940355045328,
                0.10720441474371896,
                0.44489004065791765
            ],
            [
                -0.8371138430875174,
                0.2996946060241849,
                0.1072028198209324,
                0.44489449564328926
            ],
            [
                -0.8371114754203602,
                0.2996951765357392,
                0.10720122489401934,
                0.44489895061910595
            ],
            [
                -0.8371091077303039,
                0.29969574703861046,
                0.10719962996461516,
                0.4449034055807993
            ]
        ],
        "AngularVelocity": [
            [
                3.16238646979841e-05,
                -2.880432898124293e-05,
                5.6520131658726165e-05
            ],
            [
                3.1623864697983686e-05,
                -2.880432898124763e-05,
                5.652013165872402e-05
            ],
            [
                3.162386469798325e-05,
                -2.880432898125237e-05,
                5.652013165872185e-05
            ],
            [
                3.162386469798283e-05,
                -2.880432898125708e-05,
                5.6520131658719694e-05
            ],
            [
                3.1623864697982405e-05,
                -2.8804328981261782e-05,
                5.6520131658717505e-05
            ],
            [
                3.162386469798195e-05,
                -2.88043289812665e-05,
                5.652013165871536e-05
            ]
        ]
    },
    "InstrumentPosition": {
        "SpkTableStartTime": 297088762.24158406,
        "SpkTableEndTime": 297088762.9923841,
        "SpkTableOriginalSize": 6,
        "EphemerisTimes": [
            297088762.24158406,
            297088762.3917441,
            297088762.5419041,
            297088762.69206405,
            297088762.84222406,
            297088762.9923841
        ],
        "Positions": [
            [
                -1885.298067561683,
                913.165223601331,
                -2961.966828003069
            ],
            [
                -1885.5928012826328,
                912.7436266030271,
                -2961.910568238333
            ],
            [
                -1885.8874970695995,
                912.3220111689507,
                -2961.8542488448256
            ],
            [
                -1886.1821547735613,
                911.9003774862961,
                -2961.797869848145
            ],
            [
                -1886.47677475216,
                911.4787252225885,
                -2961.7414312040696
            ],
            [
                -1886.7713566477582,
                911.0570545575632,
                -2961.684932934942
            ]
        ],
        "Velocities": [
            [
                -1.9629237646703683,
                -2.80759072221274,
                0.37446657801485306
            ],
            [
                -1.9626712192798401,
                -2.807713482051373,
                0.3748636774173111
            ],
            [
                -1.9624186346660286,
                -2.807836185534424,
                0.3752607691067297
            ],
            [
                -1.9621660109346446,
                -2.8079588326107823,
                0.37565785291714804
            ],
            [
                -1.9619133478903363,
                -2.8080814233753033,
                0.37605492915558875
            ],
            [
                -1.961660645638678,
                -2.8082039577768683,
                0.37645199765665144
            ]
        ]
    },
    "SunPosition": {
        "SpkTableStartTime": 297088762.61698407,
        "SpkTableEndTime": 297088762.61698407,
        "SpkTableOriginalSize": 1,
        "EphemerisTimes": [
            297088762.61698407
        ],
        "Positions": [
            [
                -208246643.08248276,
                -7677087.066457123,
                2103548.956999197
            ]
        ],
        "Velocities": [
            [
                -0.21020163267146563,
                -23.901883517440407,
                -10.957471339412034
            ]
        ]
    }
}
+11 −284

File changed.

Preview size limit exceeded, changes collapsed.