pygmi 2.2.7__zip

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (106) hide show
  1. pygmi-2.2.7/LICENSE.txt +623 -0
  2. pygmi-2.2.7/MANIFEST.in +7 -0
  3. pygmi-2.2.7/PKG-INFO +47 -0
  4. pygmi-2.2.7/README.md +55 -0
  5. pygmi-2.2.7/README.rst +20 -0
  6. pygmi-2.2.7/log.txt +242 -0
  7. pygmi-2.2.7/pygmi/__init__.py +22 -0
  8. pygmi-2.2.7/pygmi/clust/__init__.py +4 -0
  9. pygmi-2.2.7/pygmi/clust/crisp_clust.py +708 -0
  10. pygmi-2.2.7/pygmi/clust/datatypes.py +65 -0
  11. pygmi-2.2.7/pygmi/clust/fuzzy_clust.py +906 -0
  12. pygmi-2.2.7/pygmi/clust/graphs.py +421 -0
  13. pygmi-2.2.7/pygmi/clust/graphtool.py +667 -0
  14. pygmi-2.2.7/pygmi/clust/menu.py +139 -0
  15. pygmi-2.2.7/pygmi/clust/var_ratio.py +92 -0
  16. pygmi-2.2.7/pygmi/clust/xie_beni.py +93 -0
  17. pygmi-2.2.7/pygmi/helpdocs/linepointer.png +0 -0
  18. pygmi-2.2.7/pygmi/helpdocs/pygmi.main.html +31 -0
  19. pygmi-2.2.7/pygmi/helpdocs/pygmi.pfmod.ddisp.html +15 -0
  20. pygmi-2.2.7/pygmi/helpdocs/pygmi.pfmod.iodefs.exportkmz.html +13 -0
  21. pygmi-2.2.7/pygmi/helpdocs/pygmi.pfmod.iodefs.importpicture.html +16 -0
  22. pygmi-2.2.7/pygmi/helpdocs/pygmi.pfmod.layer.html +17 -0
  23. pygmi-2.2.7/pygmi/helpdocs/pygmi.pfmod.mext.html +44 -0
  24. pygmi-2.2.7/pygmi/helpdocs/pygmi.pfmod.misc.rangedcopy.html +16 -0
  25. pygmi-2.2.7/pygmi/helpdocs/pygmi.pfmod.param.html +37 -0
  26. pygmi-2.2.7/pygmi/helpdocs/pygmi.pfmod.prof.html +32 -0
  27. pygmi-2.2.7/pygmi/helpdocs/pygmi.pfmod.pview.html +26 -0
  28. pygmi-2.2.7/pygmi/helpdocs/pygmi.raster.cooper.gradients.html +14 -0
  29. pygmi-2.2.7/pygmi/helpdocs/pygmi.raster.cooper.tilt.html +14 -0
  30. pygmi-2.2.7/pygmi/helpdocs/pygmi.raster.cooper.visibility.html +14 -0
  31. pygmi-2.2.7/pygmi/helpdocs/pygmi.raster.dataprep.datagrid.html +13 -0
  32. pygmi-2.2.7/pygmi/helpdocs/pygmi.raster.dataprep.datamerge.html +11 -0
  33. pygmi-2.2.7/pygmi/helpdocs/pygmi.raster.dataprep.datareproj.html +11 -0
  34. pygmi-2.2.7/pygmi/helpdocs/pygmi.raster.dataprep.rtp.html +14 -0
  35. pygmi-2.2.7/pygmi/helpdocs/pygmi.raster.ginterp.html +31 -0
  36. pygmi-2.2.7/pygmi/helpdocs/pygmi.raster.igrf.html +20 -0
  37. pygmi-2.2.7/pygmi/helpdocs/pygmi.raster.normalisation.html +14 -0
  38. pygmi-2.2.7/pygmi/helpdocs/pygmi.raster.smooth.html +22 -0
  39. pygmi-2.2.7/pygmi/helpdocs/pygmi.raster.tiltdepth.html +20 -0
  40. pygmi-2.2.7/pygmi/images/Help.png +0 -0
  41. pygmi-2.2.7/pygmi/images/bringtofront.png +0 -0
  42. pygmi-2.2.7/pygmi/images/cgs2.emf +0 -0
  43. pygmi-2.2.7/pygmi/images/delete.png +0 -0
  44. pygmi-2.2.7/pygmi/images/linepointer.png +0 -0
  45. pygmi-2.2.7/pygmi/images/logo256.ico +0 -0
  46. pygmi-2.2.7/pygmi/images/logo256.png +0 -0
  47. pygmi-2.2.7/pygmi/images/pointer.png +0 -0
  48. pygmi-2.2.7/pygmi/images/pygmi.png +0 -0
  49. pygmi-2.2.7/pygmi/images/sendtoback.png +0 -0
  50. pygmi-2.2.7/pygmi/main.py +1037 -0
  51. pygmi-2.2.7/pygmi/menu_default.py +327 -0
  52. pygmi-2.2.7/pygmi/misc.py +147 -0
  53. pygmi-2.2.7/pygmi/pfmod/__init__.py +6 -0
  54. pygmi-2.2.7/pygmi/pfmod/cubes.py +1488 -0
  55. pygmi-2.2.7/pygmi/pfmod/datatypes.py +292 -0
  56. pygmi-2.2.7/pygmi/pfmod/grvmag3d.py +1265 -0
  57. pygmi-2.2.7/pygmi/pfmod/iodefs.py +1001 -0
  58. pygmi-2.2.7/pygmi/pfmod/menu.py +107 -0
  59. pygmi-2.2.7/pygmi/pfmod/misc.py +301 -0
  60. pygmi-2.2.7/pygmi/pfmod/pfmod.py +248 -0
  61. pygmi-2.2.7/pygmi/pfmod/tab_ddisp.py +425 -0
  62. pygmi-2.2.7/pygmi/pfmod/tab_layer.py +490 -0
  63. pygmi-2.2.7/pygmi/pfmod/tab_mext.py +544 -0
  64. pygmi-2.2.7/pygmi/pfmod/tab_param.py +548 -0
  65. pygmi-2.2.7/pygmi/pfmod/tab_prof.py +946 -0
  66. pygmi-2.2.7/pygmi/pfmod/tab_pview.py +740 -0
  67. pygmi-2.2.7/pygmi/raster/IGRF11.COF +1635 -0
  68. pygmi-2.2.7/pygmi/raster/__init__.py +14 -0
  69. pygmi-2.2.7/pygmi/raster/cooper.py +718 -0
  70. pygmi-2.2.7/pygmi/raster/dataprep.py +1640 -0
  71. pygmi-2.2.7/pygmi/raster/datatypes.py +123 -0
  72. pygmi-2.2.7/pygmi/raster/equation_editor.py +256 -0
  73. pygmi-2.2.7/pygmi/raster/ginterp.py +1901 -0
  74. pygmi-2.2.7/pygmi/raster/graphs.py +531 -0
  75. pygmi-2.2.7/pygmi/raster/igrf.py +860 -0
  76. pygmi-2.2.7/pygmi/raster/iodefs.py +813 -0
  77. pygmi-2.2.7/pygmi/raster/menu.py +307 -0
  78. pygmi-2.2.7/pygmi/raster/normalisation.py +187 -0
  79. pygmi-2.2.7/pygmi/raster/show_table.py +245 -0
  80. pygmi-2.2.7/pygmi/raster/smooth.py +406 -0
  81. pygmi-2.2.7/pygmi/raster/tiltdepth.py +590 -0
  82. pygmi-2.2.7/pygmi/seis/__init__.py +0 -0
  83. pygmi-2.2.7/pygmi/seis/beachball.py +751 -0
  84. pygmi-2.2.7/pygmi/seis/datatypes.py +605 -0
  85. pygmi-2.2.7/pygmi/seis/del_rec.py +410 -0
  86. pygmi-2.2.7/pygmi/seis/iodefs.py +1261 -0
  87. pygmi-2.2.7/pygmi/seis/menu.py +136 -0
  88. pygmi-2.2.7/pygmi/seis/scan_imp.py +795 -0
  89. pygmi-2.2.7/pygmi/test/__init__.py +6 -0
  90. pygmi-2.2.7/pygmi/test/pfmod.py +212 -0
  91. pygmi-2.2.7/pygmi/test/tests.py +39 -0
  92. pygmi-2.2.7/pygmi/vector/__init__.py +7 -0
  93. pygmi-2.2.7/pygmi/vector/datatypes.py +74 -0
  94. pygmi-2.2.7/pygmi/vector/graphs.py +443 -0
  95. pygmi-2.2.7/pygmi/vector/iodefs.py +345 -0
  96. pygmi-2.2.7/pygmi/vector/menu.py +127 -0
  97. pygmi-2.2.7/pygmi.egg-info/PKG-INFO +47 -0
  98. pygmi-2.2.7/pygmi.egg-info/SOURCES.txt +105 -0
  99. pygmi-2.2.7/pygmi.egg-info/dependency_links.txt +1 -0
  100. pygmi-2.2.7/pygmi.egg-info/entry_points.txt +3 -0
  101. pygmi-2.2.7/pygmi.egg-info/not-zip-safe +1 -0
  102. pygmi-2.2.7/pygmi.egg-info/requires.txt +7 -0
  103. pygmi-2.2.7/pygmi.egg-info/top_level.txt +1 -0
  104. pygmi-2.2.7/quickstart.py +35 -0
  105. pygmi-2.2.7/setup.cfg +8 -0
  106. pygmi-2.2.7/setup.py +80 -0
@@ -0,0 +1,813 @@
1
+ # -----------------------------------------------------------------------------
2
+ # Name: iodefs.py (part of PyGMI)
3
+ #
4
+ # Author: Patrick Cole
5
+ # E-Mail: pcole@geoscience.org.za
6
+ #
7
+ # Copyright: (c) 2013 Council for Geoscience
8
+ # Licence: GPL-3.0
9
+ #
10
+ # This file is part of PyGMI
11
+ #
12
+ # PyGMI is free software: you can redistribute it and/or modify
13
+ # it under the terms of the GNU General Public License as published by
14
+ # the Free Software Foundation, either version 3 of the License, or
15
+ # (at your option) any later version.
16
+ #
17
+ # PyGMI is distributed in the hope that it will be useful,
18
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
19
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20
+ # GNU General Public License for more details.
21
+ #
22
+ # You should have received a copy of the GNU General Public License
23
+ # along with this program. If not, see <http://www.gnu.org/licenses/>.
24
+ # -----------------------------------------------------------------------------
25
+ """ Import Data """
26
+
27
+ from PyQt4 import QtGui
28
+ from pygmi.raster.datatypes import Data
29
+ from pygmi.clust.datatypes import Clust
30
+ import numpy as np
31
+ from osgeo import gdal, osr
32
+ import struct
33
+ from pygmi.raster.dataprep import merge
34
+ import os
35
+ import glob
36
+
37
+
38
+ class ImportData(object):
39
+ """
40
+ Import Data - Interfaces with GDAL routines
41
+
42
+ Attributes
43
+ ----------
44
+ name : str
45
+ item name
46
+ pbar : progressbar
47
+ reference to a progress bar.
48
+ parent : parent
49
+ reference to the parent routine
50
+ outdata : dictionary
51
+ dictionary of output datasets
52
+ ifile : str
53
+ input file name. Used in main.py
54
+ ext : str
55
+ filename extension
56
+ """
57
+ def __init__(self, parent=None):
58
+ self.ifile = ""
59
+ self.name = "Import Data: "
60
+ self.ext = ""
61
+ self.pbar = None
62
+ self.parent = parent
63
+ self.indata = {}
64
+ self.outdata = {}
65
+
66
+ def settings(self):
67
+ """ Settings """
68
+ ext = \
69
+ "ERMapper (*.ers);;" + \
70
+ "ENVI (*.hdr);;" + \
71
+ "GeoTiff (*.tif);;" + \
72
+ "Geosoft UNCOMPRESSED grid (*.grd);;" + \
73
+ "Geosoft (*.gxf);;" + \
74
+ "Surfer grid (v.6) (*.grd);;" + \
75
+ "GeoPak grid (*.grd);;" + \
76
+ "ASCII with .hdr header (*.asc);;" + \
77
+ "ASCII XYZ (*.xyz);;" + \
78
+ "ArcGIS BIL (*.bil)"
79
+
80
+ filename, filt = QtGui.QFileDialog.getOpenFileNameAndFilter(
81
+ self.parent, 'Open File', '.', ext)
82
+ if filename == '':
83
+ return False
84
+ os.chdir(filename.rpartition('/')[0])
85
+ self.ifile = str(filename)
86
+ self.ext = filename[-3:]
87
+ self.ext = self.ext.lower()
88
+
89
+ if filt == 'GeoPak grid (*.grd)':
90
+ dat = get_geopak(self.ifile)
91
+ elif filt == 'Geosoft UNCOMPRESSED grid (*.grd)':
92
+ dat = get_geosoft(self.ifile)
93
+ elif filt == 'ASCII with .hdr header (*.asc)':
94
+ dat = get_ascii(self.ifile)
95
+ else:
96
+ dat = get_raster(self.ifile)
97
+
98
+ if dat is None:
99
+ if filt == 'Surfer grid (v.6) (*.grd)':
100
+ QtGui.QMessageBox.warning(self.parent, 'Error',
101
+ 'Could not import the surfer 6 '
102
+ 'grid. Please make sure it not '
103
+ 'another format, such as geosoft.',
104
+ QtGui.QMessageBox.Ok,
105
+ QtGui.QMessageBox.Ok)
106
+ elif filt == 'Geosoft UNCOMPRESSED grid (*.grd)':
107
+ QtGui.QMessageBox.warning(self.parent, 'Error',
108
+ 'Could not import the grid. '
109
+ 'Please make sure it is a Geosoft '
110
+ 'FLOAT grid, and not a compressed '
111
+ 'grid. You can export your grid to '
112
+ 'this format using the Geosoft '
113
+ 'Viewer.',
114
+ QtGui.QMessageBox.Ok,
115
+ QtGui.QMessageBox.Ok)
116
+ else:
117
+ QtGui.QMessageBox.warning(self.parent, 'Error',
118
+ 'Could not import the grid.',
119
+ QtGui.QMessageBox.Ok,
120
+ QtGui.QMessageBox.Ok)
121
+ return False
122
+
123
+ output_type = 'Raster'
124
+ if 'Cluster' in dat[0].dataid:
125
+ output_type = 'Cluster'
126
+
127
+ self.outdata[output_type] = dat
128
+ return True
129
+
130
+
131
+ def get_ascii(ifile):
132
+ """
133
+ Import ascii raster dataset
134
+
135
+ Parameters
136
+ ----------
137
+ ifile : str
138
+ filename to import
139
+
140
+ Returns
141
+ -------
142
+ dat : PyGMI raster Data
143
+ dataset imported
144
+ """
145
+
146
+ afile = open(ifile, 'r')
147
+ adata = afile.read()
148
+
149
+ adata = adata.split()
150
+ adata = np.array(adata, dtype=float)
151
+
152
+ hfile = open(ifile[:-3]+'hdr', 'r')
153
+ tmp = hfile.readlines()
154
+
155
+ xdim = float(tmp[0].split()[-1])
156
+ ydim = float(tmp[1].split()[-1])
157
+ ncols = int(tmp[2].split()[-1])
158
+ nrows = int(tmp[3].split()[-1])
159
+ nbands = int(tmp[4].split()[-1])
160
+ ulxmap = float(tmp[5].split()[-1])
161
+ ulymap = float(tmp[6].split()[-1])
162
+ bandid = ifile[:-4].rsplit('/')[-1]
163
+
164
+ adata.shape = (nrows, ncols)
165
+
166
+ if nbands > 1:
167
+ print('PyGMI only supports single band ASCII files')
168
+
169
+ dat = [Data()]
170
+ i = 0
171
+
172
+ dat[i].data = adata
173
+
174
+ nval = -9999.0
175
+
176
+ dat[i].data = np.ma.masked_equal(dat[i].data, nval)
177
+ if dat[i].data.mask.size == 1:
178
+ dat[i].data.mask = (np.ma.make_mask_none(dat[i].data.shape) +
179
+ dat[i].data.mask)
180
+
181
+ dat[i].nrofbands = nbands
182
+ dat[i].tlx = ulxmap
183
+ dat[i].tly = ulymap
184
+ dat[i].dataid = bandid
185
+ dat[i].nullvalue = nval
186
+ dat[i].rows = nrows
187
+ dat[i].cols = ncols
188
+ dat[i].xdim = xdim
189
+ dat[i].ydim = ydim
190
+
191
+ return dat
192
+
193
+
194
+ def get_raster(ifile):
195
+ """
196
+ This function loads a raster dataset off the disk using the GDAL
197
+ libraries. It returns the data in a PyGMI data object.
198
+
199
+ Parameters
200
+ ----------
201
+ ifile : str
202
+ filename to import
203
+
204
+ Returns
205
+ -------
206
+ dat : PyGMI raster Data
207
+ dataset imported
208
+ """
209
+ dat = []
210
+ bname = ifile.split('/')[-1].rpartition('.')[0]+': '
211
+ ifile = ifile[:]
212
+ ext = ifile[-3:]
213
+
214
+ # Envi Case
215
+ if ext == 'hdr':
216
+ ifile = ifile[:-4]
217
+ tmp = glob.glob(ifile+'.dat')
218
+ if len(tmp) > 0:
219
+ ifile = tmp[0]
220
+
221
+ dataset = gdal.Open(ifile, gdal.GA_ReadOnly)
222
+
223
+ if dataset is None:
224
+ return None
225
+
226
+ gtr = dataset.GetGeoTransform()
227
+ # output_type = 'Raster'
228
+
229
+ for i in range(dataset.RasterCount):
230
+ rtmp = dataset.GetRasterBand(i+1)
231
+ bandid = rtmp.GetDescription()
232
+ nval = rtmp.GetNoDataValue()
233
+
234
+ if 'Cluster' in bandid:
235
+ # output_type = 'Cluster'
236
+ dat.append(Clust())
237
+ else:
238
+ dat.append(Data())
239
+ dat[i].data = rtmp.ReadAsArray()
240
+ if dat[i].data.dtype.kind == 'i':
241
+ if nval is None:
242
+ nval = 999999
243
+ nval = int(nval)
244
+ elif dat[i].data.dtype.kind == 'u':
245
+ if nval is None:
246
+ nval = 0
247
+ nval = int(nval)
248
+ else:
249
+ if nval is None:
250
+ nval = 1e+20
251
+ nval = float(nval)
252
+ # dtype = dat[i].data.dtype
253
+ # if dtype != np.float64 and dtype != np.float32:
254
+ # dat[i].data = dat[i].data.astype(np.float32)
255
+ # if dtype == np.float64 or dtype == np.float32:
256
+ # dat[i].data[dat[i].data == nval] = np.nan
257
+ if ext == 'ers' and nval == -1.0e+32:
258
+ dat[i].data[np.ma.less_equal(dat[i].data, nval)] = -1.0e+32
259
+ # dat[i].data[np.ma.less_equal(dat[i].data, nval)] = np.nan
260
+
261
+ # dat[i].data = np.ma.masked_invalid(dat[i].data)
262
+ # Note that because the data is stored in a masked array, the array ends up
263
+ # being double the size that it was on the disk.
264
+ dat[i].data = np.ma.masked_invalid(dat[i].data)
265
+ dat[i].data.mask = dat[i].data.mask | (dat[i].data == nval)
266
+ # dat[i].data = np.ma.masked_equal(dat[i].data, nval)
267
+ if dat[i].data.mask.size == 1:
268
+ dat[i].data.mask = (np.ma.make_mask_none(dat[i].data.shape) +
269
+ dat[i].data.mask)
270
+
271
+ dat[i].nrofbands = dataset.RasterCount
272
+ dat[i].tlx = gtr[0]
273
+ dat[i].tly = gtr[3]
274
+ if bandid == '':
275
+ bandid = bname+str(i+1)
276
+ dat[i].dataid = bandid
277
+ if bandid[-1] == ')':
278
+ dat[i].units = bandid[bandid.rfind('(')+1:-1]
279
+
280
+ dat[i].nullvalue = nval
281
+ dat[i].rows = dataset.RasterYSize
282
+ dat[i].cols = dataset.RasterXSize
283
+ dat[i].xdim = abs(gtr[1])
284
+ dat[i].ydim = abs(gtr[5])
285
+ dat[i].gtr = gtr
286
+
287
+ srs = osr.SpatialReference()
288
+ srs.ImportFromWkt(dataset.GetProjection())
289
+ srs.AutoIdentifyEPSG()
290
+
291
+ dat[i].wkt = srs.ExportToWkt()
292
+
293
+ if 'Cluster' in bandid:
294
+ dat[i].no_clusters = int(dat[i].data.max()+1)
295
+ # dat[i].no_clusters = np.unique(dat[i].data).count()
296
+
297
+ return dat
298
+
299
+
300
+ class ExportData(object):
301
+ """
302
+ Export Data
303
+
304
+ Attributes
305
+ ----------
306
+ name : str
307
+ item name
308
+ pbar : progressbar
309
+ reference to a progress bar.
310
+ parent : parent
311
+ reference to the parent routine
312
+ outdata : dictionary
313
+ dictionary of output datasets
314
+ ifile : str
315
+ input file name. Used in main.py
316
+ ext : str
317
+ filename extension
318
+ """
319
+ def __init__(self, parent):
320
+ self.ifile = ""
321
+ self.name = "Export Data: "
322
+ self.ext = ""
323
+ self.pbar = None
324
+ self.parent = parent
325
+ self.indata = {}
326
+ self.outdata = {}
327
+ # self.dirname = ""
328
+
329
+ def run(self):
330
+ """ Show Info """
331
+ if 'Cluster' in self.indata:
332
+ data = self.indata['Cluster']
333
+ elif 'Raster' in self.indata:
334
+ data = self.indata['Raster']
335
+ else:
336
+ self.parent.showprocesslog('No raster data')
337
+ return
338
+
339
+ ext = \
340
+ "ENVI (*.hdr);;" + \
341
+ "ERMapper (*.ers);;" + \
342
+ "GeoTiff (*.tif);;" + \
343
+ "Geosoft (*.gxf);;" + \
344
+ "Surfer grid (v.6) (*.grd);;" + \
345
+ "ArcInfo ASCII (*.asc);;" + \
346
+ "ASCII XYZ (*.xyz);;" + \
347
+ "ArcGIS BIL (*.bil)"
348
+
349
+ filename = QtGui.QFileDialog.getSaveFileName(
350
+ self.parent, 'Save File', '.', ext)
351
+ if filename == '':
352
+ return False
353
+ os.chdir(filename.rpartition('/')[0])
354
+
355
+ self.ifile = str(filename)
356
+ self.ext = filename[-3:]
357
+
358
+ self.parent.showprocesslog('Export Data Busy...')
359
+
360
+ # Pop up save dialog box
361
+ if self.ext == 'ers':
362
+ self.export_gdal(data, 'ERS')
363
+ if self.ext == 'gxf':
364
+ self.export_gxf(data)
365
+ if self.ext == 'grd':
366
+ self.export_surfer(data)
367
+ if self.ext == 'asc':
368
+ self.export_ascii(data)
369
+ if self.ext == 'xyz':
370
+ self.export_ascii_xyz(data)
371
+ if self.ext == 'tif':
372
+ self.export_gdal(data, 'GTiff')
373
+ if self.ext == 'hdr':
374
+ self.export_gdal(data, 'ENVI')
375
+ if self.ext == 'bil':
376
+ self.export_gdal(data, 'EHdr')
377
+
378
+ self.parent.showprocesslog('Export Data Finished!')
379
+
380
+ def export_gdal(self, dat, drv):
381
+ """
382
+ Export to GDAL format
383
+
384
+ Parameters
385
+ ----------
386
+ dat : PyGMI raster Data
387
+ dataset to export
388
+ drv : str
389
+ name of the GDAL driver to use
390
+ """
391
+
392
+ data = merge(dat)
393
+ xmin = data[0].tlx
394
+ ymax = data[0].tly
395
+
396
+ driver = gdal.GetDriverByName(drv)
397
+ dtype = data[0].data.dtype
398
+
399
+ if dtype == np.uint8:
400
+ fmt = gdal.GDT_Byte
401
+ elif dtype == np.int32:
402
+ fmt = gdal.GDT_Int32
403
+ elif dtype == np.float64:
404
+ fmt = gdal.GDT_Float64
405
+ else:
406
+ fmt = gdal.GDT_Float32
407
+
408
+ tmp = self.ifile.rpartition('.')
409
+
410
+ if drv == 'GTiff':
411
+ tmpfile = tmp[0] + '.tif'
412
+ elif drv == 'EHdr':
413
+ fmt = gdal.GDT_Float32
414
+ dtype = np.float32
415
+ tmpfile = tmp[0] + '.bil'
416
+ # elif drv == 'VRT':
417
+ # tmpfile = tmp[0] + '.vrt'
418
+ else:
419
+ tmpfile = tmp[0]
420
+
421
+ if drv == 'GTiff' and dtype == np.uint8:
422
+ out = driver.Create(tmpfile, int(data[0].cols), int(data[0].rows),
423
+ len(data), fmt, options=['COMPRESS=NONE',
424
+ 'TFW=YES'])
425
+ else:
426
+ out = driver.Create(tmpfile, int(data[0].cols), int(data[0].rows),
427
+ len(data), fmt)
428
+ out.SetGeoTransform([xmin, data[0].xdim, 0, ymax, 0, -data[0].ydim])
429
+ # orig = osr.SpatialReference()
430
+ # orig.SetWellKnownGeogCS('WGS84')
431
+ # orig.ImportFromEPSG(4222) # Cape
432
+ # orig.SetTM(0.0, 31.0, 1.0, 0.0, 0.0)
433
+ # out.SetProjection(orig.ExportToWkt())
434
+
435
+ out.SetProjection(data[0].wkt)
436
+
437
+ for i in range(len(data)):
438
+ rtmp = out.GetRasterBand(i+1)
439
+ rtmp.SetDescription(data[i].dataid)
440
+
441
+ dtmp = np.ma.array(data[i].data).astype(dtype)
442
+
443
+ # This section tries to overcome null values with round off error
444
+ # in 32-bit numbers.
445
+ if dtype == np.float32:
446
+ data[i].nullvalue = np.float64(np.float32(data[i].nullvalue))
447
+ if data[i].data.min() > -1e+10:
448
+ data[i].nullvalue = np.float64(np.float32(-1e+10))
449
+ elif data[i].data.max() < 1e+10:
450
+ data[i].nullvalue = np.float64(np.float32(1e+10))
451
+
452
+ elif dtype == np.float or dtype == np.float64:
453
+ data[i].nullvalue = np.float64(dtmp.fill_value)
454
+
455
+ dtmp.set_fill_value(data[i].nullvalue)
456
+ dtmp = dtmp.filled()
457
+
458
+ if drv != 'GTiff':
459
+ rtmp.SetNoDataValue(data[i].nullvalue)
460
+ elif len(data) == 1:
461
+ rtmp.SetNoDataValue(data[i].nullvalue)
462
+ rtmp.WriteArray(dtmp)
463
+
464
+ out = None # Close File
465
+ if drv == 'ENVI':
466
+ with open(tmpfile+'.hdr', 'a') as myfile:
467
+ myfile.write('data ignore value = ' + str(data[0].nullvalue))
468
+
469
+ def export_gxf(self, data):
470
+ """
471
+ Export GXF data
472
+
473
+ Parameters
474
+ ----------
475
+ data : PyGMI raster Data
476
+ dataset to export
477
+ """
478
+ for k in data:
479
+ file_out = self.get_filename(k, 'gxf')
480
+ fno = open(file_out, 'w')
481
+
482
+ xmin = k.tlx
483
+ # xmax = k.tlx + k.cols*k.xdim
484
+ ymin = k.tly - k.rows*k.ydim
485
+ # ymax = k.tly
486
+
487
+ fno.write("#TITLE\n")
488
+ fno.write(self.name)
489
+ fno.write("\n#POINTS\n")
490
+ fno.write(str(k.cols))
491
+ fno.write("\n#ROWS\n")
492
+ fno.write(str(k.rows))
493
+ fno.write("\n#PTSEPARATION\n")
494
+ fno.write(str(k.xdim))
495
+ fno.write("\n#RWSEPARATION\n")
496
+ fno.write(str(k.ydim))
497
+ fno.write("\n#XORIGIN\n")
498
+ fno.write(str(xmin))
499
+ fno.write("\n#YORIGIN\n")
500
+ fno.write(str(ymin))
501
+ fno.write("\n#SENSE\n")
502
+ fno.write("1")
503
+ fno.write("\n#DUMMY\n")
504
+ fno.write(str(k.nullvalue))
505
+ fno.write("\n#GRID\n")
506
+ tmp = k.data.filled(k.nullvalue)
507
+
508
+ for i in range(k.data.shape[0]-1, -1, -1):
509
+ kkk = 0
510
+ # write only 5 numbers in a row
511
+ for j in range(k.data.shape[1]):
512
+ if kkk == 5:
513
+ kkk = 0
514
+ if kkk == 0:
515
+ fno.write("\n")
516
+
517
+ fno.write(str(tmp[i, j]) + " ")
518
+ kkk += 1
519
+
520
+ fno.close()
521
+
522
+ def export_surfer(self, data):
523
+ """
524
+ Export a surfer binary grid
525
+
526
+ Parameters
527
+ ----------
528
+ data : PyGMI raster Data
529
+ dataset to export
530
+ """
531
+ for k in data:
532
+ file_out = self.get_filename(k, 'grd')
533
+ fno = open(file_out, 'wb')
534
+
535
+ xmin = k.tlx
536
+ xmax = k.tlx + k.cols*k.xdim
537
+ ymin = k.tly - k.rows*k.ydim
538
+ ymax = k.tly
539
+
540
+ bintmp = struct.pack('cccchhdddddd', b'D', b'S', b'B', b'B',
541
+ k.cols, k.rows,
542
+ xmin, xmax,
543
+ ymin, ymax,
544
+ np.min(k.data),
545
+ np.max(k.data))
546
+ fno.write(bintmp)
547
+
548
+ ntmp = 1.701410009187828e+38
549
+ tmp = k.data.astype('f')
550
+ tmp = tmp.filled(ntmp)
551
+ tmp = tmp[::-1]
552
+ fno.write(tmp.tostring())
553
+
554
+ fno.close()
555
+
556
+ def export_ascii(self, data):
557
+ """
558
+ Export Ascii file
559
+
560
+ Parameters
561
+ ----------
562
+ data : PyGMI raster Data
563
+ dataset to export
564
+ """
565
+ for k in data:
566
+ file_out = self.get_filename(k, 'asc')
567
+ fno = open(file_out, 'w')
568
+
569
+ xmin = k.tlx
570
+ # xmax = k.tlx + k.cols*k.xdim
571
+ ymin = k.tly - k.rows*k.ydim
572
+ # ymax = k.tly
573
+
574
+ fno.write("ncols \t\t\t" + str(k.cols))
575
+ fno.write("\nnrows \t\t\t" + str(k.rows))
576
+ fno.write("\nxllcorner \t\t\t" + str(xmin))
577
+ fno.write("\nyllcorner \t\t\t" + str(ymin))
578
+ fno.write("\ncellsize \t\t\t" + str(k.xdim))
579
+ fno.write("\nnodata_value \t\t" + str(k.nullvalue))
580
+
581
+ tmp = k.data.filled(k.nullvalue)
582
+
583
+ for j in range(k.rows):
584
+ fno.write("\n")
585
+ for i in range(k.cols):
586
+ fno.write(str(tmp[j, i]) + " ")
587
+ # fno.write(str(data[0].data[j].data[i]) + " ")
588
+
589
+ fno.close()
590
+
591
+ def export_ascii_xyz(self, data):
592
+ """
593
+ Export and xyz file
594
+
595
+ Parameters
596
+ ----------
597
+ data : PyGMI raster Data
598
+ dataset to export
599
+ """
600
+ for k in data:
601
+ file_out = self.get_filename(k, 'xyz')
602
+ fno = open(file_out, 'w')
603
+
604
+ tmp = k.data.filled(k.nullvalue)
605
+
606
+ xmin = k.tlx
607
+ # xmax = k.tlx + k.cols*k.xdim
608
+ # ymin = k.tly - k.rows*k.ydim
609
+ ymax = k.tly
610
+
611
+ for j in range(k.rows):
612
+ for i in range(k.cols):
613
+ fno.write(str(xmin+i*k.xdim) + " " +
614
+ str(ymax-j*k.ydim) + " " +
615
+ str(tmp[j, i]) + "\n")
616
+ fno.close()
617
+
618
+ def get_filename(self, data, ext):
619
+ """
620
+ Gets a valid filename
621
+
622
+ Parameters
623
+ ----------
624
+ data : PyGMI raster Data
625
+ dataset to get filename from
626
+ ext : str
627
+ filename extension to use
628
+ """
629
+ file_band = data.dataid.split('_')[0].strip('"')
630
+ file_band = file_band.replace('/', '')
631
+ file_band = file_band.replace(':', '')
632
+ file_out = self.ifile.rpartition(".")[0]+"_"+file_band+'.'+ext
633
+
634
+ return file_out
635
+
636
+
637
+ def get_geopak(hfile):
638
+ """ GeoPak Import """
639
+
640
+ fin = open(hfile, 'rb')
641
+ fall = fin.read()
642
+ fin.close()
643
+
644
+ # bof = np.frombuffer(fall, dtype=np.uint8, count=1, offset=0)
645
+ off = 0
646
+ # fnew = b''
647
+ fnew = []
648
+ while off < len(fall):
649
+ off += 1
650
+ breclen = np.frombuffer(fall, dtype=np.uint8, count=1, offset=off)[0]
651
+
652
+ if breclen == 130:
653
+ break
654
+
655
+ reclen = breclen
656
+
657
+ if breclen == 129:
658
+ reclen = 128
659
+
660
+ off += 1
661
+
662
+ # fnew += fall[off:off+reclen]
663
+ fnew.append(fall[off:off+reclen])
664
+ off += reclen
665
+ # ereclen = np.frombuffer(fall, dtype=np.uint8, count=1, offset=off)[0]
666
+ # print(breclen, ereclen, reclen)
667
+
668
+ fnew = b''.join(fnew)
669
+ header = np.frombuffer(fnew, dtype=np.float32, count=32, offset=0)
670
+
671
+ # Lines in grid 1
672
+ # Points per line 2
673
+ # Grid factor 3
674
+ # Grid base value 4
675
+ # Grid X origin 5
676
+ # Grid Y origin 6
677
+ # Grid rotation 7
678
+ # Grid dummy value 8
679
+ # Map scale 9
680
+ # Cell size (X) 10
681
+ # Cell size (Y) 11
682
+ # Inches/unit 12
683
+ # Grid X offset 13
684
+ # Grid Y offset 14
685
+ # Grid hdr version 15
686
+ #
687
+ # Lines in grid 17
688
+ # Points per line 18
689
+ # Grid factor 21
690
+ # Grid base value 22
691
+ # Z maximum 23
692
+ # Z minimum 24
693
+ #
694
+ # Grid dummy value 26
695
+
696
+ nrows = int(header[0])
697
+ ncols = int(header[1])
698
+ gfactor = header[2]
699
+ gbase = header[3]
700
+ x0 = header[4]
701
+ y0 = header[5]
702
+ # rotation = header[6]
703
+ nval = header[7]
704
+ # mapscale = header[8]
705
+ dx = header[9]
706
+ dy = header[10]
707
+ # inches_per_unit = header[11]
708
+ # xoffset = header[12]
709
+ # yoffset = header[13]
710
+ # hver = header[14]
711
+ # zmax = header[22]
712
+ # zmin = header[23]
713
+
714
+ data = np.frombuffer(fnew, dtype=np.int16, count=(nrows*ncols), offset=128)
715
+
716
+ data = np.ma.masked_equal(data, nval)
717
+ data = data/gfactor+gbase
718
+ data.shape = (nrows, ncols)
719
+ data = data[::-1]
720
+
721
+ dat = []
722
+ dat.append(Data())
723
+ i = 0
724
+
725
+ dat[i].data = data
726
+ dat[i].nrofbands = 1
727
+ dat[i].tlx = x0
728
+ dat[i].tly = y0+dy*nrows
729
+ dat[i].dataid = hfile[:-4]
730
+
731
+ dat[i].nullvalue = nval
732
+ dat[i].rows = nrows
733
+ dat[i].cols = ncols
734
+ dat[i].xdim = dx
735
+ dat[i].ydim = dy
736
+
737
+ return dat
738
+
739
+
740
+ def get_geosoft(hfile):
741
+ """ Get geosoft file """
742
+ f = open(hfile, mode='rb')
743
+
744
+ es = np.fromfile(f, dtype=np.int32, count=1)[0] # 4
745
+ sf = np.fromfile(f, dtype=np.int32, count=1)[0] # signf
746
+ ncols = np.fromfile(f, dtype=np.int32, count=1)[0] # ncol
747
+ nrows = np.fromfile(f, dtype=np.int32, count=1)[0] # nrow
748
+ kx = np.fromfile(f, dtype=np.int32, count=1)[0] # 1
749
+
750
+ dx = np.fromfile(f, dtype=np.float64, count=1)[0] # dx
751
+ dy = np.fromfile(f, dtype=np.float64, count=1)[0] # dy
752
+ x0 = np.fromfile(f, dtype=np.float64, count=1)[0] # xllcor
753
+ y0 = np.fromfile(f, dtype=np.float64, count=1)[0] # yllcor
754
+ rot = np.fromfile(f, dtype=np.float64, count=1)[0] # rot
755
+ zbase = np.fromfile(f, dtype=np.float64, count=1)[0] # zbase
756
+ zmult = np.fromfile(f, dtype=np.float64, count=1)[0] # zmult
757
+
758
+ label = np.fromfile(f, dtype='a48', count=1)[0]
759
+ mapno = np.fromfile(f, dtype='a16', count=1)[0]
760
+
761
+ proj = np.fromfile(f, dtype=np.int32, count=1)[0]
762
+ unitx = np.fromfile(f, dtype=np.int32, count=1)[0]
763
+ unity = np.fromfile(f, dtype=np.int32, count=1)[0]
764
+ unitz = np.fromfile(f, dtype=np.int32, count=1)[0]
765
+ nvpts = np.fromfile(f, dtype=np.int32, count=1)[0]
766
+ izmin = np.fromfile(f, dtype=np.int32, count=1)[0]
767
+ izmax = np.fromfile(f, dtype=np.int32, count=1)[0]
768
+ izmed = np.fromfile(f, dtype=np.int32, count=1)[0]
769
+ izmea = np.fromfile(f, dtype=np.int32, count=1)[0]
770
+
771
+ zvar = np.fromfile(f, dtype=np.float64, count=1)[0]
772
+
773
+ prcs = np.fromfile(f, dtype=np.int32, count=1)[0]
774
+
775
+ temspc = np.fromfile(f, dtype='a324', count=1)[0]
776
+
777
+ # pdb.set_trace()
778
+
779
+ if es == 2:
780
+ nval = -32767
781
+ data = np.fromfile(f, dtype=np.int16, count=nrows*ncols)
782
+
783
+ elif es == 4:
784
+ data = np.fromfile(f, dtype=np.float32, count=nrows*ncols)
785
+ nval = -1.0E+32
786
+ else:
787
+ return None
788
+
789
+ data = np.ma.masked_equal(data, nval)
790
+
791
+ data = data/zmult + zbase
792
+ data.shape = (nrows, ncols)
793
+ data = data[::-1]
794
+
795
+ f.close()
796
+
797
+ dat = []
798
+ dat.append(Data())
799
+ i = 0
800
+
801
+ dat[i].data = data
802
+ dat[i].nrofbands = 1
803
+ dat[i].tlx = x0
804
+ dat[i].tly = y0+dy*nrows
805
+ dat[i].dataid = hfile[:-4]
806
+
807
+ dat[i].nullvalue = nval
808
+ dat[i].rows = nrows
809
+ dat[i].cols = ncols
810
+ dat[i].xdim = dx
811
+ dat[i].ydim = dy
812
+
813
+ return dat