-
-
Notifications
You must be signed in to change notification settings - Fork 231
/
export.py
135 lines (108 loc) · 4.15 KB
/
export.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
from matplotlib.cm import ScalarMappable as SM
from gempy.plot.visualization_2d import PlotData2D
import numpy as np
import os
def export_geomap2geotiff(path, geo_model, geo_map=None, geotiff_filepath=None):
"""
Args:
path (str): Filepath for the exported geotiff, must end in .tif
geo_map (np.ndarray): 2-D array containing the geological map
cmap (matplotlib colormap): The colormap to be used for the export
geotiff_filepath (str): Filepath of the template geotiff
Returns:
Saves the geological map as a geotiff to the given path.
"""
import gdal
plot = PlotData2D(geo_model)
cmap = plot._cmap
norm = plot._norm
if geo_map is None:
geo_map = geo_model.solutions.geological_map[0].reshape(geo_model.grid.topography.resolution)
if geotiff_filepath is None:
# call the other function
print('stupid')
# **********************************************************************
geo_map_rgb = SM(norm=norm, cmap=cmap).to_rgba(geo_map.T) # r,g,b,alpha
# **********************************************************************
# gdal.UseExceptions()
ds = gdal.Open(geotiff_filepath)
band = ds.GetRasterBand(1)
arr = band.ReadAsArray()
[cols, rows] = arr.shape
outFileName = path
driver = gdal.GetDriverByName("GTiff")
options = ['PROFILE=GeoTiff', 'PHOTOMETRIC=RGB', 'COMPRESS=JPEG']
outdata = driver.Create(outFileName, rows, cols, 3, gdal.GDT_Byte, options=options)
outdata.SetGeoTransform(ds.GetGeoTransform()) # sets same geotransform as input
outdata.SetProjection(ds.GetProjection()) # sets same projection as input
outdata.GetRasterBand(1).WriteArray(geo_map_rgb[:, ::-1, 0].T * 256)
outdata.GetRasterBand(2).WriteArray(geo_map_rgb[:, ::-1, 1].T * 256)
outdata.GetRasterBand(3).WriteArray(geo_map_rgb[:, ::-1, 2].T * 256)
outdata.GetRasterBand(1).SetColorInterpretation(gdal.GCI_RedBand)
outdata.GetRasterBand(2).SetColorInterpretation(gdal.GCI_GreenBand)
outdata.GetRasterBand(3).SetColorInterpretation(gdal.GCI_BlueBand)
# outdata.GetRasterBand(4).SetColorInterpretation(gdal.GCI_AlphaBand) # alpha band
# outdata.GetRasterBand(1).SetNoDataValue(999)##if you want these values transparent
outdata.FlushCache() # saves to disk
outdata = None # closes file (important)
band = None
ds = None
print("Successfully exported geological map to " +path)
def export_moose_input(geo_model, path=None):
"""
Method to export a 3D geological model as MOOSE compatible input.
Args:
path (str): Filepath for the exported input file
Returns:
"""
# get model dimensions
nx, ny, nz = geo_model.grid.regular_grid.resolution
xmin, xmax, ymin, ymax, zmin, zmax = geo_model.solutions.grid.regular_grid.extent
# get unit IDs and restructure them
ids = np.round(geo_model.solutions.lith_block)
ids = ids.astype(int)
liths = ids.reshape((nx, ny, nz))
liths = liths.flatten('F')
# create unit ID string for the fstring
idstring = '\n '.join(map(str, liths))
# create a dictionary with unit names and corresponding unit IDs
sids = dict(zip(geo_model.surfaces.df['surface'], geo_model.surfaces.df['id']))
surfs = list(sids.keys())
uids = list(sids.values())
# create strings for fstring, so in MOOSE, units have a name instead of an ID
surfs_string = ' '.join(surfs)
ids_string = ' '.join(map(str, uids))
fstring = f"""[MeshGenerators]
[./gmg]
type = GeneratedMeshGenerator
dim = 3
nx = {nx}
ny = {ny}
nz = {nz}
xmin = {xmin}
xmax = {xmax}
yim = {ymin}
ymax = {ymax}
zmin = {zmin}
zmax = {zmax}
block_id = '{ids_string}'
block_name = '{surfs_string}'
[../]
[./subdomains]
type = ElementSubdomainIDGenerator
input = gmg
subdomain_ids = '{idstring}'
[../]
[]
[Mesh]
type = MeshGeneratorMesh
[]
"""
if not path:
path = './'
if not os.path.exists(path):
os.makedirs(path)
f = open(path+'geo_model_units_moose_input.i', 'w+')
f.write(fstring)
f.close()
print("Successfully exported geological model as moose input to "+path)