Created
February 12, 2024 13:13
-
-
Save hersfeldtn/812ba0ae4a02cae519eb4479a6ca287a to your computer and use it in GitHub Desktop.
A python script to modify gplates files to shift all dates or flip east and west
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import fileinput | |
import os | |
#shifts all dates while not modifying any set to 0.0 and not moving any other dates forward of 0.01 | |
timeshift = False | |
#amount to be shifted (positive shifts back in time, negative forward in time) | |
shift = -100.0 | |
#flips east and west of all objects and rotations | |
flip = False | |
#script should be placed in folder with all files intended for modification | |
path = os.path.join(os.path.dirname(__file__), '') | |
files = [] | |
for f in os.listdir(path): | |
if f.endswith('.gpml'): | |
files.append(f) | |
if timeshift: | |
for line in fileinput.input('rotation.rot', inplace=True): | |
new_line = line | |
i=0 | |
i1=0 | |
i2=0 | |
start=False | |
space=False | |
for c in line: | |
if not start: | |
if c == ' ' or c == '/t': | |
space = True | |
elif space: | |
start = True | |
i1 = i | |
else: | |
if c.isnumeric() or c == '-' or c == '.': | |
pass | |
else: | |
i2 = i | |
num = float(line[i1:i2]) | |
if num == 0.0: | |
break | |
new_num = max(0.01, num + shift) | |
new_line = line[:i1] + str(round(new_num,2)) + line[i2:] | |
break | |
i+=1 | |
print (new_line, end = '') | |
fileinput.close() | |
for line in fileinput.input(files, inplace=True): | |
new_line = line | |
if '<gml:timePosition gml:frame="http://gplates.org/TRS/flat">' in line: | |
i=0 | |
start = False | |
first = True | |
i1=0 | |
i2=0 | |
for c in line: | |
if not start: | |
if c == '>': | |
start = True | |
else: | |
if c.isnumeric() or c == '-' or c == '.': | |
if first: | |
i1 = i | |
first = False | |
elif c == '<': | |
i2 = i | |
num = float(line[i1:i2]) | |
if num == 0.0: | |
break | |
new_num = max(0.01, num + shift) | |
new_line = line[:i1] + str(round(new_num,2)) + line[i2:] | |
break | |
else: | |
break | |
i+=1 | |
print (new_line, end = '') | |
fileinput.close() | |
if flip: | |
for line in fileinput.input('rotation.rot', inplace=True): | |
new_line = line | |
i=0 | |
w=0 | |
s=False | |
for c in line: | |
if c == ' ' or c == '/t': | |
if not s: | |
s = True | |
w += 1 | |
else: | |
if s: | |
s = False | |
if w == 3 or w == 4: | |
if c == '-': | |
new_line = new_line[:i] + new_line[i+1:] | |
i -= 1 | |
else: | |
new_line = new_line[:i] + '-' + new_line[i:] | |
i += 1 | |
i+=1 | |
print (new_line, end = '') | |
fileinput.close() | |
for line in fileinput.input(files, inplace=True): | |
new_line = line | |
if '<gml:posList gml:dimension="2">' in line or '<gml:pos>' in line: | |
i=0 | |
start = False | |
space = True | |
word = False | |
for c in line: | |
if not start: | |
if c == '>': | |
start = True | |
else: | |
if c == '<': | |
start = False | |
continue | |
if c == ' ': | |
space = True | |
elif space == True: | |
space = False | |
if word: | |
if c == '-': | |
new_line = new_line[:i] + new_line[i+1:] | |
i-=1 | |
else: | |
new_line = new_line[:i] + '-' + new_line[i:] | |
i+=1 | |
word = False | |
else: | |
word = True | |
i+=1 | |
print (new_line, end = '') | |
fileinput.close() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment