def castProfileAtPolyline(self,whatVARS,whatTIME,whatPOINTS): # ~~> Extract data # what['vars']: list of pairs variables:support2d delimited by ';' vars = subsetVariablesSLF(whatVARS,self.VARNAMES) # what['time']: list of frames or (times,) delimited by ';' t = parseArrayFrame(whatTIME,len(self.tags['cores'])) # what['extract']: could be list delimited by ';', of: # + points (x;y), # + 2D points (x;y) bundled within (x;y)#n or (x;y)@d#n, delimited by ';' # where n is a plan number and d is depth from that plane (or the surface by default) xyo = []; zpo = [] for xyi,zpi in parseArrayPoint(whatPOINTS,self.NPLAN): if type(xyi) == type(()): xyo.append(xyi) else: xyo.append( (self.MESHX[xyi],self.MESHY[xyi]) ) for p in zpi: # /!\ common deinition of plans if p not in zpo: zpo.append(p) # /!\ only allowing plans for now xys,support2d = sliceMesh(xyo,self.IKLE2,self.MESHX,self.MESHY,self.tree) # - support2d[i][0] is either the douplets of nodes for each edges crossing with the polyline # - support2d[i][1] is the plan or depth definition support3d = [ (s2d,zpo) for s2d in support2d ] # common vertical definition to all points data = getValuePolylineSLF(self.file,self.tags,t,support3d,self.NVAR,self.NPOIN3,self.NPLAN,vars) # Distance d-axis distot = 0.0 d = [ distot ] for xy in range(len(xys)-1): distot += np.sqrt( np.power(xys[xy+1][0]-xys[xy][0],2) + np.power(xys[xy+1][1]-xys[xy][1],2) ) d.append(distot) # ~~> Draw/Dump data return ('Distance (m)',d),[('v-section',vars[1],self.tags['times'][t],zpo,data)]
def castHistoryAtPoints(self,whatVARS,whatTIME,whatPOINTS): # ~~> Extract data # whatVARS: list of pairs variables:support delimited by ';' (/!\ support is ignored) vars = subsetVariablesSLF(whatVARS,self.VARNAMES) # whatTIME: list of frames or (times,) delimited by ';' t = parseArrayFrame(whatTIME,len(self.tags['cores'])) # whatPOINTS: could be list delimited by ';', of: # + points (x;y), # + 2D points (x;y) bundled within (x;y)#n or (x;y)@d#n, delimited by ';' # where n is a plan number and d is depth from that plane (or the surface by default) support2d = []; zps = [] pAP = parseArrayPoint(whatPOINTS,self.NPLAN) for xyi,zpi in pAP: if type(xyi) == type(()): support2d.append( xysLocateMesh(xyi,self.IKLE2,self.MESHX,self.MESHY,self.tree,self.neighbours) ) else: support2d.append( xyi ) zps.append( zpi ) support3d = zip(support2d,zps) # - support2d[i][0] is either the node or the triplet of nodes for each element including (x,y) # - support2d[i][1] is the plan or depth definition data = getValueHistorySLF(self.file,self.tags,t,support3d,self.NVAR,self.NPOIN3,self.NPLAN,vars) # ~~> Draw/Dump data return ('Time (s)',self.tags['times'][t]), \ [('',[n.replace(' ','_').replace(',',';') for n in vars[1]],[(str(n[0])+':'+str(m)).replace(' ','').replace(',',';') for n in pAP for m in n[1]],data)]
def whatVarsSLF(instr,vnames): # instr ~ what['vars']: list of pairs "variable:support" delimited by ';' # vnames ~ slf.VARNAMES: list of variables names from the SELAFIN file vars = []; vtypes = [] for var in instr.split(';'): v,vtype = var.split(':') vars.append( v ); vtypes.append( vtype ) return subsetVariablesSLF(';'.join(vars),vnames),vtypes
def draw(self,type,what,fig): if 'sortie' in type.lower(): # ~~> Load data sortie = getFileContent(what['file']) # ~~> Extract data data = getValueHistorySortie(sortie,what['vars']) # ~~> Deco # ~~> Draw data drawHistoryLines(plt,data,deco) elif 'SELAFIN' in type.upper(): # ~~> Load data slf = SELAFIN(what['file']) if what['type'] == 'history': # ~~> Extract data vars = subsetVariablesSLF(what["vars"],slf.VARNAMES) support = xyLocateMeshSLF(what["extract"],slf.NELEM3,slf.IKLE,slf.MESHX,slf.MESHY) data = getValueHistorySLF(slf.file,slf.tags,what['time'],support,slf.TITLE,slf.NVAR,slf.NPOIN3,vars) # ~~> Deco if what.has_key('roi'): if what['roi'] != []: deco['roi'] = what['roi'] # ~~> Draw data drawHistoryLines(plt,data,deco) elif what['type'] == 'v-section': # ~~> Extract data vars = subsetVariablesSLF(what["vars"],slf.VARNAMES) support = crossLocateMeshSLF(what["extract"],slf.NELEM3,slf.IKLE,slf.MESHX,slf.MESHY) data = getValuePolylineSLF(slf.file,slf.tags,what['time'],support,slf.TITLE,slf.NVAR,slf.NPOIN3,vars) # ~~> Deco deco['roi'] = [ [np.min(slf.MESHX),np.min(slf.MESHY)], [np.max(slf.MESHX),np.max(slf.MESHY)] ] if what.has_key('roi'): if what['roi'] != []: deco['roi'] = what['roi'] # ~~> Draw data drawPolylineLines(plt,data,deco) else: print '... do not know how to draw this type: ' + what['type'] else: print '... do not know how to draw this format: ' + type
def castValues(self,whatVARS,whatTIME): ftype,fsize = self.file['float'] # /!\ For the moment, only one frame at a time ts = whatTimeSLF(whatTIME,self.tags['cores']) # whatVARS: list of pairs variables:support2d delimited by ';' varsIndexes,varsName = subsetVariablesSLF(whatVARS,self.VARNAMES) if fsize == 4: VARSORS = np.zeros((len(ts),len(varsIndexes),self.NPOIN3),dtype=np.float32) else: VARSORS = np.zeros((len(ts),len(varsIndexes),self.NPOIN3),dtype=np.float64) for it in range(len(ts)): VARSORS[it] = self.getVariablesAt( ts[it],varsIndexes ) return VARSORS
def castVMeshAtPolyline_Plane(self,whatTIME,whatPOINTS): # whatPOINTS: could be list delimited by ';', of: # + points (x;y), # + 2D points (x;y) bundled within (x;y)#n or (x;y)@d#n, delimited by ';' # where n is a plan number and d is depth from that plane (or the surface by default) xyo = []; zpo = [] for xyi,zpi in parseArrayPoint(whatPOINTS,self.NPLAN): if xyi == []: print '... I could not find anything to extract in "',what["extract"].strip(),'" as support for the cross section.' sys.exit(1) if type(xyi) == type(()): xyo.append(xyi) else: xyo.append( (self.MESHX[xyi],self.MESHY[xyi]) ) for p in zpi: # /!\ common deinition of plans if p not in zpo: zpo.append(p) # /!\ only allowing plans for now # ~~> Extract horizontal cross MESHX xys,support2d = sliceMesh(xyo,self.IKLE2,self.MESHX,self.MESHY,self.tree) support3d = [] for s2d in support2d: support3d.append( (s2d,zpo) ) # common vertical definition to all points # Distance d-axis distot = 0.0 d = [ distot ] for xy in range(len(xys)-1): distot += np.sqrt( np.power(xys[xy+1][0]-xys[xy][0],2) + np.power(xys[xy+1][1]-xys[xy][1],2) ) d.append(distot) newx = [] newy = [] for xy in range(len(xys)): newx.append(xys[xy][0]) newy.append(xys[xy][1]) MESHX = np.repeat(newx,len(zpo)) MESHY = np.repeat(newy,len(zpo)) # ~~> Extract MESHZ for more than one time frame varz = subsetVariablesSLF('z',self.VARNAMES) t = whatTimeSLF(whatTIME,self.tags['cores']) MESHZ = np.ravel( getValuePolylineSLF(self.file,self.tags,t,support3d,self.NVAR,self.NPOIN3,self.NPLAN,varz)[0][0].T ) # ~~> Connect with IKLE, keeping quads IKLE = [] for j in range(len(d)-1): for i in range(len(zpo)-1): IKLE.append([ i+j*len(zpo),i+(j+1)*len(zpo),i+1+(j+1)*len(zpo),i+1+j*len(zpo) ]) IKLE = np.array(IKLE) return IKLE,MESHX,MESHY,MESHZ, support3d
def getSplitFromNodeValues(self,var): # ~~> Filter for 'PROCESSORS' as input to the getVariablesAt method i,vn = subsetVariablesSLF(var,self.slf.VARNAMES) if i == []: print '... Could not find ',var,', you may need another split method' sys.exit(1) # ~~> NSPLIT is the interger value of the variable PROCESSORS (time frame 0) NSPLIT = np.array( self.slf.getVariablesAt( 0,i )[0], dtype=np.int) # ~~> NPARTS is the number of parts /!\ does not check continuity vs. missing parts NPARTS = max(*NSPLIT) + 1 # User numbering NSPLIT starts from 0 KSPLIT = np.minimum(*(NSPLIT[self.slf.IKLE].T)) return NPARTS,NSPLIT,KSPLIT
def getSplitFromNodeValues(self, var): # ~~> Filter for 'PROCESSORS' as input to the getVariablesAt method i, vn = subsetVariablesSLF(var, self.slf.VARNAMES) if i == []: print "... Could not find ", var, ", you may need another split method" sys.exit(1) # ~~> NSPLIT is the interger value of the variable PROCESSORS (time frame 0) NSPLIT = np.array(self.slf.getVariablesAt(0, i)[0], dtype=np.int) # ~~> NPARTS is the number of parts /!\ does not check continuity vs. missing parts NPARTS = max(*NSPLIT) + 1 # User numbering NSPLIT starts from 0 KSPLIT = np.minimum(*(NSPLIT[self.slf.IKLE].T)) return NPARTS, NSPLIT, KSPLIT
def castHValueAtLevels(self,whatVARS,whatTIME,whatPOINTS): t = whatTimeSLF(whatTIME,self.tags['cores']) # whatVARS: list of pairs variables:support2d delimited by ';' vars = subsetVariablesSLF(whatVARS,self.VARNAMES) # whatPOINTS: could be list delimited by ';', of: # + points (x;y), # + 2D points (x;y) bundled within (x;y)#n or (x;y)@d#n, delimited by ';' # where n is a plan number and d is depth from that plane (or the surface by default) xyo = []; zpo = [] for xyi,zpi in parseArrayPoint(whatPOINTS,self.NPLAN): if xyi == [] or type(xyi) == type(()): xyo.append(xyi) else: xyo.append( (self.MESHX[xyi],self.MESHY[xyi]) ) for p in zpi: # /!\ common deinition of plans if p not in zpo: zpo.append(p) # /!\ only allowing plans for now if len(zpo) != 1: print '... the vertical definition should only have one plan in this case. It is: ',whatPOINTS,'. I will assume you wish to plot the higher plane.' # could be more than one variables including v, but only one time frame t and one plan data = getValuePolyplanSLF(self.file,self.tags,t,zpo,self.NVAR,self.NPOIN3,self.NPLAN,vars) VARSORS = [] for ivar in range(len(data)): VARSORS.append( data[ivar][0][0] ) # TODO: give me more time return VARSORS
def castVMeshAtLevels(self,whatTIME,whatPOINTS): t = whatTimeSLF(whatTIME,self.tags['cores']) zpo = self.castHMeshAtLevels(whatPOINTS)[3] # whatVARS: is set here for Z vars = [] for vname in self.VARNAMES: if 'ELEVATION' in vname: vars = subsetVariablesSLF('ELEVATION',self.VARNAMES) if 'COTE Z' in vname: vars = subsetVariablesSLF('COTE Z',self.VARNAMES) if 'WATER DEPTH' in vname: vars = subsetVariablesSLF('WATER DEPTH',self.VARNAMES) if 'HAUTEUR D\'EAU' in vname: vars = subsetVariablesSLF('HAUTEUR D\'EAU',self.VARNAMES) if 'FREE SURFACE' in vname: vars = subsetVariablesSLF('FREE SURFACE',self.VARNAMES) if 'SURFACE LIBRE' in vname: vars = subsetVariablesSLF('SURFACE LIBRE',self.VARNAMES) if vars == []: print '... Could not find [\'ELEVATION\'] or [\'COTE Z\'] in ',self.VARNAMES print ' +> Your file may not be a 3D file (?)' sys.exit(1) return self.IKLE3,self.MESHX,self.MESHY,getValuePolyplanSLF(self.file,self.tags,t,zpo,self.NVAR,self.NPOIN3,self.NPLAN,vars)[0][0]
if not path.exists(cliFile): print '... the provided geoFile does not seem to exist: ' + geoFile + '\n\n' sys.exit(1) # Read the new CLI file to get boundary node numbers print ' +> getting hold of the CONLIM file and of its liquid boundaries' cli = CONLIM(cliFile) # Keeping only open boundary nodes BOR = np.extract(cli.BOR['lih'] != 2, cli.BOR['n']) # Find corresponding (x,y) in corresponding new mesh print ' +> getting hold of the GEO file and of its bathymetry' geo = SELAFIN(geoFile) xys = np.vstack((geo.MESHX[BOR - 1], geo.MESHY[BOR - 1])).T bat = geo.getVariablesAt(0, subsetVariablesSLF("BOTTOM: ", geo.VARNAMES)[0])[0] # <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< # ~~~~ slf existing res ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ slfFile = options.args[2] if not path.exists(cliFile): print '... the provided slfFile does not seem to exist: ' + slfFile + '\n\n' sys.exit(1) slf = SELAFIN(slfFile) slf.setKDTree() slf.setMPLTri() print ' +> support extraction' # Extract triangles and weigths in 2D support2d = [] ibar = 0
print ' +> writing header' # Write header atm.appendHeaderSLF() # <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< # ~~~~ writes ATM core ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ print ' +> setting variables' # TIME and DATE extraction atmDATES = slf.DATETIME atmTIMES = slf.tags['times'] atm.tags['times'] = slf.tags['times'] # VARIABLE extraction vars = subsetVariablesSLF( "SURFACE PRESSURE: ;WIND VELOCITY U: ;WIND VELOCITY V: ;AIR TEMPERATURE: ", slf.VARNAMES) # Read / Write data, one time step at a time to support large files pbar = ProgressBar(maxval=len(slf.tags['times'])).start() for t in range(len(slf.tags['times'])): data = getValueHistorySLF(slf.file, slf.tags, [t], support3d, slf.NVAR, slf.NPOIN3, slf.NPLAN, vars) # special cases ? atm.appendCoreTimeSLF(t) atm.appendCoreVarsSLF( np.reshape( np.transpose( np.reshape(np.ravel(data), (atm.NVAR, atm.NPOIN2, atm.NPLAN)), (0, 2, 1)),
############################################################################ ##### Importing data ###### ############################################################################ # Cross section from Selafin file slf = SELAFIN("sis_sandpit.slf") slf.setKDTree() slf.setMPLTri() variable = 'bottom:line' coordinates = '(50.0;0.5)(130.0;0.5)' timef = [20] vars = subsetVariablesSLF(variable,slf.VARNAMES) xyo = []; zpo = [] for xyi,zpi in parseArrayPoint(coordinates,slf.NPLAN): if type(xyi) == type(()): xyo.append(xyi) else: xyo.append( (slf.MESHX[xyi],slf.MESHY[xyi]) ) for p in zpi: if p not in zpo: zpo.append(p) xys,support2d = sliceMesh(xyo,slf.IKLE2,slf.MESHX,slf.MESHY,slf.tree) support3d = [] for s2d in support2d: support3d.append( (s2d,zpo) ) data = getValuePolylineSLF(slf.file,slf.tags,timef,support3d,slf.NVAR,slf.NPOIN3,slf.NPLAN,vars) # Distance d-axis distot = 0.0 d = [ distot ] for xy in range(len(xys)-1): distot += np.sqrt( np.power(xys[xy+1][0]-xys[xy][0],2) + np.power(xys[xy+1][1]-xys[xy][1],2) )