-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess.bas
611 lines (537 loc) · 18.3 KB
/
process.bas
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
' script for creating surfer plots
Sub Main
' get the current dir
cwd = CurDir
configArray = readConfigFile(cwd)
' configuration settings
pdir = getConfigVal("dir", configArray, "str")
titletext = getConfigVal("titletext", configArray, "str")
operator = getConfigVal("operator", configArray, "str")
overwrite = getConfigVal("overwrite", configArray, "bool")
background = getConfigVal("background", configArray, "bool")
leaveopen = getConfigVal("leaveopen", configArray, "bool")
pagewidth = getConfigVal("pagewidth", configArray, "val")
pageheight = getConfigVal("pageheight", configArray, "val")
numcolsperpoint = getConfigVal("numcolsperpoint", configArray, "val")
numrows = getConfigVal("numrows", configArray, "val")
' map size
mapheight = getConfigVal("mapheight", configArray, "val")
mapbottommargin = getConfigVal("mapbottommargin", configArray, "val")
mapsidemargin = getConfigVal("mapsidemargin", configArray, "val")
widescale = getConfigVal("widescale", configArray, "val")
' scale size
scaletopmargin = getConfigVal("scaletopmargin", configArray, "val")
scalesidemargin = getConfigVal("scalesidemargin", configArray, "val")
scaleoffset = getConfigVal("scaleoffset", configArray, "val")
' space size
spacecellwidth = getConfigVal("spacecellwidth", configArray, "val")
'axis labels
showtopaxislabels = getConfigVal("showtopaxislabels", configArray, "bool")
showbottomaxislabels = getConfigVal("showbottomaxislabels", configArray, "bool")
showleftaxislabels = getConfigVal("showleftaxislabels", configArray, "bool")
showrightaxislabels = getConfigVal("showrightaxislabels", configArray, "bool")
' axis titles
showtopaxistitle = getConfigVal("showtopaxistitle", configArray, "bool")
showbottomaxistitle = getConfigVal("showbottomaxistitle", configArray, "bool")
showleftaxistitle = getConfigVal("showleftaxistitle", configArray, "bool")
showrightaxistitle = getConfigVal("showrightaxistitle", configArray, "bool")
axistitleoffset = getConfigVal("axistitleoffset", configArray, "val")
If showtopaxistitle Then
topaxistitle = "[meters]"
Else
topaxistitle = ""
End If
If showbottomaxistitle Then
bottomaxistitle = "[meters]"
Else
bottomaxistitle = ""
End If
If showleftaxistitle Then
leftaxistitle = "Depth [meters]"
Else
leftaxistitle = ""
End If
If showrightaxistitle Then
rightaxistitle = "Depth [feet]"
Else
rightaxistitle = ""
End If
' set directories
If Command$ <> "" Then
' rootdir = Mid(Command$, 2, Len(Command$)-2)
rootdir = Command$
ElseIf pdir <> "" Then
rootdir = pdir
Else
rootdir = cwd
End If
datafiledir = rootdir + "\Gamma\"
surfdir = rootdir + "\Surfer\"
kmldir = rootdir + "\KML\"
rapdir = rootdir + "\RAP\"
Debug.Clear
logf = FreeFile
logdiv = String(60, "*")
Open cwd + "\process.log" For Append As #logf
Print #logf, logdiv
Print #logf, "process.bas - Processing - " + getNowStr()
Print #logf, ""
Print #logf, "Working directory: " + rootdir
Print #logf, ""
Debug.Print "Processing gamma"
Debug.Print ""
Print #logf, "Processing gamma"
Print #logf, ""
Set surf = CreateObject("surfer.application")
surf.Visible = Not background
datafiles = GetFiles(datafiledir, "*_Edit.txt")
For Each datafile In datafiles
Debug.Print "Found datafile: " + datafiledir + datafile
Print #logf, "Found datafile: " + datafiledir + datafile
filename = Split(datafile, ".")(0)
gridfile = datafiledir + filename + ".grd"
If Dir(gridfile) = "" or overwrite Then
Debug.Print "Creating grid file: " + gridfile
Print #logf, "Creating grid file: " + gridfile
' create grid file
surf.GridData6(DataFile:= datafiledir + datafile, xCol:=4, yCol:=3, zCol:=2, Algorithm:=srfKriging, ShowReport:=False, OutGrid:=gridfile)
Else
Debug.Print "Found gridfile: " + gridfile
Print #logf, "Found gridfile: " + gridfile
End If
plotfile = surfdir + filename + ".srf"
If Dir(plotfile) = "" or overwrite Then
Debug.Print "Creating plotfile: " + plotfile
Print #logf, "Creating plotfile: " + plotfile
' create plot object
Set Plot = surf.Documents.Add(srfDocPlot)
Set PageSetup = Plot.PageSetup
PageSetup.Width = pagewidth
PageSetup.Height = pageheight
' create shapes object
Set Shapes = Plot.Shapes
' add contour plot
Set MapFrame = Shapes.AddContourMap(GridFileName:=gridfile)
' configure contour plot
Set ContourLayer = MapFrame.Overlays(1)
With ContourLayer
.ShowMajorLabels = False
.ShowMinorLabels = False
.FillForegroundColorMap.LoadFile(surf.Path + "\Samples\Rainbow.clr")
.FillContours = True
.ShowColorScale = True
With .ColorScale
.Visible = True
.Title = "Impulses\n Per Second\n (IPS)"
.TitlePosition = srfColorScaleTitlePositionTop
.TitleAngle = 0
.TitleFont.hAlign = srfTACenter
End With
End With
kmlfile = kmldir + filename + "_grid.kml"
If Dir(kmlfile) = "" or overwrite Then
Debug.Print "Creating kmlfile: " + kmlfile
Print #logf, "Creating kmlfile: " + kmlfile
' hide axis for kml export
For Each axis In MapFrame.Axes
axis.Visible = False
Next
ContourLayer.ColorScale.Visible = False
' export KML file
Plot.Export2(FileName:= kmldir + filename + "_grid.kml", SelectionOnly:=False)
' unhide axis and color scale
For Each axis In MapFrame.Axes
axis.Visible = True
Next
ContourLayer.ColorScale.Visible = True
Else
Debug.Print "Found kmlfile: " + kmlfile
Print #logf, "Found kmlfile: " + kmlfile
End If
' add post map
Set PostFrame = Shapes.AddPostMap(DataFileName:=datafiledir + datafile, xCol:=4, yCol:=3)
Set PostLayer = PostFrame.Overlays(1)
PostLayer.Symbol.Index = 12
PostLayer.Symbol.Size = 0.05
MapFrame.Selected = True
PostFrame.Selected = True
Set NewFrame = plot.Selection.OverlayMaps
With NewFrame.Axes
With .Item("Bottom Axis")
.Title = "Longitude"
.TitleFont.Size = 12
End With
With .Item("Left Axis")
.Title = "Latitude"
.TitleFont.Size = 12
End With
End With
' add title text box
title = "\fs200 " + titletext + "\n "
Set TitleBox = Shapes.AddText(x:=pagewidth/2, y:=pageheight-0.25, Text:=title)
TitleBox.Name = "Title"
TitleBox.Font.hAlign = srfTACenter
boxtext = "Gamma Survey\n " + operator + "\n " + getNowStr("dd.mm.yyyy")
Set StampBox = Shapes.AddText(x:=4.5, y:=pageheight-1, Text:=boxtext)
StampBox.Name = "Stamp"
StampBox.Font.Size = 14
StampBox.Font.hAlign = srfTACenter
' save the plot
Plot.SaveAs(FileName:=plotfile)
Else
Debug.Print "Found plotfile: " + plotfile
Print #logf, "Found plotfile: " + plotfile
End If
datafile = Dir()
Debug.Print ""
Print #logf, ""
Next
Debug.Print "Gamma processing completed"
Debug.Print ""
Print #logf, "Gamma processing completed"
Print #logf, ""
Debug.Print "Processing RAP"
Debug.Print ""
Print #logf, "Processing RAP"
Print #logf, ""
folders = GetDirs(rapdir)
For Each folder In folders
Debug.Print "Found folder: " + folder
Print #logf, "Found folder: " + folder
datadir = rapdir + folder + "\Data_Cor\"
datafiles = GetFiles(datadir, "*frn.txt")
For Each datafile In datafiles
Debug.Print "Found datafile: " + datafile
Print #logf, "Found datafile: " + datafile
filename = Split(datafile, ".")(0)
gridfile = datadir + filename + ".grd"
If (Dir(gridfile) = "") or overwrite Then
creategridfile = True
Else
creategridfile = False
Debug.Print "Found gridfile: " + gridfile
Print #logf, "Found gridfile: " + gridfile
End If
plotfile = surfdir + folder + " " + Split(filename, "frn")(0) + ".srf"
If (Dir(plotfile) = "") or overwrite Then
createplotfile = True
Else
createplotfile = False
Debug.Print "Found plotfile: " + plotfile
Print #logf, "Found plotfile: " + plotfile
End If
If creategridfile or createplotfile Then
' open datafile
Set wks = surf.Documents.Open(datadir + datafile)
rowCount = wks.Columns(1).RowCount
samples = wks.cells(Row:=1,Col:=1).Value
xMin = wks.cells(2,1).Value
xMax = wks.cells(rowCount,Col:=1).Value
xInt = wks.cells(samples+3,1).Value
yMin = wks.cells(2,2).Value
yMax = wks.cells(samples+1,2).Value
yInt = wks.cells(3,2).Value - yMin
' close datafile
wks.Close
End If
If creategridfile Then
Debug.Print "Creating gridfile: " + gridfile
Print #logf, "Creating gridfile: " + gridfile
NumCols = numcolsperpoint * (xMax - xMin) / xInt + 1
surf.GridData6(DataFile:= datadir + datafile, xCol:=1, yCol:=2, zCol:=3, NumCols:=NumCols, NumRows:=numrows, xMin:=xMin, xMax:=xMax, yMin:=yMin, yMax:=yMax, Algorithm:=srfKriging, ShowReport:=False, OutGrid:=gridfile)
End If
If createplotfile Then
Debug.Print "Creating plotfile: " + plotfile
Print #logf, "Creating plotfile: " + plotfile
' create plot object
Set Plot = surf.Documents.Add(srfDocPlot)
Set PageSetup = Plot.PageSetup
PageSetup.Width = pagewidth
PageSetup.Height = pageheight
scalingratio = (yMax - yMin) / mapheight
' create shapes object
Set Shapes = Plot.Shapes
' add contour map
Set MapFrame1 = Shapes.AddContourMap(GridFileName:=gridfile)
With MapFrame1
.Name = "Contour Map"
With .Axes("Top Axis")
.SetScale(MajorInterval:=xInt, LastMajorTick:=xMax)
.ShowLabels = showtopaxislabels
.Title = topaxistitle
End With
With .Axes("Bottom Axis")
.SetScale(MajorInterval:=xInt, LastMajorTick:=xMax)
.ShowLabels = showbottomaxislabels
.Title = bottomaxistitle
.TitleFont.hAlign = srfTACenter
.TitleOffset2 = axistitleoffset
End With
With .Axes("Left Axis")
.Reverse = True
.SetScale(LastMajorTick:=yMax)
.ShowLabels = showleftaxislabels
.Title = leftaxistitle
End With
With .Axes("Right Axis")
.SetScale(LastMajorTick:=yMax)
.ShowLabels = showrightaxislabels
.Title = rightaxistitle
End With
.xMapPerPU = scalingratio
.yMapPerPU = scalingratio
End With
Set ContourLayer = MapFrame1.Overlays(1)
With ContourLayer
.ShowMajorLabels = False
.ShowMinorLabels = False
.FillForegroundColorMap.LoadPreset("rap")
.FillForegroundColorMap.SetDataLimits(0, 1500)
.FillContours = True
.SetSimpleLevels(Min:=0, Max:=1500, Interval:=50)
.ShowColorScale = True
End With
Set ColorScale1 = ContourLayer.ColorScale
With ColorScale1
.Name = "Contour Map Color Scale"
.LabelFrequency = 5
.Title = "\fs120 Contour Map\n ---------------------------\n \fs100 Relative Mechanical\n Strength (Yellow)\n Or\n Weakness (Violet)\n of Strata"
.TitlePosition = srfColorScaleTitlePositionTop
.TitleAngle = 0
.TitleFont.hAlign = srfTACenter
.TitleFont.Size = 12
End With
mf1height = MapFrame1.Height
mf1width = MapFrame1.Width
cs1height = ColorScale1.Height
cs1width = ColorScale1.Width
' add color releif map
Set MapFrame2 = Plot.Shapes.AddColorReliefMap(GridFileName:=gridfile)
With MapFrame2
.Name = "Color Relief Map"
With .Axes("Top Axis")
.SetScale(MajorInterval:=xInt, LastMajorTick:=xMax)
.ShowLabels = showtopaxislabels
.Title = topaxistitle
End With
With .Axes("Bottom Axis")
.SetScale(MajorInterval:=xInt, LastMajorTick:=xMax)
.ShowLabels = showbottomaxislabels
.Title = bottomaxistitle
.TitleFont.hAlign = srfTACenter
.TitleOffset2 = axistitleoffset
End With
With .Axes("Left Axis")
.Reverse = True
.SetScale(LastMajorTick:=yMax)
.ShowLabels = showleftaxislabels
.Title = leftaxistitle
End With
With .Axes("Right Axis")
.SetScale(LastMajorTick:=yMax)
.ShowLabels = showrightaxislabels
.Title = rightaxistitle
End With
.xMapPerPU = scalingratio
.yMapPerPU = scalingratio
End With
Set ColorReliefLayer = MapFrame2.Overlays(1)
With ColorReliefLayer
.TerrainRepresentation = srfTerrainRepresentationColorOnly
.ColorMap.LoadPreset("Terrain")
.ColorMap.SetDataLimits(0, 1500)
.ShowColorScale = True
End With
Set ColorScale2 = ColorReliefLayer.ColorScale
With ColorScale2
.Name = "Color Relief Map Color Scale"
.Title = "\fs120 Color Relief Map\n ---------------------------\n \fs100 Relative Mechanical\n Strength (Green)\n Or\n Weakness (Blue)\n of Strata"
.TitlePosition = srfColorScaleTitlePositionTop
.TitleAngle = 0
.TitleFont.hAlign = srfTACenter
.TitleFont.Size = 12
End With
mf2height = MapFrame2.Height
mf2width = MapFrame2.Width
cs2height = ColorScale2.Height
cs2width = ColorScale2.Width
' make a wide copy of color relief map
Plot.Selection.DeselectAll
MapFrame2.Selected = True
Plot.Selection.Copy
Plot.Shapes.Paste
For Each shape In Plot.Selection
If shape.Name = "Color Relief Map" Then
Set MapFrame3 = shape
shape.Name = "Color Relief Map Wide"
shape.xMapPerPU = scalingratio / widescale
shape.yMapPerPU = scalingratio
End If
Next
mf3height = mapframe3.Height
mf3width = MapFrame3.Width
maptop = mf1height + mapbottommargin
mapwidth = (xMax - xMin) / scalingratio
mapcellwidth = mf1width + mapsidemargin
widemapcellwidth = mf3width + mapsidemargin
scaletop = maptop - scaletopmargin
scalecellwidth = cs1width + scalesidemargin
cellswidth = 2 * mapcellwidth + 2 * scalecellwidth + widemapcellwidth + spacecellwidth
cellsleft = (pagewidth - cellswidth) / 2
scalecell1left = cellsleft
scalecell1center = scalecell1left + scalecellwidth / 2
scalecell1right = scalecell1left + scalecellwidth
mapcell1left = scalecell1right
mapcell1center = mapcell1left + mapcellwidth / 2
mapcell1right = mapcell1left + mapcellwidth
mapcell2left = mapcell1right + spacecellwidth
mapcell2center = mapcell2left + mapcellwidth / 2
mapcell2right = mapcell2left + mapcellwidth
scalecell2left = mapcell2right
scalecell2center = scalecell2left + scalecellwidth / 2
scalecell2right = scalecell2left + scalecellwidth
mapcell3left = scalecell2right
mapcell3center = mapcell3left + widemapcellwidth / 2
mapcell3right = mapcell3left + widemapcellwidth
Plot.Selection.DeselectAll
map1left = mapcell1center - mf1width / 2
With MapFrame1
.Selected = True
Plot.Selection.Left = map1left
Plot.Selection.Top = maptop
.Selected = False
End With
scale1left = scalecell1center - cs1width / 2 + scaleoffset
With ColorScale1
.Selected = True
Plot.Selection.Left = scale1left
Plot.Selection.Top = scaletop
.Selected = False
End With
map2left = mapcell2center - mf2width / 2
With MapFrame2
.Selected = True
Plot.Selection.Left = map2left
Plot.Selection.Top = maptop
.Selected = False
End With
scale2left = scalecell2center - cs2width / 2 + scaleoffset
With ColorScale2
.Selected = True
Plot.Selection.Left = scale2left
Plot.Selection.Top = scaletop
.Selected = False
End With
' position color relief wide
map3left = mapcell3center - mf3width / 2
With MapFrame3
.Selected = True
Plot.Selection.Left = map3left
Plot.Selection.Top = maptop
.Selected = False
End With
' add title text box
title = "\fs200 " + titletext + "\n "
title = title + "\fs100 \n \fs160 " + folder + " -" + Str(xMax - xMin) + "m @" + Str(xInt) + "m spacing - Depth:" + Str(yMax) + "m"
Set TitleBox = Shapes.AddText(x:=pagewidth/2, y:=pageheight-0.25, Text:=title)
TitleBox.Name = "Title"
TitleBox.Font.hAlign = srfTACenter
boxtext = "Passive Seismic Profiles\n " + operator + "\n " + getNowStr("dd.mm.yyyy")
Set StampBox = Shapes.AddText(x:=3.5, y:=pageheight-1, Text:=boxtext)
StampBox.Name = "Stamp"
StampBox.Font.Size = 14
StampBox.Font.hAlign = srfTACenter
' save the plot
Plot.SaveAs(FileName:=plotfile)
End If
Next
Debug.Print ""
Print #logf, ""
Next
Debug.Print "RAP processing completed"
Print #logf, "RAP processing completed"
Print #logf
Print #logf, "End Log - " + getNowStr()
Print #logf, logdiv
' close logfile
Close #logf
If Not leaveopen Then
surf.Quit
End If
End Sub
' returns the value of the config parameter from the config array
Function getConfigVal(key As String, configArray As Variant, dtype As String) As Variant
getConfigVal = Null
For Each pair In configArray
If pair(0) = key Then
If dtype = "str" Then
getConfigVal = pair(1)
ElseIf dtype = "val" Then
getConfigVal = Val(pair(1))
ElseIf dtype = "bool" Then
getConfigVal = CBool(pair(1))
End If
Exit For
End If
Next
End Function
' returns an array containing the values from the config file
Function readConfigFile(cwd As String) As Variant
Dim configArray() As Variant
index = 0
' Specify the path to the configuration file
configFile = cwd + "\config.txt"
' Check if the file exists
If Dir(configFile) = "" Then
MsgBox "Config file not found!"
Exit Function
End If
' Open and read file
cfile = FreeFile
Open configFile For Input As #cfile
Do While Not EOF(cfile)
' Read the entire line into the variable
Line Input #cfile, linevals
' Split the line into key-value pairs
keyValue = Split(linevals, "=")
' Process key-value pairs
If UBound(keyValue) = 1 Then
ReDim Preserve configArray(index)
configArray(index) = Array(Trim(keyValue(0)), Trim(keyValue(1)))
index = index + 1
End If
Loop
Set readConfigFile = configArray
End Function
' returns the current date/time as a formatted string
Function getNowStr(Optional fmt As String = "yyyy.mm.dd hh:nn:ss")
getNowStr = Format(Now, fmt)
End Function
' returns an array of the folder names in path
Function GetDirs(path As String) As Variant
Dim dirs() As String
index = 0
file = Dir(path, 16)
While file <> ""
If InStr(file, ".") = 0 Then
ReDim Preserve dirs(index)
dirs(index) = file
index = index + 1
End If
file = Dir()
Wend
GetDirs = dirs
End Function
' returns and array of the file names found in path matching pattern
Function GetFiles(path As String , Optional pattern As String = "") As Variant
Dim files() As String
index = 0
file = Dir(path + pattern)
While file <> ""
ReDim Preserve files(index)
files(index) = file
index = index + 1
file = Dir()
Wend
GetFiles = files
End Function