def addFoamSymmTensorField(self, fieldpath, name, time, projectedField=False):
     """
     """
     tSurf = TriSurfaceSymmTensor.readFromFoamFile(
         fieldpath, self.triSurfaceMesh, time=time, projectedField=projectedField
     )
     self.fields[name] = tSurf
 def addFoamSymmTensorField(self,
                            fieldpath,
                            name,
                            time,
                            projectedField=False):
     '''
     '''
     tSurf = TriSurfaceSymmTensor.readFromFoamFile(
         fieldpath,
         self.triSurfaceMesh,
         time=time,
         projectedField=projectedField)
     self.fields[name] = tSurf
    def readReStressFromFoamFile(self,
                                 varsFile,
                                 pointsFile,
                                 facesFile,
                                 viewAnchor=(0,0,0),
                                 xViewBasis=(1,0,0),
                                 yViewBasis=(0,1,0),
                                 dx=None,
                                 dy=None,
                                 interpolationMethod='cubic',
                                 kind='min_E'):
        '''
        '''
        tsm = TriSurfaceMesh.readFromFoamFile(pointsFile=pointsFile,
                                              facesFile=facesFile,
                                              viewAnchor=viewAnchor,
                                              xViewBasis=xViewBasis,
                                              yViewBasis=yViewBasis)
                                              
        tsst = TriSurfaceSymmTensor.readFromFoamFile(varsFile=varsFile,
                                                     triSurfaceMesh=tsm,
                                                     time=0,
                                                     projectedField=False)                  

        points = np.vstack((tsst.x,tsst.y)).T

        #if not hasattr(self,'data'):
        if not self.data.has_key('dx') or self.data.has_key('dy'):
            print 'keys dx and dy does not exist'
            if dx==None:
                dxlist=[a for a in np.abs(np.diff(points[:,0])) if a>0]
                dx=np.min(dxlist)
            if dy==None:
                dylist=[a for a in np.abs(np.diff(points[:,1])) if a>0]
                dy=np.min(dylist)

            MaxX=np.max(points[:,0])
            MinX=np.min(points[:,0])
            MaxY=np.max(points[:,1])
            MinY=np.min(points[:,1])
            extent=[MinX,MaxX,MinY,MaxY]
            #print MinX,MaxX,MinY,MaxY



            cellsX=int((MaxX-MinX)/dx)
            cellsY=int((MaxY-MinY)/dy)
            #print cellsX,cellsY
            grid_y, grid_x = np.mgrid[MinY:MaxY:np.complex(0,cellsY),MinX:MaxX:np.complex(0,cellsX)]
            triang = tsst.triangulation            
            uu_bar=self.interpolateField(tsst.txx, grid_x, grid_y, triang, method=interpolationMethod, kind=kind)
            uv_bar=self.interpolateField(tsst.txy, grid_x, grid_y, triang, method=interpolationMethod, kind=kind)
            uw_bar=self.interpolateField(tsst.tyy, grid_x, grid_y, triang, method=interpolationMethod, kind=kind)
            vv_bar=self.interpolateField(tsst.tyy, grid_x, grid_y, triang, method=interpolationMethod, kind=kind)
            vw_bar=self.interpolateField(tsst.tyz, grid_x, grid_y, triang, method=interpolationMethod, kind=kind)
            ww_bar=self.interpolateField(tsst.tzz, grid_x, grid_y, triang, method=interpolationMethod, kind=kind)
            vx_i=np.empty(uu_bar.shape)
            vy_i=np.empty(uu_bar.shape)
            vz_i=np.empty(uu_bar.shape)
            vx_i[:]=np.NAN
            vy_i[:]=np.NAN
            vz_i[:]=np.NAN

            self.vx=np.flipud(vx_i)
            self.vy=np.flipud(vy_i)
            self.vz=np.flipud(vz_i)
            self.extent=extent
            self.dx=dx
            self.dy=dy
            self.minX=MinX
            self.maxX=MaxX
            self.minY=MinY
            self.maxY=MaxY
            self.createDataDict()

            print 'adding Tensor'
            self.data['uu_bar']=np.flipud(uu_bar)
            self.data['uv_bar']=np.flipud(uv_bar)
            self.data['uw_bar']=np.flipud(uw_bar)
            self.data['vv_bar']=np.flipud(vv_bar)
            self.data['vw_bar']=np.flipud(vw_bar)
            self.data['ww_bar']=np.flipud(ww_bar)
            self.data['TKE_bar']=0.5*(self.data['uu_bar']+self.data['vv_bar']+self.data['ww_bar'])

        else:
            print 'dict exists'
            MaxX=self.maxX
            MinX=self.minX
            MaxY=self.maxY
            MinY=self.minY

            cellsX=int((MaxX-MinX)/self.dx)
            cellsY=int((MaxY-MinY)/self.dy)
            #print cellsX,cellsY
            grid_y, grid_x = np.mgrid[MinY:MaxY:np.complex(0,cellsY),MinX:MaxX:np.complex(0,cellsX)]
            triang = tsst.triangulation
            uu_bar=self.interpolateField(tsst.txx, grid_x, grid_y, triang, method=interpolationMethod, kind=kind)
            uv_bar=self.interpolateField(tsst.txy, grid_x, grid_y, triang, method=interpolationMethod, kind=kind)
            uw_bar=self.interpolateField(tsst.tyy, grid_x, grid_y, triang, method=interpolationMethod, kind=kind)
            vv_bar=self.interpolateField(tsst.tyy, grid_x, grid_y, triang, method=interpolationMethod, kind=kind)
            vw_bar=self.interpolateField(tsst.tyz, grid_x, grid_y, triang, method=interpolationMethod, kind=kind)
            ww_bar=self.interpolateField(tsst.tzz, grid_x, grid_y, triang, method=interpolationMethod, kind=kind)
            print 'adding Tensor'
            self.data['uu_bar']=np.flipud(uu_bar)
            self.data['uv_bar']=np.flipud(uv_bar)
            self.data['uw_bar']=np.flipud(uw_bar)
            self.data['vv_bar']=np.flipud(vv_bar)
            self.data['vw_bar']=np.flipud(vw_bar)
            self.data['ww_bar']=np.flipud(ww_bar)
            self.data['TKE_bar']=0.5*(self.data['uu_bar']+self.data['vv_bar']+self.data['ww_bar'])
    def addFieldFromHdf5(self,
                         hdf5Parser,
                         key,
                         names=[],
                         projectedField=False):
        '''
        Add a list field stored in a hdf5 to the TriSurfaceContainer.
        
        Arguments:
            *hdf5Parser*: h5py parser object.
             Parser object of the source hdf5 file.
             
            *key*: python string.
             The time (as a key) to extract from the HDF5. If it does not exist, IOError
             is returned.
             
            *names*: python list of string.
             Name of the fields to extract from the HDF5. It can be a single
             field (names=['oneField']) or multiple. if names=[], all the
             fields are loaded. Default: names=[].
             
            *projectedField*: python bool
             Project the fields in the surface coordinate system.
             Default: projectedField=False.
        
        Usages:
            >>> import h5py
            >>> parser = h5py.File('myfile.h5','r')
            >>> tsc = TriSurfaceContainer.createFromHdf5(parser,xViewBasis=[1,0,0])
            >>> tsc.addFieldFromHdf5(parser,time=1.5,names=['U','S'])
        '''
        gTime = key
        time = float(key)
        if len(names) == 0:
            names = hdf5Parser[gTime].keys()
            names.pop(names.index('time'))

        for name in names:
            try:

                dataShape = hdf5Parser[gTime][name].value.shape
                if len(dataShape) == 1:  #data is a scalar
                    tss = TriSurfaceScalar.readFromHdf5(
                        hdf5Parser=hdf5Parser,
                        varName=name,
                        triSurfaceMesh=self.triSurfaceMesh,
                        key=key,
                        projectedField=projectedField)
                    self.fields[name] = tss
                elif len(dataShape
                         ) == 2 and dataShape[1] == 3:  #data is a vector
                    tsv = TriSurfaceVector.readFromHdf5(
                        hdf5Parser=hdf5Parser,
                        varName=name,
                        triSurfaceMesh=self.triSurfaceMesh,
                        key=key,
                        projectedField=projectedField)
                    self.fields[name] = tsv
                elif len(dataShape
                         ) == 2 and dataShape[1] == 6:  #data is a symmtensor
                    tsst = TriSurfaceSymmTensor.readFromHdf5(
                        hdf5Parser=hdf5Parser,
                        varName=name,
                        triSurfaceMesh=self.triSurfaceMesh,
                        key=key,
                        projectedField=projectedField)
                    self.fields[name] = tsst
                else:
                    raise IOError('variable of name "' + name + '" is not a'
                                  'scalar, not a vector, not a symmTensor.')

            except KeyError as e:
                'time "' + gTime + '" and/or name "' + name + '" does not exist as key in the HDF5 parser. Not read, but continuing'
                print e
            except KeyboardInterrupt as e:
                print e
                return False
            except MemoryError as e:
                print e
                return False
            except:
                print "Unexpected error:", sys.exc_info()[0]
                #raise IOError('time "'+gTime+'" and/or name "'+name+'" does not '
                #'exist as key in the HDF5 parser.')
                return False
        return True
    def addFieldFromHdf5(self, hdf5Parser, key, names=[], projectedField=False):
        """
        Add a list field stored in a hdf5 to the TriSurfaceContainer.
        
        Arguments:
            *hdf5Parser*: h5py parser object.
             Parser object of the source hdf5 file.
             
            *key*: python string.
             The time (as a key) to extract from the HDF5. If it does not exist, IOError
             is returned.
             
            *names*: python list of string.
             Name of the fields to extract from the HDF5. It can be a single
             field (names=['oneField']) or multiple. if names=[], all the
             fields are loaded. Default: names=[].
             
            *projectedField*: python bool
             Project the fields in the surface coordinate system.
             Default: projectedField=False.
        
        Usages:
            >>> import h5py
            >>> parser = h5py.File('myfile.h5','r')
            >>> tsc = TriSurfaceContainer.createFromHdf5(parser,xViewBasis=[1,0,0])
            >>> tsc.addFieldFromHdf5(parser,time=1.5,names=['U','S'])
        """
        gTime = key
        time = float(key)
        if len(names) == 0:
            names = hdf5Parser[gTime].keys()
            names.pop(names.index("time"))

        for name in names:
            try:

                dataShape = hdf5Parser[gTime][name].value.shape
                if len(dataShape) == 1:  # data is a scalar
                    tss = TriSurfaceScalar.readFromHdf5(
                        hdf5Parser=hdf5Parser,
                        varName=name,
                        triSurfaceMesh=self.triSurfaceMesh,
                        key=key,
                        projectedField=projectedField,
                    )
                    self.fields[name] = tss
                elif len(dataShape) == 2 and dataShape[1] == 3:  # data is a vector
                    tsv = TriSurfaceVector.readFromHdf5(
                        hdf5Parser=hdf5Parser,
                        varName=name,
                        triSurfaceMesh=self.triSurfaceMesh,
                        key=key,
                        projectedField=projectedField,
                    )
                    self.fields[name] = tsv
                elif len(dataShape) == 2 and dataShape[1] == 6:  # data is a symmtensor
                    tsst = TriSurfaceSymmTensor.readFromHdf5(
                        hdf5Parser=hdf5Parser,
                        varName=name,
                        triSurfaceMesh=self.triSurfaceMesh,
                        key=key,
                        projectedField=projectedField,
                    )
                    self.fields[name] = tsst
                else:
                    raise IOError('variable of name "' + name + '" is not a' "scalar, not a vector, not a symmTensor.")

            except KeyError as e:
                'time "' + gTime + '" and/or name "' + name + '" does not exist as key in the HDF5 parser. Not read, but continuing'
                print e
            except KeyboardInterrupt as e:
                print e
                return False
            except MemoryError as e:
                print e
                return False
            except:
                print "Unexpected error:", sys.exc_info()[0]
                # raise IOError('time "'+gTime+'" and/or name "'+name+'" does not '
                #'exist as key in the HDF5 parser.')
                return False
        return True