| Version 23 (modified by , 13 years ago) ( diff ) |
|---|
Python
Learn python at this weekly event.
Useful Links
- Python v2.6.5 Documentation
- AstroPython: "This site is a community-driven knowledge base for research in astronomy using Python"
- "Astrolib" Astronomically Useful Python Modules
- Astro Coords Documentation
- Astro Ascii Data Documentation: Astro Ascii is a useful Python module for reading and manipulating data tables.
For Astro Ascii in a nutshell, see why it's just so awesome.
- Python Switchers Guide
- Python Numpy for IDL users
Added by Lia.
Added by Jenna.
A few notes
- I've been using Python for a while, in particular Python for Astronomy, so if you have any questions I'd be happy to help!
- You may notice that there are ~30-50 Python Astronomy packages out there. Some do the same thing, perhaps none do what you want, but in any case there is an effort now to put together a universal Python Astronomy package called 'AstroPy.' The effort (from what I can tell) is being driven by the same group who make NumPy / SciPy, with a pretty strong contribution from a number of grad students around the country. I'll be attending an organizational meeting for AstroPy in October at the CfA (and will report on it's status afterwards), but I wanted to at least let you know that there is an effort to combine forces on a universal package.
- I have a Python package that I'm still updating fairly regularly called apwlib that contains various Python routines that you might find useful. Check it out, and ask me if you have any questions / suggestions / bugs!
- Adrian
Quick Start Guide (for IDL users)
How to open the door:
Once you have Python installed, you can run an interactive session by invoking "python" at the command line.[br] (This is equivalent to writing "IDL" on the command line.)[br]
% python
You will very often need to activate different packages for your python session. Numpy is one that you will probably need often.
import numpy
Now each time you need to use something from numpy, you will invoke that package:
x = numpy.array([1,2,3,4])
You can also give the package a nickname for use within your session, for example:
import numpy as np
x = np.array([1,2,3,4])
When you want to quit python, press Ctrl+D or:
exit()
Now let's say you want to run the equivalent of a "batch" file. In IDL you would do the following:
% IDL
IDL> @my_batchfile.batch
In python, you would create a file containing everything you would run from the command line (including "import numpy" etc etc). Then from the command line, you can enter:
% python -i my_pyfile.py
The code will compile and open an "interactive" session. You can access and play with all the variables you created in my_pyfile.py.
This is how I do it! Please add your own tips and tricks. (Added by lia@…)
Note that unlike IDL, Python's instantiation of lists and arrays is by reference rather than by value. For people who have used IDL for many moons, this may cause headaches. The workarounds are worth taking some time to consider carefully: read this for more.
Another difference between IDL and Python, in the interactive mode, is that once you load a package or a module it stays loaded in the form you first indicated even if you say
import <blah>
all over again. This is unlike in IDL where if you say
IDL>.run <blah>
it will reload all of your program, capturing any changes you made.
To "reload" a package in interactive Python you, somewhat unsurprisingly, use:
reload(<blah>)
But be warned that this only works for the simplest packages. If you change a python module that is called by any other module, you will have to reload all the modules in the appropriate order, which can be quite the headache. For more on this see this stackoverflow question.
Astropy
A nice bundle of stuff that astronomers like to do can be found with astropy
