mfouesneau / pyphot

suite to deal with passband photometry
https://mfouesneau.github.io/pyphot/
MIT License
57 stars 18 forks source link

TypeError: only integer scalar arrays can be converted to a scalar index #13

Closed pziphi closed 6 years ago

pziphi commented 6 years ago

Hi,

I was trying to use PyPhot to do synthetic photometry for a star, for which I have the calibrated spectra. The wavelength values are stored in the array: lamb and the calibrated flux values are stored in the array spectra. I was following the quick start guide given in the documentation and the code gives me the error: TypeError: only integer scalar arrays can be converted to a scalar index

import numpy as np
import pyphot

lib = pyphot.get_library()
f = lib['SDSS_r']

fluxes = f.get_flux(lamb, spectra, axis=-1)
Warning: assuming units are consistent
--------------------------------------------------------------------------
TypeError                                Traceback (most recent call last)
<ipython-input-44-bc534503aad0> in <module>()
----> 1 fluxes = f.get_flux(lamb, spectra, axis=-1)

/home/user/.local/lib/python2.7/site-packages/pyphot/phot.pyc in get_flux(self, slamb, sflux, axis)
    248             Energy of the spectrum within the filter
    249         """
--> 250         return self.getFlux(slamb, sflux, axis=axis)
    251 
    252     def reinterp(self, lamb):

/home/user/.local/lib/python2.7/site-packages/pyphot/phot.pyc in getFlux(self, slamb, sflux, axis)
    214                 _sflux = sflux[:, ind]
    215             except:
--> 216                 _sflux = sflux[ind]
    217             # limit integrals to where necessary
    218             ind = ifT > 0.

TypeError: only integer scalar arrays can be converted to a scalar index

When searched online I saw that the issue appears to be related to the latest version of NumPy. The version of NumPy that I am using is 1.14.5.

Can anyone suggest me a fix, please?

mfouesneau commented 6 years ago

I cannot reproduce the issue.

import pyphot
lib = pyphot.get_library()
f = lib['SDSS_r']
from pyphot.vega import Vega
vega = Vega()
f.get_flux(vega.wavelength, vega.flux)
f.get_flux(vega.wavelength.magnitude, vega.flux.magnitude)

Please provide more information. tests above done with np.__version__ = '1.14.5'

pziphi commented 6 years ago

Thank you for a quick reply.

This code works fine for me too. When I checked the type of 'vega.wavelength' and 'vega.flux', I see that they are quantities:

vega.wavelength
<Quantity([9.00452026e+02 9.01354004e+02 9.02257996e+02 ... 2.99353200e+06  2.99653275e+06 2.99953700e+06], 'angstrom')>

vega.flux
<Quantity([1.23800003e-17 1.67599994e-17 1.78000003e-17 ... 1.40099994e-19
 1.38700004e-19 1.26499994e-19], 'flam')>

When I rechecked my inputs: 'lamb' and 'spectra', I realized that they are lists (not arrays).

So, I assume the issue is with my input data type.

I have this ascii file containing the wavelength, flux and error values. What I did was, I created two lists: 'lamb' and 'spectra' from this file where 'lamb' contains wavelength data and 'spectra' contains flux data. Could you please help me by letting me know the right way to proceed? Also, where do I incorporate the units?

mfouesneau commented 6 years ago

You can use numpy arrays instead of lists and that will work with a unit warning.

The only necessary unit is on the wavelength (the code does not handle flux units yet).

A simple multiplication between your array and the unit object is enough. (very similar to astropy units)

from pyphot import unit

wave_unit = wave * unit['nm']