gammapy/gammapy — pull request #200 by cdeil (2014-09-20 19:27:04+00:00)
Title: Quantity errors from gammapy.astro.source models

@adonath After updating to Numpy 1.9, I'm getting unit test fails on my Mac from the `SNR` and `PWN` code and tests you wrote.

E.g. this code:
```python
from astropy.units import Quantity
from gammapy.astro.source import SNR
t = Quantity([0, 1, 10, 100, 1000, 10000], 'yr')
snr = SNR()
snr.radius(t)
```
fails like this:
```
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
/private/tmp/gammapy/a.py in ()
4 t = Quantity([0, 1, 10, 100, 1000, 10000], 'yr')
5 snr = SNR()
----> 6 snr.radius(t)
7

/private/tmp/gammapy/gammapy/astro/source/snr.py in radius(self, t)
85 self._radius_free_expansion(t).to('cm'))
86 import IPython; IPython.embed()
---> 87 return Quantity(r, 'cm')
88
89 def _radius_free_expansion(self, t):

/Users/deil/Library/Python/3.4/lib/python/site-packages/astropy-1.0.dev9928-py3.4-macosx-10.9-x86_64.egg/astropy/units/quantity.py in __new__(cls, value, unit, dtype, copy, order, subok, ndmin)
176 if isinstance(value, Quantity):
177 if unit is not None and unit is not value.unit:
--> 178 value = value.to(unit)
179 # the above already makes a copy (with float dtype)
180 copy = False

/Users/deil/Library/Python/3.4/lib/python/site-packages/astropy-1.0.dev9928-py3.4-macosx-10.9-x86_64.egg/astropy/units/quantity.py in to(self, unit, equivalencies)
577 unit = Unit(unit)
578 new_val = np.asarray(
--> 579 self.unit.to(unit, self.value, equivalencies=equivalencies))
580 return self._new_view(new_val, unit)
581

AttributeError: 'NoneType' object has no attribute 'to'
```

I've traced it down to the `np.where` calls ... they return an un-initialised Quantity as mentioned [here](http://astropy.readthedocs.org/en/latest/units/quantity.html#known-issues-with-conversion-to-numpy-arrays) (I think this is a new issue that appeared with the update to numpy 1.9 !?):
```python
>>> np.where([True, False], Quantity([1,2], 'cm'), Quantity([10, 20], 'cm'))

```
Fixing this now ... something to watch out in the future.