]> git.treefish.org Git - phys/latscripts.git/commitdiff
...
authorAlexander Schmidt <alex@treefish.org>
Fri, 24 Jan 2014 14:12:30 +0000 (15:12 +0100)
committerAlexander Schmidt <alex@treefish.org>
Fri, 24 Jan 2014 14:12:30 +0000 (15:12 +0100)
meanstep.py

index 64f28f0c49aa3a13781a17d29c906e52da9edfee..536e0075cea4170c025fb25160aee233a54cf11b 100755 (executable)
@@ -9,17 +9,28 @@ import os
 
 results = {}
 
-if len(sys.argv) != 4:
-    print "usage: ./meanstep.py <datadir> <matching col 1>,...,<matching col n> <mean col 1>,...,<mean col n>"
+if len(sys.argv) != 4 and len(sys.argv) != 5:
+    print "usage: ./meanstep.py <datadir> <matching col 1>,...,<matching col n> <mean col 1>,...,<mean col n> [<outdir>]"
     exit(0)
 
 matchcols = sys.argv[2].split(",")
+meancols = sys.argv[3].split(",")
+
+if len(sys.argv) == 5:
+    outdir = sys.argv[4]
+    writetofile = True
+else:
+    writetofile = False
 
 for root, dirs, filenames in os.walk(sys.argv[1]):
     for f in filenames:
         if f[-4:] != ".dat":
             continue
 
+        if not f[:-4] in results:
+            results[f[:-4]] = {}
+            print "Found new fileid: " + f[:-4]
+
         with open(root + "/" + f) as inf:
             for line in inf:
                 if line[0:1] == "#":
@@ -28,8 +39,44 @@ for root, dirs, filenames in os.walk(sys.argv[1]):
                 linecols = line.split()
                 matchkey = ""
                 for imatchcol in range(0,len(matchcols)):
-                    matchkey = matchkey + linecols[int(matchcols[imatchcol])]
-                
+                    matchkey = matchkey + linecols[int(matchcols[imatchcol])-1]
                 
+                if not matchkey in results[f[:-4]]:
+                    results[f[:-4]][matchkey] = linecols + [1]
+                else:
+                    results[f[:-4]][matchkey][ len(results[f[:-4]][matchkey]) - 1 ] = \
+                        results[f[:-4]][matchkey][ len(results[f[:-4]][matchkey]) - 1 ] + 1 
+         
+                    for imeancol in range(0,len(meancols)):
+                        results[f[:-4]][matchkey][int(meancols[imeancol])-1] = \
+                            float(results[f[:-4]][matchkey][int(meancols[imeancol])-1]) + \
+                            float(linecols[int(meancols[imeancol])-1])
+
+for fileid in results:
+    print "\n[" + fileid + "]"
+
+    if writetofile:
+        outfile = open(outdir + "/" + fileid + "_meanstep.dat", "w+")
+
+    for matchkey in results[fileid]:
+        for imeancol in range(0,len(meancols)):
+            results[fileid][matchkey][int(meancols[imeancol])-1] = \
+                float(results[fileid][matchkey][int(meancols[imeancol])-1]) / \
+                results[fileid][matchkey][ len(results[f[:-4]][matchkey]) - 1 ]
+        
+        for icol in range(0,len(results[fileid][matchkey])-1):
+            sys.stdout.write(str(results[fileid][matchkey][icol]))
+            if writetofile:
+                outfile.write(str(results[fileid][matchkey][icol]));
+
+            if icol != len(results[fileid][matchkey])-2:
+                sys.stdout.write("\t")
+                if writetofile:
+                    outfile.write("\t")
+                                  
+        print ""
+        if writetofile:
+            outfile.write("\n")
 
-        #results[f[:-4]] = "tefds"
+    if writetofile:
+        outfile.close()