Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.31 KB | None | 0 0
  1. egion strings
  2. --------------
  3.  
  4. We could consider adding a helper function to ``astropy-regions`` or Gammapy
  5. that we call everywhere in Gammapy at the start of functions or methods that
  6. have a ``region`` argument, like this::
  7.  
  8. >>> from regions import DS9Parser
  9. >>> def make_region(region):
  10. ... if isinstance(region, str):
  11. ... return DS9Parser(region).shapes[0].to_region()
  12. ... else:
  13. ... return region
  14. ...
  15. >>> make_region("image;circle(42,43,3)")
  16. <CirclePixelRegion(PixCoord(x=41.0, y=42.0), radius=3.0)>
  17. >>> make_region("galactic;circle(42,43,3)")
  18. <CircleSkyRegion(<SkyCoord (Galactic): (l, b) in deg
  19. (42., 43.)>, radius=3.0 deg)>
  20.  
  21. This is convenient, a simple ``region="galactic;circle(42,43,3)"`` is much
  22. shorter than having to remember and type this equivalent code::
  23.  
  24. >>> from astropy.coordinates import SkyCoord, Angle
  25. >>> from regions import CircleSkyRegion
  26. >>> center = SkyCoord(42, 43, unit="deg", frame="galactic")
  27. >>> radius = Angle(3, "deg")
  28. >>> region = CircleSkyRegion(center, radius)
  29. >>> region
  30. <CircleSkyRegion(<SkyCoord (Galactic): (l, b) in deg
  31. (42., 43.)>, radius=3.0 deg)>
  32.  
  33. There is precendence for this pattern in Gammapy, we usually pass ``Quantity``
  34. and ``Angle`` arguments through via e.g. ``angle = Angle(angle)`` in Gammapy
  35. functions, and often call them with strings ``angle="3 deg"``.
  36.  
  37. This ``make_region`` function could also be the place where we type checks and
  38. do the ``SkyRegion`` to ``PixelRegion`` conversion, avoiding duplicated code for
  39. this in each Gammapy function that has a ``region`` argument, like this::
  40.  
  41. from regions import DS9Parser, SkyRegion, PixelRegion
  42.  
  43. def make_pixel_region(region, wcs=None):
  44. if isinstance(region, str):
  45. region = DS9Parser(region).shapes[0].to_region()
  46.  
  47. if isinstance(region, SkyRegion):
  48. return region.to_pixel(wcs)
  49. elif isinstance(region, PixelRegion):
  50. return region
  51. else:
  52. raise TypeError("...")
  53.  
  54. The drawback of adding this now is that everyone will start using it and we
  55. should after Gammapy v1.0 keep supporting it, i.e. we are committing to the DS9
  56. region format in Gammapy, without having considered other possible formats in
  57. detail, or having an equivalent solution in place for ``SkyCoord``.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement