1 | #!/usr/bin/env python |
---|
2 | |
---|
3 | |
---|
4 | ####################################### |
---|
5 | # |
---|
6 | # Projet Manager XMF utilities |
---|
7 | # |
---|
8 | ######################################## |
---|
9 | # This class allows to handle a cluster of h5/xmf files |
---|
10 | # for visualization purpose, through ensight and paraview. |
---|
11 | # this clas is originally used by common_visualize.py. |
---|
12 | # |
---|
13 | # Created by A. Dauptain |
---|
14 | # |
---|
15 | # Last modified : 16 septembre 2013 A Dauptain (comments) |
---|
16 | # |
---|
17 | ######################################## |
---|
18 | |
---|
19 | |
---|
20 | import os |
---|
21 | from XDR import * |
---|
22 | |
---|
23 | |
---|
24 | class XMF_Utilities(object): |
---|
25 | def __init__(self,code_id): |
---|
26 | self.code = code_id |
---|
27 | self.post_proc_dir = os.path.join(os.getcwd(),code_id.upper(),"SPATIAL") |
---|
28 | self.filename = os.path.join(self.post_proc_dir,"c3sm_xdmfgroup.xmf") |
---|
29 | |
---|
30 | self.filelocation = [] |
---|
31 | if self.code == "avbp" : |
---|
32 | self.filelocation = ["SOLUT"] |
---|
33 | if self.code == "avtp" : |
---|
34 | self.filelocation = ["SOLUT"] |
---|
35 | |
---|
36 | self.time_id_list = [0.0] |
---|
37 | self.faketime = False |
---|
38 | if self.code == "avsp" : |
---|
39 | self.faketime = True |
---|
40 | |
---|
41 | |
---|
42 | # cleaning |
---|
43 | for f in getFileList(self.post_proc_dir,"*.xmf","relative") : |
---|
44 | print "Cleaning file :",f |
---|
45 | os.remove(f) |
---|
46 | |
---|
47 | |
---|
48 | def CreateTemporaryXMFs(self,selection_files,projectLocations): |
---|
49 | """ This procedure create the main XMF solution file according to |
---|
50 | post_proc_dir is a string of the form AVSP, AVBP or YALES2, |
---|
51 | the data will be generated in the relative path ./AVSP/SPATIAL for exemple |
---|
52 | |
---|
53 | selection_files is a list of the form [file1],[0],[file2],[1],[file3],[1],[file6],[0] |
---|
54 | with files of the form "identifier # simulation # projectname # name " |
---|
55 | |
---|
56 | projectLocations is a dictionnary with project names as keys, and project relative pathes as values |
---|
57 | created smthg similar to: |
---|
58 | projectLocations={} |
---|
59 | for sc_project in getChildrenName("projects","allprojects"): |
---|
60 | project_xml=getValue("sc_project","sc_project") |
---|
61 | project_folder=project_xml.replace(".xml","") |
---|
62 | project_name = os.path.splitext( os.path.basename(project_xml) )[0] |
---|
63 | projectLocations[project_name] = project_folder |
---|
64 | """ |
---|
65 | |
---|
66 | |
---|
67 | |
---|
68 | |
---|
69 | print "Create main XMF file "+self.filename |
---|
70 | |
---|
71 | xmfsolgroup = open(self.filename, 'w') |
---|
72 | |
---|
73 | # header |
---|
74 | xmfsolgroup.write("""<?xml version="1.0" ?> |
---|
75 | <!DOCTYPE Xdmf SYSTEM "Xdmf.dtd" > |
---|
76 | <Xdmf xmlns:xi="http://www.w3.org/2003/XInclude" Version="2.0"> |
---|
77 | <Domain> |
---|
78 | <Grid GridType="Collection" CollectionType="Temporal" > |
---|
79 | """) |
---|
80 | |
---|
81 | |
---|
82 | # listy ids stores the data necessary for tags in images (enspythoncut) |
---|
83 | self.list_ids = [] |
---|
84 | |
---|
85 | for item,onoff in pairwise(selection_files) : |
---|
86 | #print onoff |
---|
87 | if onoff=="1" : |
---|
88 | # retreive project path |
---|
89 | sol_identifier = item.split('#')[0] |
---|
90 | sol_simu = item.split('#')[1] |
---|
91 | sol_project_name = item.split('#')[2] |
---|
92 | sol_run = item.split('#')[3] |
---|
93 | sol_name = item.split('#')[4] |
---|
94 | |
---|
95 | #Check if coupling or not on AVTP or AVBP |
---|
96 | addfolder="" |
---|
97 | if self.code == "avtp" : |
---|
98 | cpldir = os.path.join(projectLocations[sol_project_name],sol_run,"AVTP01") |
---|
99 | cpl_avtpfile = getFileList(cpldir,"input_couple.dat") |
---|
100 | cplfile="input_couple.dat" |
---|
101 | if cplfile in cpl_avtpfile : |
---|
102 | print "we are in couple mode of AVTP" |
---|
103 | addfolder="AVTP01" |
---|
104 | if self.code == "avbp" : |
---|
105 | cpldir = os.path.join(projectLocations[sol_project_name],sol_run,"AVBP01") |
---|
106 | cpl_avtpfile = getFileList(cpldir,"input_couple.dat") |
---|
107 | cplfile="input_couple.dat" |
---|
108 | if cplfile in cpl_avtpfile : |
---|
109 | print "we are in couple mode of AVBP" |
---|
110 | addfolder="AVBP01" |
---|
111 | |
---|
112 | |
---|
113 | temporary_name = sol_project_name+"_"+sol_run+"_"+sol_name |
---|
114 | sol_path =os.path.join(projectLocations[sol_project_name],sol_run,addfolder,*self.filelocation) |
---|
115 | |
---|
116 | time_id = self.getTimeInXMF(os.path.join(sol_path,sol_name)) |
---|
117 | |
---|
118 | # declaration of the file |
---|
119 | xmfsolgroup.write(' <xi:include href="'+temporary_name+'" xpointer="xpointer(//Xdmf/Domain/Grid)" />\n') |
---|
120 | |
---|
121 | # copy of directing file |
---|
122 | self.copy_XMF_file_updating_path(sol_name,sol_path,temporary_name,self.post_proc_dir,time_id) |
---|
123 | |
---|
124 | # solution description |
---|
125 | self.list_ids.append(sol_identifier+"#"+sol_simu+"#"+sol_project_name+"#"+sol_run) |
---|
126 | |
---|
127 | |
---|
128 | # end |
---|
129 | xmfsolgroup.write(""" </Grid> |
---|
130 | </Domain> |
---|
131 | </Xdmf> |
---|
132 | """) |
---|
133 | |
---|
134 | xmfsolgroup.close() |
---|
135 | |
---|
136 | |
---|
137 | def getTimeInXMF(self,filename): |
---|
138 | """ get the time id in an XMF file """ |
---|
139 | |
---|
140 | # search for time |
---|
141 | if not os.path.exists(filename) : |
---|
142 | msg = "File "+ filename+ " Does not exists" |
---|
143 | error(msg) |
---|
144 | else : |
---|
145 | # if time must be faken |
---|
146 | if self.faketime : |
---|
147 | time_id = self.time_id_list[-1] + 1.0 |
---|
148 | else : |
---|
149 | xmffile = open(filename,"r") |
---|
150 | lines = xmffile.readlines() |
---|
151 | xmffile.close() |
---|
152 | time_id = "-1" |
---|
153 | for line in lines : |
---|
154 | if "Time Value=" in line : |
---|
155 | time_id = float(line.split('"')[1].strip()) |
---|
156 | |
---|
157 | # case time is not faken, but no time foun |
---|
158 | if time_id == "-1" : |
---|
159 | msg = "No Time value stored in the file "+ filename+ ". Exiting..." |
---|
160 | error(msg) |
---|
161 | |
---|
162 | # avoid time duplicate |
---|
163 | if time_id in self.time_id_list : |
---|
164 | time_id += self.time_id_list[-1]/100 |
---|
165 | |
---|
166 | self.time_id_list.append(time_id) |
---|
167 | return str(time_id) |
---|
168 | |
---|
169 | |
---|
170 | |
---|
171 | def copy_XMF_file_updating_path(self,filename,location,new_filename,new_location,time_id): |
---|
172 | """ create a local copy of the target file, replacing the local relative path references by absolute ones |
---|
173 | time_id is written in the XMF to serve as an additionnal data |
---|
174 | """ |
---|
175 | |
---|
176 | print "Copy "+filename+ " into"+ new_location+", updating the inner pathes" |
---|
177 | |
---|
178 | location = location.rstrip('/') |
---|
179 | |
---|
180 | initfile = open(os.path.join(location,filename),"r") |
---|
181 | lines = initfile.readlines() |
---|
182 | initfile.close() |
---|
183 | |
---|
184 | location_m1=os.path.dirname(location) |
---|
185 | location_m2=os.path.dirname(location_m1) |
---|
186 | location_m3=os.path.dirname(location_m2) |
---|
187 | |
---|
188 | h5file = filename.replace(".xmf",".h5") |
---|
189 | |
---|
190 | final_lines=[] |
---|
191 | # parsing document |
---|
192 | for line in lines : |
---|
193 | newline=line |
---|
194 | |
---|
195 | # time value must be given After the Grid statment |
---|
196 | if "Time Value" in line: |
---|
197 | newline = '' |
---|
198 | |
---|
199 | if "<Grid" in newline : |
---|
200 | newline = '<Grid name="'+self.code.upper()+' by C3Sm">\n'+ ' <Time Value=" {0}"/>\n'.format(time_id) |
---|
201 | |
---|
202 | |
---|
203 | if h5file in newline : |
---|
204 | newline = newline.replace(h5file,os.path.join(location,h5file)) |
---|
205 | |
---|
206 | if "../../../" in newline: |
---|
207 | newline=newline.replace("../../../",location_m3+"/") |
---|
208 | elif "../../" in newline: |
---|
209 | newline=newline.replace("../../",location_m2+"/") |
---|
210 | elif "../" in newline: |
---|
211 | newline=newline.replace("../",location_m1+"/") |
---|
212 | elif "./" in newline: |
---|
213 | newline=newline.replace("./",location+"/") |
---|
214 | final_lines.append(newline) |
---|
215 | |
---|
216 | |
---|
217 | newfile = open(os.path.join(new_location,new_filename), 'w') |
---|
218 | newfile.writelines(final_lines) |
---|
219 | newfile.close() |
---|
220 | |
---|
221 | |
---|
222 | |
---|
223 | |
---|
224 | if __name__ == '__main__': |
---|
225 | pass # Do tests |
---|