def test_path_input(tmp_path):
    file_path = tmp_path/"test.asdf"
    with asdf.AsdfFile() as af:
        af["meta"] = {"telescope": "magnifying glass"}
        af.write_to(file_path)

    # Test with PurePath input:
    with datamodels.open(file_path) as model:
        assert model.meta.telescope == "magnifying glass"
        af = model._asdf

    # When open creates the file pointer, it should be
    # closed when the model is closed:
    assert af._closed

    # Test with string input:
    with datamodels.open(str(file_path)) as model:
        assert model.meta.telescope == "magnifying glass"
        af = model._asdf

    assert af._closed

    # Appropriate error when file is missing:
    with pytest.raises(FileNotFoundError):
        with datamodels.open(tmp_path/"missing.asdf"):
            pass
Exemple #2
0
def test_core_schema(tmp_path):
    # Set temporary asdf file
    file_path = tmp_path / "test.asdf"

    # Define meta dictionary of required key / value pairs
    meta = {
        "meta": {
            "instrument": {
                "detector": "WFI01",
                "name": "WFI"
            },
            "telescope":
            "ROMAN",
            "model_type":
            "RomanDataModel",
            "date":
            Time('2021-02-20T02:20:00.123456789', format='isot', scale='utc')
        }
    }

    # Testing bad core asdf file
    with asdf.AsdfFile(meta) as af:
        # Test for invalid entry
        af["meta"]["telescope"] = "NOTROMAN"
        af.write_to(file_path)

        with datamodels.open(file_path) as model:
            with pytest.warns(ValidationWarning):
                model.validate()
            assert model["meta"]["telescope"] == "NOTROMAN"
def test_memmap(tmp_path):
    file_path = tmp_path/"test.asdf"
    with asdf.AsdfFile() as af:
        af["data"] = np.zeros((1024,))
        af["meta"] = {}
        af.write_to(file_path)

    with datamodels.open(file_path, memmap=True) as model:
        assert isinstance(model.data.base, np.memmap)

    with datamodels.open(file_path, memmap=False) as model:
        assert not isinstance(model.data.base, np.memmap)

    # Default should be false:
    with datamodels.open(file_path) as model:
        assert not isinstance(model.data.base, np.memmap)
Exemple #4
0
def test_core_schema(tmp_path):
    # Set temporary asdf file
    file_path = tmp_path / "test.asdf"

    # Define meta dictionary of required key / value pairs
    meta = {"meta":
                {"instrument": {
                     "detector": "WFI01",
                     "optical_element": "F158",
                     "name": "WFI"
                 },
                 "telescope": "ROMAN",
                 "model_type": "RomanDataModel",
                 "date": "2020-12-20"
                 }
            }

    # Testing bad core asdf file
    with asdf.AsdfFile(meta) as af:
        # Test for invalid entry
        af["meta"]["telescope"] = "NOTROMAN"
        af.write_to(file_path)

        with datamodels.open(file_path) as model:
            model.validate()
            with pytest.warns(ValidationWarning):
                model.validate()
            assert model["meta"]["telescope"] == "NOTROMAN"
def test_model_input(tmp_path):
    file_path = tmp_path/"test.asdf"
    data = np.random.uniform(size=(1024, 1024))
    with asdf.AsdfFile() as af:
        af["meta"] = {"telescope": "jeweler's loupe"}
        af["data"] = data
        af.write_to(file_path)

    original_model = datamodels.open(file_path)
    reopened_model = datamodels.open(original_model)

    # It's essential that we get a new instance so that the original
    # model can be closed without impacting the new model.
    assert reopened_model is not original_model

    assert_array_equal(original_model.data, data)
    original_model.close()
    assert_array_equal(reopened_model.data, data)
    reopened_model.close()
def test_asdf_file_input():
    with asdf.AsdfFile() as af:
        af["meta"] = {"telescope": "binoculars"}

        model = datamodels.open(af)
        assert model.meta.telescope == "binoculars"

        # When an already open file is passed in, the model
        # is not responsible for closing it.
        model.close()
        assert not af._closed
Exemple #7
0
def test_reference_file_model_base(tmp_path):
    # Set temporary asdf file
    file_path = tmp_path / "test.asdf"

    # Get reference dictionary base
    meta = REFERENCEFILE_SCHEMA_DICT

    # Testing referencefile asdf file
    with asdf.AsdfFile(meta) as af:
        af.write_to(file_path)

        # Ensure that base referencefile schema contains core schema
        assert_referenced_schema(datamodels.reference_files.referencefile.ReferenceFileModel.schema_url,
                                 datamodels.core.RomanDataModel.schema_url)

        # Test that asdf file opens properly
        assert datamodels.open(file_path)

        # Confirm that asdf file is opened as base referencefile model
        with datamodels.open(file_path) as model:
            assert isinstance(model, datamodels.reference_files.referencefile.ReferenceFileModel)
Exemple #8
0
def get_data_model_flat_dict(filepath):
    """Get the header from `filepath` using the roman data model.  Data model
    dotted object paths are reduced to capitalized dot-separated FITS-like
    keyword strings and a simple key,value dictionary format vs. nested objects.

    e.g.  meta.instrument.name  -->  "META.INSTRUMENT.NAME'

    Returns   { file_keyword : keyword_value, ... }
    """
    datamodels = get_datamodels()
    log.info("Checking Roman datamodels.")
    try:
        with datamodels.open(filepath) as d_model:
            flat_dict = d_model.to_flat_dict(include_arrays=False)
    except Exception as exc:
        raise exceptions.ValidationError("Roman Data Models:", str(exc).replace("u'","'")) from exc
    return flat_dict
Exemple #9
0
def test_flat_model(tmp_path):
    # Set temporary asdf file
    file_path = tmp_path / "test.asdf"

    # Get reference dictionary base
    meta = REFERENCEFILE_SCHEMA_DICT

    # Testing flat file asdf file
    with asdf.AsdfFile(meta) as af:
        # Add required flat file elements
        af["meta"]["reftype"] = "FLAT"
        af["meta"]["model_type"] = "FlatModel"
        af["data"] = np.zeros((4096, 4096))
        af["dq"] = np.zeros((4096, 4096))
        af["derr"] = np.zeros((4096, 4096))
        af.write_to(file_path)

        # Test that asdf file opens properly
        with datamodels.open(file_path) as model:
            with pytest.warns(None):
                model.validate()

            # Confirm that asdf file is opened as flat file model
            assert isinstance(model, datamodels.reference_files.flat.FlatModel)
Exemple #10
0
def test_invalid_input():
    with pytest.raises(TypeError):
        datamodels.open(fits.HDUList())