-
Notifications
You must be signed in to change notification settings - Fork 1
/
G2SUR.py
134 lines (120 loc) · 4.29 KB
/
G2SUR.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
#G2SUR
#Program to convert gravsoft grids (.gri) to surfer ascii format (.grd)
#Input:
# gravsoft grid file (usually .gri) {iFile}
# surfer file name (should be .grd) {oFile}
# ifile = input("Enter the Input File: ")
# ofile = input("Enter the Output File: ")
ifile = 'ipfile.gri' #Predefined Input File
ofile = 'opfile.grd' #Predefined Output File
#Declaring Variables
nmax = 26000000
ficont = [] #Final List Paragraph Wise
templist = [] #Temporary list
finlist = [] #Final Formatted list
icontraw = open(ifile,'r+') #Raw Input File
icont = icontraw.readlines() #List of lines in a paragraph raw-data
rfi1, rfi2, rla1, rla2, dfi, dla = icont[0].split() #Extracting rfi1, rfi2, rla1, rla2, dfi, dla
icont.pop(0) #removing spaces
icont.pop(0) #removing spaces
#Converting the values of rfi1, rfi2, rla1, rla2, dfi, dla into Float values
rfi1, rfi2, rla1, rla2, dfi, dla = float(rfi1), float(rfi2), float(rla1), float(rla2), float(dfi), float(dla)
#Deciding iell and izone
izone = 1
total_no_of_elem = 0
for i in icont:
if i == '\n':
izone+=1
else:
res = len(i.split())
total_no_of_elem+=res
iell = total_no_of_elem/izone
iell = int(iell)
#Creating the final formatted list finlist
icont = list(map(lambda s: s.strip(), icont))
icont = list(filter(lambda a: a != '', icont))
icont = list(map(lambda s: s.split(), icont))
for elem in icont:
for e in elem:
ficont.append(e)
elem_counter = 1
for i in ficont:
if elem_counter == iell:
templist.append(i)
clist = templist[:]
finlist.append(clist)
templist.clear()
elem_counter = 1
else:
templist.append(i)
elem_counter+=1
#final formatted list finlist created
#Assuming Grids
if abs(rfi1)>100 or abs(rfi2)>100:
if iell > 0:
print(f"UTM Grid Assumed., izone: {izone}, iell: {iell}")
if iell < 0:
print("Polar Stereographic Grid Assumed.")
lutm = True
else:
lutm = False
#Deciding nn,ne
nn = (rfi2 - rfi1)/dfi + 1.5
ne = (rla2 - rla1)/dla + 1.5
nn,ne = int(nn),int(ne)
#To Exit the Program if nn x ne > nmax.
if nn*ne > nmax:
print('The value of nn*ne > nmax.')
print('Program Executed Successfully.')
exit()
#Deciding Zmin Zmax
zmin = 9999.9
zmax = -9999.9
j = 0
for listelem in finlist:
for cj in listelem:
cj = float(cj)
if j > nmax:
print("Taken Too small grid increase Nmax.")
print('Program Executed Successfully.')
exit()
if cj > 9999:
cj = 9999
else:
if cj < zmin:
zmin = cj
if cj > zmax:
zmax = cj
j+=1
finlist.reverse() #Reversing the finlist
#Deciding Formatted value if lutm is true
# formattedInfo = f'DSAA\n {ne} {nn}\n {rla1:.5f} {rla2:.5f}\n {rfi1:.5f} {rfi2:.5f}\n {zmin:.2f} {zmax:.2f}\n'
if lutm == True:
rfi1, rfi2, rla1, rla2 = round(rfi1), round(rfi2), round(rla1), round(rla2)
formattedInfo = f'DSAA\n {ne} {nn}\n {rla1:.5f} {rla2:.5f}\n {rfi1:.5f} {rfi2:.5f}\n {zmin:.2f} {zmax:.2f}\n'
else:
formattedInfo = f'DSAA\n {ne} {nn}\n {rla1:.5f} {rla2:.5f}\n {rfi1:.5f} {rfi2:.5f}\n {zmin:.2f} {zmax:.2f}\n'
#Creating & Formating Output File (.grd)
no_of_columns = 6 #To Print Data in x Columns
noc_count = 0 #To count no. of columns printed on each row
opFileVar = open(ofile, 'w')
print(formattedInfo, file = opFileVar)
for listelem in finlist:
for elem in listelem:
elem = float(elem)
print(format(elem, '12.5f'),end=' ',file=opFileVar)
noc_count += 1
if noc_count == no_of_columns:
print('',file=opFileVar)
noc_count = 0
noc_count = 0
print('\n',file=opFileVar)
opFileVar.close()
print(f"{ofile} created successfully.")
print('Program Executed Successfully.')
#End of Program
# Short-Notations used in program are ->
# ifile - Input File | ofile - Output File
# icontraw - List of Raw Input Content | icont - List of Input Content Line Wise
# ficont - List of Final Input Content | templist - Temporary List to Store Data in finlist Para-wise
# finlist - Final Formatted List of Elements (Paragraphs) Stored as List Inside a List