-
Notifications
You must be signed in to change notification settings - Fork 1
/
Config.jl
603 lines (539 loc) · 18.2 KB
/
Config.jl
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
using Spice
using Requests
import Requests: get
function get_spice(path)
root_url = "http://naif.jpl.nasa.gov/pub/naif/toolkit/C/"
if is_linux()
platform_url = "PC_Linux_GCC_64bit/"
elseif is_osx()
platform_url = "MacIntel_OSX_AppleC_64bit/"
elseif is_windows()
println("Windows not supported")
quit()
end
pkg_name = "packages/cspice.tar.Z"
full_url = root_url * platform_url * pkg_name
println()
println(" - Downloading SPICE library:")
println(" ", full_url)
print(" This might take a moment... ")
for j in 1:5
try
cspice_archive = get(full_url)
save(cspice_archive, joinpath(path, "cspice.tar.Z"))
println("OK")
break
catch
println()
print(" could not download cspice, trying again...")
sleep(1)
end
end
println()
println(" giving up!")
return 1
end
function get_spice_kernels()
#url = "https://www.dropbox.com/s/7qr8e1kmeij2e71/spiceKernels.zip?dl=1"
url = "https://dl.dropboxusercontent.com/u/1910400/ices-desktop-data/spiceKernels.zip"
println()
println(" - Downloading SPICE kernels:")
println(" ", url)
print(" This might take a moment... ")
for j in 1:5
try
kernels = get(url)
save(kernels, "spiceKernels.zip")
println("OK")
break
catch
println()
print("could not download kernels, trying again...")
sleep(1)
end
end
println()
println(" giving up!")
return 1
end
function get_additional_data()
url = "https://dl.dropboxusercontent.com/u/1910400/ices-desktop-data/additionalData.zip"
#url = "https://www.dropbox.com/s/rp23i57dgwjpkob/additionalData.zip?dl=1"
println()
println(" - Downloading additional data:")
println(" ", url)
print(" This might take a moment... ")
for j in 1:5
try
data = get(url)
save(data, "additionalData.zip")
println("OK")
return 0
catch
println()
print(" could not download additionalData.zip, trying again...")
sleep(1)
end
end
println()
println(" giving up!")
return 1
end
function get_object_files(path)
objectFiles = AbstractString[]
for fileName in readdir(path)
if split(fileName, '.')[end] == "o"
push!(objectFiles, fileName)
end
end
return objectFiles
end
function compileLinux(datadir)
previousDir = pwd()
cd(joinpath(datadir, "lib"))
run(`ar -x cspice.a`)
run(`ar -x csupport.a`)
objectFiles = get_object_files(".")
run(`gcc -shared -fPIC -lm $objectFiles -o spice.so`)
cd(previousDir)
return objectFiles
end
function compileClibLinux(fileName)
previousDir = pwd()
cd(dirname(fileName))
cFile = basename(fileName)
run(`gcc -shared -c -fPIC $cFile`)
println(`gcc -shared -c -fPIC $cFile`)
objectFiles = get_object_files(".")
run(`gcc -shared -fPIC -lm $objectFiles -o clib.so`)
println(`gcc -shared -fPIC -lm $objectFiles -o clib.so`)
run(`rm $objectFiles`)
println(`rm $objectFiles`)
cd(previousDir)
end
function compileOSX(datadir)
previousDir = pwd()
cd(joinpath(datadir, "lib"))
run(`ar -x cspice.a`)
run(`ar -x csupport.a`)
objectFiles = get_object_files(".")
run(`gcc -dynamiclib -lm $objectFiles -o spice.dylib`)
cd(previousDir)
return objectFiles
end
function compileClibOSX(fileName)
previousDir = pwd()
cd(dirname(fileName))
cFile = basename(fileName)
run(`gcc -c -fPIC $cFile`)
println(`gcc -c -fPIC $cFile`)
objectFiles = get_object_files(".")
run(`gcc -dynamiclib -lm $objectFiles -o clib.dylib`)
println(`gcc -dynamiclib -lm $objectFiles -o clib.dylib`)
run(`rm $objectFiles`)
println(`rm $objectFiles`)
cd(previousDir)
end
function linux()
global os
os = "linux"
end
function osx()
global os
os = "osx"
end
function updateSettings(keyword, newValue)
mv(".userSettings.conf", ".userSettings.bkp")
iFile = open(".userSettings.bkp", "r")
oFile = open(".userSettings.conf", "w")
foundKeyword = false
while !eof(iFile)
line = readline(iFile)
if contains(line, keyword)
if length(newValue) >= 1
write(oFile, keyword, newValue, "\n")
end
foundKeyword = true
else
write(oFile, line)
end
end
if (!foundKeyword) & (length(newValue) > 0)
write(oFile, keyword, newValue, "\n")
end
close(iFile)
close(oFile)
rm(".userSettings.bkp")
end
function get_working_dir()
keyword = "workingDir:"
iFile = open(".userSettings.conf", "r")
while !eof(iFile)
line = readline(iFile)
if contains(line, keyword)
value = string(string(split(line, keyword)[2][1:end-1]))
close(iFile)
return value
end
end
println(" - workingDir not found in .userSettings.conf.")
println(" - run julia Config.jl --datadir <path to datadir>")
close(iFile)
exit()
end
function config_data_file(ARGS, case="dataFile:")
try
dataFile = ARGS[2]
workingdir = get_working_dir()
if contains(dataFile, ".h5")
if !(dataFile == joinpath(workingdir, "input/"*basename(dataFile)))
cp(dataFile, joinpath(workingdir, "input/"*basename(dataFile)), remove_destination=true)
end
updateSettings("dataFile:", joinpath(workingdir, "input/"*basename(dataFile)))
else
print(" - selected data file is not in HDF5 format. Build new .h5 file? (y/n) ")
answ = parseCmdLineArg()
if !contains(answ, "n")
println(" - generating new file, this might take a few seconds.")
cd("src")
try
run(`julia prepareAmpsData.jl $dataFile`)
catch
println(" - !!!Coulnd not convert File!!!")
end
cd("..")
path = dirname(dataFile)
baseName = basename(dataFile)
ext = split(baseName, ".")[end]
newBase = baseName[1:end-(length(ext)+1)] * ".h5"
newDataFile = joinpath(path, newBase)
if !(newDataFile == joinpath(workingdir, "input/"*newBase))
mv(newDataFile, joinpath(workingdir, "input/"*newBase), remove_destination=true)
end
updateSettings(case, joinpath(workingdir, "input/"*newBase))
end
end
catch
println(" --------------------------------------------------------------------")
println(" - There was an error with setting up the data file, try again later")
println(" - to set it with 'julia Config.jl --dataFile /path/to/data/file.dat'")
println(" --------------------------------------------------------------------")
end
end
function config_workingdir(ARGS)
try
rundir = ""
try
rundir = ARGS[2]
catch
rundir = pwd()
end
if isdir(rundir)
println()
println(" - datadir already exists")
if (isdir(joinpath(rundir, "lib")) | isdir(joinpath(rundir, "input")))
print(" - directory 'lib', 'input' or 'output' already exist, replace them? (y/n): ")
answer = readline(STDIN)
if contains(lowercase(answer), "y")
for dirname in ["lib", "input", "output"]
try
rm(joinpath(rundir, dirname), recursive=true)
catch
end
end
mkdir(joinpath(rundir, "lib"))
mkdir(joinpath(rundir, "input"))
mkdir(joinpath(rundir, "output"))
end
else
println(" - create new directories 'lib', 'input' and 'output'")
mkdir(joinpath(rundir, "lib"))
mkdir(joinpath(rundir, "input"))
mkdir(joinpath(rundir, "output"))
end
else
mkdir(rundir)
mkdir(joinpath(rundir, "lib"))
mkdir(joinpath(rundir, "input"))
mkdir(joinpath(rundir, "output"))
end
touch(".userSettings.conf")
updateSettings("workingDir:", rundir)
catch
println(" ------------------------------------------------------------------")
println(" - There was an error with setting up the workingDir, try again later")
println(" - to set it with 'julia Config.jl --workingDir /path/to/dir'")
println(" ------------------------------------------------------------------")
end
end
function config_spicelib(ARGS, isLib=false)
if !isLib
try
sharedLibPath = ARGS[2]
workingdir = get_working_dir()
cp(joinpath(sharedLibPath, "cspice.a"),
joinpath(workingdir, "lib/cspice.a"),
remove_destination=true)
cp(joinpath(sharedLibPath, "csupport.a"),
joinpath(workingdir, "lib/csupport.a"),
remove_destination=true)
if os == "linux"
objectFiles = compileLinux(datadir)
updateSettings("spicelib:", joinpath(workingdir, "lib/spice.so"))
else
objectFiles = compileOSX(datadir)
updateSettings("spicelib:", joinpath(workingdir, "lib/spice.dylib"))
end
previousDir = pwd()
cd(joinpath(workingdir, "lib"))
run(`rm $objectFiles`)
cd(previousDir)
catch
println(" ------------------------------------------------------------------")
println(" - There was an error with setting up the spicelib, try again later")
println(" - to set it with 'julia Config.jl --spicelib /path/to/spicelib'")
println(" ------------------------------------------------------------------")
end
else
sharedLib = ARGS[2]
updateSettings("spicelib:", sharedLib)
end
end
function config_kernelfile(ARGS)
try
kernelFile = ARGS[2]
updateSettings("kernelFile:", kernelFile)
catch
println(" ------------------------------------------------------------------")
println(" - There was an error with setting up the kernelFile, try again later")
println(" - to set it with 'julia Config.jl --spicelib /path/to/metafile.tm'")
println(" ------------------------------------------------------------------")
end
end
function config_clib(ARGS)
clibFile = ARGS[2]
datadir = get_data_dir()
if os == "linux"
ext = ".so"
else
ext = ".dylib"
end
try
cp(clibFile, joinpath(datadir, "lib/"*basename(clibFile)), remove_destination=true)
sharedLibName = joinpath(datadir, "lib/clib" * ext )
updateSettings("clibFile:", sharedLibName)
if os == "linux"
compileClibLinux(joinpath(datadir, "lib/"*basename(clibFile)))
else
compileClibOSX(joinpath(datadir, "lib/"*basename(clibFile)))
end
catch
println("Did not find c library! ")
updateSettings("clibFile:", "")
end
end
function config_meshfile(ARGS)
try
meshFile = ARGS[2]
workingdir = get_working_dir()
if !(meshFile == joinpath(workingdir, "input/"*basename(meshFile)))
cp(meshFile, joinpath(workingdir, "input/"*basename(meshFile)), remove_destination=true)
updateSettings("meshFile:", joinpath(workingdir, "input/"*basename(meshFile)))
else
updateSettings("meshFile:", meshFile)
end
catch
println(" ------------------------------------------------------------------")
println(" - There was an error with setting up the meshfile, try again later")
println(" - to set it with 'julia Config.jl --meshfile /path/to/meshfile.ply'")
println(" ------------------------------------------------------------------")
end
end
function config_meshfileshadow(ARGS)
try
meshFile = ARGS[2]
workingdir = get_working_dir()
cp(meshFile, joinpath(workingdir, "input/"*basename(meshFile)), remove_destination=true)
cp(meshFile, joinpath(workingdir, "input/"*basename(meshFile)), remove_destination=true)
updateSettings("meshFileShadow:", joinpath(workingdir, "input/"*basename(meshFile)))
catch
println(" ------------------------------------------------------------------")
println(" - There was an error with setting up the meshfileshadow, try again later")
println(" - to set it with 'julia Config.jl --meshfileshadow /path/to/meshfile.ply'")
println(" ------------------------------------------------------------------")
end
end
function config_docheckshadow(ARGS)
doCheckShadow = ARGS[2]
updateSettings("doCheckShadow:", doCheckShadow)
end
function parseCmdLineArg()
arg = strip(string(string(readline(STDIN)[1:end-1])))
end
################################################################################
# start main
################################################################################
os = "operatingSystem"
is_linux() ? linux() : osx()
if lowercase(ARGS[1]) == "--workingdir"
config_workingdir(ARGS)
elseif lowercase(ARGS[1]) == "--spicelib"
config_spicelib(ARGS)
elseif lowercase(ARGS[1]) == "--kernelfile"
config_kernelfile(ARGS)
elseif lowercase(ARGS[1]) == "--clib"
config_clib(ARGS)
elseif lowercase(ARGS[1]) == "--noclib"
updateSettings("clibFile:", "")
elseif lowercase(ARGS[1]) == "--meshfile"
config_meshfile(ARGS)
elseif lowercase(ARGS[1]) == "--meshfileshadow"
config_meshfileshadow(ARGS)
elseif lowercase(ARGS[1]) == "--docheckshadow"
config_docheckshadow(ARGS)
elseif lowercase(ARGS[1]) == "--datafile"
config_data_file(ARGS)
elseif lowercase(ARGS[1]) == "--datafiletestgas"
config_data_file(ARGS, "dataFileTestGas:")
elseif lowercase(ARGS[1]) == "--datafiletestdust"
config_data_file(ARGS, "dataFileTestDust:")
elseif lowercase(ARGS[1]) == "--clean"
workingdir = get_working_dir()
previousDir = pwd()
cd(joinpath(workingdir, "lib"))
allFiles = readdir()
if length(allFiles) > 0
run(`rm $allFiles`)
end
cd(joinpath(workingdir, "input"))
allFiles = readdir()
if length(allFiles) > 0
run(`rm $allFiles`)
end
cd(previousDir)
elseif lowercase(ARGS[1]) == "--show"
iFile = open(".userSettings.conf", "r")
while !eof(iFile)
print(" \u2764 " * readline(iFile))
end
close(iFile)
elseif lowercase(ARGS[1]) == "--help"
print(" \U2665 say please: ")
answ = readline(STDIN)
if contains(lowercase(answ), "please")
println(" - Thanks! You have the following options to call Config.jl:")
println(" - All paths have to be absolute.")
println(" - option arguments are case insensitive (datadir == datadir)")
println("")
println("--workingdir directory where files for runs are stored")
println("--spicelib directory to cspice.a and csupport.a")
println("--kernelfile spice kernel metafile")
println("--clib custom shared library to be used in LOS calculation")
println("--noclib do not use any shared c library")
println("--meshfile .ply file of the body surface mesh")
println("--meshfileshadow .ply file of the body surface mesh for shadow calc.")
println("--docheckshadow yes or no if shadow calculation is needed")
println("--datafile Full path to .h5 or .dat AMPS output file")
println("--datadir Full path to .h5 or .dat AMPS output file")
println("--clean remove 'lib' and 'input' dirs in tmpfile")
println("--help show this message")
else
println(" \U0001f631 Too bad, better luck next time...")
end
elseif lowercase(ARGS[1]) == "--auto"
println(" - START auto setup:")
println(" - This will setup the necessary parameters for the ices-desktop")
println(" - tool. settings are stored in ices-desktop/.userSettings.conf")
println(" - You have the following options to set:")
println(" - (All paths have to be absolute.)")
println("")
println(" - --workingdir directory where files for runs are stored")
println(" - --spicelib directory to cspice.a and csupport.a")
println(" - --kernelfile spice kernel metafile")
println(" - --meshfile .ply file of the body surface mesh")
println(" - --datafile Full path to .h5 or .dat AMPS output file")
println(" - --datadir Full path to .h5 or .dat AMPS output file")
println(" - --clib custom shared library to be used in LOS calculation")
println(" - --meshfileshadow .ply file of the body surface mesh for shadow calc.")
println(" - --docheckshadow yes or no if shadow calculation is needed")
println("")
println("")
currentDir = pwd()
defaultDir = joinpath(currentDir, "work")
workingDir = joinpath(currentDir, "work")
print(" - --workingdir ", workingDir, " ")
config_workingdir(["", workingDir])
println("OK")
print(" --spicelib ", sharedLib, " ")
println("OK")
isLib = true
config_spicelib(["", sharedLib], isLib)
# installation of spice kernels
if !isdir("spiceKernels")
get_spice_kernels()
run(`unzip -qq spiceKernels.zip`)
end
if isfile(joinpath(pwd(), "spiceKernels/metafiles/operationalKernels.tm"))
defaultFile = joinpath(pwd(), "spiceKernels/metafiles/operationalKernels.tm")
print(" --kernelfile ", defaultFile, " ")
config_kernelfile(["", defaultFile])
println("OK")
try
rm("spiceKernels.zip")
catch
end
else
println(" - Default SPICE kernels not found: ", defaultFile )
println(" - Please set it up later with 'Julia Config.jl --kernelfile <path to kernelfile>'")
end
get_additional_data()
run(`unzip -qq additionalData.zip`)
for fileName in readdir(joinpath(currentDir, "additionalData"))
cp(joinpath(currentDir, "additionalData", fileName), joinpath(workingDir, "input", fileName),
remove_destination=true)
end
rm("additionalData.zip")
run(`rm -r additionalData`)
meshfile = joinpath(workingDir, "input", "SHAP5.ply")
println(" - Meshfile: ", meshfile)
config_meshfile(["", meshfile])
datafile = joinpath(workingDir, "input", "SHAP5-2.2-20150304T1200.H2O.dat")
println(" - DataFile: ", datafile)
config_data_file(["", datafile])
println()
println(" - You can check/modify your settings in the userSettings.conf file.")
println()
println(" - Mandatory settings done, continue with optional settings? ")
println(" skip a parameter by hitting enter without giving an input.")
print( " continue? (y/n) ")
answ = readline(STDIN)
if !contains(answ, "n")
println()
print(" - --clib ")
arg = parseCmdLineArg()
if length(arg) < 1
println()
println(" - ...skipped")
else
config_clib(["", arg])
println("OK")
end
print(" - --meshFileShadow ")
arg = parseCmdLineArg()
if length(arg) < 1
println()
println(" - ...skipped")
else
config_meshfileshadow(["", arg])
println("OK")
end
print("--doCheckShadow ")
arg = parseCmdLineArg()
if length(arg) < 1
println(" - ...skipped")
else
config_docheckshadow(["", arg])
println("OK")
end
end
end