Skip to content

Commit

Permalink
reformat with black
Browse files Browse the repository at this point in the history
  • Loading branch information
Kent Inverarity committed Nov 30, 2023
1 parent b5068f3 commit dc3e5a8
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 99 deletions.
16 changes: 7 additions & 9 deletions wellcadformats/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from .wa import Comments

__version__ = '0.1'
__version__ = "0.1"


class UnknownFormat(IOError):
Expand All @@ -16,15 +16,14 @@ class UnknownFormat(IOError):
def read(filename):
name, ext = os.path.splitext(filename)
ext = ext.lower()
if ext == 'waf':
if ext == "waf":
return WAF(filename)
elif ext == 'wai':
elif ext == "wai":
return WAI(filename)
elif ext == 'waw':
elif ext == "waw":
return WAW(filename=filename)
else:
raise UnknownFormat('Cannot read in %s files' % ext)

raise UnknownFormat("Cannot read in %s files" % ext)


class MultipleExport(object):
Expand All @@ -42,9 +41,9 @@ def read(self, filename):
self.refresh()

def readpath(self, path):
fns = glob.glob(os.path.join(path, '*.w??'))
fns = glob.glob(os.path.join(path, "*.w??"))
self.readfiles(fns)

def readfiles(self, *fns):
for fn in fns:
self.read(fn)
Expand All @@ -61,4 +60,3 @@ def readfiles(self, *fns):
# indices[index_key] = 1
# else:
# indices[index_key] += 1

99 changes: 54 additions & 45 deletions wellcadformats/arraydata.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@


class WAF(object):

def __init__(self, filename=None):
if filename is not None:
self.read(filename)
Expand All @@ -34,7 +33,6 @@ def read(self, filename):
data = alldata[:, 1:]
self.generate(data, depths, times)


@classmethod
def from_data(cls, data, depths, times):
self = cls()
Expand Down Expand Up @@ -74,42 +72,45 @@ def print_depths(self):

def imshow(self, vampl=None, fig=None, figsize=None, ax=None, **kws):
"""Show a variable density log (VDL).
Args:
"""
if ax is None:
if figsize:
fig = plt.figure(figsize=figsize)
elif fig is None:
elif fig is None:
fig = plt.figure()
ax = fig.add_subplot(111)
if vampl:
self.vampl = vampl
defkws = dict(
cmap=self.cmap, vmin=-1*self.vampl, vmax=self.vampl,
interpolation="nearest", origin="upper",
cmap=self.cmap,
vmin=-1 * self.vampl,
vmax=self.vampl,
interpolation="nearest",
origin="upper",
# left, right, bottom, top
aspect="auto", extent=[
self.times[0], self.times[-1],
self.depths[-1], self.depths[0]])
aspect="auto",
extent=[self.times[0], self.times[-1], self.depths[-1], self.depths[0]],
)
defkws.update(**kws)
ax.imshow(self.data, **defkws)
return ax

def extract(self, drange=None, trange=None):
"""Extract of subset of data as a ``wellcadformats.WAF`` object.
Args:
drange (tuple of length 2): the range of depths to extract.
If None, all depths are extracted.
trange (tuple of length 2): the range of times to extract.
if None, all times are extracted.
Returns:
WAF object.
"""
if drange is None:
drange = (None, None)
Expand All @@ -135,32 +136,34 @@ def extract(self, drange=None, trange=None):
t1i = np.argmin((self.times - t1) ** 2)
# print('%s %s %s %s' % (d0, d1, d0i, d1i))
# print('%s %s %s %s' % (t0, t1, t0i, t1i))
depths = self.depths[d0i: d1i]
times = self.times[t0i: t1i]
depths = self.depths[d0i:d1i]
times = self.times[t0i:t1i]
new = WAF()
new.generate(self.data[d0i: d1i, t0i: t1i], depths, times, parent=self)
new.generate(self.data[d0i:d1i, t0i:t1i], depths, times, parent=self)
return new

def htrace(self, depth):
"""Return a horizontal (i.e. depth-invariant) trace.
Args:
depth (float): if it is not present, the nearest
depth will be returned, no interpolation
between frames will be done.
Returns:
depth, data (tuple) - depth as requested, data
is an array of length *m*
"""
di = np.argmin((self.depths - depth) ** 2)
logger.debug(f"self.data.shape = {self.data.shape} - self.times.shape = {self.times.shape}")
logger.debug(
f"self.data.shape = {self.data.shape} - self.times.shape = {self.times.shape}"
)
return self.depths[di], self.data[di, :]

def vtrace(self, time, interptime=True):
"""Return a vertical (i.e. time-invariant) trace.
Args:
time (float)
interptime (bool): interpolate the data to
Expand All @@ -170,16 +173,18 @@ def vtrace(self, time, interptime=True):
Returns:
time, data (tuple) - time is a float, data
is an array of length *n*
"""
ti = np.argmin((self.times - time) ** 2)
logger.debug(f"self.data.shape = {self.data.shape} - self.depths.shape = {self.depths.shape}")
logger.debug(
f"self.data.shape = {self.data.shape} - self.depths.shape = {self.depths.shape}"
)
if interptime:
# Still to implement interpolation of times.
return time, self.data[:, ti]
else:
return self.times[ti], self.data[:, ti]

def to_file(self, file_obj):
file_obj.write(",".join(["Depth"] + [f"{t:.2f} us" for t in self.times]) + "\n")
file_obj.write(",".join(["m"] + ["" for t in self.times]) + "\n")
Expand All @@ -188,10 +193,7 @@ def to_file(self, file_obj):
file_obj.write(",".join([str(x) for x in out_data[i, :]]) + "\n")




class WAI(object):

def __init__(self, fn=None):
self.__vmin = None
self.__vmax = None
Expand Down Expand Up @@ -252,14 +254,13 @@ def plot_amplitude_hist(self, fig=None, ax=None):
amplitudes = ax.hist(self.data.ravel(), bins=30, log=True)

def print_depths(self):
print('%s %s %s' % ("depth range", self.depths[:3], "..."))
print('%s %s %s' % (self.depths[self.n-3:], "n =", self.n))
print('%s %s %s' % ("azimuths range", self.azimuths[:3], "..."))
print('%s %s %s' % (self.azimuths[self.m-3:], "m =", self.m))
print("%s %s %s" % ("depth range", self.depths[:3], "..."))
print("%s %s %s" % (self.depths[self.n - 3 :], "n =", self.n))
print("%s %s %s" % ("azimuths range", self.azimuths[:3], "..."))
print("%s %s %s" % (self.azimuths[self.m - 3 :], "m =", self.m))
print(str(self.data.shape))

def imshow(
self, vmin=None, vmax=None, vampl=None, fig=None, ax=None, **kws):
def imshow(self, vmin=None, vmax=None, vampl=None, fig=None, ax=None, **kws):
if ax is None:
if fig is None:
fig = plt.figure()
Expand All @@ -268,10 +269,19 @@ def imshow(
vmin = -1 * vampl
vmax = vampl
defkws = dict(
cmap=self.cmap, vmin=vmin, vmax=vmax, interpolation="nearest",
origin="upper", aspect="auto", extent=[
self.azimuths[0], self.azimuths[-1],
self.depths[-1], self.depths[0]])
cmap=self.cmap,
vmin=vmin,
vmax=vmax,
interpolation="nearest",
origin="upper",
aspect="auto",
extent=[
self.azimuths[0],
self.azimuths[-1],
self.depths[-1],
self.depths[0],
],
)
defkws.update(**kws)
ax.imshow(self.data, **defkws)

Expand All @@ -281,7 +291,7 @@ def plot_tt_slice(self, depth, fig=None, ax=None, **kws):
fig = plt.figure()
ax = fig.add_subplot(111, polar=True)
ax.set_theta_direction(-1)
ax.set_theta_offset(np.pi / 2.)
ax.set_theta_offset(np.pi / 2.0)
actualdepth, data = self.htrace(depth, retdepth=True)
thetas = np.deg2rad(self.azimuths)
ax.plot(thetas, data, **kws)
Expand Down Expand Up @@ -311,13 +321,12 @@ def extract(self, drange=None, azrange=None):
t1i = len(self.azimuths) - 1
else:
t1i = np.argmin((self.azimuths - t1) ** 2)
print('%s %s %s %s' % (d0, d1, d0i, d1i))
print('%s %s %s %s' % (t0, t1, t0i, t1i))
depths = self.depths[d0i: d1i]
azimuths = self.azimuths[t0i: t1i]
print("%s %s %s %s" % (d0, d1, d0i, d1i))
print("%s %s %s %s" % (t0, t1, t0i, t1i))
depths = self.depths[d0i:d1i]
azimuths = self.azimuths[t0i:t1i]
new = FWSWaf()
new.generate(
self.data[d0i: d1i, t0i: t1i], depths, azimuths, parent=self)
new.generate(self.data[d0i:d1i, t0i:t1i], depths, azimuths, parent=self)
return new

def htrace(self, depth, retdepth=False):
Expand Down
22 changes: 11 additions & 11 deletions wellcadformats/curvedata.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

class WAW(object):

'''Open .waw file.
"""Open .waw file.
Args:
file_obj (file-like object): optional open file-like object
Expand All @@ -19,7 +19,7 @@ class WAW(object):
df (pandas.DataFrame): dataframe of data
index (numpy.ndarray): reference data for the log (i.e. depth)
'''
"""

def __init__(self, filename=None, file_obj=None):
self._units = {}
Expand All @@ -28,20 +28,20 @@ def __init__(self, filename=None, file_obj=None):

def read(self, file_obj=None, filename=None):
if not filename is None:
file_obj = open(filename, mode='r')
file_obj = open(filename, mode="r")
for i, line in enumerate(file_obj.readlines()):
line = line.strip('\n')
line = line.strip("\n")
if i == 0:
names = line.split(',')
names = line.split(",")
elif i == 1:
units = [u.strip() for u in line.split(',')]
units = [u.strip() for u in line.split(",")]
else:
break
for name, unit in zip(names, units):
self.set_unit(name, unit)

file_obj.seek(0)
data = np.loadtxt(file_obj, skiprows=2, delimiter=',')
data = np.loadtxt(file_obj, skiprows=2, delimiter=",")
self.df = pd.DataFrame(data, columns=names)

if not filename is None:
Expand All @@ -58,7 +58,7 @@ def get_unit(self, name):
return self.get_unit(name)
else:
raise KeyError(f"Well log '{name}' not known")

@classmethod
def from_df(cls, df, units=None):
self = cls()
Expand All @@ -82,7 +82,7 @@ def to_lasio_object(self):
for curve in las.curves:
curve.unit = self.get_unit(curve.mnemonic)
return las

def to_las(self, *args, **kwargs):
las = self.to_lasio_object()
return las.write(*args, **kwargs)
return las.write(*args, **kwargs)

0 comments on commit dc3e5a8

Please sign in to comment.