Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed some CRTF write functionality #313

Merged
merged 25 commits into from May 21, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/crtf.rst
Expand Up @@ -47,7 +47,7 @@ Serialisation is done using the `~regions.crtf_objects_to_string` function

>>> from regions import crtf_objects_to_string
>>> crtf_objects_to_string(regions, coordsys='galactic')
'#CRTF\ncircle[[144.559169deg, -14.923593deg], 3.000000deg], coord=GALACTIC, color=green\n'
'#CRTFv0\nglobal coord=galactic\ncircle[[144.559169deg, -14.923593deg], 3.000000deg], color=green\n'

There's also `~regions.write_crtf` and `~regions.read_crtf` which write to and
read from a file in addition to doing the region serialisation and parsing.
Expand Down
25 changes: 10 additions & 15 deletions regions/io/core.py
Expand Up @@ -71,12 +71,12 @@ class RegionConversionError(ValueError):
coordsys_mapping = {'DS9': {x: x for x in valid_coordsys['DS9']},
'CRTF': {x: x for x in valid_coordsys['CRTF']}
}
coordsys_mapping['CRTF']['geocentrictrueecliptic'] = 'ecliptic'
coordsys_mapping['CRTF']['fk5'] = 'j2000'
coordsys_mapping['CRTF']['fk4'] = 'b1950'
coordsys_mapping['CRTF']['supergalactic'] = 'supergal'
coordsys_mapping['CRTF']['geocentrictrueecliptic'] = 'ECLIPTIC'
coordsys_mapping['CRTF']['fk5'] = 'J2000'
coordsys_mapping['CRTF']['fk4'] = 'B1950'
coordsys_mapping['CRTF']['supergalactic'] = 'SUPERGAL'

coordsys_mapping['DS9']['geocentrictrueecliptic'] = 'ecliptic'
coordsys_mapping['DS9']['geocentrictrueecliptic'] = 'ECLIPTIC'
keflavich marked this conversation as resolved.
Show resolved Hide resolved


class ShapeList(list):
Expand Down Expand Up @@ -137,7 +137,7 @@ def to_crtf(self, coordsys='fk5', fmt='.6f', radunit='deg'):
'line': '{0}line[[{1:FMT}deg, {2:FMT}deg], [{3:FMT}deg, {4:FMT}deg]]'
}

output = '#CRTF\n'
output = '#CRTFv0\n'
JonahDW marked this conversation as resolved.
Show resolved Hide resolved

if radunit == 'arcsec':
# what's this for?
Expand All @@ -156,7 +156,7 @@ def to_crtf(self, coordsys='fk5', fmt='.6f', radunit='deg'):

# CASA does not support global coordinate specification, even though the
# documentation for the specification explicitly states that it does.
# output += 'global coord={}\n'.format(coordsys)
output += 'global coord={}\n'.format(coordsys_mapping['CRTF'][coordsys])
JonahDW marked this conversation as resolved.
Show resolved Hide resolved

for shape in self:

Expand All @@ -178,11 +178,6 @@ def to_crtf(self, coordsys='fk5', fmt='.6f', radunit='deg'):
'coord', 'text', 'range', 'corr',
'type'))

# the first item should be the coordinates, since CASA cannot
# recognize a region without an inline coordinate specification
# It can be, but does not need to be, comma-separated at the start
meta_str = "coord={}, ".format(coordsys.upper()) + meta_str

if 'comment' in shape.meta:
keflavich marked this conversation as resolved.
Show resolved Hide resolved
meta_str += ", " + shape.meta['comment']
if 'range' in shape.meta:
Expand Down Expand Up @@ -224,9 +219,9 @@ def to_crtf(self, coordsys='fk5', fmt='.6f', radunit='deg'):
else:
line = crtf_strings['point'].format(include, *coord)
elif shape.region_type == 'ellipse':
coord[2:] = [x / 2 for x in coord[2:]]
if len(coord) % 2 == 1:
coord[-1] *= 2
# coord[2:] = [x / 2 for x in coord[2:]]
# if len(coord) % 2 == 1:
# coord[-1] *= 2
keflavich marked this conversation as resolved.
Show resolved Hide resolved
line = crtf_strings['ellipse'].format(include, *coord)
elif shape.region_type == 'text':
line = crtf_strings['text'].format(include, *coord, text=shape.meta['text'])
Expand Down
8 changes: 4 additions & 4 deletions regions/io/crtf/read.py
Expand Up @@ -406,10 +406,10 @@ def make_shape(self):
"""
Make shape object
"""
if self.region_type == 'ellipse':
self.coord[2:] = [x * 2 for x in self.coord[2:]]
if len(self.coord) % 2 == 1: # This checks if the angle is present.
self.coord[-1] /= 2
# if self.region_type == 'ellipse':
# self.coord[2:] = [x * 2 for x in self.coord[2:]]
# if len(self.coord) % 2 == 1: # This checks if the angle is present.
# self.coord[-1] /= 2

if self.region_type == 'box':
x = (self.coord[0] + self.coord[2]) / 2
Expand Down
2 changes: 1 addition & 1 deletion regions/io/crtf/tests/data/CRTFgeneral.crtf
@@ -1,4 +1,4 @@
#CRTF0
#CRTFv0
keflavich marked this conversation as resolved.
Show resolved Hide resolved
global coord=B1950, frame=BARY, corr=[I, Q], color=blue

# A simple circle region:
Expand Down
17 changes: 9 additions & 8 deletions regions/io/crtf/tests/data/CRTFgeneraloutput.crtf
@@ -1,8 +1,9 @@
#CRTF
ann circle[[273.100deg, -23.183deg], 0.001deg], coord=FK4, frame=BARY, color=blue, corr=[I, Q]
rotbox[[180.392deg, 12.392deg], [0.050deg, 0.017deg], 12.000deg], coord=FK4, frame=BARY, color=blue, range=[-1240.0km/s, 1240.0km/s], corr=[I, Q]
annulus[[267.763deg, -45.297deg], [0.100deg, 4.120deg]], coord=FK4, label='My label here', frame=BARY, color=red, corr=[I, Q, U, V]
-ellipse[[12.000deg, 45.000deg], [0.250deg, 1.340deg], 2578.310deg], coord=FK4, label='Removed this', frame=BARY, color=green, range=[1.42GHz, 1.421GHz], corr=[Q]
symbol[[31.470deg, 11.905deg], D], coord=FK4, frame=BARY, color=blue, linewidth=2, symsize=2, corr=[I, Q]
text[[31.470deg, 11.905deg], 'my text'], coord=FK4, frame=BARY, color=blue, linewidth=2, corr=[I, Q]
rotbox[[2.000deg, 3.000deg], [2.000deg, 2.000deg], 0.000deg], coord=FK4, frame=BARY, color=blue, corr=[I, Q]
#CRTFv0
global coord=B1950
ann circle[[273.100deg, -23.183deg], 0.001deg], frame=BARY, color=blue, corr=[I, Q]
rotbox[[180.392deg, 12.392deg], [0.050deg, 0.017deg], 12.000deg], frame=BARY, color=blue, range=[-1240.0km/s, 1240.0km/s], corr=[I, Q]
annulus[[267.763deg, -45.297deg], [0.100deg, 4.120deg]], label='My label here', frame=BARY, color=red, corr=[I, Q, U, V]
-ellipse[[12.000deg, 45.000deg], [0.250deg, 1.340deg], 2578.310deg], label='Removed this', frame=BARY, color=green, range=[1.42GHz, 1.421GHz], corr=[Q]
symbol[[31.470deg, 11.905deg], D], frame=BARY, color=blue, linewidth=2, symsize=2, corr=[I, Q]
text[[31.470deg, 11.905deg], 'my text'], frame=BARY, color=blue, linewidth=2, corr=[I, Q]
rotbox[[2.000deg, 3.000deg], [2.000deg, 2.000deg], 0.000deg], frame=BARY, color=blue, corr=[I, Q]
5 changes: 3 additions & 2 deletions regions/io/crtf/write.py
Expand Up @@ -36,8 +36,9 @@ def crtf_objects_to_string(regions, coordsys='fk5', fmt='.6f', radunit='deg'):
>>> from regions import CircleSkyRegion, crtf_objects_to_string
>>> reg_sky = CircleSkyRegion(SkyCoord(1 * u.deg, 2 * u.deg), 5 * u.deg)
>>> print(crtf_objects_to_string([reg_sky]))
#CRTF
circle[[1.000007deg, 2.000002deg], 5.000000deg], coord=FK5,
#CRTFv0
global coord=J2000
circle[[1.000007deg, 2.000002deg], 5.000000deg]

"""
shapelist = to_shape_list(regions, coordsys)
Expand Down
4 changes: 2 additions & 2 deletions regions/io/tests/test_shape_object.py
Expand Up @@ -111,8 +111,8 @@ def test_valid_ellipse_crtf():
assert_quantity_allclose(shape1.coord[:2], [1, 2])
assert_quantity_allclose(shape2.coord[:2], [Quantity("1deg"), Quantity("2deg")])

assert_quantity_allclose(shape1.coord[2:-1], [6, 8])
assert_quantity_allclose(shape2.coord[2:-1], [Quantity("6deg"), Quantity("8deg")])
assert_quantity_allclose(shape1.coord[2:-1], [3, 4])
assert_quantity_allclose(shape2.coord[2:-1], [Quantity("3deg"), Quantity("4deg")])

assert_quantity_allclose(shape1.coord[-1], Quantity("5deg"))
assert_quantity_allclose(shape2.coord[-1], Quantity("5deg"))