def test_augment_artifact_polarization_from_blueprint(): test_blueprint = ObsBlueprint(polarization_axis=1) test_blueprint.set('Chunk.polarizationAxis', '1') test_blueprint.set('Chunk.polarization.axis.axis.ctype', 'STOKES') test_blueprint.set('Chunk.polarization.axis.function.refCoord.pix', '1.0') test_blueprint.set('Chunk.polarization.axis.function.refCoord.val', '1.0') test_blueprint.set('Chunk.polarization.axis.function.delta', '1.0') test_blueprint.set('Chunk.polarization.axis.function.naxis', '1') test_fitsparser = FitsParser(sample_file_4axes, test_blueprint, uri='test_parser') test_chunk = Chunk() test_fitsparser._try_polarization_with_blueprint(test_chunk, 0) ex = _get_from_str_xml(EXPECTED_POLARIZATION_XML, ObservationReader()._get_polarization_wcs, 'polarization') result = get_differences(ex, test_chunk.polarization) assert result is None
def test_load_from_file_configure(): ob = ObsBlueprint() assert not ob._pos_axes_configed, \ 'Failure to initialize configure_position_axes' assert not ob._energy_axis_configed, \ 'Failure to initialize configure_energy_axis' ob.add_fits_attribute('Chunk.position.axis.axis1.ctype', 'CTYPE1') ob.add_fits_attribute('Chunk.position.axis.axis2.ctype', 'CTYPE2') ob.set('Chunk.energy.axis.axis.ctype', 'WAVE') ob._guess_axis_info_from_plan() assert ob._pos_axes_configed, 'Failure to call configure_position_axes' assert ob._energy_axis_configed, 'Failure to call configure_energy_axis' assert ob._wcs_std['Chunk.energy.axis.axis.ctype'] == 'CTYPE3', \ ob._wcs_std['Chunk.energy.axis.axis.ctype'] ob = ObsBlueprint() ob.add_fits_attribute('Chunk.position.axis.axis1.ctype', 'CTYPE3') ob.add_fits_attribute('Chunk.position.axis.axis2.ctype', 'CTYPE4') ob.set('Chunk.energy.axis.axis.ctype', 'WAVE') ob._guess_axis_info_from_plan() assert ob._pos_axes_configed, 'Failure to call configure_position_axes' assert ob._energy_axis_configed, 'Failure to call configure_energy_axis' assert ob._wcs_std['Chunk.energy.axis.axis.ctype'] == 'CTYPE1', \ ob._wcs_std['Chunk.energy.axis.axis.ctype'] ob = ObsBlueprint() ob.set('Chunk.energy.axis.axis.ctype', 'WAVE') ob._guess_axis_info_from_plan() assert ob._wcs_std['Chunk.energy.axis.axis.ctype'] == 'CTYPE3', \ ob._wcs_std['Chunk.energy.axis.axis.ctype']
def test_load_from_file_configure(): ob = ObsBlueprint() assert not ob._pos_axes_configed, \ 'Failure to initialize configure_position_axes' assert not ob._energy_axis_configed, \ 'Failure to initialize configure_energy_axis' ob.add_fits_attribute('Chunk.position.axis.axis1.ctype', 'CTYPE1') ob.add_fits_attribute('Chunk.position.axis.axis2.ctype', 'CTYPE2') ob.set('Chunk.energy.axis.axis.ctype', 'WAVE') ob._guess_axis_info_from_plan() assert ob._pos_axes_configed, 'Failure to call configure_position_axes' assert ob._energy_axis_configed, 'Failure to call configure_energy_axis' assert ob._wcs_std['Chunk.energy.axis.axis.ctype'] == 'CTYPE3', \ ob._wcs_std['Chunk.energy.axis.axis.ctype'] ob = ObsBlueprint() ob.add_fits_attribute('Chunk.position.axis.axis1.ctype', 'CTYPE3') ob.add_fits_attribute('Chunk.position.axis.axis2.ctype', 'CTYPE4') ob.set('Chunk.energy.axis.axis.ctype', 'WAVE') ob._guess_axis_info_from_plan() assert ob._pos_axes_configed, 'Failure to call configure_position_axes' assert ob._energy_axis_configed, 'Failure to call configure_energy_axis' assert ob._wcs_std['Chunk.energy.axis.axis.ctype'] == 'CTYPE1', \ ob._wcs_std['Chunk.energy.axis.axis.ctype'] ob = ObsBlueprint() ob.set('Chunk.energy.axis.axis.ctype', 'WAVE') ob._guess_axis_info_from_plan() assert ob._wcs_std['Chunk.energy.axis.axis.ctype'] == 'CTYPE3', \ ob._wcs_std['Chunk.energy.axis.axis.ctype']
def test_has_chunk(): # the CFHT case ob = ObsBlueprint() ob.configure_position_axes((1, 2)) ob.set('Chunk', '{ignore}') ob.set('Chunk.position.axis.axis1.ctype', 'RA---SIN', 1) assert not ob.has_chunk(0) assert ob.has_chunk(1) # the OMM case ob = ObsBlueprint() ob.configure_position_axes((1, 2)) ob.set('Chunk', '{ignore}', 1) assert ob.has_chunk(0) assert not ob.has_chunk(1)
def test_has_chunk(): # the CFHT case ob = ObsBlueprint() ob.configure_position_axes((1, 2)) ob.set('Chunk', '{ignore}') ob.set('Chunk.position.axis.axis1.ctype', 'RA---SIN', 1) assert not ob.has_chunk(0) assert ob.has_chunk(1) # the OMM case ob = ObsBlueprint() ob.configure_position_axes((1, 2)) ob.set('Chunk', '{ignore}', 1) assert ob.has_chunk(0) assert not ob.has_chunk(1)
def test_augment_observation(): test_obs_blueprint = ObsBlueprint(position_axes=(1, 2)) test_obs_blueprint.set('Observation.target.name', 'CGPS Mosaic MA1') test_obs_blueprint.set('Observation.target.standard', False) test_obs_blueprint.set('Observation.telescope.name', 'DRAO-ST') test_obs_blueprint.set('Observation.instrument.name', 'DRAO-ST') test_obs_blueprint.set('Observation.telescope.geoLocationX', '-2100330.87517') test_obs_blueprint.set('Observation.telescope.geoLocationY', '-3694247.82445') test_obs_blueprint.set('Observation.telescope.geoLocationZ', '4741018.33097') test_obs_blueprint.set('Plane.dataProductType', 'cube') test_obs_blueprint.set('Artifact.productType', 'info') test_obs_blueprint.set('Artifact.releaseType', 'data') test_obs_blueprint.set('Plane.calibrationLevel', '2') test_fitsparser = FitsParser(sample_file_4axes_obs, test_obs_blueprint) test_fitsparser.blueprint = test_obs_blueprint test_obs = SimpleObservation('collection', 'MA1_DRAO-ST', Algorithm('exposure')) test_fitsparser.augment_observation(test_obs, sample_file_4axes_uri, product_id='HI-line') assert test_obs is not None assert test_obs.planes is not None assert len(test_obs.planes) == 1 test_plane = test_obs.planes['HI-line'] assert test_plane.artifacts is not None assert len(test_plane.artifacts) == 1 test_artifact = test_plane.artifacts[sample_file_4axes_uri] assert test_artifact is not None test_part = test_artifact.parts['0'] # remove the chunk bit, as it's part of other tests - # results in <caom2:chunks/> xml output test_part.chunks.pop() output = BytesIO() ow = ObservationWriter(False, False, "caom2", obs_reader_writer.CAOM20_NAMESPACE) ow.write(test_obs, output) result = output.getvalue().decode('UTF-8') output.close() expected = _get_obs(EXPECTED_OBS_XML) actual = _get_obs(result) diff_result = get_differences(expected, actual, 'Observation') assert diff_result is None
def test_augment_artifact_time_from_blueprint(): test_blueprint = ObsBlueprint(time_axis=1) test_blueprint.set('Chunk.timeAxis', '1') test_blueprint.set('Chunk.time.exposure', '0.02') test_blueprint.set('Chunk.time.resolution', '0.02') test_blueprint.set('Chunk.time.timesys', 'UTC') test_blueprint.set('Chunk.time.axis.axis.ctype', 'TIME') test_blueprint.set('Chunk.time.axis.axis.cunit', 'd') test_blueprint.set('Chunk.time.axis.error.syser', '1e-07') test_blueprint.set('Chunk.time.axis.error.rnder', '1e-07') test_blueprint.set('Chunk.time.axis.function.refCoord.pix', '0.5') test_blueprint.set('Chunk.time.axis.function.refCoord.val', '56789.4298069') test_blueprint.set('Chunk.time.axis.function.delta', '2.31481e-07') test_blueprint.set('Chunk.time.axis.function.naxis', '1') test_fitsparser = FitsParser(sample_file_4axes, test_blueprint, uri='ad:TEST/test_blueprint') test_chunk = Chunk() test_fitsparser._try_time_with_blueprint(test_chunk, 0) ex = _get_from_str_xml(EXPECTED_CFHT_WIRCAM_RAW_GUIDE_CUBE_TIME, ObservationReader()._get_temporal_wcs, 'time') result = get_differences(ex, test_chunk.time) assert result is None
def test_augment_artifact_position_from_blueprint(): test_blueprint = ObsBlueprint(position_axes=(1, 2)) test_blueprint.set('Chunk.positionAxis1', '1') test_blueprint.set('Chunk.positionAxis2', '2') test_blueprint.set('Chunk.position.axis.axis1.ctype', 'GLON-CAR') test_blueprint.set('Chunk.position.axis.axis1.cunit', 'deg') test_blueprint.set('Chunk.position.axis.axis2.ctype', 'GLAT-CAR') test_blueprint.set('Chunk.position.axis.axis2.cunit', 'deg') test_blueprint.set('Chunk.position.axis.function.cd11', '-0.004999999') test_blueprint.set('Chunk.position.axis.function.cd12', '0.0') test_blueprint.set('Chunk.position.axis.function.cd21', '0.0') test_blueprint.set('Chunk.position.axis.function.cd22', '0.004999999') test_blueprint.set('Chunk.position.axis.function.dimension.naxis1', '1') test_blueprint.set('Chunk.position.axis.function.dimension.naxis2', '1') test_blueprint.set('Chunk.position.axis.range.start.coord1.pix', '513.0') test_blueprint.set('Chunk.position.axis.range.start.coord1.val', '128.7499990027') test_blueprint.set('Chunk.position.axis.range.start.coord2.pix', '513.0') test_blueprint.set('Chunk.position.axis.range.start.coord2.val', '-0.9999999922536') test_fitsparser = FitsParser(sample_file_4axes, test_blueprint, uri='test_parser') test_chunk = Chunk() test_fitsparser._try_position_with_blueprint(test_chunk, 0) ex = _get_from_str_xml(EXPECTED_POSITION_XML, ObservationReader()._get_spatial_wcs, 'position') result = get_differences(ex, test_chunk.position) assert result is None
def test_augment_artifact_energy_from_blueprint(): test_blueprint = ObsBlueprint(energy_axis=1) test_blueprint.set('Chunk.energyAxis', 1) test_blueprint.set('Chunk.energy.specsys', 'LSRK') test_blueprint.set('Chunk.energy.axis.axis.ctype', 'VRAD') test_blueprint.set('Chunk.energy.axis.axis.cunit', 'm / s') test_blueprint.set('Chunk.energy.axis.function.refCoord.pix', '145.0') test_blueprint.set('Chunk.energy.axis.function.refCoord.val', '-60000.0') test_blueprint.set('Chunk.energy.axis.function.delta', '-824.46002') test_blueprint.set('Chunk.energy.axis.function.naxis', '1') test_fitsparser = FitsParser(sample_file_4axes, test_blueprint, uri='ad:TEST/test_blueprint') test_chunk = Chunk() test_fitsparser._try_energy_with_blueprint(test_chunk, 0) ex = _get_from_str_xml(EXPECTED_ENERGY_XML, ObservationReader()._get_spectral_wcs, 'energy') result = get_differences(ex, test_chunk.energy) assert result is None
def test_augment_artifact_bounds_range_from_blueprint(): test_blueprint = ObsBlueprint(energy_axis=1, time_axis=2, polarization_axis=3, position_axes=(4, 5)) test_blueprint.set('Chunk.energy.axis.range.start.pix', '145.0') test_blueprint.set('Chunk.energy.axis.range.start.val', '-60000.0') test_blueprint.set('Chunk.energy.axis.range.end.pix', '-824.46002') test_blueprint.set('Chunk.energy.axis.range.end.val', '1') test_blueprint.set('Chunk.time.axis.range.start.pix', '145.0') test_blueprint.set('Chunk.time.axis.range.start.val', '-60000.0') test_blueprint.set('Chunk.time.axis.range.end.pix', '-824.46002') test_blueprint.set('Chunk.time.axis.range.end.val', '1') test_blueprint.set('Chunk.polarization.axis.range.start.pix', '145.0') test_blueprint.set('Chunk.polarization.axis.range.start.val', '-60000.0') test_blueprint.set('Chunk.polarization.axis.range.end.pix', '-824.46002') test_blueprint.set('Chunk.polarization.axis.range.end.val', '1') test_blueprint.set('Chunk.position.axis.range.start.coord1.pix', '145.0') test_blueprint.set('Chunk.position.axis.range.start.coord1.val', '-60000.0') test_blueprint.set('Chunk.position.axis.range.end.coord1.pix', '-824.46002') test_blueprint.set('Chunk.position.axis.range.end.coord1.val', '1') test_blueprint.set('Chunk.position.axis.range.start.coord2.pix', '145.0') test_blueprint.set('Chunk.position.axis.range.start.coord2.val', '-60000.0') test_blueprint.set('Chunk.position.axis.range.end.coord2.pix', '-824.46002') test_blueprint.set('Chunk.position.axis.range.end.coord2.val', '1') test_fitsparser = FitsParser(sample_file_4axes, test_blueprint, uri='ad:TEST/test_blueprint') test_chunk = Chunk() test_chunk.energy = SpectralWCS(CoordAxis1D(Axis('WAVE', 'm')), 'TOPOCENT') test_chunk.time = TemporalWCS(CoordAxis1D(Axis('TIME', 'd'))) test_chunk.polarization = PolarizationWCS(CoordAxis1D(Axis('STOKES'))) test_chunk.position = SpatialWCS( CoordAxis2D(Axis('RA', 'deg'), Axis('DEC', 'deg'))) test_fitsparser._try_range_with_blueprint(test_chunk, 0) assert test_chunk.energy.axis.range is not None, \ 'chunk.energy.axis.range should be declared' assert test_chunk.time.axis.range is not None, \ 'chunk.time.axis.range should be declared' assert test_chunk.polarization.axis.range is not None, \ 'chunk.polarization.axis.range should be declared' assert test_chunk.position.axis.range is not None, \ 'chunk.position.axis.range should be declared' ex = _get_from_str_xml(EXPECTED_ENERGY_RANGE_BOUNDS_XML, ObservationReader()._get_spectral_wcs, 'energy') assert ex is not None, \ 'energy string from expected output should be declared' result = get_differences(ex, test_chunk.energy) assert result is None ex = _get_from_str_xml(EXPECTED_TIME_RANGE_BOUNDS_XML, ObservationReader()._get_temporal_wcs, 'time') assert ex is not None, \ 'time string from expected output should be declared' result = get_differences(ex, test_chunk.time) assert result is None ex = _get_from_str_xml(EXPECTED_POL_RANGE_BOUNDS_XML, ObservationReader()._get_polarization_wcs, 'polarization') assert ex is not None, \ 'polarization string from expected output should be declared' result = get_differences(ex, test_chunk.polarization) assert result is None ex = _get_from_str_xml(EXPECTED_POS_RANGE_BOUNDS_XML, ObservationReader()._get_spatial_wcs, 'position') assert ex is not None, \ 'position string from expected output should be declared' result = get_differences(ex, test_chunk.position) assert result is None
def test_obs_blueprint(): # test the CAOM2_ELEMENTS property assert ObsBlueprint.CAOM2_ELEMENTS == ObsBlueprint._CAOM2_ELEMENTS # updating the CAOM2_ELEMENTS property should not update the original elems = ObsBlueprint.CAOM2_ELEMENTS elems = elems[2:] assert elems != ObsBlueprint.CAOM2_ELEMENTS # default config (one entry per row...) assert str(ObsBlueprint()).count('\n') == 24 print(ObsBlueprint()) # default config with WCS info assert str( ObsBlueprint(position_axes=(1, 2), energy_axis=3, polarization_axis=4, time_axis=5, obs_axis=6, custom_axis=7)).count('\n') == 90 ob = ObsBlueprint() ob.configure_position_axes(axes=(1, 2)) ob.configure_energy_axis(axis=3) ob.configure_polarization_axis(axis=4) ob.configure_time_axis(axis=5) ob.configure_observable_axis(axis=6) ob.configure_custom_axis(axis=7) # test that configuring something that's already configured doesn't break # anything ob.configure_position_axes(axes=(1, 2)) ob.configure_energy_axis(axis=3) ob.configure_polarization_axis(axis=4) ob.configure_time_axis(axis=5) ob.configure_observable_axis(axis=6) ob.configure_custom_axis(axis=7) # set attribute ob.set('Observation.instrument.name', 'NIRI') assert ob._plan['Observation.instrument.name'] == 'NIRI' assert 'Observation.instrument.name = NIRI' in str(ob) # set default ob.clear('Observation.instrument.keywords') ob.add_fits_attribute('Observation.instrument.keywords', 'INSTMODE') assert "Observation.instrument.keywords = ['INSTMODE'], default = None" \ in str(ob) ob.set_default('Observation.instrument.keywords', 'TEST') assert ob._plan['Observation.instrument.keywords'][1] == 'TEST' assert ob._plan['Observation.instrument.keywords'][0] == ['INSTMODE'] assert "Observation.instrument.keywords = ['INSTMODE'], default = TEST" \ in str(ob) # set fits attribute ob.add_fits_attribute('Observation.proposal.id', 'PROP') ob.add_fits_attribute('Observation.proposal.id', 'PROP2') ob.set_default('Observation.proposal.id', 'NOPROP') assert ob._plan['Observation.proposal.id'][0] == ['PROP2', 'PROP', 'RUNID'] assert ob._plan['Observation.proposal.id'][1] == 'NOPROP' if sys.version.startswith('2.7.'): assert ("Observation.proposal.id = ['PROP2', 'PROP', u'RUNID'], " "default = NOPROP") in str(ob) else: assert ("Observation.proposal.id = ['PROP2', 'PROP', 'RUNID'], " "default = NOPROP") in str(ob) # set in extension ob.set('Chunk.energy.velang', 33, extension=1) extension1_str = str(ob)[str(ob).index('extension 1'):] assert 'Chunk.energy.velang = 33' in extension1_str # set fits attribute in extension ob.add_fits_attribute('Chunk.energy.axis.axis.ctype', 'MYCTYPE', extension=1) ob.add_fits_attribute('Chunk.energy.axis.axis.ctype', 'MYCTYPE2', extension=1) ob.set_default('Chunk.energy.axis.axis.ctype', 'NOCTYPE', extension=1) extension1_str = str(ob)[str(ob).index('extension 1'):] assert ("Chunk.energy.axis.axis.ctype = ['MYCTYPE2', 'MYCTYPE'], " "default = NOCTYPE") in extension1_str # set in a different extension ob.set('Chunk.energy.velang', 44, extension=2) extension2_str = str(ob)[str(ob).index('extension 2'):] assert 'Chunk.energy.velang = 44' in extension2_str # test get assert ob._get('Observation.instrument.name') == 'NIRI' assert ob._get('Observation.instrument.keywords')[0] == ['INSTMODE'] assert ob._get('Observation.instrument.keywords')[1] == 'TEST' assert ob._get('Chunk.energy.velang', extension=2) == 44 assert ob._get('Chunk.energy.velang', extension=1) == 33 assert ob._get('Chunk.energy.axis.axis.ctype', extension=1)[0] ==\ ['MYCTYPE2', 'MYCTYPE'] assert ob._get('Chunk.energy.axis.axis.ctype', extension=1)[1] == 'NOCTYPE' # test get when keyword not present in extension and the default is used assert ob._get('Chunk.energy.specsys', extension=33)[0] == ['SPECSYS'] assert ob._get('Chunk.energy.specsys', extension=44)[1] is None # get element not set assert 'Observation.target.redshift' not in ob._plan assert ob._get('Observation.target.redshift') is None # bintable :) ob.add_table_attribute('CompositeObservation.members', 'FICS', extension=0) result = ob._get('CompositeObservation.members', extension=0) assert result is not None assert len(result) == 3, len(result) # delete attributes assert len(ob._plan) != 0 assert len(ob._extensions) != 0 for i in ObsBlueprint.CAOM2_ELEMENTS: ob.delete(i) assert len(ob._plan) == 0 # clear ob.clear('Chunk.energy.axis.axis.ctype', extension=1) # delete attributes from extensions ob.delete('Chunk.energy.velang', extension=1) ob.delete('Chunk.energy.axis.axis.ctype', extension=1) ob.delete('CompositeObservation.members', extension=0) ob.delete('Chunk.energy.velang', extension=2) assert len(ob._extensions) == 0 # set defaults in extension ob.set_default('Chunk.energy.axis.axis.ctype', 'NOCTYPE', extension=3) extension3_str = str(ob)[str(ob).index('extension 3'):] assert "Chunk.energy.axis.axis.ctype = NOCTYPE" in extension3_str assert len(ob._extensions) == 1 # testing error cases ob = ObsBlueprint() ob.configure_position_axes(axes=(1, 2)) ob.configure_energy_axis(axis=3) ob.configure_polarization_axis(axis=4) ob.configure_time_axis(axis=5) # non CAOM2 element name with pytest.raises(KeyError): ob.set('Nonexistent', 33) with pytest.raises(KeyError): ob.add_fits_attribute('Nonexistent', 33) with pytest.raises(KeyError): ob.set_default('Nonexistent', 33) with pytest.raises(KeyError): ob._get('Nonexistent') with pytest.raises(KeyError): ob.delete('Nonexistent') with pytest.raises(KeyError): ob.clear('Nonexistent') # repeat with extension specified with pytest.raises(KeyError): ob.set('Chunk.Nonexistent', 33, extension=1) with pytest.raises(KeyError): ob.add_fits_attribute('Chunk.Nonexistent', 33, extension=1) with pytest.raises(KeyError): ob.set_default('Chunk.Nonexistent', 33, extension=1) with pytest.raises(KeyError): ob._get('Nonexistent', extension=33) with pytest.raises(KeyError): ob.delete('Chunk.Nonexistent', extension=1) with pytest.raises(KeyError): ob.clear('Chunk.Nonexistent', extension=1) # CAOM2 element not Chunk with extension specified with pytest.raises(ValueError): ob.set('Observation.observationID', 33, extension=1) with pytest.raises(ValueError): ob.set_default('Observation.observationID', 33, extension=1) with pytest.raises(ValueError): ob.add_fits_attribute('Observation.observationID', 'AA', extension=1) with pytest.raises(ValueError): ob.delete('Observation.observationID', extension=1) with pytest.raises(ValueError): ob.clear('Observation.observationID', extension=1) # CAOM2 element Chunk with extension with pytest.raises(ValueError): ob.clear('Chunk.energy.axis.axis.cunit', extension=33) # add FITS attribute to element that does not exist assert 'Chunk.energy.transition' not in ob._plan ob.set('Chunk.energy.transition', 'Name') with pytest.raises(AttributeError): ob.add_fits_attribute('Chunk.energy.transition', 'BP') # call set_fits_attribute with argument other than list with pytest.raises(AttributeError): ob.add_fits_attribute('Chunk.energy.transition', 33) # delete element from a non-existent extension with pytest.raises(ValueError): ob.delete('Chunk.energy.transition', extension=66) # adding the same thing twice does nothing - the test values are defaults result = ob._get('Observation.metaRelease') initial_result_length = (len(result[0])) ob.add_fits_attribute('Observation.metaRelease', 'DATE-OBS') result = ob._get('Observation.metaRelease') add_result_length = (len(result[0])) assert initial_result_length == add_result_length # in an extension result = ob._get('Chunk.energy.specsys', extension=1) initial_result_length = (len(result[0])) ob.add_fits_attribute('Chunk.energy.specsys', 'SPECSYS') result = ob._get('Chunk.energy.specsys', extension=1) add_result_length = (len(result[0])) assert initial_result_length == add_result_length, result
def test_load_from_file_configure(): ob = ObsBlueprint() assert not ob._pos_axes_configed, \ 'Failure to initialize configure_position_axes' assert not ob._energy_axis_configed, \ 'Failure to initialize configure_energy_axis' assert not ob._custom_axis_configed, 'custom config' assert not ob._obs_axis_configed, 'obs config' assert not ob._polarization_axis_configed, 'pol config' assert not ob._time_axis_configed, 'time config' ob.add_fits_attribute('Chunk.position.axis.axis1.ctype', 'CTYPE1') ob.add_fits_attribute('Chunk.position.axis.axis2.ctype', 'CTYPE2') ob.set('Chunk.energy.axis.axis.ctype', 'WAVE') ob._guess_axis_info_from_plan() assert ob._pos_axes_configed, 'Failure to call configure_position_axes' assert ob._energy_axis_configed, 'Failure to call configure_energy_axis' assert ob._wcs_std['Chunk.energy.axis.axis.ctype'] == 'CTYPE3', \ ob._wcs_std['Chunk.energy.axis.axis.ctype'] ob = ObsBlueprint() ob.add_fits_attribute('Chunk.position.axis.axis1.ctype', 'CTYPE3') ob.add_fits_attribute('Chunk.position.axis.axis2.ctype', 'CTYPE4') ob.set('Chunk.energy.axis.axis.ctype', 'WAVE') ob._guess_axis_info_from_plan() assert ob._pos_axes_configed, 'Failure to call configure_position_axes' assert ob._energy_axis_configed, 'Failure to call configure_energy_axis' assert ob._wcs_std['Chunk.energy.axis.axis.ctype'] == 'CTYPE1', \ ob._wcs_std['Chunk.energy.axis.axis.ctype'] ob = ObsBlueprint() ob.set('Chunk.energy.axis.axis.ctype', 'WAVE') ob._guess_axis_info_from_plan() assert ob._wcs_std['Chunk.energy.axis.axis.ctype'] == 'CTYPE3', \ ob._wcs_std['Chunk.energy.axis.axis.ctype'] ob = ObsBlueprint() ob.set('Chunk.polarization.axis.axis.ctype', 'STOKES') ob._guess_axis_info_from_plan() assert ob._wcs_std['Chunk.polarization.axis.axis.ctype'] == 'CTYPE5', \ ob._wcs_std['Chunk.polarization.axis.axis.ctype'] assert ob._polarization_axis_configed, 'pol config' ob = ObsBlueprint() ob.set('Chunk.observable.axis.axis.ctype', 'COUNT') ob._guess_axis_info_from_plan() assert ob._wcs_std['Chunk.observable.axis.axis.ctype'] == 'CTYPE6', \ ob._wcs_std['Chunk.observable.axis.axis.ctype'] assert ob._obs_axis_configed, 'obs config' ob = ObsBlueprint() ob.set('Chunk.custom.axis.axis.ctype', 'FARDEP') ob._guess_axis_info_from_plan() assert ob._wcs_std['Chunk.custom.axis.axis.ctype'] == 'CTYPE7', \ ob._wcs_std['Chunk.custom.axis.axis.ctype'] assert ob._custom_axis_configed, 'custom config' ob = ObsBlueprint() ob.set('Chunk.time.axis.axis.ctype', 'TIME') ob._guess_axis_info_from_plan() assert ob._wcs_std['Chunk.time.axis.axis.ctype'] == 'CTYPE4', \ ob._wcs_std['Chunk.time.axis.axis.ctype'] assert ob._time_axis_configed, 'time config' # should get the position axes by default ob = ObsBlueprint() ob._guess_axis_info_from_plan() assert ob._pos_axes_configed, 'pos config' assert not ob._energy_axis_configed, 'energy config' assert not ob._custom_axis_configed, 'custom config' assert not ob._obs_axis_configed, 'obs config' assert not ob._polarization_axis_configed, 'pol config' assert not ob._time_axis_configed, 'time config' # for the truly creative instrument scientist ob = ObsBlueprint() ob.add_fits_attribute('Chunk.polarization.axis.axis.ctype', 'CTYPE1') ob.add_fits_attribute('Chunk.custom.axis.axis.ctype', 'CTYPE2') ob.add_fits_attribute('Chunk.position.axis.axis1.ctype', 'CTYPE3') ob.add_fits_attribute('Chunk.position.axis.axis2.ctype', 'CTYPE4') ob.add_fits_attribute('Chunk.time.axis.axis.ctype', 'CTYPE5') ob.add_fits_attribute('Chunk.energy.axis.axis.ctype', 'CTYPE6') ob.add_fits_attribute('Chunk.observable.axis.axis.ctype', 'CTYPE7') ob._guess_axis_info_from_plan() assert ob._wcs_std['Chunk.polarization.axis.axis.ctype'] == 'CTYPE1', \ ob._wcs_std['Chunk.polarization.axis.axis.ctype'] assert ob._wcs_std['Chunk.custom.axis.axis.ctype'] == 'CTYPE2', \ ob._wcs_std['Chunk.custom.axis.axis.ctype'] assert ob._wcs_std['Chunk.position.axis.axis1.ctype'] == 'CTYPE3', \ ob._wcs_std['Chunk.position.axis.axis1.ctype'] assert ob._wcs_std['Chunk.position.axis.axis2.ctype'] == 'CTYPE4', \ ob._wcs_std['Chunk.position.axis.axis2.ctype'] assert ob._wcs_std['Chunk.time.axis.axis.ctype'] == 'CTYPE5', \ ob._wcs_std['Chunk.time.axis.axis.ctype'] assert ob._wcs_std['Chunk.energy.axis.axis.ctype'] == 'CTYPE6', \ ob._wcs_std['Chunk.energy.axis.axis.ctype'] assert ob._wcs_std['Chunk.observable.axis.axis.ctype'] == 'CTYPE7', \ ob._wcs_std['Chunk.observable.axis.axis.ctype'] with pytest.raises(ValueError): ob = ObsBlueprint() ob.add_fits_attribute('Chunk.polarization.axis.axis.ctype', 'CTYPE1') ob._guess_axis_info_from_plan()
def test_obs_blueprint(): # test the CAOM2_ELEMENTS property assert ObsBlueprint.CAOM2_ELEMENTS == ObsBlueprint._CAOM2_ELEMENTS # updating the CAOM2_ELEMENTS property should not update the original elems = ObsBlueprint.CAOM2_ELEMENTS elems = elems[2:] assert elems != ObsBlueprint.CAOM2_ELEMENTS # default config (one entry per row...) assert str(ObsBlueprint()).count('\n') == 20 print(ObsBlueprint()) # default config with WCS info assert str( ObsBlueprint(position_axis=(1, 2), energy_axis=3, polarization_axis=4, time_axis=5)).count('\n') == 77 ob = ObsBlueprint() ob.configure_position_axes(axes=(1, 2)) ob.configure_energy_axis(axis=3) ob.configure_polarization_axis(axis=4) ob.configure_time_axis(axis=5) # set attribute ob.set('Observation.instrument.name', 'NIRI') assert ob._plan['Observation.instrument.name'] == 'NIRI' assert 'Observation.instrument.name = NIRI' in str(ob) # set default ob.set_fits_attribute('Observation.instrument.keywords', [str('INSTMODE')]) assert "Observation.instrument.keywords = ['INSTMODE'], default = None" \ in str(ob) ob.set_default('Observation.instrument.keywords', 'TEST') assert ob._plan['Observation.instrument.keywords'][1] == 'TEST' assert ob._plan['Observation.instrument.keywords'][0] == ['INSTMODE'] assert "Observation.instrument.keywords = ['INSTMODE'], default = TEST" \ in str(ob) # set fits attribute ob.set_fits_attribute('Observation.proposal.id', [str('PROP')]) ob.add_fits_attribute('Observation.proposal.id', str('PROP2')) ob.set_default('Observation.proposal.id', 'NOPROP') assert ob._plan['Observation.proposal.id'][0] == ['PROP2', 'PROP'] assert ob._plan['Observation.proposal.id'][1] == 'NOPROP' assert ("Observation.proposal.id = ['PROP2', 'PROP'], " "default = NOPROP") in str(ob) # set in extension ob.set('Chunk.energy.velang', 33, extension=1) extension1_str = str(ob)[str(ob).index('extension 1'):] assert 'Chunk.energy.velang = 33' in extension1_str # set fits attribute in extension ob.set_fits_attribute('Chunk.energy.axis.axis.ctype', [str('MYCTYPE')], extension=1) ob.add_fits_attribute('Chunk.energy.axis.axis.ctype', str('MYCTYPE2'), extension=1) ob.set_default('Chunk.energy.axis.axis.ctype', 'NOCTYPE', extension=1) extension1_str = str(ob)[str(ob).index('extension 1'):] assert ("Chunk.energy.axis.axis.ctype = ['MYCTYPE2', 'MYCTYPE'], " "default = NOCTYPE") in extension1_str # set in a different extension ob.set('Chunk.energy.velang', 44, extension=2) extension2_str = str(ob)[str(ob).index('extension 2'):] assert 'Chunk.energy.velang = 44' in extension2_str # test get assert ob._get('Observation.instrument.name') == 'NIRI' assert ob._get('Observation.instrument.keywords')[0] == ['INSTMODE'] assert ob._get('Observation.instrument.keywords')[1] == 'TEST' assert ob._get('Chunk.energy.velang', extension=2) == 44 assert ob._get('Chunk.energy.velang', extension=1) == 33 assert ob._get('Chunk.energy.axis.axis.ctype', extension=1)[0] ==\ ['MYCTYPE2', 'MYCTYPE'] assert ob._get('Chunk.energy.axis.axis.ctype', extension=1)[1] == 'NOCTYPE' # test get when keyword not present in extension and the default is used assert ob._get('Chunk.energy.specsys', extension=33)[0] == ['SPECSYS'] assert ob._get('Chunk.energy.specsys', extension=44)[1] is None # get element not set assert 'Observation.target.redshift' not in ob._plan assert ob._get('Observation.target.redshift') is None # delete attributes assert len(ob._plan) != 0 assert len(ob._extensions) != 0 for i in ObsBlueprint.CAOM2_ELEMENTS: ob.delete(i) assert len(ob._plan) == 0 # delete attributes from extensions ob.delete('Chunk.energy.velang', extension=1) ob.delete('Chunk.energy.axis.axis.ctype', extension=1) ob.delete('Chunk.energy.velang', extension=2) assert len(ob._extensions) == 0 # testing error cases ob = ObsBlueprint() ob.configure_position_axes(axes=(1, 2)) ob.configure_energy_axis(axis=3) ob.configure_polarization_axis(axis=4) ob.configure_time_axis(axis=5) # non CAOM2 element name with pytest.raises(KeyError): ob.set('Nonexistent', 33) with pytest.raises(KeyError): ob.set_fits_attribute('Nonexistent', 33) with pytest.raises(KeyError): ob.add_fits_attribute('Nonexistent', 33) with pytest.raises(KeyError): ob.set_default('Nonexistent', 33) with pytest.raises(KeyError): ob._get('Nonexistent') with pytest.raises(KeyError): ob.delete('Nonexistent') # repeat with extension specified with pytest.raises(KeyError): ob.set('Chunk.Nonexistent', 33, extension=1) with pytest.raises(KeyError): ob.set_fits_attribute('Chunk.Nonexistent', 33, extension=1) with pytest.raises(KeyError): ob.add_fits_attribute('Chunk.Nonexistent', 33, extension=1) with pytest.raises(KeyError): ob.set_default('Chunk.Nonexistent', 33, extension=1) with pytest.raises(KeyError): ob._get('Nonexistent', extension=33) with pytest.raises(KeyError): ob.delete('Chunk.Nonexistent', extension=1) # CAOM2 element not Chunk with extension specified with pytest.raises(ValueError): ob.set('Observation.observationID', 33, extension=1) with pytest.raises(ValueError): ob.set_fits_attribute('Observation.observationID', ['AA'], extension=1) with pytest.raises(ValueError): ob.set_default('Observation.observationID', 33, extension=1) with pytest.raises(ValueError): ob.add_fits_attribute('Observation.observationID', 'AA', extension=1) with pytest.raises(ValueError): ob.delete('Observation.observationID', extension=1) # add FITS attribute to element that does not exist assert 'Chunk.energy.transition' not in ob._plan with pytest.raises(KeyError): ob.add_fits_attribute('Chunk.energy.transition', 'BP') ob.set('Chunk.energy.transition', 'Name') with pytest.raises(AttributeError): ob.add_fits_attribute('Chunk.energy.transition', 'BP') # call set_fits_attribute with argument other than list with pytest.raises(AssertionError): ob.set_fits_attribute('Chunk.energy.transition', 33) # delete element from a non-existent extension with pytest.raises(ValueError): ob.delete('Chunk.energy.transition', extension=66)
def test_augment_observation(test_file, test_file_uri): # logging.basicConfig(level=logging.DEBUG, stream=sys.stdout) test_obs_blueprint = ObsBlueprint(position_axis=(1, 2)) test_obs_blueprint.set('Observation.target.name', 'CGPS Mosaic MA1') test_obs_blueprint.set('Observation.telescope.name', 'DRAO-ST') test_obs_blueprint.set('Observation.instrument.name', 'DRAO-ST') test_obs_blueprint.set('Observation.telescope.geoLocationX', '-2100330.87517') test_obs_blueprint.set('Observation.telescope.geoLocationY', '-3694247.82445') test_obs_blueprint.set('Observation.telescope.geoLocationZ', '4741018.33097') test_obs_blueprint.set('Plane.dataProductType', 'cube') test_obs_blueprint.set('Plane.calibrationLevel', '2') test_fitsparser = FitsParser(test_file, test_obs_blueprint) test_fitsparser.blueprint = test_obs_blueprint test_obs = Observation('collection', 'MA1_DRAO-ST', Algorithm('exposure')) test_fitsparser.augment_observation(test_obs, test_file_uri, product_id='HI-line') assert test_obs is not None assert test_obs.planes is not None assert len(test_obs.planes) == 1 test_plane = test_obs.planes['HI-line'] assert test_plane.artifacts is not None assert len(test_plane.artifacts) == 1 test_artifact = test_plane.artifacts[test_file_uri] assert test_artifact is not None test_part = test_artifact.parts['0'] # remove the chunk bit, as it's part of other tests - # results in <caom2:chunks/> xml output test_part.chunks.pop() output = BytesIO() ow = ObservationWriter(False, False, "caom2", obs_reader_writer.CAOM20_NAMESPACE) ow.write(test_obs, output) result = output.getvalue().decode('UTF-8') output.close() compare = re.sub(r'caom2:id=".*"', 'caom2:id=""', result) assert compare == EXPECTED_OBS_XML # , result
def test_obs_blueprint(): # test the CAOM2_ELEMENTS property assert ObsBlueprint.CAOM2_ELEMENTS == ObsBlueprint._CAOM2_ELEMENTS # updating the CAOM2_ELEMENTS property should not update the original elems = ObsBlueprint.CAOM2_ELEMENTS elems = elems[2:] assert elems != ObsBlueprint.CAOM2_ELEMENTS # default config (one entry per row...) assert str(ObsBlueprint()).count('\n') == 21 print(ObsBlueprint()) # default config with WCS info assert str(ObsBlueprint(position_axes=(1, 2), energy_axis=3, polarization_axis=4, time_axis=5, obs_axis=6)).count('\n') == 81 ob = ObsBlueprint() ob.configure_position_axes(axes=(1, 2)) ob.configure_energy_axis(axis=3) ob.configure_polarization_axis(axis=4) ob.configure_time_axis(axis=5) ob.configure_observable_axis(axis=6) # test that configuring something that's already configured doesn't break # anything ob.configure_position_axes(axes=(1, 2)) ob.configure_energy_axis(axis=3) ob.configure_polarization_axis(axis=4) ob.configure_time_axis(axis=5) ob.configure_observable_axis(axis=6) # set attribute ob.set('Observation.instrument.name', 'NIRI') assert ob._plan['Observation.instrument.name'] == 'NIRI' assert 'Observation.instrument.name = NIRI' in str(ob) # set default ob.clear('Observation.instrument.keywords') ob.add_fits_attribute('Observation.instrument.keywords', str('INSTMODE')) assert "Observation.instrument.keywords = ['INSTMODE'], default = None" \ in str(ob) ob.set_default('Observation.instrument.keywords', 'TEST') assert ob._plan['Observation.instrument.keywords'][1] == 'TEST' assert ob._plan['Observation.instrument.keywords'][0] == ['INSTMODE'] assert "Observation.instrument.keywords = ['INSTMODE'], default = TEST" \ in str(ob) # set fits attribute ob.add_fits_attribute('Observation.proposal.id', str('PROP')) ob.add_fits_attribute('Observation.proposal.id', str('PROP2')) ob.set_default('Observation.proposal.id', 'NOPROP') assert ob._plan['Observation.proposal.id'][0] == ['PROP2', 'PROP', 'RUNID'] assert ob._plan['Observation.proposal.id'][1] == 'NOPROP' if sys.version.startswith('2.7.'): assert ("Observation.proposal.id = ['PROP2', 'PROP', u'RUNID'], " "default = NOPROP") in str(ob) else: assert ("Observation.proposal.id = ['PROP2', 'PROP', 'RUNID'], " "default = NOPROP") in str(ob) # set in extension ob.set('Chunk.energy.velang', 33, extension=1) extension1_str = str(ob)[str(ob).index('extension 1'):] assert 'Chunk.energy.velang = 33' in extension1_str # set fits attribute in extension ob.add_fits_attribute('Chunk.energy.axis.axis.ctype', str('MYCTYPE'), extension=1) ob.add_fits_attribute('Chunk.energy.axis.axis.ctype', str('MYCTYPE2'), extension=1) ob.set_default('Chunk.energy.axis.axis.ctype', 'NOCTYPE', extension=1) extension1_str = str(ob)[str(ob).index('extension 1'):] assert ("Chunk.energy.axis.axis.ctype = ['MYCTYPE2', 'MYCTYPE'], " "default = NOCTYPE") in extension1_str # set in a different extension ob.set('Chunk.energy.velang', 44, extension=2) extension2_str = str(ob)[str(ob).index('extension 2'):] assert 'Chunk.energy.velang = 44' in extension2_str # test get assert ob._get('Observation.instrument.name') == 'NIRI' assert ob._get('Observation.instrument.keywords')[0] == ['INSTMODE'] assert ob._get('Observation.instrument.keywords')[1] == 'TEST' assert ob._get('Chunk.energy.velang', extension=2) == 44 assert ob._get('Chunk.energy.velang', extension=1) == 33 assert ob._get('Chunk.energy.axis.axis.ctype', extension=1)[0] ==\ ['MYCTYPE2', 'MYCTYPE'] assert ob._get('Chunk.energy.axis.axis.ctype', extension=1)[1] == 'NOCTYPE' # test get when keyword not present in extension and the default is used assert ob._get('Chunk.energy.specsys', extension=33)[0] == ['SPECSYS'] assert ob._get('Chunk.energy.specsys', extension=44)[1] is None # get element not set assert 'Observation.target.redshift' not in ob._plan assert ob._get('Observation.target.redshift') is None # bintable :) ob.add_table_attribute('CompositeObservation.members', 'FICS', extension=0) result = ob._get('CompositeObservation.members', extension=0) assert result is not None assert len(result) == 3, len(result) # delete attributes assert len(ob._plan) != 0 assert len(ob._extensions) != 0 for i in ObsBlueprint.CAOM2_ELEMENTS: ob.delete(i) assert len(ob._plan) == 0 # clear ob.clear('Chunk.energy.axis.axis.ctype', extension=1) # delete attributes from extensions ob.delete('Chunk.energy.velang', extension=1) ob.delete('Chunk.energy.axis.axis.ctype', extension=1) ob.delete('CompositeObservation.members', extension=0) ob.delete('Chunk.energy.velang', extension=2) assert len(ob._extensions) == 0 # testing error cases ob = ObsBlueprint() ob.configure_position_axes(axes=(1, 2)) ob.configure_energy_axis(axis=3) ob.configure_polarization_axis(axis=4) ob.configure_time_axis(axis=5) # non CAOM2 element name with pytest.raises(KeyError): ob.set('Nonexistent', 33) with pytest.raises(KeyError): ob.add_fits_attribute('Nonexistent', 33) with pytest.raises(KeyError): ob.set_default('Nonexistent', 33) with pytest.raises(KeyError): ob._get('Nonexistent') with pytest.raises(KeyError): ob.delete('Nonexistent') with pytest.raises(KeyError): ob.clear('Nonexistent') # repeat with extension specified with pytest.raises(KeyError): ob.set('Chunk.Nonexistent', 33, extension=1) with pytest.raises(KeyError): ob.add_fits_attribute('Chunk.Nonexistent', 33, extension=1) with pytest.raises(KeyError): ob.set_default('Chunk.Nonexistent', 33, extension=1) with pytest.raises(KeyError): ob._get('Nonexistent', extension=33) with pytest.raises(KeyError): ob.delete('Chunk.Nonexistent', extension=1) with pytest.raises(KeyError): ob.clear('Chunk.Nonexistent', extension=1) # CAOM2 element not Chunk with extension specified with pytest.raises(ValueError): ob.set('Observation.observationID', 33, extension=1) with pytest.raises(ValueError): ob.set_default('Observation.observationID', 33, extension=1) with pytest.raises(ValueError): ob.add_fits_attribute('Observation.observationID', 'AA', extension=1) with pytest.raises(ValueError): ob.delete('Observation.observationID', extension=1) with pytest.raises(ValueError): ob.clear('Observation.observationID', extension=1) # CAOM2 element Chunk with extension with pytest.raises(ValueError): ob.clear('Chunk.energy.axis.axis.cunit', extension=33) # add FITS attribute to element that does not exist assert 'Chunk.energy.transition' not in ob._plan ob.set('Chunk.energy.transition', 'Name') with pytest.raises(AttributeError): ob.add_fits_attribute('Chunk.energy.transition', 'BP') # call set_fits_attribute with argument other than list with pytest.raises(AttributeError): ob.add_fits_attribute('Chunk.energy.transition', 33) # delete element from a non-existent extension with pytest.raises(ValueError): ob.delete('Chunk.energy.transition', extension=66) # adding the same thing twice does nothing - the test values are defaults result = ob._get('Observation.metaRelease') initial_result_length = (len(result[0])) ob.add_fits_attribute('Observation.metaRelease', 'DATE-OBS') result = ob._get('Observation.metaRelease') add_result_length = (len(result[0])) assert initial_result_length == add_result_length # in an extension result = ob._get('Chunk.energy.specsys', extension=1) initial_result_length = (len(result[0])) ob.add_fits_attribute('Chunk.energy.specsys', 'SPECSYS') result = ob._get('Chunk.energy.specsys', extension=1) add_result_length = (len(result[0])) assert initial_result_length == add_result_length, result