SDSS fits cutouts

Since quite some time I have been trying to get image cutouts from the SDSS in fits format. The jpg images in the cutout service are ok for simple tasks but nothing serious. Recently a colleague told me about Montage, a great tool to automate fits cutout, mosaic generation, coverage masks etc. Since Montage doesn’t include a script to retrieve SDSS cutouts in a simple way (there is actually one but I could not make it run) I wrote the following bash script based on Inseok Song’s pleiades script.

You need to install Montage first and add Montage’s bin folder to your PATH

Download and install Montage:

Add Montage’s bin folder to your path, this can be done in your .bashrc file. My Montage installation is in /home/miguel/Packages/Montage :

export PATH="/home/miguel/Packages/Montage/bin:$PATH"


This is the fits cutout script:


# Cutout fits images from SDSS
# Adapted from Inseok Song, 2007
# # Arguments:
# $1 ra
# $2 dec
# $3 size
# $4 base_name
# Usage: sh 199.54198 -1.2436732 0.05 gal_000
# Miguel Aragon-Calvo, 2017
echo ra $1
echo dec $2
echo size $3
echo file $4

#--- Working directory
mkdir $4;
cd $4;

mHdr -p 0.4 "\"$1 $2\"" $3 $4.hdr

for bands in u g r i z; do echo Processing ${bands};
mkdir $bands;
cd $bands;
mkdir raw projected;
cd raw;
mArchiveList sdss ${bands} "\"$1 $2\"" $3 $3 remote.tbl;
mArchiveExec remote.tbl;
cd .. ;
mImgtbl raw rimages.tbl ;
mProjExec -p raw rimages.tbl ../$4.hdr projected stats.tbl ;
mImgtbl projected pimages.tbl ;
mAdd -p projected pimages.tbl ../$4.hdr ${bands}.fits ;
cd .. ;
mv ${bands}/${bands}.fits .
rm -rf ${bands}

cd .. ;
mJPEG -blue $4/g.fits -1s "99.999%" gaussian-log -green $4/r.fits -1s "99.999%" gaussian-log -red $4/i.fits -1s "99.999%" gaussian-log -out $4.jpg

For instance, to retrieve and generate fits images for all SDSS filters (u,g,r,i,z) centered at ra=19.54198, dec=-1.2436732 (the galaxy at the top of this post) with 0.05 degrees of size we call the script as follows:

$ sh 199.54198 -1.2436732 0.05 gal_000

The final fits files can be found in the folder gal_000 as gal_000/u.fits, gal_000/g.fits, etc.

The (commented) last line produces a jpg image using logarithmic scaling. The asinh scaling ( is a better option.

Note that Montage will retrieve fits of complete sdss fields, probably more than one file per band. From these files it will cut the desired image. In the case of (small) individual galaxies this can be very  IO wasteful. For the galaxy called above Montage will download ~400MB of sdss field fits files in order to generate 5 fits files with 1.6MB each. The script automatically deletes the intermediate files.




Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s