Mercurial > repos > matces > carpet_toolsuite
comparison carpet-src-1/tools/CARPET/Raw_data.py @ 0:cdd489d98766
Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
| author | matces |
|---|---|
| date | Tue, 07 Jun 2011 16:50:41 -0400 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:cdd489d98766 |
|---|---|
| 1 #!/usr/bin/env python | |
| 2 | |
| 3 # Copyright 2009 Matteo Cesaroni, Lucilla Luzi | |
| 4 # | |
| 5 # This program is free software; ; you can redistribute it and/or modify | |
| 6 # it under the terms of the GNU Lesser General Public License as published by | |
| 7 # the Free Software Foundation; either version 3 of the License, or (at your | |
| 8 # option) any later version. | |
| 9 # | |
| 10 # This program is distributed in the hope that it will be useful, | |
| 11 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 # GNU General Public License for more details. | |
| 14 . | |
| 15 | |
| 16 import sys | |
| 17 from rpy import * | |
| 18 | |
| 19 | |
| 20 def stop_err(msg): | |
| 21 sys.stderr.write(msg) | |
| 22 sys.exit() | |
| 23 | |
| 24 def main(): | |
| 25 | |
| 26 # Handle input params | |
| 27 in_fname = sys.argv[1] | |
| 28 out_fname = sys.argv[2] | |
| 29 #sys.stdout=open('log.txt','a') | |
| 30 try: | |
| 31 column = int( sys.argv[3] ) - 1 | |
| 32 column_x = int( sys.argv[4] ) - 1 | |
| 33 column_y = int( sys.argv[5] ) - 1 | |
| 34 except: | |
| 35 stop_err( "..Column not specified, your query does not contain a column of numerical data." ) | |
| 36 | |
| 37 | |
| 38 title = sys.argv[6] | |
| 39 | |
| 40 skipped_lines = 0 | |
| 41 first_invalid_line = 0 | |
| 42 invalid_value = '' | |
| 43 | |
| 44 riga = [] | |
| 45 for tuo in range(1,1025): | |
| 46 riga.append(int(0)) | |
| 47 #print riga | |
| 48 | |
| 49 matrice = [] | |
| 50 for mio in range(1,769): | |
| 51 #print mio | |
| 52 matrice.append(riga) | |
| 53 | |
| 54 #print matrix | |
| 55 matrix1 = array(matrice) | |
| 56 #print matrix1 | |
| 57 for i, line in enumerate( file( in_fname ) ): | |
| 58 valid = True | |
| 59 line = line.rstrip('\r\n') | |
| 60 # Skip comments | |
| 61 if line and not line.startswith( '#' ): | |
| 62 # Extract values and convert to floats | |
| 63 row = [] | |
| 64 val = 0 | |
| 65 val_x = 0 | |
| 66 val_y = 0 | |
| 67 try: | |
| 68 fields = line.split( "\t" ) | |
| 69 val = fields[column] | |
| 70 val_x = (int(fields[column_x])-1) | |
| 71 val_y = (int(fields[column_y])-1) | |
| 72 matrix1[val_x][val_y]=float(val) | |
| 73 | |
| 74 | |
| 75 except: | |
| 76 valid = False | |
| 77 skipped_lines += 1 | |
| 78 if not first_invalid_line: | |
| 79 first_invalid_line = i+1 | |
| 80 else: | |
| 81 try: | |
| 82 row.append( float( val ) ) | |
| 83 except ValueError: | |
| 84 valid = False | |
| 85 skipped_lines += 1 | |
| 86 if not first_invalid_line: | |
| 87 first_invalid_line = i+1 | |
| 88 invalid_value = fields[column] | |
| 89 else: | |
| 90 valid = False | |
| 91 skipped_lines += 1 | |
| 92 if not first_invalid_line: | |
| 93 first_invalid_line = i+1 | |
| 94 | |
| 95 output_prima=sys.stdout | |
| 96 fsock=open('log.txt','w') | |
| 97 sys.stdout=fsock | |
| 98 | |
| 99 | |
| 100 for i in range(768): | |
| 101 for j in range(1024): | |
| 102 if j<1022: | |
| 103 print "%s\t" %matrix1[i][j], | |
| 104 if j==1023: | |
| 105 print "%s" %matrix1[i][j] | |
| 106 sys.stdout=output_prima | |
| 107 fsock.close() | |
| 108 | |
| 109 set_default_mode(NO_CONVERSION) | |
| 110 if skipped_lines < i: | |
| 111 #print "..on columnn %s" %sys.argv[3] | |
| 112 a=r.read_table("log.txt") | |
| 113 b=r.as_matrix(a) | |
| 114 #r.print_(b) | |
| 115 #b=r.cbind(a[1],a[1]) | |
| 116 r.pdf( out_fname, 8, 8 ) | |
| 117 r.image(z=r.log2(b),col=r.terrain_colors(100000),main=title, xlab="X", ylab="Y") | |
| 118 r.dev_off() | |
| 119 | |
| 120 else: | |
| 121 print "..all values in column %s are non-numeric." %sys.argv[3] | |
| 122 | |
| 123 if skipped_lines > 0: | |
| 124 print "..skipped %d invalid lines starting with line #%d. Value '%s' is not numeric." % ( skipped_lines, first_invalid_line, invalid_value ) | |
| 125 | |
| 126 r.quit( save="no" ) | |
| 127 | |
| 128 if __name__ == "__main__": | |
| 129 main() | |
| 130 |
