Stopping a script for debugging

Dylan Gregersen brings us another nice code snippet which might be especially welcome for IDL users who miss the STOP procedure:Here's a snippet I'm using often while writing scripts, executing via run -i <file> or execfile(<file>), and I want to quickly truncate the code so that the remaining part... View More

Convert IPython notebook to ApJ or A&A article

This guest post was contributed by Moritz Guenther:When I first encountered the IPython notebook, I thought this was a solution looking for a problem. However, I have since been converted! The tipping point for me was this: I want to version control my papers and I always had multiple directories for analysis code, plotting code, LaT... View More

Installing scientific Python on Fedora

As part of a recent Python for Astronomers workshop on installation, Jon Chappell created a nice bash script to do a root install of scientific Python that works on recent Fedora machines.  You might find it useful!#!/bin/bash ### This script installs a number of base and addon packages to create a # scientific python distribution... View More

Skipping inconsistent rows in asciitable

The asciitable module provides a way to deal with tables that have one or more lines which don't match the format of the rest of the file.  This is done by overriding the asciitable.BaseReader.inconsistent_handler function with your own custom function.  The very simplest action is to just ignore the line entirely by returning No... View More

Fix the WCS for a FITS image file

The code snippet which follows will allow you to fix an error in the astrometry of a FITS image.  This is especially common with HST images because the astrometrical errors can be an arcsecond or more.  The method used assumes that you have extracted the positions (RA, Dec) for at least 2 image point sources for which you... View More

Fast fractals with python and numpy: Mandelbrot sets

Source: http://thesamovar.wordpress.com/2009/03/22/fast-fractals-with-python-and-numpy/ # Python code heredef mandel(n, m, itermax, xmin, xmax, ymin, ymax): ix, iy = mgrid[0:n, 0:m] x = linspace(xmin, xmax, n)[ix] y = linspace(ymin, ymax, m)[iy] c = x+complex(0,1)*y del x, y img = zeros(c.shape, dtype=int) ix.shape = n*... View More

Basic Sherpa SED Fitting

This code snippet goes through the basic steps of importing and fitting a spectral energy distribution with Sherpa.  The data were saved from NED in XML (VO Table) format and are imported into python using the vo module.  After a little data processing, the SED is fit with a simple 2 power-law function. The script uses Sherpa's i... View More

Interpolation without SciPy

This code snippet shows a simple way to do linear or nearest-neighbor interpolation using only NumPy.  This is handy if you don't have SciPy installed or don't want to introduce a dependency on SciPy in distributed code.  By using the numpy.searchsorted() method and vectorized operations it is reasonably fast, though I have n... View More

Fast Lomb-Scargle algorithm

Astropython.org visitor "Morgan" contributed a Python implementation of Lomb-Scargle via the comments to [Question] period-finding packages in python.  This script is based on:    Press, W. H. & Rybicki, G. B. 1989    ApJ vol. 338, p. 277-280.    Fast algorithm for spectral analysis of unevenly sampled data    bib c... View More

Save sherpa fit and conf results to a JSON file

After doing a fit with Sherpa you may want to dump all the results to a file for later processing or inspection.  The following snippet writes the results from model fitting and confidence estimation to a file using the JSON format for data serialization.  This format is widely supported in other programming languages and... View More

Parallel map using multiprocessing

This module (courtesy Brian Refsdal at SAO) implements a parallelized version of the native Python map function that utilizes the Python multiprocessing module to divide and conquer an iterable.  It takes advantage of the nifty NumPy function array_split() to divide an input iterable into approximately equal chunks.  It a... View More

Easier python logging

The python logging module is a very good option for producing output in most code and especially in modules that will distributed.  It is incredibly flexible but that comes with the price of a steep learning curve.  The developers recognized this and included a one-stop method logging.basicConfig() to set up a basic logger that will ... View More

chdir context manager

This context manager restores the value of the current working directory (cwd) after the enclosed code block completes or raises an exception.  If a directory name is supplied to the context manager then the cwd is changed prior to running the code block.  from __future__ import with_statement # only for python 2.5 impor... View More