[Tutor] Request for advice on Python code

Elaina Ann Hyde elainahyde at gmail.com
Tue Feb 14 02:09:07 CET 2012


On Tue, Feb 14, 2012 at 11:52 AM, Alan Gauld <alan.gauld at btinternet.com>wrote:

> On 13/02/12 23:50, Elaina Ann Hyde wrote:
>
>> Hi, I'm working on a routine for reading in the Schlegel dustmaps.  I
>> have an ascii table with values, Ra-Dec and I'm trying to convert 2
>> columns to l,b and get the dust values out, this is a 2 part problem as
>> I need to first convert to l,b, keep those values for other uses
>> (calculating VLSR), and get extinctions for each l,b value,
>>
>
> That sounds very interesting but almost totally meaningless to most of us
> on this list I suspect! :-)
>
>
>  import astropysics
>> from astropysics.coords import ICRSCoordinates,**GalacticCoordinates
>> import asciitable
>>
>
> From those lines alone I suspect you might have more luck on either a
> specialised forum or on the general Python mailing list/newsgroup.
> It looks like a much more specialised query than is usual on this list
> which is for beginning Python programmers.
>
> However....
>
>
>  dustmap='SFD_dust_4096_ngp.**fits'
>> EB_V=astropysics.obstools.get_**SFD_dust(l,b,dustmap,**interpolate=True)
>> -------
>> this gives the error 'Only length-1 arrays can be converted to Python
>> scalars' ... however, I cannot do:
>>
>
> Always post the full error text not just the last line or a summary.
> It usually contains a lot of useful information, especially what code
> Python actually saw when it tried to execute your code.
>
>  gcoords=ICRSCoordinates(dat['**ra-drad'],dat['dec-drad'],**
>> radians=True).convert(**GalacticCoordinates)
>>
>> without the same error.  Any ideas would be appreciated, thanks!
>>
>
> On the surface I have no idea because that looks like an issue
> related to the use of the astrophysics module. But the error
> message may reveal a more fundamental issue underneath.
>
>
> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
>
> ______________________________**_________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/**mailman/listinfo/tutor<http://mail.python.org/mailman/listinfo/tutor>
>

Thanks Alan,
   I'm pretty sure that the problem is that the module expects a scalar and
I want it to read and return a vector from a table.  This is a general-ish
problem since I haven't had any real experience operating vectors, although
the code I'm working on now should give me that.  I had a moment of
puzzelment earlier when I realized that .math doesn't operate on vectors
either so you have to do np.sin() instead of math.sin().  I am
thinking/hoping this might be a similarly easy problem....  Anyone
performing a module operation on a column from a file would encounter a
similar message (I have in other instances as well, some tasks don't seem
to care, other's do...).  If I do figure it out and it's a general thing
shall I post the solution here? The entire error is:
Traceback (most recent call last):
  File "read_plot.py", line 63, in <module>
    gcoords=ICRSCoordinates(dat['ra-drad'],dat['dec-drad'],
radians=True).convert(GalacticCoordinates)
  File
"/Library/Frameworks/Python.framework/Versions/7.2/lib/python2.7/site-packages/Astropysics-0.1.dev_r1142-py2.7.egg/astropysics/coords/coordsys.py",
line 1845, in __init__

EpochalLatLongCoordinates.__init__(self,kwargs['ra'],kwargs['dec'],kwargs['raerr'],kwargs['decerr'])
  File
"/Library/Frameworks/Python.framework/Versions/7.2/lib/python2.7/site-packages/Astropysics-0.1.dev_r1142-py2.7.egg/astropysics/coords/coordsys.py",
line 1730, in __init__
    LatLongCoordinates.__init__(self,long,lat,longerr,laterr,distancepc)
  File
"/Library/Frameworks/Python.framework/Versions/7.2/lib/python2.7/site-packages/Astropysics-0.1.dev_r1142-py2.7.egg/astropysics/coords/coordsys.py",
line 1307, in __init__
    self.lat = lat
  File
"/Library/Frameworks/Python.framework/Versions/7.2/lib/python2.7/site-packages/Astropysics-0.1.dev_r1142-py2.7.egg/astropysics/coords/coordsys.py",
line 1375, in _setLat
    rads = AngularCoordinate(val).radians%_twopi
  File
"/Library/Frameworks/Python.framework/Versions/7.2/lib/python2.7/site-packages/Astropysics-0.1.dev_r1142-py2.7.egg/astropysics/coords/coordsys.py",
line 254, in __init__
    self._decval = radians(inpt)
  File
"/Library/Frameworks/Python.framework/Versions/7.2/lib/python2.7/site-packages/numpy/ma/core.py",
line 3782, in __float__
    raise TypeError("Only length-1 arrays can be converted "\
TypeError: Only length-1 arrays can be converted to Python scalars
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20120214/accf9201/attachment-0001.html>


More information about the Tutor mailing list