def oldstyle(): outf=open("report_2.txt", "w") with open(os.environ["HOMEPATH"]+"\\Documents\\abrupt\\c_test\\x.x_08Jun",'r') as f: lines=f.readlines() for line in lines: #print line pos1=line.find('"') if pos1>=0: pos2=line.find('"',pos1+1) #print pos1,pos2 fn=line[:pos1-1] #print fn+":" yset=eval(line[pos1:pos2+1]) #print "YSET", yset _,yr,pre,post=line[pos2+1:].split()[:4] # print yr,pre,post dfn=fn[:-len('final.csv')-1] tfn=os.environ["HOMEPATH"]+"\\Documents\\abrupt\\c_test\\"+dfn #print fn, tfn if dfn[:len("Global")]=="Global" or dfn[0] == 'r': print "do",tfn try: direc=glob.glob(os.environ["HOMEPATH"]+"\\Documents\\abrupt\\c_test\\"+fn[:-len('final.csv')-1]+'/*.trace')[0] bp=statbreaks.brkrpt(direc) #print bp.years() yrix=int(list(bp.years()).index(float(yr))) preix=int(list(bp.years()).index(float(pre))) postix=int(list(bp.years()).index(float(post))) #print bp.years()[preix:postix+1] ancova=doancova.ANCOVA(bp.years()[preix:postix+1], bp.ys()[preix:postix+1], bp.years()[yrix+1],Rpath='"C:\\Program Files\\R\\R-3.0.2\\bin\\Rscript.exe"') ancova.parseResult(ancova.results()) print >>outf, "slope",line[:-1],":",ancova._ANCOVA__breakYear,ancova.firstSlope(),formatprobs(ancova.firstSlopeProb()), ancova.firstRsq(), ancova.secondSlope(),formatprobs(ancova.secondSlopeProb()),ancova.secondRsq(),ancova.shift(),formatprobs(ancova._ANCOVA__prDiffShift),":", if dfn[:len("Global")]=="Global": print >>outf, cmip3_info.matchfn(tfn), elif dfn[0] == 'r': print >>outf, cmip5_info.matchfn(tfn), print >>outf, formatprobs(ancova.changeOfTrendProb(),ancova._ANCOVA__prDiffShift),formatprobs(ancova.changeOfTrendProb()*ancova._ANCOVA__prDiffShift) #if except Exception as e: fl=os.environ["HOMEPATH"]+"\\Documents\\abrupt\\c_test\\"+fn[:-len('final.csv')-1]+'/*.trace' print >>outf,"\n",line,"Error getting files",fl print "using ", fl #print glob.glob(fl) #raise Exception(str(e)) pass #print direc, outf.close() print "done"
def getannual(finalfn,datapath="C:\\Users\\s4493222\\Documents\\abrupt\\c_test\\"): #assume *.final.csv direc=os.path.splitext(os.path.splitext(finalfn)[0])[0] anfn=datapath+direc+"\\"+direc+"*_0.trace" anfn=glob.glob(anfn) if len(anfn) <1: #print "cannot find ",anfn,finalfn #sys.exit() return None,None,None,None else: anfn=anfn[0] #print os.path.exists(anfn) sb=statbreaks.brkrpt(anfn) return sb.breaks(), sb.years(), sb.ys(), sb.segments()
def __init__(self, rootname): self.__rootname = rootname self.__finalfile = rootname + ".final.csv" self.__lines = [] with open(self.__finalfile) as f: lines = f.readlines() for line in lines: yrset = line[: line.find('"', 1) + 1] tail = line[line.find('"', 1) + 2 : -1] self.__lines.append([yrset, tail.split(" ")]) # print self.__lines[0][1][0] will be the frequency count # now we have a list of lists [[str(breaks), [str(parameters)]]] # find the first line with the max freq maxi = 0 maxix = 0 maxs = [] self.__METADATA = [] for l in range(len(self.__lines)): try: fr = int(self.__lines[l][1][0]) if fr > maxi: maxi = fr maxix = l maxs = self.__lines[l][0] except: # metadate at bottom self.__METADATA.append([rootname, self.__lines[l]]) pass # open any trace file in the _self.__rootnamedir and extract the years and yi values try: samplefn = glob.glob(rootname + "\\*.trace")[0] except: samplefn = "" sb = statbreaks.brkrpt(samplefn) self.__years = sb.years() self.__ys = sb.ys() self.__maxset = maxs # now finds all the lines nelonging to modal self.__analysis = [] for line in self.__lines: if maxs == line[0]: self.__analysis.append(line[1])
def newstyle(csvrepfn,outfn): #from csv files outf=open(outfn, "w") with open(csvrepfn,'r') as f: lines=f.readlines() for line in lines: #print line if line[:5] == '"Seq"': print >>outf,csvHeader+',":",'+cmip5_info.header+ancovaHeader else: if line.find("no breaks")>=0: nobreaks = True else: nobreaks = False words=line.split('"') try: # nobreaks=False fn=os.path.basename(words[0].split(",")[1]) except Exception as e: print "Oops", line, words, str(e) if line.find("no breaks")>=0: nobreaks = True pass else: raise e #print fn+":" if len(words) < 2: print "oops2" , line, words if not nobreaks: yset=eval(words[1]) #print "YSET", yset yrss=words[2].split(',')[8:11] yr=str(yrss[0]) pre=str(yrss[1]) post=str(yrss[2]) # print yr,pre,post dfn=fn[:-len('final.csv')-1] if dfn[:len("Global-temps")]=="Global-temps": dfn = "Global temps"+dfn[len("Global-temps"):] tfn=os.environ["HOMEPATH"]+"\\Documents\\abrupt\\c_test\\"+dfn #print fn, tfn if dfn[:len("Global")]=="Global" or dfn[0] == 'r': print "do",tfn try: direc=glob.glob(os.environ["HOMEPATH"]+"\\Documents\\abrupt\\c_test\\"+dfn+'/*.trace')[0] bp=statbreaks.brkrpt(direc) #print bp.years() yrix=int(list(bp.years()).index(float(yr))) preix=int(list(bp.years()).index(float(pre))) postix=int(list(bp.years()).index(float(post))) #print bp.years()[preix:postix+1] ancova=doancova.ANCOVA(bp.years()[preix:postix+1], bp.ys()[preix:postix+1], bp.years()[yrix],Rpath='"C:\\Program Files\\R\\R-3.0.2\\bin\\Rscript.exe"') ancova.parseResult(ancova.results()) print >>outf,line[:-1],',":"', if dfn[:len("Global")]=="Global": info=cmip3_info.matchfn(tfn) for inf in info: print >>outf, ",",inf, elif dfn[0] == 'r': info=cmip5_info.matchfn(tfn) for inf in info: print >>outf, ",",inf, print >>outf, ',":"', for inf in [ ancova._ANCOVA__breakYear, ancova.firstSlope(), formatprobs(ancova.firstSlopeProb()), ancova.firstRsq(), ancova.secondSlope(), formatprobs(ancova.secondSlopeProb()), ancova.secondRsq(), ancova.shift(), formatprobs(ancova._ANCOVA__prDiffShift), ":", formatprobs(ancova.changeOfTrendProb(), ancova._ANCOVA__prDiffShift), formatprobs(ancova.changeOfTrendProb()* ancova._ANCOVA__prDiffShift) ]: print >>outf,",",inf, print >>outf #if except Exception as e: fl=os.environ["HOMEPATH"]+"\\Documents\\abrupt\\c_test\\"+fn[:-len('final.csv')-1]+'/*.trace' print >>outf,line[:-1],"Error getting files",fl print "using ", fl #print glob.glob(fl) #raise Exception(str(e)) pass #print direc, outf.close() print "done"
def __init__(self, fn): self.__filename = fn self.__data=statbreaks.brkrpt(fn) self.__yearsegs, self.__ysegs, self.__xsegs = self.__data.segments()
return self.__prDiffTrend def firstRsq(self): return self.__PriorRsq def secondRsq(self): return self.__PostRsq def prBreak(self): return self__prbreak if __name__ == "__main__": fn1=os.environ["HOMEPATH"]+"\\Documents\\abrupt\\c_test\\GISSTEMP_v3_April_2015_withDiffs_from_2014_24N44N\\GISSTEMP_v3_April_2015_withDiffs_from_2014.csv_24N44N_0.trace" #os.environ["HOMEPATH"]+"\\Documents\\abrupt\\4Roger_Nature_SVN_264\\HadCRUT.4.2.0.0.annual_ns_avg//HadCRUT.4.2.0.0.annual_ns_avg.txt_0.trace") bp=statbreaks.brkrpt(fn1) yearsegs, ysegs, xsegs = bp.segments() pre98Years=yearsegs[-2] pre98temps=ysegs[-2] post98Years=yearsegs[-1] post98temps=ysegs[-1] Years=list(pre98Years[:]) Years.extend(list(post98Years[:])) ys=list(pre98temps[:]) ys.extend(list(post98temps[:])) print bp.breaks() ancova=ANCOVA(Years, ys, bp.breaks()[-2], Rpath='"C:\\Program Files\\R\\R-3.0.2\\bin\\Rscript.exe"') print ancova.parseResult(ancova.results()) for l in ancova.formatted():
passyr=float(word[:-1].strip()) except: passyr=word[:-2].strip() pass if state == 2: #find the last date should be returned words = line.split() word=words[0] while word[0] == "[" or word[-1]!= "]": word=words.pop(0) try: if word[-2] == ".": finalyr=float(word[:-2].strip()) else: finalyr=float(word[:-1].strip()) except: finalyr=word[:-2].strip() pass if state==3: filename=line state = 4 if state == 4: if passyr!= finalyr: sf=statbreaks.brkrpt(filename[:-1]) brks=sf.breaks() if passyr > brks[-2]: print passyr== finalyr,passyr, finalyr, brks[-2:], filename[:-1] state=0