Exemple #1
0
    def setFile(self,fName):
        if self.opts.namePrefix:
            fName=self.opts.namePrefix+"_"+fName
        if self.opts.pictureDest:
            fName=path.join(self.opts.pictureDest,fName)

        name=fName
        if self.opts.cleanFilename:
            name=cleanFilename(fName)
        return 'set output "%s"\n' % name
Exemple #2
0
    def run(self):
        global vtk
        import vtk

        caseName=path.basename(path.abspath(self.parser.getArgs()[0]))
        samples=SurfaceDirectory(self.parser.getArgs()[0],dirName=self.opts.dirName)
        self.hasPipeline=False
        
        if self.opts.info:
            print "Times    : ",samples.times
            print "Surfaces : ",samples.surfaces()
            print "Values   : ",samples.values()
            sys.exit(0)

        surfaces=samples.surfaces()
        times=samples.times
        values=samples.values()
        
        if self.opts.surface==None:
            #            error("At least one line has to be specified. Found were",samples.lines())
            self.opts.surface=surfaces
        else:
            for l in self.opts.surface:
                if l not in surfaces:
                    error("The line",l,"does not exist in",lines)

        if self.opts.maxTime or self.opts.minTime:
            if self.opts.time:
                error("Times",self.opts.time,"and range [",self.opts.minTime,",",self.opts.maxTime,"] set: contradiction")
            self.opts.time=[]
            if self.opts.maxTime==None:
                self.opts.maxTime= 1e20
            if self.opts.minTime==None:
                self.opts.minTime=-1e20

            for t in times:
                if float(t)<=self.opts.maxTime and float(t)>=self.opts.minTime:
                    self.opts.time.append(t)

            if len(self.opts.time)==0:
                error("No times in range [",self.opts.minTime,",",self.opts.maxTime,"] found: ",times)
        elif self.opts.time:
            iTimes=self.opts.time
            self.opts.time=[]
            for t in iTimes:
                if t in samples.times:
                    self.opts.time.append(t)
                elif self.opts.fuzzyTime:
                    tf=float(t)
                    use=None
                    dist=1e20
                    for ts in samples.times:
                        if abs(tf-float(ts))<dist:
                            use=ts
                            dist=abs(tf-float(ts))
                    if use and use not in self.opts.time:
                        self.opts.time.append(use)
                else:
                    self.warning("Time",t,"not found in the sample-times. Use option --fuzzy")
            
        if not self.opts.silent:
            print "Getting data about plots"
        plots=[]

        if self.opts.time==None:
            self.opts.time=samples.times
        elif len(self.opts.time)==0:
            self.error("No times specified. Exiting")
        if self.opts.field==None:
            self.opts.field=samples.values()
        for s in self.opts.surface:
            for t in self.opts.time:
                for f in self.opts.field:
                    plt=samples.getData(surface=[s],
                                        value=[f],
                                        time=[t])
                    if plt:
                        plots+=plt

        vRanges=None
        if self.opts.scaled:
            if not self.opts.silent:
                print "Getting ranges"
            if self.opts.scaleAll:
                vRange=None
            else:
                vRanges={}
                
            for p in plots:
                f,tm,surf,nm=p
                self.setFilename(f)

                mi,ma=self.getCurrentRange()
                if not self.opts.scaleAll:
                    if nm in vRanges:
                        vRange=vRanges[nm]
                    else:
                        vRange=None

                if vRange==None:
                    vRange=mi,ma
                else:
                    vRange=min(vRange[0],mi),max(vRange[1],ma)
                if not self.opts.scaleAll:
                    vRanges[nm]=vRange

        for p in plots:
            f,time,surf,nm=p

            name=""
            if self.opts.namePrefix:
                name+=self.opts.namePrefix+"_"
            name+=self.opts.dirName
            tIndex=times.index(time)
            
            name+="_"+surf

            name+="_%s_%04d"   % (nm,tIndex)
            title="%s : %s - %s   t=%f" % (caseName,self.opts.dirName,surf,float(time))
                        
            name+=".png"
            if self.opts.cleanFilename:
                name=cleanFilename(name)
            
            if self.opts.pictureDest:
                name=path.join(self.opts.pictureDest,name)

            self.setFilename(f)
            if self.opts.scaleAll:
                if vRange:
                    self.setRange(vRange)
            else:
                if vRanges:
                    if nm in vRanges:
                        self.setRange(vRanges[nm])
                    
            self.setTitles(title,nm)

            if not self.opts.silent:
                print "Writing picture",name
                
            self.writePicture(name)
Exemple #3
0
    def run(self):
        # remove trailing slashif present
        if self.opts.dirName[-1]==path.sep:
            self.opts.dirName=self.opts.dirName[:-1]
                    
        samples=SampleDirectory(self.parser.getArgs()[0],
                                dirName=self.opts.dirName,
                                postfixes=self.opts.fieldPostfix,
                                prefixes=self.opts.fieldPrefix)
        reference=None
        if self.opts.reference and self.opts.referenceCase:
            self.error("Options --reference-directory and --reference-case are mutual exclusive")
        if self.opts.csvFile and (self.opts.compare or self.opts.metrics):
            self.error("Options --csv-file and --compare/--metrics are mutual exclusive")
            
        if self.opts.reference:
            reference=SampleDirectory(self.parser.getArgs()[0],
                                      dirName=self.opts.reference,
                                      postfixes=self.opts.fieldPostfix,
                                      prefixes=self.opts.fieldPrefix)
        elif self.opts.referenceCase:
            reference=SampleDirectory(self.opts.referenceCase,
                                      dirName=self.opts.dirName,
                                      postfixes=self.opts.fieldPostfix,
                                      prefixes=self.opts.fieldPrefix)

        if reference:
            if path.samefile(reference.dir,samples.dir):
                self.error("Used sample directory",samples.dir,
                           "and reference directory",reference.dir,
                           "are the same")
        
        lines=samples.lines()
        times=samples.times
        values=samples.values()
        
        if self.opts.info:
            if not self.opts.silent:
                print "Times : ",samples.times
                print "Lines : ",samples.lines()
                print "Fields: ",samples.values()

            self.setData({'times'  : samples.times,
                          'lines'  : samples.lines(),
                          'values' : samples.values()})

            if reference:
                if not self.opts.silent:
                    print "\nReference Data:"
                    print "Times : ",reference.times
                    print "Lines : ",reference.lines()
                    print "Fields: ",reference.values()
                self.setData({'reference':{'times'  : samples.times,
                                           'lines'  : samples.lines(),
                                           'values' : samples.values()}})
                
            return 0
            
        if self.opts.line==None:
            #            error("At least one line has to be specified. Found were",samples.lines())
            self.opts.line=lines
        else:
            for l in self.opts.line:
                if l not in lines:
                    error("The line",l,"does not exist in",lines)

        if self.opts.latestTime:
            if self.opts.time:
                self.opts.time.append(samples.times[-1])
            else:
                self.opts.time=[samples.times[-1]]

        if self.opts.maxTime or self.opts.minTime:
            if self.opts.time:
                error("Times",self.opts.time,"and range [",self.opts.minTime,",",self.opts.maxTime,"] set: contradiction")
            self.opts.time=[]
            if self.opts.maxTime==None:
                self.opts.maxTime= 1e20
            if self.opts.minTime==None:
                self.opts.minTime=-1e20

            for t in times:
                if float(t)<=self.opts.maxTime and float(t)>=self.opts.minTime:
                    self.opts.time.append(t)

            if len(self.opts.time)==0:
                error("No times in range [",self.opts.minTime,",",self.opts.maxTime,"] found: ",times)
        elif self.opts.time:
            iTimes=self.opts.time
            self.opts.time=[]
            for t in iTimes:
                if t in samples.times:
                    self.opts.time.append(t)
                elif self.opts.fuzzyTime:
                    tf=float(t)
                    use=None
                    dist=1e20
                    for ts in samples.times:
                        if abs(tf-float(ts))<dist:
                            use=ts
                            dist=abs(tf-float(ts))
                    if use and use not in self.opts.time:
                        self.opts.time.append(use)
                else:
                    pass
                #                    self.warning("Time",t,"not found in the sample-times. Use option --fuzzy")
        if self.opts.tolerantReferenceTime:
            if self.opts.referenceTime:
                self.error("--tolerant-reference-time and --reference-time can't be used at the same time")
            refTimes={}
            for t in self.opts.time:
                dist=1e20
                for rt in reference.times:
                    if abs(float(t)-float(rt))<dist:
                        refTimes[t]=rt
                        dist=abs(float(t)-float(rt))

        plots=[]
        oPlots=[]
        rPlots=[]
        
        if self.opts.mode=="separate":
            if self.opts.time==None:
                self.opts.time=samples.times
            if self.opts.field==None:
                self.opts.field=samples.values()
            if self.opts.line==None:
                self.opts.line=samples.lines()
            for t in self.opts.time:
                for f in self.opts.field:
                    for l in self.opts.line:
                        plot=samples.getData(line=[l],
                                             value=[f],
                                             time=[t])
                        oPlots.append(plot[:])
                        if reference:
                            rT=[t]
                            if self.opts.referenceTime:
                                rT=[self.opts.referenceTime]
                            elif self.opts.tolerantReferenceTime:
                                rT=[refTimes[t]]
                            p=reference.getData(line=[l],
                                                value=[f],
                                                time=rT,
                                                note=self.opts.refprefix+" ")
                            rPlots.append(p)
                            plot+=p
                        plots.append(plot)
                    
        elif self.opts.mode=="timesInOne":
            if self.opts.field==None:
                self.opts.field=samples.values() 
            if self.opts.line==None:
                self.opts.line=samples.lines()
            for f in self.opts.field:
                for l in self.opts.line:
                    plot=samples.getData(line=[l],
                                         value=[f],
                                         time=self.opts.time)
                    oPlots.append(plot[:])

                    if reference:
                        rT=self.opts.time
                        if self.opts.referenceTime:
                            rT=[self.opts.referenceTime]
                        elif self.opts.tolerantReferenceTime:
                            rT=[refTimes[t]]
                        p=reference.getData(line=[l],
                                            value=[f],
                                            time=rT,
                                            note=self.opts.refprefix+" ")
                        rPlots.append(p)
                        plot+=p

                    plots.append(plot)
                
        elif self.opts.mode=="fieldsInOne":
            if self.opts.scaled and not self.opts.scaleAll:
                warning("In mode '",self.opts.mode,"' all fields are scaled to the same value")
                self.opts.scaleAll=True
                
            if self.opts.time==None:
                self.opts.time=samples.times
            if self.opts.line==None:
                self.opts.line=samples.lines()
            for t in self.opts.time:
                for l in self.opts.line:
                    plot=samples.getData(line=[l],
                                         value=self.opts.field,
                                         time=[t])
                    oPlots.append(plot[:])
                    if reference:
                        rT=t
                        if self.opts.referenceTime:
                            rT=self.opts.referenceTime
                        elif self.opts.tolerantReferenceTime:
                            rT=refTimes[t]
                        p=reference.getData(line=[l],
                                            value=self.opts.field,
                                            time=[rT],
                                            note=self.opts.refprefix+" ")
                        rPlots.append(p)
                        plot+=p

                    plots.append(plot)
                
        elif self.opts.mode=="linesInOne":
            if self.opts.field==None:
                self.opts.field=samples.values() 
            if self.opts.time==None:
                self.opts.time=samples.times()
            for f in self.opts.field:
                for t in self.opts.time:
                    plot=samples.getData(line=self.opts.line,
                                         value=[f],
                                         time=[t])
                    oPlots.append(plot[:])

                    if reference:
                        rT=t
                        if self.opts.referenceTime:
                            rT=self.opts.referenceTime
                        elif self.opts.tolerantReferenceTime:
                            rT=refTimes[t]
                        p=reference.getData(line=self.opts.line,
                                            value=[f],
                                            time=[rT],
                                            note=self.opts.refprefix+" ")
                        rPlots.append(p)
                        plot+=p

                    plots.append(plot)
                
        elif self.opts.mode=="complete":
            if self.opts.scaled and not self.opts.scaleAll:
                warning("In mode '",self.opts.mode,"' all fields are scaled to the same value")
                self.opts.scaleAll=True

            plot=samples.getData(line=self.opts.line,
                                 value=self.opts.field,
                                 time=self.opts.time)
            oPlots.append(plot[:])
            if reference:
                rT=self.opts.time
                if self.opts.referenceTime:
                    rT=[self.opts.referenceTime]
                elif self.opts.tolerantReferenceTime:
                    rT=[refTimes[t]]
                p=reference.getData(line=self.opts.line,
                                    value=self.opts.field,
                                    time=rT,
                                    note=self.opts.refprefix+" ")
                plot+=p
                rPlots.append(p)
                
            plots.append(plot)
            
        if self.opts.scaled:
            if self.opts.scaleAll:
                vRange=None
            else:
                vRanges={}
                
            for p in plots:
                for d in p:
                    mi,ma=d.range(component=self.opts.component)
                    nm=d.name
                    if not self.opts.scaleAll:
                        if nm in vRanges:
                            vRange=vRanges[nm]
                        else:
                            vRange=None
                            
                    if vRange==None:
                        vRange=mi,ma
                    else:
                        vRange=min(vRange[0],mi),max(vRange[1],ma)
                    if not self.opts.scaleAll:
                        vRanges[nm]=vRange
            
        result="set term png\n"

        for p in plots:
            if len(p)<1:
                continue

            name=""
            
            if self.opts.namePrefix:
                name+=self.opts.namePrefix+"_"
            name+=self.opts.dirName
            title=None
            tIndex=times.index(p[0].time())
            
            #            name+="_"+string.join(self.opts.line,"_")

            if self.opts.mode=="separate":
                name+="_%s"        % (p[0].line())
                name+="_%s_%04d"   % (p[0].name,tIndex)
                title="%s at t=%f on %s" % (p[0].name,float(p[0].time()),p[0].line())
            elif self.opts.mode=="timesInOne":
                name+="_%s"        % (p[0].line())
                if self.opts.time!=None:
                    name+="_"+"_".join(["t="+t for t in self.opts.time])
                name+="_%s" % p[0].name
                title="%s on %s"  % (p[0].name,p[0].line())
            elif self.opts.mode=="fieldsInOne":
                name+="_%s"        % (p[0].line())
                if self.opts.field!=None:
                    name+="_"+string.join(self.opts.field,"_")
                if self.opts.time!=None:
                    name+="_"+"_".join(["t="+t for t in self.opts.time])
                name+="_%04d" % tIndex
                title="t=%f on %s" % (float(p[0].time()),p[0].line())
            elif self.opts.mode=="linesInOne":
                name+="_%s"        % (p[0].name)
                if self.opts.line!=None:
                    name+="_"+string.join(self.opts.line,"_")
                name+="_t=%f" % float(p[0].time())
                title="%s at t=%f" % (p[0].name,float(p[0].time()))
            elif self.opts.mode=="complete":
                pass
            
            name+=".png"
            if self.opts.pictureDest:
                name=path.join(self.opts.pictureDest,name)

            if self.opts.cleanFilename:
                name=cleanFilename(name)
                
            result+='set output "%s"\n' % name
            if title!=None:
                result+='set title "%s"\n' % title.replace("_","\\_")
                
            result+="plot "
            if self.opts.scaled:
                if not self.opts.scaleAll:
                    vRange=vRanges[p[0].name]

                # only scale if extremas are sufficiently different
                if abs(vRange[0]-vRange[1])>1e-5*max(abs(vRange[0]),abs(vRange[1])) and max(abs(vRange[0]),abs(vRange[1]))>1e-10:
                    result+="[][%g:%g] " % vRange

            first=True

            for d in p:
                if first:
                    first=False
                else:
                    result+=", "

                colSpec="%s" % (d.index+1)
                if d.isVector():
                    if self.opts.component!=None:
                        colSpec="%d" % (d.index+1+self.opts.component)
                    else:
                        colSpec="(sqrt($%d**2+$%d**2+$%d**2))" % (d.index+1,d.index+2,d.index+3)
                    
                result+='"%s" using 1:%s ' % (d.file,colSpec)
                
                title=d.note
                if self.opts.mode=="separate":
                    title+=""
                elif self.opts.mode=="timesInOne":
                    title+="t=%f" % float(d.time())
                elif self.opts.mode=="fieldsInOne":
                    title+="%s"   % d.name
                elif self.opts.mode=="linesInOne":
                    title+="t=%f"   % float(d.time())
                elif self.opts.mode=="complete":
                    title+="%s at t=%f" % (d.name,float(d.time()))

                if len(self.opts.line)>1:
                    title+=" on %s" % d.line()

                if title=="":
                    result+="notitle "
                else:
                    result+='title "%s" ' % title.replace("_","\\_")

                result+="with %s " % self.opts.style

            result+="\n"

        if self.opts.csvFile:
            tmp=sum(plots,[])
            c=tmp[0]()
            for p in tmp[1:]:
                try:
                    c+=p()
                except WrongDataSize,e:
                    if self.opts.resampleReference:
                        sp=p()
                        for n in sp.names()[1:]:
                            data=c.resample(sp,
                                            n,
                                            extendData=self.opts.extendData)
                            try:
                                c.append(n,data)
                            except ValueError:
                                c.append(self.opts.refprefix+" "+n,data)
                    else:
                        self.warning("Try the --resample-option")
                        raise
                    
            c.writeCSV(self.opts.csvFile)