-
Notifications
You must be signed in to change notification settings - Fork 0
/
jose.py
executable file
·100 lines (79 loc) · 3.13 KB
/
jose.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
#!/usr/bin/env python3
import numpy as np
import logging
import yaml
import datetime
import sys
import argparse
import os
import astropy.io.fits as fits
import jose
log = logging.getLogger(__name__)
# TODO: should this copy the data?
def write_output_files(extraction, resdir):
r'''Writes results of extraction to resdir
Parameters
----------
extraction : jose.Extraction
Extraction object resulting from running jose
targetDir : str
Name of directory
Notes
-----
Writes out relevant data from `extraction` object as FITS files
and .npy Numpy files. Puts copy of configuration used under config.yaml
and human readable summary under results.txt.
'''
# results.txt
np.savetxt(os.path.join(resdir, 'spec.txt'), extraction.optimal_spectrum)
def write_figures(extraction, targetDir):
r'''docstrng'''
f, ax = extraction.make_spectrum_figure()
f.savefig(os.path.join(targetDir, 'spectrum.png'))
# TODO: output image, fitted profile, other useful stuff, inspired by IDL plottype flags from other code
def create_template(directory, filename):
r'''Creates template YAML config file in specified directory'''
raise NotImplementedError()
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Extract spectral data from FITS image")
parser.add_argument('-o' , "--output-dir",
help="Directory to output results, defaults to current working directory")
configFileGroup = parser.add_mutually_exclusive_group(required=True)
configFileGroup.add_argument('-c', "--config",
help="Configuration file with extraction options")
configFileGroup.add_argument('-r', "--regenerate-config",
help="Creates a template config file with the specified name")
args = parser.parse_args()
#TODO: create process args function to clean up main
if args.output_dir == None: # default to cwd
output_dir = os.getcwd();
else:
output_dir = args.output_dir
if not args.regenerate_config == None: # create standard template file
create_template(output_dir, args.regenerate_config)
sys.exit()
# read in config file
with open(args.config, 'r') as ymlfile:
cfg = yaml.load(ymlfile, Loader=yaml.FullLoader)
# object bounds need to be supplied by the user
# require output directory
hdulist = fits.open(cfg['data'])
outputdir = os.path.join(os.getcwd(), cfg['outdir'])
if not os.path.isdir(outputdir):
os.makedirs('output')
try:
fnum = cfg['fnum']
except KeyError:
fnum = 0
extract = jose.extraction.Extraction(hdulist[fnum], cfg)
extract.calculate_extraction()
# get output from extract and write to file depending on arguments
# write images, figures, data, print info to user
print('Extraction complete.')
resdir = os.path.join(outputdir,
datetime.datetime.now().strftime('%Y-%m-%d_%H-%M-%S')+
'-' + cfg['label'])
os.makedirs(resdir)
write_output_files(extract, resdir)
write_figures(extract, resdir)
extract.save(os.path.join(resdir, 'extract.obj'))