Skip to content

Commit

Permalink
Merge branch 'xraypy:master' into struct2xasV2407
Browse files Browse the repository at this point in the history
  • Loading branch information
maurov authored Aug 19, 2024
2 parents 5d449e8 + 000bae7 commit bd68559
Show file tree
Hide file tree
Showing 49 changed files with 3,041 additions and 3,128 deletions.
5,217 changes: 2,603 additions & 2,614 deletions examples/Jupyter/MonochromatorCalibrationErrors.ipynb

Large diffs are not rendered by default.

18 changes: 8 additions & 10 deletions larch/apps.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
"""
main Larch Applications
"""
import os
import sys
import locale
import inspect
import shutil
from argparse import ArgumentParser

from pathlib import Path

import matplotlib
from pyshortcuts import make_shortcut, ico_ext, get_desktop
Expand Down Expand Up @@ -53,17 +52,16 @@ def __init__(self, name, script, icon=None, description=None,
def make_desktop_shortcut(self, folder='Larch'):
"""make (or remake) desktop shortcuts for Larch apps"""
bindir = 'Scripts' if uname == 'win' else 'bin'
bindir = os.path.join(sys.prefix, bindir)
bindir = Path(sys.prefix, bindir).absolute()
script = self.script
if not self.script.startswith('_'):
script = os.path.normpath(os.path.join(bindir, self.script))

script = Path(bindir, self.script).absolute()

icon = os.path.join(icondir, self.icon)
icon = Path(icondir, self.icon)
if isinstance(ico_ext, (list, tuple)):
for ext in ico_ext:
ticon = f"{self.icon:s}.{ext:s}"
if os.path.exists(ticon):
if Path(ticon).exists():
icon = ticon

make_shortcut(script, name=self.name, folder=folder, icon=icon,
Expand All @@ -83,7 +81,7 @@ def prep_cli(self):
args = parser.parse_args()
self.filename = None
if 'filename' in args and args.filename is not None:
self.filename = os.path.abspath(args.filename)
self.filename = Path(args.filename).absolute()
self.wx_inspect = args.wx_inspect
self.run_mode = args.run_mode
if self.is_wxapp:
Expand Down Expand Up @@ -237,8 +235,8 @@ def run_larch():

# create desktop icons
if args.makeicons:
larchdir = os.path.join(get_desktop(), 'Larch')
if os.path.exists(larchdir):
larchdir = Path(get_desktop(), 'Larch').absolute()
if Path(larchdir).exists():
shutil.rmtree(larchdir)

for n, app in LarchApps.items():
Expand Down
3 changes: 2 additions & 1 deletion larch/inputText.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from __future__ import print_function
import os
from pathlib import Path
import sys
import time
from collections import deque
Expand Down Expand Up @@ -152,7 +153,7 @@ def clear(self):
def load(self, filename=None):
if filename is not None:
self.filename = filename
if os.path.exists(self.filename):
if Path(filename).exists():
self.clear()
text = read_textfile(filename).split('\n')
for hline in text:
Expand Down
9 changes: 5 additions & 4 deletions larch/interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
numpy functions are imported if available and used.
"""
import os
from pathlib import Path
import sys
import types
import ast
Expand Down Expand Up @@ -400,7 +401,7 @@ def show_errors(self):

def run_init_scripts(self):
for fname in site_config.init_files:
if os.path.exists(fname):
if Path(fname).exists():
try:
self.runfile(fname)
except:
Expand Down Expand Up @@ -1048,7 +1049,7 @@ def import_module(self, name, asname=None,
"""
st_sys = self.symtable._sys
for idir in st_sys.path:
if idir not in sys.path and os.path.exists(idir):
if idir not in sys.path and Path(idir).exists():
sys.path.append(idir)

# step 1 import the module to a global location
Expand All @@ -1066,11 +1067,11 @@ def import_module(self, name, asname=None,
islarch = False
larchname = "%s.lar" % name
for dirname in st_sys.path:
if not os.path.exists(dirname):
if not Path(dirname).exists():
continue
if larchname in sorted(os.listdir(dirname)):
islarch = True
modname = os.path.abspath(os.path.join(dirname, larchname))
modname = Path(dirname, larchname).absolute()
try:
thismod = self.runfile(modname, new_module=name)
except:
Expand Down
5 changes: 1 addition & 4 deletions larch/io/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@

from .fileutils import (increment_filename, new_filename, new_dirname,
fix_filename, fix_varname, pathOf, unixpath,
winpath, nativepath, strip_quotes, get_timestamp,
asciikeys)
strip_quotes, get_timestamp, asciikeys)

from .columnfile import (read_ascii, write_ascii, write_group, set_array_labels,
guess_filereader, look_for_nans, read_fdmnes, sum_fluor_channels)
Expand Down Expand Up @@ -58,8 +57,6 @@ def read_tiff(fname, *args, **kws):
fix_varname=fix_varname,
pathOf=pathOf,
unixpath=unixpath,
winpath=winpath,
nativepath=nativepath,
strip_quotes=strip_quotes,
get_timestamp=get_timestamp,
asciikeys=asciikeys,
Expand Down
20 changes: 9 additions & 11 deletions larch/io/athena_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
"""

import os
import io
import sys
import time
import json
import platform
from pathlib import Path
from fnmatch import fnmatch
from gzip import GzipFile
from copy import deepcopy
Expand All @@ -18,7 +18,7 @@

from larch import Group, repr_value
from larch import __version__ as larch_version
from larch.utils.strutils import bytes2str, str2bytes, fix_varname, asfloat
from larch.utils import bytes2str, str2bytes, fix_varname, asfloat, unixpath

from xraydb import guess_edge
import asteval
Expand Down Expand Up @@ -49,7 +49,7 @@ def _read_raw_athena(filename):
# try gzip
text = None
try:
fh = GzipFile(filename)
fh = GzipFile(unixpath(filename))
text = bytes2str(fh.read())
except Exception:
errtype, errval, errtb = sys.exc_info()
Expand Down Expand Up @@ -624,7 +624,7 @@ def __init__(self, filename=None):
self.journal = None
self.filename = filename
if filename is not None:
if os.path.exists(filename) and is_athena_project(filename):
if Path(filename).exists() and is_athena_project(filename):
self.read(filename)

def add_group(self, group, signal=None):
Expand Down Expand Up @@ -711,10 +711,8 @@ def save(self, filename=None, use_gzip=True):
buff.extend(["", "@journal = {};", "", "1;", "", "",
"# Local Variables:", "# truncate-lines: t",
"# End:", ""])
fopen =open
if use_gzip:
fopen = GzipFile
fh = fopen(self.filename, 'w')
fopen = GzipFile if use_gzip else open
fh = fopen(unixpath(self.filename), 'w')
fh.write(str2bytes("\n".join([bytes2str(t) for t in buff])))
fh.close()

Expand Down Expand Up @@ -756,12 +754,12 @@ def read(self, filename=None, match=None, do_preedge=True, do_bkg=False,
"""
if filename is not None:
self.filename = filename
if not os.path.exists(self.filename):
if not Path(self.filename).exists():
raise IOError("%s '%s': cannot find file" % (ERR_MSG, self.filename))

from larch.xafs import pre_edge, autobk, xftf

if not os.path.exists(filename):
if not Path(filename).exists():
raise IOError("file '%s' not found" % filename)

text = _read_raw_athena(filename)
Expand Down Expand Up @@ -927,7 +925,7 @@ def read_athena(filename, match=None, do_preedge=True, do_bkg=False,
zn_data = read_athena('Zn on Stuff.prj', match='*merge*', do_bkg=True, do_fft=True)
"""
if not os.path.exists(filename):
if not Path(filename).exists():
raise IOError("%s '%s': cannot find file" % (ERR_MSG, filename))

aprj = AthenaProject()
Expand Down
55 changes: 7 additions & 48 deletions larch/io/columnfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@
import sys
import time
import string
from pathlib import Path
from collections import namedtuple
import numpy as np
from dateutil.parser import parse as dateparse
from math import log10
from larch import Group
from larch.symboltable import isgroup
from ..utils import read_textfile, format_exception
from ..utils import read_textfile, format_exception, gformat
from .fileutils import fix_varname
from .xafs_beamlines import guess_beamline

Expand Down Expand Up @@ -121,48 +122,6 @@ def colname(txt):
return fix_varname(txt.strip().lower()).replace('.', '_')


def lformat(val, length=12):
"""Format a number with fixed-length format, somewhat like '%g' except that
a) the length of the output string will be the requested length.
b) positive numbers will have a leading blank.
b) the precision will be as high as possible.
c) trailing zeros will not be trimmed.
The precision will typically be length-7, but may be better than
that for values with absolute value between 1.e-5 and 1.e8.
Arguments:
val value to be formatted
length length of output string
Returns
-------
string of specified length.
Notes
------
Positive values will have leading blank.
"""
try:
expon = int(log10(abs(val)))
except (OverflowError, ValueError):
expon = 0
length = max(length, 7)
form = 'e'
prec = length - 7
if abs(expon) > 99:
prec -= 1
elif ((expon > 0 and expon < (prec+4)) or
(expon <= 0 and -expon < (prec-1))):
form = 'f'
prec += 4
if expon > 0:
prec -= expon
fmt = '{0: %i.%i%s}' % (length, prec, form)
return fmt.format(val)

def parse_labelline(labelline, header):
"""
parse the 'label line' for an ASCII file.
Expand Down Expand Up @@ -333,7 +292,7 @@ def read_ascii(filename, labels=None, simple_labels=False,
read_xdi, write_ascii
"""
if not os.path.isfile(filename):
if not Path(filename).is_file():
raise OSError("File not found: '%s'" % filename)
if os.stat(filename).st_size > MAX_FILESIZE:
raise OSError("File '%s' too big for read_ascii()" % filename)
Expand Down Expand Up @@ -402,11 +361,11 @@ def read_ascii(filename, labels=None, simple_labels=False,
header_attrs[key] = words[1].strip()


path, fname = os.path.split(filename)
fpath = Path(filename).absolute()
filename = fpath.as_posix()
attrs = {'filename': filename}
group = Group(name='ascii_file %s' % filename,
path=filename,
filename=fname,
path=filename, filename=fpath.name,
header=headers, data=[], array_labels=[])

if len(data) == 0:
Expand Down Expand Up @@ -597,7 +556,7 @@ def write_ascii(filename, *args, commentchar='#', label=None, header=None):

arrays = np.array(arrays)
for i in range(arraylen):
w = [" %s" % lformat(val[i], length=14) for val in arrays]
w = [" %s" % gformat(val[i], length=14) for val in arrays]
buff.append(' '.join(w))
buff.append('')

Expand Down
8 changes: 4 additions & 4 deletions larch/io/save_restore.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
# from lmfit.minimizer import Minimizer, MinimizerResult

from larch import Group, isgroup, __date__, __version__, __release_version__
from ..utils import (isotime, bytes2str, str2bytes, fix_varname, is_gzip,
read_textfile, unique_name, format_exception)
from ..utils import (isotime, bytes2str, str2bytes, fix_varname,
read_textfile, unique_name, format_exception, unixpath)
from ..utils.jsonutils import encode4js, decode4js

SessionStore = namedtuple('SessionStore', ('config', 'command_history', 'symbols'))
Expand Down Expand Up @@ -43,7 +43,7 @@ def save_groups(fname, grouplist):

buff.append("")

fh = GzipFile(fname, "w")
fh = GzipFile(unixpath(fname), "w")
fh.write(str2bytes("\n".join(buff)))
fh.close()

Expand Down Expand Up @@ -175,7 +175,7 @@ def save_session(fname=None, symbols=None, histbuff=None,
buff.append("##</Symbols>")
buff.append("")

fh = GzipFile(fname, "w")
fh = GzipFile(unixpath(fname), "w")
fh.write(str2bytes("\n".join(buff)))
fh.close()

Expand Down
2 changes: 0 additions & 2 deletions larch/io/xafs_beamlines.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
"""

import re

import numpy as np
from .fileutils import fix_varname

Expand Down
Loading

0 comments on commit bd68559

Please sign in to comment.