Commit b317fdb2 authored by jlaura's avatar jlaura Committed by Trent Hare
Browse files

Adds serialization (#10)

* Adds Cassini-ISS supports and begins refactor to generic.

* Finalizes refactor to generic framer

* Refactors out bad class attribute

* mend

* Adds conftest fixtures and model serialization.
parent 205636a5
Loading
Loading
Loading
Loading
+53 −4
Original line number Diff line number Diff line
@@ -733,10 +733,59 @@ std::string UsgsAstroFrameSensorModel::getModelState() const {
}


void UsgsAstroFrameSensorModel::replaceModelState(const std::string& argState) {
  throw csm::Error(csm::Error::UNSUPPORTED_FUNCTION,
                   "Unsupported function",
                   "UsgsAstroFrameSensorModel::replaceModelState");
void UsgsAstroFrameSensorModel::replaceModelState(const std::string& modelState) {
    auto state = json::parse(modelState);
    for(auto &key : _STATE_KEYWORD){
        if (state.find(key) == state.end()){
            csm::Error::ErrorType aErrorType = csm::Error::INVALID_SENSOR_MODEL_STATE;
            std::string aMessage = "State key %s mission", key;
            std::string aFunction = "UsgsAstroFramePlugin::replaceModelState()";
            throw csm::Error(aErrorType, aMessage, aFunction);
        }
        // TODO: This is pulled right out of the plugin - good reason to have the state be a
        // distinct class a la the generic line scan model.
        m_ccdCenter[0] = state["m_ccdCenter"][0];
        m_ccdCenter[1] = state["m_ccdCenter"][1];
        m_ephemerisTime = state["m_ephemerisTime"];
        m_focalLength = state["m_focalLength"];
        m_focalLengthEpsilon = state["m_focalLengthEpsilon"];
        m_ifov = state["m_ifov"];
        m_instrumentID = state["m_instrumentID"];

        m_majorAxis = state["m_majorAxis"];
        m_minorAxis = state["m_minorAxis"];
        m_startingDetectorLine = state["m_startingDetectorLine"];
        m_startingDetectorSample = state["m_startingDetectorSample"];
        m_line_pp = state["m_line_pp"];
        m_sample_pp = state["m_sample_pp"];
        m_originalHalfLines = state["m_originalHalfLines"];
        m_originalHalfSamples = state["m_originalHalfSamples"];
        m_spacecraftName = state["m_spacecraftName"];
        m_pixelPitch = state["m_pixelPitch"];
        m_nLines = state["m_nLines"];
        m_nSamples = state["m_nSamples"];
        m_minElevation = state["m_minElevation"];
        m_maxElevation = state["m_maxElevation"];

        for (int i=0;i<3;i++){
            m_boresight[i] = state["m_boresight"][i];
            m_iTransL[i] = state["m_iTransL"][i];
            m_iTransS[i] = state["m_iTransS"][i];

            m_transX[i] = state["m_transX"][i];
            m_transY[i] = state["m_transY"][i];
            m_spacecraftVelocity[i] = state["m_spacecraftVelocity"][i];
            m_sunPosition[i] = state["m_sunPosition"][i];
        }

        // Having types as vectors, instead of arrays makes interoperability with
        // the JSON library very easy.
        m_currentParameterValue = state["m_currentParameterValue"].get<std::vector<double>>();
        m_odtX = state["m_odtX"].get<std::vector<double>>();
        m_odtY = state["m_odtY"].get<std::vector<double>>();

        m_currentParameterCovariance = state["m_currentParameterCovariance"].get<std::vector<double>>();
    }
}


tests/conftest.py

0 → 100644
+50 −0
Original line number Diff line number Diff line
import json
import os

import pytest

from cycsm import isd
import cycsm.csm as csm
import usgscam as cam

data_path = os.path.dirname(__file__)


#TODO: This should be a single fixture that accepts the json file as an arg

@pytest.fixture(params=['CW1071364100B_IU_5.json', 'cassini_nac.json', 'EN1007907102M.json'])
def generic_model(request):
    csm_isd = isd.Isd()
    with open(os.path.join(data_path,request.param), 'r') as f:
        d = json.load(f)
    for k, v in d.items():
        csm_isd.addparam(k, v)

    plugin = cam.genericframe.Plugin()
    return plugin.from_isd(csm_isd, plugin.modelname(1))

@pytest.fixture
def mdis_wac_model():
    csm_isd = isd.Isd()
    with open(os.path.join(data_path,'CW1071364100B_IU_5.json'), 'r') as f:
        d = json.load(f)
    for k, v in d.items():
        csm_isd.addparam(k, v)

    plugin = cam.genericframe.Plugin()
    return plugin.from_isd(csm_isd, plugin.modelname(1))

@pytest.fixture
def ctx_model():
    path = os.path.join(data_path, 'J03_046060_1986_XN_18N282W_8bit_keywords.lis')
    csm_isd = isd.Isd.read_socet_file(path)
    plugin = cam.genericls.Plugin()
    return plugin.from_isd(csm_isd, plugin.modelname(1))

@pytest.fixture
def cassini_model():
    path = os.path.join(data_path,'cassini_nac.json')
    with open(path, 'r') as f:
        csm_isd = isd.Isd.load(f)
    plugin = cam.genericframe.Plugin()
    return plugin.from_isd(csm_isd, plugin.modelname(1))
+5 −22
Original line number Diff line number Diff line
@@ -9,14 +9,6 @@ import usgscam as cam
data_path = os.path.dirname(__file__)

class TestCassiniNAC:
    @pytest.fixture
    def cassini_model(self):
        path = os.path.join(data_path,'cassini_nac.json')
        with open(path, 'r') as f:
            csm_isd = isd.Isd.load(f)
        plugin = cam.genericframe.Plugin()
        return plugin.from_isd(csm_isd, plugin.modelname(1))

    @pytest.mark.parametrize('image, ground',[
                              ((512, 512, 0), (232745.39404384792, 108032.64063985727, 1416.4229696467519)),
                              ((1024, 1024, 0), (208934.79595478065, 147272.0343555567, 21924.160632191226)),
@@ -42,25 +34,16 @@ class TestCassiniNAC:
        assert iy == pytest.approx(y, rel=11)


class TestMdisWac:
    @pytest.fixture
    def model(self):
        csm_isd = isd.Isd()
        with open(os.path.join(data_path,'CW1071364100B_IU_5.json'), 'r') as f:
            d = json.load(f)
        for k, v in d.items():
            csm_isd.addparam(k, v)

        plugin = cam.genericframe.Plugin()
        return plugin.from_isd(csm_isd, plugin.modelname(1))

class TestMdisWac:
    @pytest.mark.parametrize('image, ground',[
                              ((512, 512, 0), (-73589.5516508502, 562548.342040933, 2372508.44060771)),
                              ((100, 100, 0), (-48020.2164819883, 539322.805489926, 2378549.41724731))
    ])
    def test_image_to_ground(self, model, image, ground):
    def test_image_to_ground(self, mdis_wac_model, image, ground):
        gx, gy, gz = ground
        x, y, z = model.imageToGround(*image)
        x, y, z = mdis_wac_model.imageToGround(*image)
        assert x == pytest.approx(gx, rel=1)
        assert y == pytest.approx(gy, rel=1)
        assert z == pytest.approx(gz, rel=1)
@@ -69,8 +52,8 @@ class TestMdisWac:
                              ((512, 512, 0), (-73589.5516508502, 562548.342040933, 2372508.44060771)),
                              ((100, 100, 0), (-48020.2164819883, 539322.805489926, 2378549.41724731))
    ])
    def test_ground_to_image(self, model, image, ground):
        y, x = model.groundToImage(*ground)
    def test_ground_to_image(self, mdis_wac_model, image, ground):
        y, x = mdis_wac_model.groundToImage(*ground)
        ix, iy, _ = image

        assert x == pytest.approx(ix)
+35 −0
Original line number Diff line number Diff line
import os
import json
import pytest

from cycsm import isd
import cycsm.csm as csm
import usgscam as cam

data_path = os.path.dirname(__file__)

class TestGenericLs:

    @pytest.mark.parametrize('image, ground',[
                              ((2500, 9216, 0), (-73589.5516508502, 562548.342040933, 2372508.44060771))
    ])
    def test_image_to_ground(self, ctx_model, image, ground):
        gx, gy, gz = ground
        x, y, z = ctx_model.imageToGround(*image)
        #TODO: Get this test up and running.
        #print(x, y, z)
        #assert False
        #assert x == pytest.approx(gx, rel=1)
        #assert y == pytest.approx(gy, rel=1)
        #assert z == pytest.approx(gz, rel=1)

    #@pytest.mark.parametrize('image, ground',[
    #                          ((512, 512, 0), (-73589.5516508502, 562548.342040933, 2372508.44060771)),
    #                          ((100, 100, 0), (-48020.2164819883, 539322.805489926, 2378549.41724731))
    #])
    #def test_ground_to_image(self, model, image, ground):
    #    y, x = model.groundToImage(*ground)
    #    ix, iy, _ = image
#
    #    assert x == pytest.approx(ix)
    #    assert y == pytest.approx(iy)
+12 −0
Original line number Diff line number Diff line
import pickle

import pytest

import usgscam as cam

# TODO: This should cycle through all of our JSON instantiation examples
def test_pickle_io(generic_model):
    pfile = pickle.dumps(generic_model, 2)
    loaded_model = pickle.loads(pfile)
    assert generic_model.name == loaded_model.name
    assert isinstance(loaded_model, cam.genericframe.SensorModel)
Loading