-
Notifications
You must be signed in to change notification settings - Fork 3
/
PyASCII.py
executable file
·1259 lines (1079 loc) · 50.1 KB
/
PyASCII.py
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
PyASCII is a module of PySHEMAT, a free set of Python modules to create and process input
files for fluid and heat flow simulation with SHEMAT (http://137.226.107.10/aw/cms/website/zielgruppen/gge/research_gge/~uuv/Shemat/?lang=de)
******************************************************************************************
PySHEMAT is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
PySHEMAT is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with PyTOUGH. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************************
For module, update and documentation of PySHEMAT please see:
https://github.com/flohorovicic/PySHEMAT
If you use PySHEMAT for a scientific study, please cite our publication in Computers and Geoscience
PyASCII provides methods to handle of ASCII-Gridfiles:
- load as Object
- define Object Overload:
- __add__: add two ASCII Gridobjects (and save as new one)
- other calculations possible (multiply __mul__, __sub__, ...)
- __repr__: print with header
- __len__: ASCII Grid dimensions and discretisation (makes comparison easier?)
- __cmp__: Comparison: header and data!
- grid trim to given Values, if compatible with header data
(be careful not to change absolute data positioning!)
- methods to analyse
- methods to plot??
- methods to write to file (incl. conversion/ formatting for FracSYS)
- methods to write as xyz
- methods to cut to given parameters (x, y, but also z- Values?)
- methods to perform calculations?
- cross-check data_array_3D data to exported XYZ grid with VerticalMappper!!
"""
from sys import exit
import string
import numpy as np
class ASCII_File:
def __init__(self, *args):
# read ASCII_File directly with object instantiation
if len(args) != 0:
file_name = args[0]
self.file_name_str = file_name
self.f_ascii = self.load_grid(file_name)
self.header = self.read_header(self.f_ascii)
# store data in list to avoid problems of pointer in file...
self.data = self.f_ascii.readlines()
else:
# create default Attributes
self.header = {}
self.data = []
self.file_name_str = "__no_Filename_given__"
def __repr__(self):
"""define Object representation: header and data in string, can be
used to output Object"""
# ToDo: output format as FracSYS input format -> make conversion easier
# print header
header_str = ''
header_str += "ncols %d\n" % self.header['ncol']
header_str += "nrows %d\n" % self.header['nrow']
header_str += "xllcorner %d\n" % self.header['xllcorner']
header_str += "yllcorner %d\n" % self.header['yllcorner']
header_str += "cellsize %d\n" % self.header['cellsize']
header_str += "NODATA_value %f\n" % self.header['NODATA_value']
# print data out of data_array row per row
data_str = ''
for row in self.data_array:
line_str = ''
for val in row:
line_str += "%f " % val
line_str += "\n"
data_str += line_str
return header_str + data_str
def __sub__(self, other_AFO):
"""overload - function: substract values of two ASCII File objects
if header data are equal
returns new AFO with substracted values and same header data and
NODATA_values of both grids
flo 04/2008"""
# test, if header data are equal
other_AFO.check_data_array()
if not self.header_equal_to(other_AFO):
print "Header Data not equal or not an ASCII Fiel Object, substraction not reasonable..."
return
try:
from numpy import array
except ImportError:
print "Module NumPy not found but needed for calulation! Install/ check and try again"
return
self.check_data_array()
other_AFO.check_data_array()
data1 = array(self.data_array)
data2 = array(other_AFO.data_array)
# keep NODATA values -> not possible as simple calculation with numpy...
# or: try with masks? Advantage: once created, can be used for several calculations -> save time?
result = data1 - data2
# evaluate NODATA_Values with masks
self.check_mask()
other_AFO.check_mask()
data1_mask = array(self.data_array_mask)
data2_mask = array(other_AFO.data_array_mask)
# evaluate, where both grids have values and substract to get again a mask with 0 and 1 only
# and keep only values, where both grids have data
data_out_mask = (data1_mask + data2_mask) // 2
AFO_out = ASCII_File()
AFO_out.data_array = list(result)
# check with mask for NODATA_values and change in data_array
AFO_out.header = self.header
print AFO_out.header
AFO_out.set_NODATA_values(list(data_out_mask), AFO_out.header['NODATA_value'])
return AFO_out
def __add__(self, other_AFO):
"""overload - function: substract values of two ASCII File objects
if header data are equal
returns new AFO with substracted values and same header data and
NODATA_values of both grids
flo 04/2008"""
# test, if header data are equal
other_AFO.check_data_array()
if not self.header_equal_to(other_AFO):
print "Header Data not equal or not an ASCII Fiel Object, substraction not reasonable..."
return
try:
from numpy import array
except ImportError:
print "Module NumPy not found but needed for calulation! Install/ check and try again"
return
self.check_data_array()
other_AFO.check_data_array()
data1 = array(self.data_array)
data2 = array(other_AFO.data_array)
# keep NODATA values -> not possible as simple calculation with numpy...
# or: try with masks? Advantage: once created, can be used for several calculations -> save time?
result = data1 + data2
# evaluate NODATA_Values with masks
self.check_mask()
other_AFO.check_mask()
data1_mask = array(self.data_array_mask)
data2_mask = array(other_AFO.data_array_mask)
# evaluate, where both grids have values and substract to get again a mask with 0 and 1 only
# and keep only values, where both grids have data
data_out_mask = (data1_mask + data2_mask) // 2
AFO_out = ASCII_File()
AFO_out.data_array = list(result)
# check with mask for NODATA_values and change in data_array
AFO_out.header = self.header
print AFO_out.header
AFO_out.set_NODATA_values(list(data_out_mask), AFO_out.header['NODATA_value'])
return AFO_out
def __mul__(self, other_AFO):
"""overload - function: multiply values of two ASCII File objects
if header data are equal
returns new AFO with multiplied values and same header data and
NODATA_values of both grids
flo 04/2008"""
# test, if header data are equal
other_AFO.check_data_array()
if not self.header_equal_to(other_AFO):
print "Header Data not equal or not an ASCII Fiel Object, substraction not reasonable..."
return
try:
from numpy import array
except ImportError:
print "Module NumPy not found but needed for calulation! Install/ check and try again"
return
self.check_data_array()
other_AFO.check_data_array()
data1 = array(self.data_array)
data2 = array(other_AFO.data_array)
# keep NODATA values -> not possible as simple calculation with numpy...
# or: try with masks? Advantage: once created, can be used for several calculations -> save time?
result = data1 * data2
# evaluate NODATA_Values with masks
self.check_mask()
other_AFO.check_mask()
data1_mask = array(self.data_array_mask)
data2_mask = array(other_AFO.data_array_mask)
# evaluate, where both grids have values and substract to get again a mask with 0 and 1 only
# and keep only values, where both grids have data
data_out_mask = (data1_mask + data2_mask) // 2
AFO_out = ASCII_File()
AFO_out.data_array = list(result)
# check with mask for NODATA_values and change in data_array
AFO_out.header = self.header
print AFO_out.header
AFO_out.set_NODATA_values(list(data_out_mask), AFO_out.header['NODATA_value'])
return AFO_out
def __div__(self, other_AFO):
"""overload - function: divide values of two ASCII File objects
if header data are equal
returns new AFO with divided values and same header data and
NODATA_values of both grids
flo 04/2008"""
# test, if header data are equal
other_AFO.check_data_array()
if not self.header_equal_to(other_AFO):
print "Header Data not equal or not an ASCII Fiel Object, substraction not reasonable..."
return
try:
from numpy import array
except ImportError:
print "Module NumPy not found but needed for calulation! Install/ check and try again"
return
self.check_data_array()
other_AFO.check_data_array()
data1 = array(self.data_array)
data2 = array(other_AFO.data_array)
# keep NODATA values -> not possible as simple calculation with numpy...
# or: try with masks? Advantage: once created, can be used for several calculations -> save time?
result = data1 / data2
# evaluate NODATA_Values with masks
self.check_mask()
other_AFO.check_mask()
data1_mask = array(self.data_array_mask)
data2_mask = array(other_AFO.data_array_mask)
# evaluate, where both grids have values and substract to get again a mask with 0 and 1 only
# and keep only values, where both grids have data
data_out_mask = (data1_mask + data2_mask) // 2
AFO_out = ASCII_File()
AFO_out.data_array = list(result)
# check with mask for NODATA_values and change in data_array
AFO_out.header = self.header
print AFO_out.header
AFO_out.set_NODATA_values(list(data_out_mask), AFO_out.header['NODATA_value'])
return AFO_out
def size(self):
"""Calculate grid extend
returns (x_min, x_max, y_min, y_max)
flo, 04/2008"""
x_min = self.header['xllcorner']
x_max = x_min + self.header['ncol'] * self.header['cellsize']
y_min = self.header['yllcorner']
y_max = y_min + self.header['nrow'] * self.header['cellsize']
return((x_min, x_max, y_min, y_max))
def resize_grid(self, extend):
"""Trim grid to given extend; only performed, if in accordance with header data
and absolute positioning is not changed:
remainder of (x_min - xllcorner) % 500 should be 0 (other points equivalent)
if new grid extends old grid: fill with NODATA_values (does not work yet!!!)
extend is tuple with (x_min, x_max, y_min, y_max) as produced with ASCII_File.size()
x_min: will be new xllcorner
y_min: will be new yllcorner
x_max: not included (check if senseful!!!)
y_max: not included
Caution: AFO itself is changed!
flo, 04/2008"""
x_min = extend[0]
x_max = extend[1]
y_min = extend[2]
y_max = extend[3]
# check, if given values are in accordance to grid metadata
if not (
((x_min - self.header['xllcorner']) % self.header['cellsize'] == 0) &
((x_max - self.header['xllcorner']) % self.header['cellsize'] == 0) &
((y_min - self.header['yllcorner']) % self.header['cellsize'] == 0) &
((y_max - self.header['yllcorner']) % self.header['cellsize'] == 0)):
print "Grid resizing not possible, given values not in accordance to header data"
# calculate offset of x and y values
x_min_offset = (x_min - self.header['xllcorner']) // self.header['cellsize']
x_max_offset = (x_max - self.header['xllcorner']) // self.header['cellsize']
y_min_offset = (y_min - self.header['yllcorner']) // self.header['cellsize']
y_max_offset = (y_max - self.header['yllcorner']) // self.header['cellsize']
# check, if values are in current grid extend
if not ((0 < x_min_offset < self.header['ncol']) &
(0 < x_max_offset < self.header['ncol']) &
(0 < y_min_offset < self.header['nrow']) &
(0 < y_max_offset < self.header['nrow'])):
print "Grid resizing not possible, given values extend grid"
return
# calculate new grid
self.check_data_array()
# process every row separately: is there a more elegant way to perform the slicing?
data_array_temp = []
# Caution! Data rows are in reverse order, as llcorner as reference!
self.data_array.reverse()
for i in range(int(y_min_offset), int(y_max_offset)):
row = self.data_array[i]
data_array_temp.append(row[int(x_min_offset):int(x_max_offset)])
data_array_temp.reverse()
self.data_array = data_array_temp
# adjust header data
self.header['xllcorner'] = x_min
self.header['yllcorner'] = y_min
self.header['ncol'] = (x_max_offset - x_min_offset)
self.header['nrow'] = (y_max_offset - y_min_offset)
return
def header_equal_to(self, other_AFO):
"""Check if header data of ASCII File object instance are equal to other
AFO instance
returns boolean
flo 04/2008"""
# test, if other_AFO is really an ASCII File Object
if not isinstance(other_AFO, ASCII_File):
print "Not an ASCII File object!"
return False
if self.header == other_AFO.header:
return True
else:
return False
def load_grid(self, file_name):
# print "load File " + file_name
try:
f_ascii = open(file_name)
except IOError, (nr, string_err):
print "\n\tNot able to open file:", string_err
print "\tPlease check file name and run program again\n"
exit(0)
return(f_ascii)
def read_header(self, f_ascii):
""" returns the 6 line header as a dict"""
# print "read Header of file " + self.file_name_str
header = {}
line = f_ascii.readline().split()
header['ncol'] = int(line[1])
line = f_ascii.readline().split()
header['nrow'] = int(line[1])
line = f_ascii.readline().split()
header['xllcorner'] = float(line[1])
line = f_ascii.readline().split()
header['yllcorner'] = float(line[1])
line = f_ascii.readline().split()
header['cellsize'] = float(line[1])
line = f_ascii.readline().split()
header['NODATA_value'] = int(line[1])
return(header)
def import_SHEMAT_2D_array(self, S1, property_xy, **kwds):
"""Import a 2D array, created from a PySHEMAT object
This method can be used to import a 2D array created from a PySHEMAT object,
for example mean temperatures of a specific formation, created with:
temperature_xy = S1.calculate_mean_form_temp(formation_id);
The header of the PyASCII object is adapted according to the dimensions
of the PySHEMAT object.
..Attention..: ASCII grid object are per definition regular grid objects!
Therefore, the conversion only works when the original SHEMAT grid is completely regular (dx = dy).
**Arguments**:
- *S1* = PySHEMAT.Shemat_file : original SHEMAT object (for dimensions, etc.)
- *property_xy* = 2D property array, created with PySHEMAT
**Optional Keywords**:
- *set_nodata_value* = True/ False : set Nodata value for a defined range
- *nodata_range_min* = float : minimum for nodata range
- *nodata_range_max* = float : maximum for nodata range
- filename = string : filename of ASCII grid file
**Returns**: None
"""
# this has to be defined:
self.header = {'ncol' : int(S1.get("IDIM")),
'nrow' : int(S1.get("JDIM")),
'xllcorner' : float(S1.get("I0")),
'yllcorner' : float(S1.get("J0")),
'cellsize' : float(S1.get_array("DELX")[0]),
'NODATA_value' : -9999
}
# set nodata value, if required
if kwds.has_key('set_nodata_value') and kwds['set_nodata_value']:
for i,t in enumerate(property_xy):
if t < kwds['nodata_range_max'] and t > kwds['nodata_range_min']:
property_xy[i] = self.header['NODATA_value']
# data organisation in ASCII grid: one row per list entry
n = 0
rows_tmp = []
for row in range(self.header['nrow']):
data_row = []
for col in range(self.header['ncol']):
data_row.append(property_xy[n])
n += 1
rows_tmp.append(data_row)
# reverse rows_temp because of strange orientation of lines in ASCII
# grid file (first line corresponds to most Northern line)
rows_tmp.reverse()
self.data_array = rows_tmp
if kwds.has_key('filename'):
self.file_name_str = kwds['filename']
else:
self.file_name_str = "imported_from_PySHEMAT"
def write_file(self, **kwds):
"""Write ASCII grid to file
Write the ASCII grid object to a .txt file. This file should be usable
for both MapInfo and ArcGIS. Per default, the filename defined in
self.file_name_str is used, but another filename can be defined with
an optional keyword.
**Optional Keywords**:
- *filename* = string : filename (with or without extension); default: self.file_name_str
- *path* = string : path where file is saved, default: cwd
"""
from os import path, chdir, getcwd
if kwds.has_key('filename'):
if path.splitext(kwds['filename'])[1] == "":
filename = kwds['filename']+".txt"
else:
filename = kwds['filename']
else:
if path.splitext(self.file_name_str)[1] == "":
filename = self.file_name_str+".txt"
else:
filename = self.file_name_str
if kwds.has_key('path'):
ori_dir = getcwd()
chdir(path)
f = open(filename, 'w')
f.write(repr(self))
f.close()
if kwds.has_key('path'):
# return to original working directory
chdir(ori_dir)
def convert_SHEMAT_results_to_header(self, S1, property_xy):
"""convert a calulcated SHEMAT property array to an ASCII grid
using the original SHEMAT object to set the header properties
!!! ATTENTION: ASCII grid only for regular space grids with dx = dy!
S1 : Shemat Object
property_xy : property array (1D) with 2.5 D grid info as written by various
Shemat_file methods, e.g. mean properties
"""
# this has to be defined:
self.header = {'ncol' : int(S1.get("IDIM")),
'nrow' : int(S1.get("JDIM")),
'xllcorner' : float(S1.get("I0")),
'yllcorner' : float(S1.get("J0")),
'cellsize' : float(S1.get_array("DELX")[0]),
'NODATA_value' : -9999
}
# data organisation in ASCII grid: one row per list entry
n = 0
rows_tmp = []
for row in range(self.header['nrow']):
data_row = []
for col in range(self.header['ncol']):
data_row.append(property_xy[n])
n += 1
rows_tmp.append(data_row)
# reverse rows_temp because of strange orientation of lines in ASCII
# grid file (first line corresponds to most Northern line)
rows_tmp.reverse()
self.data_array = rows_tmp
self.file_name_str = "__no_Filename_given__"
def save(self, filename, **kwds):
"""save ASCII grid to file;
filename = string : filename of grid file
optional keywords:
dir = directory path : path where file is saved"""
if kwds.has_key('dir'):
from os import getcwd, chdir
ori_dir = getcwd()
chdir(kwds['dir'])
# write ASCII grid to new file
myfile = open(filename, 'w')
# use repr to derive a string representation of the Object, as defined in __repr__
myfile.write(repr(self))
myfile.close()
# go back to original directory
if kwds.has_key('dir'):
chdir(ori_dir)
def print_detailed_header_data(self):
# print header data to output (flo, 04/2008)
# print "Number of Columns:\t%d" % self.header['ncol']
# print "Number of Rows:\t\t%d" % self.header['nrow']
print "%s is a %d x %d grid (columns x rows)" % (self.file_name_str, self.header['ncol'], self.header['nrow'])
print "with "
print "\tlower-left corner at \t(%d, %d)" % (self.header['xllcorner'], self.header['yllcorner'])
print "\tupper-right corner at \t(%d, %d)" % (self.header['xllcorner']+self.header['ncol']*self.header['cellsize'],
self.header['yllcorner']+self.header['nrow']*self.header['cellsize'])
print "and a cellsize of %d" % self.header['cellsize']
# what for???
def float2str(self, number):
""" strips off trailing zeros from coordinates """
s = "%f" % number
if "." in s:
s = s.rstrip("0").rstrip(".")
return s
## def process_z_values_to_array(self):
## x_list = self.xcoords(self.header)
## y_list = self.ycoords(self.header)
## return self.grid_z_values_to_array(self.data, self.header, x_list, y_list)
def xcoords(self, header):
""" calculates x coordinates of a grid"""
x = []
for col in range(header['ncol']):
x.append(ASCII_File.float2str(self,header['xllcorner'] + (0.5 * header['cellsize']) + (col * header['cellsize'])))
return(x)
def ycoords(self, header):
""" calculates y coordinates of a grid"""
y = []
for (j, row) in enumerate(range(header['nrow'])):
y.append(ASCII_File.float2str(self,header['yllcorner'] -
(0.5 * header['cellsize']) + (header['nrow'] - row) * header['cellsize']))
return(y)
def process_z_values_to_array(self):
""" Read data in list and write to array
..Note: This function returns a list of the data lines, i.e. one list entry
per y-value (2-D), and not a 1-D list of the data itself.
"""
# if talk: print "\tread data file line by line"
self.data_array = []
# L_col = []
for row in range(self.header['nrow']):
for col in range(self.header['ncol']):
# Get new data if necessary
if col == 0:
L_col = []
line = self.data[row].split()
# Write output to array
L_col.append(string.atof(line[col]))
self.data_array.append(L_col)
return self.data_array
def export_to_meshgrid(self):
"""Export ASCII grid into a format than can be used with meshgrid in Matlab/ Pylab
This method can be used to export the mesh structure and the data to create
a plot with the meshgrid functions, used in Matlab and Pylab. The method exports
the x- and y-coordinates of the grid in a 1-D array and the z-data in a x-dominant
1-D array.
**Returns:
*(x_coords, y_coords, z_values)
"""
# determine x and y corrds, can be done with predefined functions
xcoords = [float(x) for x in self.xcoords(self.header)]
ycoords = [float(y) for y in self.ycoords(self.header)]
# get z-values
self.get_z_values()
return (xcoords, ycoords, self.z_values)
def check_data_array(self):
""" Check if data_array already exists, if not -> create """
try:
self.data_array
except AttributeError:
# print "Create z-Value Data array"
self.process_z_values_to_array()
def check_mask(self):
"""Check, if data_array_masks exisist, if not -> create """
try:
self.data_array_mask
except AttributeError:
print "Create Mask for Data with values:"
print "0: if NODATA_value, 1: else"
self.create_z_value_mask()
def check_hist(self):
"""Check, if hist_data exists, if not -> create """
try:
self.hist_data
except AttributeError:
# print "Calculate histogram data"
self.calculate_histogram()
def set_NODATA_values(self, data_mask, NODATA_value = -9999):
"""write NODATA_value (defined in header) to all positions, where
data_mask == 0
flo, 04/2008"""
# Maybe all this mask-thing too complicated?? Advantage: can be handled
# with Matrix functionality implemented in numpy...
try:
from numpy import array
except ImportError:
print "Module NumPy not found but needed for calulation! Install/ check and try again"
return
data_mask = array(data_mask)
data_mask_inv = 1 - data_mask
self.check_data_array()
data_array = array(self.data_array)
data_array = data_array * data_mask + data_mask_inv * NODATA_value
self.data_array = list(data_array)
def create_z_value_mask(self):
""" Create grid mask out of data_array (0 for 'NODATA_value', 1 else) """
# Check if data_array already exists, if not -> create
## try:
## self.data_array
## except AttributeError:
## print "Create z-Value Data array"
## self.process_z_values_to_array
self.check_data_array()
self.data_array_mask = [] # = self.data_array[:][:]
# TO DO: simplify with "list comprehensives"???
# self.data_array_mask = [0 for val in self.data_array if val=='-9999']
for row in self.data_array:
data_row = []
for val in row:
if val == self.header['NODATA_value']:
data_row.append(0)
else:
data_row.append(1)
self.data_array_mask.append(data_row)
##
##
## for val in self.data_array:
## if j==2:
## print val
##
def process_xyz_values_to_3D_array(self):
"""Read data and store X,Y,Z values in 3D array, flo 03/2008 """
self.data_array_3D = []
# read self.x_data and self.y_data if they do not already exist...
# to save computation time??
try:
(self.x_data, self.y_data)
except AttributeError:
# print "Create x- and y- coordinates"
self.x_data = self.xcoords(self.header)
self.y_data = self.ycoords(self.header)
for row in range(self.header['nrow']):
for col in range(self.header['ncol']):
# Get new data if necessary
if col == 0:
L_col = []
line = self.data[row].split()
L_col.append([self.x_data[col], self.y_data[row], line[col]])
self.data_array_3D.append(L_col)
return self.data_array_3D
def calculate_histogram(self):
"""Read Data from data_array and create histogram over z-values
uses matplotlib/
"""
# test, if data_array exists, if not -> create
try:
self.data_array
except AttributeError:
# print "Create z-Value Data array"
self.process_z_values_to_array()
self.hist_data = []
for row in self.data_array:
for val in row:
if val != self.header['NODATA_value']:
self.hist_data.append(val)
# convert to numpy array
self.hist_data = np.array(self.hist_data)
def get_z_values(self, **kwds):
"""Get z-values from grid and write to 1-D x-dominant data array
**Optional Keywords**:
- *nodata_value* = float : set nodata entries to specific value
"""
try:
self.data_array
except AttributeError:
# print "Create z-Value Data array"
self.process_z_values_to_array()
self.z_values = []
for row in self.data_array:
for val in row:
if val == self.header['NODATA_value'] and kwds.has_key('nodata_value'):
self.z_values.append(kwds['nodata_value'])
else:
self.z_values.append(val)
def plot_ASCII_grid_histogram(self, n=100, **kwds):
"""Plot histogram created with self.calculate_histogram()
**Arguments**:
- *n* = int : number of bins (default=100)
**Optional Keywords**:
- *smooth* = bool : create a smoothed version (default: False)
- *add_stats* = bool : add percentile lines in plot (default: False)
.. note:: Statistics are calculated for original (not scaled) dataset!
- *return_stats* = bool : return statistics as dictionary
- *exclude_zero* = bool : exclude zero values (default: False)
- *figsize* = (x,y) : matplotlib figsize
- *savefig* = bool : save figure to file (default: False)
- *fig_filename* = string : filename (default: "Histogram fname")
- *vmin* = float : lower limit
- *vmax* = float : limit to maximum value
- *title* = string : title of plot
"""
# test, if self.hist_data exists, if not -> create
try:
self.hist_data
except AttributeError:
self.calculate_histogram()
# Import matplotlib modules, test, if matplotlib installed
try:
import matplotlib.pyplot as plt
except ImportError:
print "Sorry, Module matplotlib is not installed."
print "Histogram can not be plotted."
print "Install Matplotlib and try again ;-) "
return
# set flags
exclude_zero = kwds.get("exclude_zero", False)
smooth = kwds.get("smooth", False)
add_stats = kwds.get("add_stats", False)
savefig = kwds.get("savefig", False)
figsize = kwds.get("figsize", (6,4))
fig_filename = kwds.get("fig_filename", "histogram_%s.png" % self.file_name_str)
return_stats = kwds.get("return_stats", False)
title = kwds.get("title", "")
if exclude_zero:
h_data = self.hist_data[self.hist_data > 0]
if kwds.has_key("vmax"):
h_data = h_data[h_data < kwds['vmax']]
if kwds.has_key("vmin"):
h_data = h_data[h_data > kwds['vmin']]
# generate figure
fig = plt.figure(figsize=figsize)
ax = fig.add_subplot(111)
if smooth:
# first: get histogram data
h1 = np.histogram(h_data, bins = n)
x = h1[1][:-1] # note: should use centroids, but too lazy
y = h1[0]
from scipy.interpolate import interp1d
f2 = interp1d(x, y, kind='cubic')
x_new = np.linspace(1.01*min(h_data),0.99*max(h_data),len(x)/2.)
ax.fill_between(x_new,f2(x_new), color='0.1')
ax.set_xlim((min(h_data), max(h_data)))
_, ymax = ax.get_ylim()
ax.set_ylim((0,ymax))
ax.set_title(title)
else:
# plot normal histogram
ax.hist(h_data,n, color = '0.1', lw=0, fc = '0.1')
ax.set_xlim((min(h_data), max(h_data)))
ax.set_title(title)
if add_stats:
# calculate statistics and add on plot:
# compute statistics
# define light color
col = '1.0'
med = np.median(self.hist_data)
p5 = np.percentile(self.hist_data,5)
p25 = np.percentile(self.hist_data,25)
p75 = np.percentile(self.hist_data,75)
p95 = np.percentile(self.hist_data,95)
ax.axvline(med, c='#CC0000', lw=2)
ax.axvline(p25, c=col, lw=2)
ax.axvline(p75, c=col, lw=2)
ax.set_xlabel("Thickness")
ax.set_ylabel("Counts")
if savefig:
plt.savefig(fig_filename)
else:
plt.show()
if return_stats:
return {'p5' : p5,
'p25' : p25,
'median' : med,
'p75' : p75,
'p95' : p95}
def plot_ASCII_grid_2D(self, **kwds):
"""Create 2D plot of ASCII grid
**Optional keywords**:
- *filename* = string : filename (and, implicitly, the format) of plot file
- *cmap* = maptlotlib colormap: colormap for plot (default: gray)
- *figsize* = (float, float) : figure size in x,y (default: 8,6)
- *colorbar* = bool : plot colorbar (default: True)
- *title* = string : plot title (default: filename)
- *vmin* = float : minimum valule to plot (default: min of data)
- *vmax* = float : maximum value to plot (default: max of data)
- *ax* = matplotlib.axis : axis object to append plot (axis is returned!)
- *interpolation* = 'spline36', 'nearest', etc. : matplotlib interpolation types
(default: spline36)
- *fraction* = float [0-1] : fraction of width for colorbar (default: 0.15)
- *colorbar_title* = string: title of colorbar
- *rotate_labels* = bool : rotate y-axis labels (default: False)
- *max_labels_x* = int : maximum number of labels on x-axis
- *max_labels_y* = int : maximum number of labels on y-axis
- *adjust_coords* = bool : adjust axes coordinates to grid (default: cell number)
-
"""
# check all keywords and assign default values
cmap = kwds.get("cmap", 'gray')
figsize = kwds.get("figsize", (8,6))
colorbar = kwds.get("colorbar", True)
title = kwds.get("title", self.file_name_str)
interpolation = kwds.get("interpolation", "spline36")
fraction = kwds.get("fraction", 0.15)
colorbar_title = kwds.get("colorbar_title", "")
rotate_labels = kwds.get("rotate_labels", False)
self.check_hist()
vmin = kwds.get("vmin", min(self.hist_data))
vmax = kwds.get("vmax", max(self.hist_data))
# hack to adjust labels
from matplotlib.ticker import MultipleLocator, FormatStrFormatter
from matplotlib import rcParams
majorLocator = MultipleLocator(50)
majorFormatter = FormatStrFormatter('%d')
minorLocator = MultipleLocator(25)
rcParams.update({'font.size': 15})
# read self.x_data and self.y_data if they do not already exist...
# to save computation time??
try:
(self.x_data, self.y_data)
except AttributeError:
# print "Create x- and y- coordinates"
self.x_data = self.xcoords(self.header)
self.y_data = self.ycoords(self.header)
# check, if data_array with z values is already created
try:
self.data_array
except AttributeError:
# print "Create z-Value Data array"
self.process_z_values_to_array
# Import matplotlib modules, test, if matplotlib installed
# export test to separate function?
try:
import matplotlib.pyplot as plt
except ImportError:
print "Sorry, Module matplotlib is not installed."
print "Histogram can not be plotted."
print "Install Matplotlib and try again ;-) "
return
# if axes is not passed as keyword, create stand alone figure
if kwds.has_key('ax'):
ax = kwds['ax']
else:
fig = plt.figure(figsize = figsize)
ax = fig.add_subplot(111)
# [X,Y] = meshgrid(self.xcoords, self.ycoords)
#
# self.check_data_array()
im = ax.imshow(self.data_array, vmin=vmin, vmax=vmax,
cmap=cmap, interpolation=interpolation)
if kwds.has_key("adjust_coords") and kwds['adjust_coords']:
# setting to real coordinates
im.set_extent([self.header['xllcorner'],
self.header['xllcorner'] + self.header['ncol'] * self.header['cellsize'],
self.header['yllcorner'],
self.header['yllcorner'] + self.header['nrow'] * self.header['cellsize']])
from matplotlib.ticker import MaxNLocator
if kwds.has_key("max_labels_x"):
im.axes.xaxis.set_major_locator(MaxNLocator(kwds['max_labels_x']))
if kwds.has_key("max_labels_y"):
im.axes.yaxis.set_major_locator(MaxNLocator(kwds['max_labels_y']))
# rotate labels on y-acis:
if rotate_labels:
for label in im.axes.yaxis.get_ticklabels():
label.set_rotation(-90)
# def mjrFormatter(x, pos):
## x = x - self.header['yllcorner']
# return "{0}".format(x)
# # return "$2^{{{0}}}$".format(x)
#
# import matplotlib as mpl
# im.axes.yaxis.set_major_formatter(mpl.ticker.FuncFormatter(mjrFormatter))
#
# im = imshow(self.data_array)
if colorbar:
cbar = plt.colorbar(im, fraction=fraction) # , location="bottom")
cbar.set_label(colorbar_title)
# plot contour lines on top? -> TEST!!
# axis('off')
ax.set_title(title)
if kwds.has_key('filename'):
plt.savefig(kwds['filename'])
else:
plt.savefig('ascii_grid.png')
if kwds.has_key("ax"):
return ax
def plot_grid_and_hist(self, **kwds):
"""Create a 2-D plot of the grid in space and a histogram of values
The histogram contains median and percentiles
**Optional Keywords**:
- *filename* = string : filename (and, implicitly, the format) of plot file
- *cmap* = maptlotlib colormap: colormap for plot (default: gray)
- *figsize* = (float, float) : figure size in x,y (default: 8,6)
- *colorbar* = bool : plot colorbar (default: True)
- *title* = string : plot title (default: filename)
- *vmin* = float : minimum valule to plot (default: min of data)
- *vmax* = float : maximum value to plot (default: max of data)
- *n* = int : number of bins for histogram (default: 100)
"""
cmap = kwds.get("cmap", "gray")
figsize = kwds.get("figsize", (8,6))
colorbar = kwds.get("colorbar", True)
title = kwds.get("title", self.file_name_str)
n = kwds.get("n", 100)
self.check_hist()
vmin = kwds.get("vmin", min(self.hist_data))
vmax = kwds.get("vmax", max(self.hist_data))
# read self.x_data and self.y_data if they do not already exist...
# to save computation time??
try:
(self.x_data, self.y_data)
except AttributeError:
# print "Create x- and y- coordinates"
self.x_data = self.xcoords(self.header)
self.y_data = self.ycoords(self.header)
# check, if data_array with z values is already created
try:
self.data_array
except AttributeError:
# print "Create z-Value Data array"
self.process_z_values_to_array
# Import matplotlib modules, test, if matplotlib installed
# export test to separate function?
try:
import matplotlib.pyplot as plt
except ImportError: