| 1 |
from .core import TransformConverterBase, parameter_to_value |
| 2 |
|
| 3 |
|
| 4 |
class Rotate3DConverter(TransformConverterBase): |
| 5 |
""" |
| 6 |
ASDF support for serializing rotation models that |
| 7 |
use the rotate3d tag. |
| 8 |
""" |
| 9 |
|
| 10 |
tags = ("tag:stsci.edu:asdf/transform/rotate3d-*",) |
| 11 |
types = ( |
| 12 |
"astropy.modeling.rotations.RotateNative2Celestial", |
| 13 |
"astropy.modeling.rotations.RotateCelestial2Native", |
| 14 |
"astropy.modeling.rotations.EulerAngleRotation", |
| 15 |
) |
| 16 |
|
| 17 |
def to_yaml_tree_transform(self, model, tag, ctx): |
| 18 |
from astropy.modeling import rotations |
| 19 |
|
| 20 |
if isinstance(model, rotations.RotateNative2Celestial): |
| 21 |
node = { |
| 22 |
"phi": parameter_to_value(model.lon), |
| 23 |
"theta": parameter_to_value(model.lat), |
| 24 |
"psi": parameter_to_value(model.lon_pole), |
| 25 |
"direction": "native2celestial", |
| 26 |
} |
| 27 |
elif isinstance(model, rotations.RotateCelestial2Native): |
| 28 |
node = { |
| 29 |
"phi": parameter_to_value(model.lon), |
| 30 |
"theta": parameter_to_value(model.lat), |
| 31 |
"psi": parameter_to_value(model.lon_pole), |
| 32 |
"direction": "celestial2native", |
| 33 |
} |
| 34 |
else: |
| 35 |
node = { |
| 36 |
"phi": parameter_to_value(model.phi), |
| 37 |
"theta": parameter_to_value(model.theta), |
| 38 |
"psi": parameter_to_value(model.psi), |
| 39 |
"direction": model.axes_order, |
| 40 |
} |
| 41 |
|
| 42 |
return node |
| 43 |
|
| 44 |
def from_yaml_tree_transform(self, node, tag, ctx): |
| 45 |
from astropy.modeling import rotations |
| 46 |
|
| 47 |
if node["direction"] == "native2celestial": |
| 48 |
return rotations.RotateNative2Celestial(node["phi"], node["theta"], node["psi"]) |
| 49 |
|
| 50 |
if node["direction"] == "celestial2native": |
| 51 |
return rotations.RotateCelestial2Native(node["phi"], node["theta"], node["psi"]) |
| 52 |
|
| 53 |
return rotations.EulerAngleRotation(node["phi"], node["theta"], node["psi"], axes_order=node["direction"]) |
| 54 |
|
| 55 |
|
| 56 |
class RotationSequenceConverter(TransformConverterBase): |
| 57 |
""" |
| 58 |
ASDF support for serializing rotation sequence models. |
| 59 |
""" |
| 60 |
|
| 61 |
tags = ("tag:stsci.edu:asdf/transform/rotate_sequence_3d-*",) |
| 62 |
types = ( |
| 63 |
"astropy.modeling.rotations.RotationSequence3D", |
| 64 |
"astropy.modeling.rotations.SphericalRotationSequence", |
| 65 |
) |
| 66 |
|
| 67 |
def to_yaml_tree_transform(self, model, tag, ctx): |
| 68 |
from astropy.modeling import rotations |
| 69 |
|
| 70 |
node = {"angles": list(model.angles.value)} |
| 71 |
node["axes_order"] = model.axes_order |
| 72 |
if isinstance(model, rotations.SphericalRotationSequence): |
| 73 |
node["rotation_type"] = "spherical" |
| 74 |
elif isinstance(model, rotations.RotationSequence3D): |
| 75 |
node["rotation_type"] = "cartesian" |
| 76 |
else: |
| 77 |
msg = f"Cannot serialize model of type {type(model)}" |
| 78 |
raise TypeError(msg) |
| 79 |
return node |
| 80 |
|
| 81 |
def from_yaml_tree_transform(self, node, tag, ctx): |
| 82 |
from astropy.modeling import rotations |
| 83 |
|
| 84 |
angles = node["angles"] |
| 85 |
axes_order = node["axes_order"] |
| 86 |
rotation_type = node["rotation_type"] |
| 87 |
if rotation_type == "cartesian": |
| 88 |
return rotations.RotationSequence3D(angles, axes_order=axes_order) |
| 89 |
|
| 90 |
if rotation_type == "spherical": |
| 91 |
return rotations.SphericalRotationSequence(angles, axes_order=axes_order) |
| 92 |
|
| 93 |
msg = f"Unrecognized rotation_type: {rotation_type}" |
| 94 |
raise ValueError(msg) |