summaryrefslogtreecommitdiffstats
path: root/importdata.py
blob: a72f71ea0a7cc083f2c3130d4a2f7665aed6db0a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# @author: bryan newbold
# @email: bnewbold@mit.edu
# @date: nov 2006

import shutil, os, scipy, pylab;

def dothing(arg,thisdir,files):
    global all_data
    for fname in files:
        if os.path.isfile(thisdir + '/' + fname):
            if (fname.find('.') == -1):
                try:
                    dat = pylab.load(thisdir + '/' + fname)
                    all_data[fname.split('.')[0]] = dat
                    print "Loaded (no type): %s/%s" % (thisdir,fname);
                except:
                    print "Tried, but didn't load (no type):  %s/%s" % (thisdir,fname);
            elif fname.endswith('.TKA'):
                try:
                    dat = pylab.load(thisdir + '/' + fname)
                    #print all_data
                    all_data[fname.split('.')[0]] = dat
                    print "Loaded .TKA: %s/%s" % (thisdir,fname);
                    #print "all_data has %d" % len(all_data)
                except:
                    print "Tried, but didn't load .TKA: %s/%s" % (thisdir,fname);
            elif fname.endswith('.txt'):
                try:
                    dat = pylab.load(thisdir + '/' + fname,skiprows=1)
                    #print all_data
                    all_data[fname.split('.')[0]] = dat
                    print "Loaded .txt: %s/%s" % (thisdir,fname);
                    #print "all_data has %d" % len(all_data)
                except:
                    print "Tried, but didn't load .txt: %s/%s" % (thisdir,fname);
            elif fname.endswith('.Spe!!!'):
#                try:
                    dat = pylab.load(thisdir+'/'+fname,skiprows=12);
                    # also have to skip 15 from the end...
                    print "trying: " + fname;
#                    dat = scipy.io.read_array(thisdir + '/' + fname,lines=((14,-40,),))
#                    print len(dat)
#                    print dat
                    dat = scipy.array(dat);
                    all_data[fname.split('.')[0]] = dat
                    print "Loaded .Spe: %s/%s" % (thisdir,fname);
#                except:
                    print "Tried, but didn't load .Spe: %s/%s" % (thisdir,fname);
            else:
                print "Didn't load %s/%s" % (thisdir,fname);
        else:
            print "Wasn't a file: %s/%s" % (thisdir,fname);

def import_data(outfile='data.pydat',root='.',saveit=True,recurse=False):
    """Imports all that yummah files..."""
    global all_data
    if saveit:
        ofile = file(outfile,'w');
        ofile.close()
    all_data = {}
    if recurse: 
        os.path.walk(root,dothing,'')
    else:
        dothing('',root,os.listdir(os.getcwd()));
    if saveit: 
        print "Saving %d objects to %s" % (len(all_data), outfile);
        scipy.io.objsave(ofile.name,globals(),all_data);
    g = globals()
    for a in all_data.keys():
        g[a.replace('-','_')] = all_data[a]

def load_imported_data(infile='data.pydat'):
    global all_data
    scipy.io.objload(infile,globals())
    g=globals()
    for a in all_data.keys():
        if(a.startswith('11')):
            g['d3' + a.replace('-','_')[9:]]=all_data[a]
        else:
            g[a.replace('-','_')]=all_data[a]

def dorenamething(arg,thisdir,files):
    for fname in files:
        if os.path.isfile(thisdir + '/' + fname):
            if fname.endswith('.Spe'):
                try:
                    shutil.copy(thisdir+'/'+fname,thisdir + '/' + fname[:-4]);
                    print "Copied .Spe: %s/%s" % (thisdir,fname);
                except:
                    print "Tried, but didn't load .Spe: %s/%s" % (thisdir,fname);
            else:
                print "Didn't load %s/%s" % (thisdir,fname);
        else:
            print "Wasn't a file: %s/%s" % (thisdir,fname);

def rename_data_files(root='.',ext='.Spe',recurse=False):
    """Only works for .Spe..."""
    if recurse: 
        os.path.walk(root,dorenamething,'')
    else:
        dorenamething('',root,os.listdir(os.getcwd()));