1
2
3 """
4 Interface for creating plotting environments.
5
6 Author: R. Lombaert
7
8 """
9
10 import os
11 from time import gmtime
12 import cPickle
13 import subprocess
14
15 import cc.path
16 from cc.tools.io import DataIO
17
18
19
21
22 """
23 Plotting environment for data and models of circumstellar shells.
24
25 """
26
27 - def __init__(self,star_name='model',inputfilename=None,\
28 path='',code='GASTRoNOoM',fn_add_star=1):
29
30 """
31 Initializing an instance of PlottingSession.
32
33 @keyword star_name: name of the star from Star.dat, use default only
34 when never using any star model specific things
35
36 (default: "model")
37 @type star_name: string
38 @keyword path: Output modeling folder in code home folder
39
40 (default: '')
41 @type path: string
42 @keyword inputfilename: name of inputfile that is also copied to the
43 output folder of the plots,
44 if None nothing is copied
45
46 (default: None)
47 @type inputfilename: string
48 @keyword code: the modeling code
49
50 (default: GASTRoNOoM)
51 @type code: string
52 @keyword fn_add_star: Add the star name to the requested plot filename.
53 Only relevant if fn_plt is given in a sub method.
54
55 (default: 1)
56 @type fn_add_star: bool
57
58 """
59
60 self.inputfilename = inputfilename
61 self.star_name = star_name
62 self.star_index = DataIO.getInputData(path=cc.path.usr).index(star_name)
63 self.star_name_plots = DataIO.getInputData(path=cc.path.usr,
64 keyword='STAR_NAME_PLOTS',\
65 remove_underscore=1,\
66 rindex=self.star_index)
67
68
69
70
71 if not path:
72 print('Warning! %s model output folder not set.'%code)
73 self.path = path
74 fn_mcm = os.path.join(cc.path.aux,'Mutable_Parameters_MCMax.dat')
75 self.mutable_mcmax = [line[0]
76 for line in DataIO.readFile(fn_mcm,delimiter=' ')
77 if ''.join(line).strip()]
78 self.mutable_mcmax = [line
79 for line in self.mutable_mcmax
80 if line[0] != '#']
81 fn_gas = os.path.join(cc.path.aux,'Mutable_Parameters_GASTRoNOoM.dat')
82 self.mutable_gastronoom = [line[0]
83 for line in DataIO.readFile(fn_gas,\
84 delimiter=' ')
85 if ''.join(line).strip()]
86 self.mutable_gastronoom = [line
87 for line in self.mutable_gastronoom
88 if line[0] != '#']
89 self.mutable = self.mutable_mcmax + self.mutable_gastronoom
90 self.plot_id = 'plot_%.4i-%.2i-%.2ih%.2i-%.2i-%.2i' \
91 %(gmtime()[0],gmtime()[1],gmtime()[2],\
92 gmtime()[3],gmtime()[4],gmtime()[5])
93 self.code = code
94
95
96 pout = os.path.join(getattr(cc.path,self.code.lower()),self.path,\
97 'stars')
98 pstar = os.path.join(pout,self.star_name)
99 self.pplot = os.path.join(pstar,self.plot_id)
100 for pp in [pout,pstar]:
101 DataIO.testFolderExistence(pp)
102
103 self.fn_add_star = fn_add_star
104
105
106
108
109 """
110 Check which parameters change throughout the modeling in the mutable
111 list.
112
113 @param star_grid: The parameter sets
114 @type star_grid: list[Star()]
115
116 @return: parameters in the mutable list that change
117 @rtype: list[string]
118
119 """
120
121 all_values = [[par.find('FILE')==-1 \
122 and (star[par],) \
123 or (bool(star[par]),)
124 for star in star_grid]
125 for par in self.mutable ]
126 return [self.mutable[par_index]
127 for par_index,value_list in enumerate(all_values)
128 if len(set(value_list)) > 1]
129
130
131
132
133
134
136
137 '''
138 Return a list of model id's in the star_grid.
139
140 @param star_grid: The parameter sets
141 @type star_grid: list[Star()]
142 @param id_type: the type of model id (MCMAX or GASTRONOOM or PACS)
143 @type id_type: string
144
145 @return: the model_ids
146 @rtype: list[string]
147
148 '''
149
150 return [star['LAST_'+id_type.upper()+'_MODEL']
151 for star in star_grid
152 if star['LAST_'+id_type.upper()+'_MODEL']]
153
154
155
156 - def setFnPlt(self,fn_plt,fn_suffix='',fn_subfolder=''):
157
158 '''
159 Create a plot filename. The base filename (fn_plt) defines which of 3
160 cases is requested:
161
162 1) path is given, and the object has fn_add_star set to 1.
163 2) path is given, and the object has fn_add_star set to 0.
164 3) path is not given, in which case the star name is never added (it
165 is included in the default folder name)
166
167 In all cases, a base filename must be given, and a suffix can be defined
168 in addition.
169
170 Returns empty string if fn_plt is not defined or empty.
171
172 @param fn_plt: A plot filename that can be given to each plotting sub
173 method, or through the cfg file (managed by the sub
174 method). If not given, each sub method defines a
175 default. This includes the path if required.
176 @type fn_plt: string
177 @keyword fn_suffix: An optional suffix to be appended to the filename
178
179 (default: '')
180 @type fn_suffix: str
181 @keyword fn_subfolder: An additional subfolder in the default self.pplot
182 can be added. Not used when path is included in
183 fn_plt.
184
185 (default: '')
186 @type fn_subfolder: str
187
188 @return: The full path + filename without extension is returned.
189 @rtype: str
190
191 '''
192
193 if not fn_plt: return ''
194
195
196 path, pfn = os.path.split(fn_plt)
197 pfn = os.path.splitext(fn_plt)[0]
198
199
200 if path and self.fn_add_star: pfn = '_'.join([pfn,self.star_name])
201 if fn_suffix: pfn = '_'.join([pfn,fn_suffix])
202
203
204 if not path:
205 DataIO.testFolderExistence(self.pplot)
206 if not self.inputfilename is None:
207 ipfn = os.path.split(self.inputfilename)[1]
208 newf = os.path.join(self.pplot,ipfn)
209 if not os.path.isfile(newf):
210 subprocess.call([' '.join(['cp',self.inputfilename,newf])],\
211 shell=True)
212
213
214 if not path and fn_subfolder:
215 path = os.path.join(self.pplot,fn_subfolder)
216 DataIO.testFolderExistence(path)
217 elif not path:
218 path = self.pplot
219
220 return os.path.join(path,pfn)
221