Source Extractor from within Python - UMKCgeg/Wiki GitHub Wiki
Source Extractor is a great tool to use for doing photometry. If you want to run SE on many images, though, it can be a hassle. If you are familiar with Python, you can make a wrapper to run SE from within Python.
First, we need to get the location of the SE executable. From a terminal, run which sex
. This will give the path of the executable. You also need to know the directory that has all your SE configuration files, like .nnw
and .conv
.
This code snippet assumes that your catalogs and images are somewhere other than SE directory, and that you want the output catalog in that same directory. So after it runs, the file structure will look something like this...
SE_files/
default.nnw
default.conv
... a bunch of other files ...
your_directory/
run_se.py
config.sex
config.param
image.fits
catalog.cat
This is the cleanest way to have all your files organized, so the SE_files/
doesn't get cluttered with every catalog imaginable.
If your setup is like this, we can then put things in a script.
import os
import subprocess
# store our original location, so we can go back later
original_dir = os.getcwd()
# define locations of files and executables
se_executable = "output of `which sex`"
sex_file = os.path.abspath("config.sex") # use abspath to transform relative local path to absolute ones SE can handle.
param_file = os.path.abspath("config.param")
se_dir = os.path.abspath("place with SE configuration files")
image = os.path.abspath("image.fits")
output_cat_name = os.path.abspath("catalog.cat")
# move to the directory with all the SE configuration files
os.chdir(se_dir)
# then we can run SE
p = subprocess.Popen([se_executable, image, "-c", sex_file, "-PARAMETERS_NAME", param_file,
"-CATALOG_NAME", output_cat_name], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate()
print(err) # comment out if you don't care about what SE prints.
# then go back to the directory we started in
os.chdir(original_dir)
You'll notice that the os.path.abspath()
turns the local file references (like "image.fits"
) into absolute paths that will also work in the SE directory. You can just specify absolute paths if you want, though. The code moves to the SE directory, so it can find the config files, then runs SE from there. It will still write the catalog to the location in the original directory, though. Then it goes back to the directory you started in. It does this so that if you want to write more in the script to mess with your catalogs, you can.
The actual code to run SE is a little messy. The subprocess.Popen()
function takes a list of command line arguments. If you were to type them yourself on the command line there would just be spaces between everything, but this command wants them all in a list. You can put any flags or any other modifiers there other than the example above. The stdout
and stderr
control where things are printed to. If you don't want to see what SE prints, you can comment out the print statement above. When you are running many things at once, that output can be overwhelming. Check out the documentation for the subprocess module if you want more info.
The code above runs with just one image, but is easy to modify to run on multiple images.