[3006] | 1 | """Read realisations and return MxN Numeric array: data, where M is the number of timesteps and N the number of realisations |
---|
| 2 | """ |
---|
| 3 | |
---|
| 4 | import os |
---|
| 5 | import project |
---|
| 6 | from Numeric import Float, zeros |
---|
[3035] | 7 | from RandomArray import randint |
---|
[3006] | 8 | from caching import cache |
---|
| 9 | |
---|
| 10 | def read_realisations(subdir, max_realisations = None, gauge_number=0, |
---|
[3035] | 11 | exclude=None, |
---|
| 12 | sorting=None, |
---|
| 13 | use_cache=False, |
---|
| 14 | verbose=True): |
---|
[3006] | 15 | """Read realisations |
---|
| 16 | """ |
---|
| 17 | |
---|
| 18 | if use_cache is True: |
---|
| 19 | result = cache(_read_realisations, |
---|
| 20 | (subdir,), |
---|
| 21 | {'max_realisations': max_realisations, |
---|
| 22 | 'gauge_number': gauge_number, |
---|
[3035] | 23 | 'exclude': exclude, |
---|
| 24 | 'sorting': sorting}, |
---|
[3006] | 25 | verbose=verbose) |
---|
| 26 | else: |
---|
| 27 | result = _read_realisations(subdir, |
---|
| 28 | max_realisations = max_realisations, |
---|
| 29 | gauge_number=gauge_number, |
---|
[3035] | 30 | exclude=exclude, |
---|
| 31 | sorting=sorting) |
---|
[3006] | 32 | |
---|
| 33 | return result |
---|
| 34 | |
---|
[3035] | 35 | |
---|
[3006] | 36 | def _read_realisations(subdir, max_realisations = None, gauge_number=0, |
---|
[3035] | 37 | exclude=None, sorting=None, |
---|
| 38 | use_cache=False, verbose=True): |
---|
[3006] | 39 | """Read realisations |
---|
| 40 | """ |
---|
| 41 | |
---|
| 42 | |
---|
| 43 | gauge = '%s.txt' %project.gauge_names[gauge_number] |
---|
| 44 | |
---|
| 45 | if subdir[-1] != os.sep: |
---|
| 46 | subdir += os.sep |
---|
| 47 | |
---|
| 48 | # Establish dimensions and record filenames |
---|
| 49 | filenames = [] |
---|
| 50 | for filename in os.listdir(project.working_dir + subdir): |
---|
| 51 | if filename.startswith(project.basename) and filename.endswith(gauge): |
---|
| 52 | if exclude is not None and filename.find(exclude) >= 0: |
---|
| 53 | print 'Excluded: %s' %filename |
---|
| 54 | else: |
---|
| 55 | filenames.append(project.working_dir + subdir + filename) |
---|
| 56 | |
---|
| 57 | if max_realisations is not None and len(filenames) == max_realisations: |
---|
| 58 | break |
---|
| 59 | |
---|
| 60 | time = zeros(project.number_of_timesteps, Float) |
---|
| 61 | data = zeros((project.number_of_timesteps, len(filenames)), Float) |
---|
[3035] | 62 | |
---|
| 63 | if sorting is None: |
---|
| 64 | pass |
---|
| 65 | elif sorting == 'numerical': |
---|
| 66 | filenames = sort_numerically(filenames) |
---|
| 67 | elif sorting == 'randomised': |
---|
| 68 | filenames = randomise(filenames) |
---|
| 69 | else: |
---|
| 70 | print 'Invalid value for sorting: %s' %sorting |
---|
| 71 | import sys; sys.exit() |
---|
| 72 | |
---|
| 73 | |
---|
[3006] | 74 | # Read data |
---|
| 75 | |
---|
| 76 | for j, filename in enumerate(filenames): |
---|
| 77 | print 'Reading filename %s (column %d)' %(filename, j) |
---|
| 78 | fid = open(filename) |
---|
| 79 | for i, line in enumerate(fid.readlines()): |
---|
| 80 | if i < data.shape[0]: |
---|
| 81 | fields = line.strip().split() |
---|
| 82 | time[i] = float(fields[0]) |
---|
| 83 | data[i,j] = float(fields[1]) |
---|
| 84 | |
---|
| 85 | fid.close() |
---|
| 86 | |
---|
| 87 | |
---|
| 88 | return time, data, filenames |
---|
[3035] | 89 | |
---|
| 90 | |
---|
| 91 | def sort_numerically(filenames): |
---|
| 92 | """Sort filenames numerically |
---|
| 93 | simulation_realisation_n_ch.... (sort by n) |
---|
| 94 | """ |
---|
| 95 | |
---|
| 96 | print 'Sorting' |
---|
| 97 | sorted_filenames = [] |
---|
| 98 | |
---|
| 99 | numbers = [] |
---|
| 100 | for filename in filenames: |
---|
| 101 | fields = filename.split('_') |
---|
| 102 | n = int(fields[2]) |
---|
| 103 | numbers.append(n) |
---|
| 104 | |
---|
| 105 | A = zip(numbers, filenames) |
---|
| 106 | A.sort() |
---|
| 107 | |
---|
| 108 | sorted_filenames = [a[1] for a in A] |
---|
| 109 | |
---|
| 110 | return sorted_filenames |
---|
| 111 | |
---|
| 112 | |
---|
| 113 | def randomise(filenames): |
---|
| 114 | """Randomise filenames |
---|
| 115 | """ |
---|
| 116 | |
---|
| 117 | print 'Randomising' |
---|
| 118 | randomised_filenames = [] |
---|
| 119 | |
---|
| 120 | |
---|
| 121 | while len(filenames) > 0: |
---|
| 122 | n = randint(0, len(filenames)) |
---|
| 123 | filename = filenames[n] |
---|
| 124 | del filenames[n] |
---|
| 125 | randomised_filenames.append(filename) |
---|
| 126 | |
---|
| 127 | return randomised_filenames |
---|