pylot/pylot/tomography/fmtomo_tools/gmtslice_sidehook.py
2025-04-10 13:58:01 +02:00

175 lines
5.8 KiB
Python

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
import os
import numpy as np
from obspy.geodetics import gps2dist_azimuth, locations2degrees
from pylot.tomography.utils import get_coordinate_from_dist_baz
R=6371.
def main():
profiles_infile = '/home/marcel/AlpArray/vtk_files/points_mark.txt'
gmtslice_dir = '/rscratch/minos13/marcel/fmtomo_alparray/v3.5/alparray_mantle_diehl_crust_included_hf_gradient_smoothing/plot'
####
# maximum point spacing in km (decreases with depth)
dpoints = 1.
dpi = 100
model_depth = 606.
###
with open(profiles_infile, 'r') as infile:
profiles = infile.readlines()
for profile in profiles:
print('Profile: ', profile)
name = profile.split()[0]
lon1, lat1, lon2, lat2 = [float(item) for item in profile.split()[1:]]
def get_gmtslice_gc(gmtplot_dir, lat1, lon1, lat2, lon2, model_depth=606., dpoints=1, fn_out_slice='grid2dvgc.z',
rel=True, fname_vgrid='../vgrids.in', fname_vgrid_ref='../vgridsref.in'):
parfile_str_len = 28
assert (len(fname_vgrid) < parfile_str_len), 'Check length of filename: {}'.format(fname_vgrid)
assert (len(fname_vgrid_ref) < parfile_str_len), 'Check length of filename: {}'.format(fname_vgrid_ref)
infile_default = 'gmtslice_default.in'
fn_out = 'gmtslice.in'
bounds_out_gmt = 'boundgc.gmt'
cwd = os.getcwd()
os.chdir(gmtplot_dir)
with open(infile_default, 'r') as infile:
gmt_infile = infile.readlines()
# calculate great circle distance
max_dist = gps2dist_azimuth(lat1, lon1, lat2, lon2, a=R * 1e3, f=0)[0]/1e3
npointsR = int(model_depth / dpoints + 1)
npointsLateral = int(max_dist / dpoints + 1)
print('nR, nLateral:', npointsR, npointsLateral)
# filename
gmt_infile[3] = '{} \n'.format(fname_vgrid)
gmt_infile[4] = '{} \n'.format(fname_vgrid_ref)
# activate gc generation
gmt_infile[51] = '1 \n'
# modify lines with lat lon boundaries
gmt_infile[52] = '{:5.2f} {:5.2f}\n'.format(lat1, lon1)
gmt_infile[53] = '{:5.2f} {:5.2f}\n'.format(lat2, lon2)
abs_rel = 1 if rel else 0
gmt_infile[58] = '{} \n'.format(abs_rel)
# modify lines with n Points
gmt_infile[65] = '{:<5d} {:<5d}\n'.format(npointsLateral, npointsR)
with open(fn_out, 'w') as outfile:
for line in gmt_infile:
outfile.write(line)
print('Executing gmtslice...')
os.system('gmtslice')
print('Done!')
with open(bounds_out_gmt, 'r') as infile:
bds = [float(bd) for bd in infile.readlines()]
bounds = '-R{bds[0]}/{bds[1]}/{bds[2]}/{bds[3]}'.format(bds=bds)
xyz_string = 'gmt xyz2grd {grid_out} -Ggrid2dvgc.grd -I{bds[4]}+/{bds[5]}+ -ZLB {bounds}'.format(
grid_out=fn_out_slice, bds=bds, bounds=bounds)
print(xyz_string)
os.system(xyz_string)
grid = np.loadtxt(fn_out_slice)
dist_grid = []
lon_grid = []
lat_grid = []
_, azim, bazim = gps2dist_azimuth(lat1, lon1, lat2, lon2, a=R * 1e3, f=0)
#ddist = / (npointsLateral - 1)
#ddepth = (bds[3] - bds[2]) / (npointsR - 1)
for depth in np.linspace(-bds[3], -bds[2], num=npointsR):
for dist in np.linspace(0, locations2degrees(lat1, lon1, lat2, lon2), num=npointsLateral):
lon, lat = get_coordinate_from_dist_baz((lon1, lat1), dist, azim)
lon_grid.append(lon)
lat_grid.append(lat)
#dist = ddist * indexLat
#depth = ddepth * indexR
dist_grid.append(np.array([dist, depth]))
dist_grid = np.array(dist_grid)
lat_grid = np.array(lat_grid)
lon_grid = np.array(lon_grid)
os.chdir(cwd)
return grid, dist_grid, lat_grid, lon_grid, max_dist
def get_gmtslice_depth(gmtplot_dir, depth, fn_out_slice='grid2dvd.z', fname_vgrid='../vgrids.in',
fname_vgrid_ref='../vgridsref.in'):
infile_default = 'gmtslice_default.in'
fn_out = 'gmtslice.in'
bounds_out_gmt = 'bounddp.gmt'
cwd = os.getcwd()
os.chdir(gmtplot_dir)
with open(infile_default, 'r') as infile:
gmt_infile = infile.readlines()
# filename
gmt_infile[3] = '{} \n'.format(fname_vgrid)
gmt_infile[4] = '{} \n'.format(fname_vgrid_ref)
# activate depth slice generation
gmt_infile[41] = '1 \n'
# set depth (negative for whatever reason)
gmt_infile[42] = '{:5.2f} \n'.format(-depth)
with open(fn_out, 'w') as outfile:
for line in gmt_infile:
outfile.write(line)
print('Executing gmtslice...')
os.system('gmtslice')
print('Done!')
with open(bounds_out_gmt, 'r') as infile:
bds = [float(bd) for bd in infile.readlines()]
bounds = '-R{bds[0]}/{bds[1]}/{bds[2]}/{bds[3]}'.format(bds=bds)
xyz_string = 'gmt xyz2grd {grid_out} -Ggrid2dvd.grd -I{bds[4]}+/{bds[5]}+ -ZLB {bounds}'.format(
grid_out=fn_out_slice, bds=bds, bounds=bounds)
print(xyz_string)
#os.system(xyz_string)
grid = np.loadtxt(fn_out_slice)
lonlat = []
lon0, lon1 = bds[:2]
lat0, lat1 = bds[2:4]
nlon = int(bds[4])
nlat = int(bds[5])
for lon in np.linspace(lon0, lon1, num=nlon):
for lat in np.linspace(lat0, lat1, num=nlat):
lonlat.append(np.array([lon, lat]))
lonlat = np.array(lonlat)
os.chdir(cwd)
return grid, lonlat
def grdimage_slice(lat1, lon1, lat2, lon2, bounds, name, npointsLateral, npointsR, dpi):
proj = "-JX{:05.2f}i/{:05.2f}i".format(npointsLateral / dpi, npointsR / dpi)
fnout = 'gmtslice_{}_{:.1f}_{:.1f}-{:.1f}_{:.1f}'.format(name, lat1, lon1, lat2, lon2)
grdimage_string = 'gmt grdimage grid2dvgc.grd {bounds} {proj} -Ba50f10/a50f10 -Cpolar_inv_3.cpt -K > {fnout}'.format(
bounds=bounds, proj=proj, fnout=fnout+'.ps')
print(grdimage_string)
os.system(grdimage_string)
print('Convert to png...')
os.system('convert {} {}'.format(fnout + '.ps', fnout + '.png'))