Skip to content

Instantly share code, notes, and snippets.

@mtb-za
Last active April 20, 2021 19:31
Show Gist options
  • Save mtb-za/3f94ffc426e804e7b2c778c2f0c6f051 to your computer and use it in GitHub Desktop.
Save mtb-za/3f94ffc426e804e7b2c778c2f0c6f051 to your computer and use it in GitHub Desktop.
CSV parsers for Striplog
fname = './log-1_tops_only.csv'
import csv
from striplog import Striplog
from striplog import Lexicon
from striplog import Interval
lexicon = Lexicon.default()
with open(fname) as f:
data_dict = {}
for ii in csv.DictReader(f):
for k,v in ii.items():
try:
v = int(v)
except ValueError:
try:
v = float(v)
except ValueError:
v = str(v)
try:
data_dict[k].append(v)
except KeyError:
data_dict[k] = [v,]
for ii in Striplog._build_list_of_Intervals(data_dict, lexicon=lexicon):
print(ii)
fname = './log-1_tops_only.csv'
import numpy as np
from striplog import Striplog
from striplog import Lexicon
lexicon = Lexicon.default()
names, dtype = True, None
# Reading this in now as bytes. That means that we need to convert them
# to more useful types later. Do we expect anything more other than
# `int`, `float` and `str`?
# We can also read a sequence of dtypes, which might be more useful, but
# it might be too much to ask for from users?
data = np.genfromtxt(fname, dtype=dtype, names=names, delimiter=',', encoding=None)
data_dict = {}
if names:
print('checking names')
for name in data.dtype.names:
# We expect `top` and `base`, but we might get `tops` and `bases`.
if name == 'tops': # There might be other cases worth checking?
data_dict.update({'top': data[name].astype(float)})
if name == 'bases': # There might be other cases worth checking?
data_dict.update({'base': data[name].astype(float)})
else: # Everything else is handled here, which is quite nice.
if dtype:
data_dict.update({name: data[name]})
else:
# This feels pretty nasty, to be honest.
try:
data_dict.update({name: data[name].astype(int)})
except ValueError:
try:
data_dict.update({name: data[name].astype(float)})
except:
data_dict.update({name: data[name].astype(str)})
print(data_dict)
for ii in Striplog._build_list_of_Intervals(data_dict, lexicon=lexicon):
print(ii)
top description
77.23 grey sandstone
70.745 brown shale with interbedded sandstone
70.044 crystalline dolomite
69.168 oolitic limestone
67.591 grey sandstone
67.24 brown sandstone
63.209 red mudstone
61.719 grey sandstone with interbedded red mudstone
60.492 black shale with dropstones
59.265 red sandstone with thin yellow tuff
56.812 coarse grey sandstone
54.971 conglomerate
54.621 granite basement
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment