-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMockFit.py
More file actions
105 lines (102 loc) · 4.05 KB
/
MockFit.py
File metadata and controls
105 lines (102 loc) · 4.05 KB
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
import os
import gc
import sys
import gsf
import warnings
import traceback
import importlib
import numpy as np
import rel_SED_Toolkit as sedt
from optparse import OptionParser
from astropy.table import Table
#Include the config directory#
#----------------------------#
if os.path.isdir("configs"):
sys.path.append("configs/")
#Parse the commands#
#-------------------#
parser = OptionParser()
parser.add_option("-l", "--list", dest="list", default=None, metavar="FILE",
help="Provide a list of target info to fit.")
parser.add_option("-n", "--usename", dest="usename",
action="store_true", default=False,
help="Try to find the config file specified with the target name.")
parser.add_option("-r", "--refit", dest="refit",
action="store_true", default=False,
help="Refit the SED though there is a result found.")
parser.add_option("-w", "--warning", dest="warning",
action="store_true", default=False,
help="Stop ignoring the warnings.")
(options, args) = parser.parse_args()
if len(args) == 0:
raise AssertionError("The config file is not specified!")
#Some times the warning may stop the code, so we ignore the warnings by default.
if options.warning:
pass
else:
warnings.simplefilter("ignore")
configName = args[0]
config = importlib.import_module(configName.split("/")[-1].split(".")[0])
targetList = options.list
if targetList is None: #->If the target list is not provided, only fit one target according to the config file.
targname = config.targname
redshift = config.redshift
sedFile = config.sedFile
sedPck = sedt.Load_SED(sedFile, config.sedRng, config.spcRng, config.spcRebin)
print("#--------------------------------#")
print("Target: {0}".format(targname))
print("SED file: {0}".format(sedFile))
print("Config file: {0}".format(configName))
print("#--------------------------------#")
with open(sedFile, "r") as f:
linesT = f.readlines()
exec linesT[-1][1:]
config.parTruth = inputPars
#print "parTruth:"
#print config.parTruth
gsf.fitter(targname, redshift, sedPck, config)
else: #->If the target list is provided, fit the targets one by one.
if len(args) == 2:
sedPath = args[1]
else:
sedPath = ""
targTable = Table.read(targetList , format="ascii.ipac")
nameList = targTable["Name"]
zList = targTable["z"]
sedList = targTable["sed"]
print("\n***There are {0} targets to fit!\n".format(len(nameList)))
for loop in range(len(nameList)):
targname = nameList[loop]
redshift = zList[loop]
sedname = sedList[loop]
if not options.refit: #Omit the target if there is a fitting result.
fileList = os.listdir(".")
if "{0}_bestfit.txt".format(targname) in fileList:
print("\n***{0} has been fitted!\n".format(targname))
continue
if options.usename: #Try to use the config file of the target itself.
fileList = os.listdir(".")
configTry = "config_{0}.py".format(targname)
if configTry in fileList:
configName = configTry
sedFile = sedPath + sedname
sedPck = sedt.Load_SED(sedFile, config.sedRng, config.spcRng, config.spcRebin)
print("#--------------------------------#")
print("Target: {0}".format(targname))
print("SED file: {0}".format(sedFile))
print("Config file: {0}".format(configName))
print("#--------------------------------#")
with open(sedFile, "r") as f:
linesT = f.readlines()
exec linesT[-1][1:]
config.parTruth = inputPars
#print "parTruth:"
#print config.parTruth
try:
gsf.fitter(targname, redshift, sedPck, config)
except:
print("\n---------------------------")
print("***Fitting {0} is failed!".format(targname))
traceback.print_exc()
print("---------------------------")
gc.collect()