Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
quasi-MC now works with the updated python!! However, the time conversion may be messed up
  • Loading branch information
blevins2 committed Mar 8, 2022
1 parent ec5048f commit b9e63d8
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 13 deletions.
4 changes: 4 additions & 0 deletions compile.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
./autogen.sh
cd build
../configure
make clean; make

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 0 additions & 4 deletions examples/global-lunar-bombardment/.idea/misc.xml

This file was deleted.

4 changes: 2 additions & 2 deletions examples/global-lunar-bombardment/ctem.in
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ runtype single ! Run type: options are normal /

! CTEM required inputs
seed 76535 ! Random number generator seed
gridsize 200 ! Size of grid in pixels
gridsize 2000 ! Size of grid in pixels
numlayers 10 ! Number of perched layers
pix 3.08e4 ! Pixel size (m)
pix 3.08e3 ! Pixel size (m)
mat rock ! Material (rock or ice)
! Bedrock scaling parameters
mu_b 0.55e0 ! Experimentally derived parameter for bedrock crater scaling law
Expand Down
File renamed without changes.
29 changes: 25 additions & 4 deletions python/ctem/ctem/driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ def __init__(self, param_file="ctem.in", isnew=True):
'sfdcompare': None,
'sfdfile': None,
'quasimc': None,
'realcraterlist': None
'realcraterlist': None,
}

self.user = util.read_user_input(self.user)

# This is containsall files generated by the main Fortran CTEM program plus the ctem.dat file that
Expand Down Expand Up @@ -113,8 +114,28 @@ def __init__(self, param_file="ctem.in", isnew=True):

#Read list of real craters
print("quasi-MC mode is ON")
# Use self.compute_one_interval() to generate craterlist.dat
rclist = util.read_formatted_ascii(self.user['realcraterlist'], skip_lines = 0)
self.tempfile = os.path.join(currentdir, 'temp.in')

# Generate craterlist.dat
with open(self.user['ctemfile']) as perm, open(self.tempfile, "w") as temp:
for line in perm:
temp.write(line)

#Write a temporary input file to generate the necessary quasimc files
self.permfile = self.user['ctemfile']
self.temp = util.write_temp_input(self.tempfile)
self.user['ctemfile'] = self.tempfile
self.user = util.read_user_input(self.user)
self.permin = os.path.join(currentdir, 'perm.in')
self.ctemin = os.path.join(currentdir, 'ctem.in')
os.rename(self.permfile, self.permin)
os.rename(self.tempfile, self.ctemin)
self.run()

os.replace(self.permin, self.ctemin)
self.user['ctemfile'] = self.permfile
self.user = util.read_user_input(self.user)

#Interpolate craterscale.dat to get impactor sizes from crater sizes given
df = pandas.read_csv(self.output_filenames['craterscale'], sep='\s+')
Expand All @@ -130,7 +151,7 @@ def __init__(self, param_file="ctem.in", isnew=True):
rclist = rclist[rclist[:,5].argsort()]

#Export to dat file
util.write_realcraters(user, rclist)
util.write_realcraters(self.output_filenames['craterlist'], rclist)

util.write_datfile(self.user, self.output_filenames['dat'], self.seedarr)
else:
Expand Down Expand Up @@ -179,7 +200,7 @@ def run(self):
# Read in output files
self.read_output()

# Process the o utput files
# Process the output files
self.process_output()

# Update ncount
Expand Down
44 changes: 42 additions & 2 deletions python/ctem/ctem/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from matplotlib.colors import LightSource
import matplotlib.cm as cm
import matplotlib.pyplot as plt
import re
from tempfile import mkstemp

# Set pixel scaling common for image writing, at 1 pixel/ array element
dpi = 72.0
Expand Down Expand Up @@ -301,6 +303,7 @@ def read_user_input(user):
if ('impfile' == fields[0].lower()): user['impfile'] = os.path.join(user['workingdir'],fields[1])
if ('maxcrat' == fields[0].lower()): user['maxcrat'] = real2float(fields[1])
if ('sfdcompare' == fields[0].lower()): user['sfdcompare'] = os.path.join(user['workingdir'], fields[1])
if ('realcraterlist' == fields[0].lower()): user['realcraterlist'] = os.path.join(user['workingdir'], fields[1])
if ('interval' == fields[0].lower()): user['interval'] = real2float(fields[1])
if ('numintervals' == fields[0].lower()): user['numintervals'] = int(fields[1])
if ('popupconsole' == fields[0].lower()): user['popupconsole'] = fields[1]
Expand Down Expand Up @@ -378,6 +381,33 @@ def real2float(realstr):
"""
return float(realstr.replace('d', 'E').replace('D', 'E'))

def sed(pattern, replace, source, count=0):
"""Python implementation of unix sed command; not fully functional sed."""

fin = open(source, 'r')
num_replaced = 0

fd, name = mkstemp()
fout = open(name, 'w')

for line in fin:
out = re.sub(pattern, replace, line)
fout.write(out)

if out != line:
num_replaced += 1
if count and num_replaced > count:
break

fout.writelines(fin.readlines())


fin.close()
fout.close()

shutil.move(name, source)

return

def write_datfile(user, filename, seedarr):
# Write various user and random number seeds into ctem.dat file
Expand All @@ -402,10 +432,20 @@ def write_production(user, production):
return


def write_realcraters(user, realcraters):
def write_realcraters(filename, realcraters):
"""Writes file of real craters for use in quasi-MC runs"""

filename = user['craterlist']
np.savetxt(filename, realcraters, fmt='%1.8e', delimiter='\t')

return

def write_temp_input(filename):
"""Makes changes to a temporary input file for use when generating craterlist.dat for quasimc runs"""

sed('testflag', 'testflag T!', filename)
sed('testimp', 'testimp 10 !', filename)
sed('quasimc', 'quasimc F!', filename)
sed('interval', 'interval 1 !', filename)
sed('numinterval 1 !s', 'numintervals 1 !', filename)

return

0 comments on commit b9e63d8

Please sign in to comment.