Monday, February 25, 2013

Importing large .csv files via sqlite3 command line interface

If they are too big to import via the GUI manager, that's one way to go (I created a database of emission line data):
CREATE TABLE "elines" ("CALIFA_id" text,"morph" text,"bar"
text,"radius" float,"xpos" float,"ypos" float,"Hbeta_flux"
float,"Hbeta_eflux_float" float,"Hgamma_flux_float"
float,"Hgamma_eflux" float,"Hdelta_flux" float,"Hdelta_eflux"
float,"Halpha_flux" float,"Halpha_eflux" float,"OIII5007_flux"
float,"OIII5007_eflux" float,"OI6300_flux" float,"OI6300_eflux"
float,"NII6584_flux" float,"NII6584_eflux" float,"SII6717_flux"
float,"SII6717_eflux" float,"SII6730_flux" float,"SII6730_eflux"
float,"vel" float,"evel" float,"disp" float,"edisp" float,"Ha_cont"

.separator ","

.nullvalue -999

.import elines.csv elines

Thursday, February 21, 2013

Python string to boolean

I wanted to pass True/False as boolean values from the command line, this is a nice way to do it.

Tuesday, February 19, 2013

Centering and clipping image, sigma-clipping (ds9 like) in Matplotlib

I wanted to cut out the galaxies I ran Galfit on, then clip the pixel values and save to an image (which is picked up by LaTeX). Here's the source.

Matplotlib: strings as axis labels

I wanted to make a plot that shows the relation of some galaxy properties vs. their Hubble subtype. The subtypes were saved as strings at my database, I selected them, converted back to numbers. Why back? Because some colleagues of mine, who think in words, classified all the 900 galaxies by their Hubble types, then their classifications were converted into numbers, means and probabilities calculated, and converted back to types.
Anyway, I wanted to have types on my axis, so I did it thus. Here's the plot:

Matplotlib: colorbar too large

If the colorbar is too big, setting the 'shrink' property helps:

  p = ax2.imshow(image,, interpolation='nearest')
  cb = plt.colorbar(p, shrink=0.455)

Monday, February 18, 2013

Parallel processing in Python

I just wrote a series of scripts that run batch GALFIT fits. Since it's boring to parallelise this manually (e.g. splitting the list of input ID's into smaller lists, then running same script separately for each sublist, or setting the chunks of IDs from command line), I wrote a simple multithreading routine using multiprocessing. It's quite trivial, as the parallel processes here don't have to communicate (unless I see some terrible race conditions arising). The ID lists are made by hand, but it'll be easy to split the list into smaller chunks of desired length by looping through it, or by using list comprehension.

ds9: an alias for multi-extension data cubes

Since I chronically cannot remember the command-line options to ds9, I added this to my ~/.bashrc: alias meds9=' -scale mode 99.5 -cmap Heat -medatacube'

Friday, February 15, 2013

lit: Intrinsic properties of SDSS galaxies

Maller et al.. It's important in many other ways, but here is an interesting excerpt on why TF relation is different:
However, with the notable exception of the Tully-Fisher relation these distributions and relations are traditionally measured in terms of the observed properties of galaxies. That is, the measurements used are K-corrected and corrected for foreground dust extinction, but no correction is attempted to compensate for the viewing angle from which the galaxies are observed. In contrast, the Tully-Fisher relation is not a relationship between a galaxy's observed luminosity and rotation velocity, but a relation between a galaxy luminosity and rotation velocity corrected for inclination. The inclination correction attempts to recover the intrinsic properties of a galaxy and not properties that are measured because of the particular angle from which the galaxy is viewed. For a long time there was great controversy over whether spiral galaxies are optically thin or optically thick (e.g., Holmberg 1958; Disney et al. 1989), but in the early 1990s it was established that galaxies become redder and fainter as they are inclined (e.g., Burstein et al. 1991).

Wednesday, February 13, 2013

Pyfits: quickly overwriting a .fits header

I had some .fits files that lost their header information during analysis. Here's a quick kludge I coded up to retrieve the original header and overwrite it for a single file.

import pyfits 
import sys

fileName = sys.argv[1]

originalFile = "../data/SDSS/r/"+fileName[:-4]+"fit.gz"
OriginalHDUList =
OriginalHeader = OriginalHDUList[0].header

HDUList =, mode = 'update')
HDUList[0].header = OriginalHeader
print HDUList[0].header

Monday, February 11, 2013

Thursday, February 7, 2013

SDSS: a query for clean photometry galaxies with redshifts

SELECT g.objid, g.ra, g.dec, g.petroMag_u, g.petroMag_g, g.petromag_r, g.petromag_i, g.petromag_z, s.z, g.petroR50_r, g.petroR90_r, g.rho, g.isoA_r, g.isoB_r, g.isophi_r,
dbo.fPhotoFlagsN(flags) as flags into mydb.galaxies from Galaxy as G
   JOIN SpecObj as S ON G.objId=S.bestObjId 
WHERE ((flags & 0x10000000) != 0)       
AND ((flags & 0x8100000c00a0) = 0)     
AND (((flags & 0x400000000000) = 0) or (psfmagerr_g <= 0.2))
AND (((flags & 0x100000000000) = 0) or (flags & 0x1000) = 0)

Monday, February 4, 2013

lit review: The shape of the dark matter halo in the early-type galaxy NGC 2974

The shape of the dark matter halo in the early-type galaxy NGC 2974, by Weijmans, Krajnovic et al. They use kinemetry to analyse gas and stellar kinematics, with clear description of the AD correction. (Thanks, Mariya!)

lit review: a generalization of photometry to the higher moments of the line-of-sight velocity distribution

by Krajnovic et al.. This will probably be one of the important papers when writing: an interesting breakdown and discussion of velocity map moments, also many useful references (esp. Schoenmakers et al. (1997), the degeneracy of q, center and systemic velocity v0.
We have presented a generalization of surface photometry to the higher-order moments of the LOSVD, observed with integral-field spectrograph. We call our method kinemetry. For even moments of the LOSVD, kinemetry reduces to photometry. For odd moments, kinemetry is based on the assumption that it is possible to define an ellipse such that the kinematic profile extracted along the ellipse can be well described by a simple cosine law. This assumption is satisfied in the case of simple axisymmetric rotators.