Skip to content

Commit

Permalink
Revert "changed EpochPropagation"
Browse files Browse the repository at this point in the history
This reverts commit baf9201.
  • Loading branch information
somilia committed Nov 24, 2023
1 parent baf9201 commit 264a8de
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 145 deletions.
9 changes: 4 additions & 5 deletions docs/mivot/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,10 @@ Example for epoch propagation
.. doctest-remote-data::
>>> with ModelViewer(votable) as m_viewer: # doctest: +SKIP
... row_view = m_viewer.get_next_row_view()
... epoch_propagation = EpochPropagation(row_view)
... past_ra, past_dec = epoch_propagation.apply_space_motion(dt=-42 * u.year)
... future_ra, future_dec = epoch_propagation.apply_space_motion(dt=2 * u.year)
... print("past_ra, past_dec :", epoch_propagation.apply_space_motion(dt=-42 * u.year))
... print("future_ra, future_dec :", epoch_propagation.apply_space_motion(dt=2 * u.year))
... past_ra, past_dec = row_view.epoch_propagation.apply_space_motion(dt=-42 * u.year)
... future_ra, future_dec = row_view.epoch_propagation.apply_space_motion(dt=2 * u.year)
... print("past_ra, past_dec :", row_view.epoch_propagation.apply_space_motion(dt=-42 * u.year))
... print("future_ra, future_dec :", row_view.epoch_propagation.apply_space_motion(dt=2 * u.year))
past_ra, past_dec : (<Longitude 9.9998763 deg>, <Latitude 10.00024364 deg>)
future_ra, future_dec : (<Longitude 10.00000563 deg>, <Latitude 9.99998891 deg>)

Expand Down
151 changes: 29 additions & 122 deletions pyvo/mivot/features/epoch_propagation.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
"""
from astropy.coordinates import SkyCoord
import astropy.units as u
from astropy.time import Time

from pyvo.mivot.viewer.mivot_class import MivotClass
from pyvo.utils.prototype import prototype_feature


Expand All @@ -20,154 +17,64 @@ class EpochPropagation:
"""
fields = ["longitude", "latitude", "pm_longitude", "pm_latitude"]

def __init__(self, row_view):
"""
Constructor of the EpochPropagation class.
Parameters
----------
row_view : ~`pyvo.mivot.viewer.mivot_class.MivotClass`
The data row as a MivotClass.
"""
self.longitude = None
self.latitude = None
self.pm_longitude = None
self.pm_latitude = None
self.radial_velocity = None
self.parallax = None
self.epoch = None
self.frame = None
self.updateEpoch(row_view)

def updateEpoch(self, mivot_class, type_epoch=False):
"""
Initialize and update the attributes of the EpochPropagation object.
Parameters
----------
mivot_class : ~`pyvo.mivot.viewer.mivot_class.MivotClass`
The data row as a MivotClass.
type_epoch : bool, optional
If True, it means that the current MivotClass comes from an EpochPosition instance.
Default is False.
"""
for key, value in mivot_class.__dict__.items():
if isinstance(value, list):
for item in value:
if mivot_class.dmtype == 'EpochPosition' or type_epoch is True:
print("Liste | EpochProp: ", key, value)
self.updateEpoch(item, True)
else:
print("Liste | Pas EpochProp : ", key, value)
print("Liste | Pas EpochProp : ", item.__dict__)
self.updateEpoch(item)

elif isinstance(value, MivotClass):
if isinstance(value.__dict__, dict):
if 'value' not in value.__dict__:
if mivot_class.dmtype == 'EpochPosition' or type_epoch is True:
print("Pas une feuille | EpochProp: ", key, value)
self.updateEpoch(value, True)
else:
print("Pas EpochProp : ", key, value)
self.updateEpoch(value)

elif 'value' in value.__dict__:
if mivot_class.dmtype == 'EpochPosition' or type_epoch is True:
print("Feuille | EpochProp: ", key, value, value.__dict__)
self._fill_epoch_propagation(key.lower(), value.__dict__)
else:
print("Pas EpochProp : ", key, value)
self.updateEpoch(value)
else:
self.updateEpoch(value)

def _fill_epoch_propagation(self, key_low, value):
"""
Fill the attributes EpochPropagation object.
Parameters
----------
key_low : str
The key of the dictionary in lowercase.
value : dict
The value of the dictionary.
"""
print(key_low, value, "\n")
if "frame" in key_low and "string" in value["dmtype"]:
self.frame = value["value"].lower()
if ("longitude" or "ra") in key_low:
if "pm" not in key_low and value["unit"] == "deg":
self.longitude = value['value']
elif "pm" in key_low and value["unit"] == "mas/year":
self.pm_longitude = value['value']
if ("latitude" or "dec") in key_low:
if "pm" not in key_low and value["unit"] == "deg":
self.latitude = value['value']
elif "pm" in key_low and value["unit"] == "mas/year":
self.pm_latitude = value['value']
if ("radial" or "velocity") in key_low and value["unit"] == "km/s":
self.radial_velocity = value["value"]
if "parallax" in key_low and value["unit"] == ("mas" or "pc"):
self.parallax = value["value"]
if "epoch" in key_low and value["unit"] == "year":
self.epoch = Time(value["value"], format="decimalyear")
def __init__(self, name):
self.REFERENCE = {}
self.name = name

@property
def ref_long(self):
return self.longitude
return self.REFERENCE.get("longitude")

@ref_long.setter
def ref_long(self, value):
self.longitude = value
self.REFERENCE["longitude"] = value

@property
def ref_lat(self):
return self.latitude
return self.REFERENCE.get("latitude")

@ref_lat.setter
def ref_lat(self, value):
self.latitude = value
self.REFERENCE["latitude"] = value

@property
def ref_pm_long(self):
return self.pm_longitude
return self.REFERENCE.get("pm_longitude")

@ref_pm_long.setter
def ref_pm_long(self, value):
self.pm_longitude = value
self.REFERENCE["pm_longitude"] = value

@property
def ref_pm_lat(self):
return self.pm_latitude
return self.REFERENCE.get("pm_latitude")

@ref_pm_lat.setter
def ref_pm_lat(self, value):
self.pm_latitude = value
self.REFERENCE["pm_latitude"] = value

def SkyCoordinate(self):
"""
Returns a SkyCoord object from the REFERENCE of the XML object.
"""
if self.frame == ('icrs' or 'fk5' or 'fk4'):
return SkyCoord(distance=(self.parallax / 4) * u.pc,
radial_velocity=self.radial_velocity * u.km / u.s,
ra=self.longitude * u.degree,
dec=self.latitude * u.degree,
pm_ra_cosdec=self.pm_longitude * u.mas / u.yr,
pm_dec=self.pm_latitude * u.mas / u.yr,
frame=self.frame,
obstime=self.epoch)

elif self.frame == 'galatic':
return SkyCoord(distance=self.parallax * u.pc,
radial_velocity=self.radial_velocity * u.km / u.s,
l=self.longitude * u.degree,
b=self.latitude * u.degree,
pm_l_cosb=self.pm_longitude * u.mas / u.yr,
pm_b=self.pm_latitude * u.mas / u.yr,
frame=self.frame,
obstime=self.epoch)
if self.REFERENCE["frame"] == ('icrs' or 'fk5' or 'fk4'):
return SkyCoord(distance=(self.REFERENCE["parallax"] / 4) * u.pc,
radial_velocity=self.REFERENCE["radial_velocity"] * u.km / u.s,
ra=self.REFERENCE["longitude"] * u.degree,
dec=self.REFERENCE["latitude"] * u.degree,
pm_ra_cosdec=self.REFERENCE["pm_longitude"] * u.mas / u.yr,
pm_dec=self.REFERENCE["pm_latitude"] * u.mas / u.yr,
frame=self.REFERENCE["frame"],
obstime=self.REFERENCE["epoch"])

elif self.REFERENCE["frame"] == 'galatic':
return SkyCoord(distance=self.REFERENCE["parallax"] * u.pc,
l=self.REFERENCE["longitude"] * u.degree,
b=self.REFERENCE["latitude"] * u.degree,
pm_l_cosb=self.REFERENCE["pm_longitude"] * u.mas / u.yr,
pm_b=self.REFERENCE["pm_latitude"] * u.mas / u.yr,
frame=self.REFERENCE["frame"],
obstime=self.REFERENCE["epoch"])

def apply_space_motion(self, dt):
"""
Expand Down
15 changes: 7 additions & 8 deletions pyvo/mivot/tests/test_epoch_propagation.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import astropy.units as u
from astropy.time import Time

from pyvo.mivot.features.epoch_propagation import EpochPropagation
from pyvo.mivot.version_checker import check_astropy_version
from pyvo.mivot.viewer.model_viewer import ModelViewer
from pyvo.utils import activate_features
Expand All @@ -22,7 +21,7 @@ def test_epoch_propagation(m_viewer):
pytest.skip("MIVOT test skipped because of the astropy version.")

row_view = m_viewer.get_next_row_view()
EpochProcess = EpochPropagation(row_view)
epoch_propagation = row_view.EpochPropagation
sky_coord_to_compare = (SkyCoord(distance=(row_view.parallax.value / 4) * u.pc,
radial_velocity=row_view.radialVelocity.value * u.km / u.s,
ra=row_view.longitude.value * u.degree,
Expand All @@ -33,14 +32,14 @@ def test_epoch_propagation(m_viewer):
.PhysicalCoordSys_frame.spaceRefFrame.value.lower(),
obstime=Time(row_view.epoch.value, format="decimalyear")))

assert sky_coord_to_compare == EpochProcess.SkyCoordinate()
assert sky_coord_to_compare == epoch_propagation.SkyCoordinate()
assert ((sky_coord_to_compare.apply_space_motion(dt=-42 * u.year).ra,
sky_coord_to_compare.apply_space_motion(dt=-42 * u.year).dec)
== EpochProcess.apply_space_motion(dt=-42 * u.year))
assert EpochProcess.ref_long == 10.0
assert EpochProcess.ref_lat == 10.0
assert EpochProcess.ref_pm_long == 10.0
assert EpochProcess.ref_pm_lat == -20.0
== epoch_propagation.apply_space_motion(dt=-42 * u.year))
assert epoch_propagation.ref_long == 10.0
assert epoch_propagation.ref_lat == 10.0
assert epoch_propagation.ref_pm_long == 10.0
assert epoch_propagation.ref_pm_lat == -20.0


@pytest.fixture
Expand Down
6 changes: 2 additions & 4 deletions pyvo/mivot/utils/xml_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,8 @@ def pretty_string(xmltree):
str
The pretty string representation of the XML tree.
"""
if hasattr(xmltree, 'getroot'):
xmltree = xmltree.getroot()
XmlUtils.indent(xmltree)
new_xml = ET.tostring(xmltree, encoding='unicode')
XmlUtils.indent(xmltree.getroot())
new_xml = ET.tostring(xmltree.getroot(), encoding='unicode')
return new_xml.replace("ns0:", "")

@staticmethod
Expand Down
50 changes: 44 additions & 6 deletions pyvo/mivot/viewer/mivot_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
"""
MivotClass keep as an attribute dictionary __dict__ all XML objects.
"""
from astropy.time import Time

from pyvo.mivot.features.epoch_propagation import EpochPropagation
from pyvo.utils.prototype import prototype_feature


Expand All @@ -15,6 +18,8 @@ class MivotClass:
"key" : "value" means key is an element of ATTRIBUTE
"key" : [] means key is the dmtype of a COLLECTION
"""
EpochPropagation = EpochPropagation("EpochPropagation")
REFERENCE = {}

def __init__(self, **kwargs):
"""
Expand All @@ -31,13 +36,46 @@ def __init__(self, **kwargs):
for item in value:
self.__dict__[self._remove_model_name(key)].append(MivotClass(**item))

elif isinstance(value, dict):
if not self._is_leaf(**value):
self.__dict__[self._remove_model_name(key, True)] = MivotClass(**value)
if self._is_leaf(**value):
self.__dict__[self._remove_model_name(key)] = MivotClass(**value)
elif isinstance(value, dict) and 'value' not in value:
self.__dict__[self._remove_model_name(key, True)] = MivotClass(**value)

else:
self.__dict__[self._remove_model_name(key)] = self._remove_model_name(value)
if isinstance(value, dict) and self._is_leaf(**value):
self.__dict__[self._remove_model_name(key)] = MivotClass(**value)
if self.dmtype == "EpochPosition":
self._fill_epoch_propagation(key.lower(), value)
if "frame" in key.lower() and "string" in value["dmtype"]:
self.EpochPropagation.REFERENCE["frame"] = value["value"].lower()
else:
self.__dict__[self._remove_model_name(key)] = self._remove_model_name(value)

def _fill_epoch_propagation(self, key_low, value):
"""
Fill the REFERENCE dictionary of the EpochPropagation object.
Parameters
----------
key_low : str
The key of the dictionary in lowercase.
value : dict
The value of the dictionary.
"""
if ("longitude" or "ra") in key_low:
if "pm" not in key_low and value["unit"] == "deg":
self.EpochPropagation.REFERENCE["longitude"] = value['value']
elif "pm" in key_low and value["unit"] == "mas/year":
self.EpochPropagation.REFERENCE["pm_longitude"] = value['value']
if ("latitude" or "dec") in key_low:
if "pm" not in key_low and value["unit"] == "deg":
self.EpochPropagation.REFERENCE["latitude"] = value['value']
elif "pm" in key_low and value["unit"] == "mas/year":
self.EpochPropagation.REFERENCE["pm_latitude"] = value['value']
if ("radial" or "velocity") in key_low and value["unit"] == "km/s":
self.EpochPropagation.REFERENCE["radial_velocity"] = value["value"]
if "parallax" in key_low and value["unit"] == ("mas" or "pc"):
self.EpochPropagation.REFERENCE["parallax"] = value["value"]
if "epoch" in key_low and value["unit"] == "year":
self.EpochPropagation.REFERENCE["epoch"] = Time(value["value"], format="decimalyear")

def _remove_model_name(self, value, role_instance=False):
"""
Expand Down

0 comments on commit 264a8de

Please sign in to comment.