-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsextractor_engine.py
executable file
·153 lines (122 loc) · 7.3 KB
/
sextractor_engine.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/usr/bin/env python
#import subprocess
#import sys
#import time
import os
import copy
#import utils
#from defaults import *
class DEFAULT:
IMAGE = 'NONE'
WEIGHT = 'NONE'
CHECK = 'NONE'
CHECK_NAME = 'NONE'
WORKING_DIR = '/'.join(os.path.realpath(__file__).split('/')[:-1])
ASTRO_DIR = os.path.join(WORKING_DIR, 'astro_config')
SEX_CONFIG = os.path.join(ASTRO_DIR, 'sextractor.config')
SEX_PARAM = os.path.join(ASTRO_DIR, 'rrg.param')
SEX_NNW = os.path.join(ASTRO_DIR, 'default.nnw')
SEX_CONV = os.path.join(ASTRO_DIR, 'gauss_1.5_3x3.conv')
class SextractorEngine():
"""Overview of SextractorEngine class:
This is an object for running sextractor. Most importantly, it contains a dictionary of the configuration parameters given to sextractor.
This dictionary is named SextractorEngine().config. Editing the dictionary directly is one way to change the sextractor configuration.
Values in the dictionary override those specified in the config file itself. A default call to SextractorEngine will load default
files for the sextractor config, param, nnw, and filter filters. These defaults are located in the astro_config directory packed with
this file.
Technically SextractorEngine() can be called with no arguments, but the sextractor run will fail if you do not supply an image and a weight file.
The fields that can be supplied in the constructor are the sextractor config parameters which take file paths (and CHECKIMAGE_TYPE, which is related to CHECKIMAGE_NAMES).
IMAGE
WEIGHT_IMAGE
CATALOG_NAME
c
PARAMETERS_NAME
STARNNW_NAME
FILTER_NAME
CHECKIMAGE_TYPE
CHECKIMAGE_NAME
However, any other parameters available to configure sextractor can also be modified simply by adding them to the object's config dictionary.
The image(s) to give to sextractor are normally only given on the command line (i.e. not in the config file). The dictionary keyword for it here is 'IMAGE'.
When editing the dictionary do not use any -'s in the keywords.
For example when running 'sex image.fits -c sex.config -WEIGHT_IMAGE weight.fits',
you would have SE.config['c'] = 'sex.config', SE.config['IMAGE'] = image.fits, SE.config['WEIGHT_IMAGE'] in the config dictionary
In the sextractor documenation sextractor keyword, value pairs in the config file are primarily upper case. I stuck with the same case conventions as sextractor,
so formatting should be the same as in the sextractor documentation (even though this can be slightly annoying to type).
Methods of SextractorEngine:
Use the run() method to run sextractor. The optional logfile argument is where to write the sextractor logging output. By default, it goes
to the same directory as the catalog, stripping off .fits type extensions and appending _log.txt
auto_checkimage_name() is a convenience function for automatically populating the CHECKIMAGE_NAME field, based on the CHECKIMAGE_TYPE and current
image or catalog name. By default the output directory for the check files is the same as the catalogs, and the check files are named with the same
base naming scheme as the image, but with a _IMAGETYPE appended to the file, e.g. image_BACKGROUND.fits. The optional named_by argument can either be
'image' or 'catalog' to choose if the base name is from the image or catalog file. The optional dir argument changes the output directory for
check files. It can be a single directory or a list the same length as the number of check files.
A few notes:
If you change some config parameters in the dictionary, and then change the config file referenced in the dictionary, the values in the dictionary will still take
precedence over those in the (new) file. Anything in the dictionary will become an explicit command line argument given to sextractor.
To avoid this, if you would like to start fresh, with a new config file, call the constructor, e.g.
SE = SextractorEngine( IMAGE=image.fits, WEIGHT_IMAGE=weight.fits, c=new_sex.config )
where you could also specificy different params file, etc. if you wanted.
Currently I do not have a way for changing the params (which will be output in the catalog), other than chaning the 'PARAMTERS_NAME' keyword. I may add another way.
"""
# You should not need to used any of these functions. They are things which helping under the hood.
def _catalog_name(self, image, catalog_name):
if catalog_name is None:
self.config['CATALOG_NAME'] = os.path.join(
os.getcwd(), image.strip().split('/')[-1].replace('.fits', '.cat.fits'))
else:
self.config['CATALOG_NAME'] = catalog_name
def _checkimage(self, checkimage_type, checkimage_name):
if checkimage_type != 'NONE':
self.config['CHECKIMAGE_TYPE'] = checkimage_type
if checkimage_name != 'NONE':
self.config['CHECKIMAGE_NAME'] = checkimage_name
else:
self.auto_checkimage_name()
# 3
def __init__(self, IMAGE=DEFAULT.IMAGE, WEIGHT_IMAGE=DEFAULT.WEIGHT, CATALOG_NAME=None, c=DEFAULT.SEX_CONFIG, PARAMETERS_NAME=DEFAULT.SEX_PARAM, STARNNW_NAME=DEFAULT.SEX_NNW, FILTER_NAME=DEFAULT.SEX_CONV, CHECKIMAGE_TYPE=DEFAULT.CHECK, CHECKIMAGE_NAME=DEFAULT.CHECK_NAME, setup=None):
# print locals()
self.config = {}
self.config['IMAGE'] = IMAGE
#self.config['WEIGHT_IMAGE'] = WEIGHT_IMAGE
self.config['c'] = c
self.config['PARAMETERS_NAME'] = PARAMETERS_NAME
self.config['STARNNW_NAME'] = STARNNW_NAME
self.config['FILTER_NAME'] = FILTER_NAME
self._catalog_name(IMAGE, CATALOG_NAME)
self._checkimage(CHECKIMAGE_TYPE, CHECKIMAGE_NAME)
self.path = 'sex'
self.setup = setup
def Path(self, path):
self.path = path
def auto_checkimage_name(self, dir=None, named_by='image'):
tlist = self.config['CHECKIMAGE_TYPE'].split(',')
for i in range(len(tlist)):
tlist[i] = '_' + tlist[i].strip().replace('-', 'm') + '.fits'
stripped_cat = self._strip('CATALOG_NAME', ['.cat.fits', '.fits'])[-1]
stripped_image = self._strip('IMAGE', ['.fits']).split('/')[-1]
cat_dir = '/'.join(self.config['CATALOG_NAME'].split('/')[:-1])
if named_by == 'image':
stripped = stripped_image
elif named_by == 'catalog':
stripped = stripped_cat
for i in range(len(tlist)):
file = stripped + tlist[i]
if dir is None:
d = cat_dir
elif type(dir) == str:
d = dir
elif type(dir) == list:
d = dir[i]
tlist[i] = os.path.join(d, file)
nstr = ','.join(tlist)
self.config['CHECKIMAGE_NAME'] = nstr
def run(self, msg=None):
args = [self.path, self.config['IMAGE']]
for key in list(self.config.keys()):
if key == 'IMAGE':
continue
else:
args.append('-%s' % key)
args.append(str(self.config[key]))
cmd = ' '.join(args)
os.system(cmd)