high precision time handling

debug-parimap-hang
Sebastian Heimann 6 years ago
parent d30d3cea3d
commit befb5a64ec

@ -175,6 +175,7 @@ type: docker
name: tests-base
steps:
- name: tests-base
image: pyrocko-nest
pull: never
@ -186,6 +187,23 @@ steps:
- python3 -m coverage run --parallel-mode -m nose test.base
- for x in .coverage.* ; do mv $x $${x#.} ; done
- name: tests-base-hptime
image: pyrocko-nest
pull: never
commands:
- python3 setup.py install -f
- pip3 install utm
- pip3 install git+https://github.com/pyrocko/kite
- pip3 install obspy
- python3 -m coverage run --parallel-mode -m nose test.base
- for x in .coverage.* ; do mv $x $${x#.} ; done
environment:
PYROCKO_USE_HIGH_PRECISION_TIME: 1
when:
branch:
- hptime
- candidate
- name: stage
image: pyrocko-aux
pull: never
@ -199,6 +217,7 @@ steps:
RSYNC_KEY:
from_secret: rsync-key
---
kind: pipeline
@ -217,6 +236,23 @@ steps:
- python3 -m coverage run --parallel-mode -m nose test.gf
- for x in .coverage.* ; do mv $x $${x#.} ; done
- name: tests-gf-hptime
image: pyrocko-nest
pull: never
commands:
- python3 setup.py install -f
- pip3 install utm
- pip3 install git+https://github.com/pyrocko/kite
- pip3 install obspy
- python3 -m coverage run --parallel-mode -m nose test.gf
- for x in .coverage.* ; do mv $x $${x#.} ; done
environment:
PYROCKO_USE_HIGH_PRECISION_TIME: 1
when:
branch:
- hptime
- candidate
- name: stage
image: pyrocko-aux
pull: never
@ -248,6 +284,23 @@ steps:
- python3 -m coverage run --parallel-mode -m nose test.examples
- for x in .coverage.* ; do mv $x $${x#.} ; done
- name: tests-examples-hptime
image: pyrocko-nest
pull: never
commands:
- python3 setup.py install -f
- pip3 install utm
- pip3 install git+https://github.com/pyrocko/kite
- pip3 install obspy
- python3 -m coverage run --parallel-mode -m nose test.examples
- for x in .coverage.* ; do mv $x $${x#.} ; done
environment:
PYROCKO_USE_HIGH_PRECISION_TIME: 1
when:
branch:
- hptime
- candidate
- name: stage
image: pyrocko-aux
pull: never
@ -277,6 +330,21 @@ steps:
- xvfb-run -s '-screen 0 640x480x24' python3 -m coverage run --parallel-mode -m nose test.gui test.base.test_obspy_compat:ObsPyCompatTestCase.test_obspy_fiddle test.base.test_obspy_compat:ObsPyCompatTestCase.test_obspy_snuffle
- for x in .coverage.* ; do mv $x $${x#.} ; done
- name: tests-gui-hptime
image: pyrocko-nest
pull: never
commands:
- python3 setup.py install -f
- pip3 install obspy
- xvfb-run -s '-screen 0 640x480x24' python3 -m coverage run --parallel-mode -m nose test.gui test.base.test_obspy_compat:ObsPyCompatTestCase.test_obspy_fiddle test.base.test_obspy_compat:ObsPyCompatTestCase.test_obspy_snuffle
- for x in .coverage.* ; do mv $x $${x#.} ; done
environment:
PYROCKO_USE_HIGH_PRECISION_TIME: 1
when:
branch:
- hptime
- candidate
- name: stage
image: pyrocko-aux
pull: never

@ -272,7 +272,10 @@ def process_signature(app, what, name, obj, options, signature,
if what == 'class' and issubclass(obj, guts.Object):
if obj.dummy_for is not None:
return ('(dummy)', '%s' % obj.dummy_for.__name__)
return ('(dummy)', '%s' % (
obj.dummy_for_description
if hasattr(obj, 'dummy_for_description')
else obj.dummy_for.__name__))
return

@ -1,5 +1,3 @@
import time
import calendar
from pyrocko import (pile, io, util)
from pyrocko.example import get_example_data
@ -11,10 +9,10 @@ get_example_data('test.mseed')
p = pile.make_pile(['test.mseed'])
# get timestamp for full hour before first data sample in all selected traces
tmin = calendar.timegm(time.gmtime(p.tmin)[:4] + (0, 0))
tmin = util.hour_start(p.tmin)
# iterate over the data, with a window length of one hour
for traces in p.chopper(tmin=tmin, tinc=3600):
for traces in p.chopper(tmin=tmin, tinc=3600.):
if traces: # the list could be empty due to gaps
window_start = traces[0].wmin
timestring = util.time_to_str(window_start, format='%Y-%m-%d_%H')

@ -1,15 +1,13 @@
from pyrocko import pile, io, util
import time
import calendar
''' Chope a pile of waveform traces into segments '''
''' Chop a pile of waveform traces into segments '''
p = pile.make_pile(['test.mseed'])
# get timestamp for full hour before first data sample in all selected traces
tmin = calendar.timegm(time.gmtime(p.tmin)[:4] + (0, 0))
tmin = util.hour_start(p.tmin)
# iterate over the data, with a window length of one hour
for traces in p.chopper(tmin=tmin, tinc=3600):
for traces in p.chopper(tmin=tmin, tinc=3600.):
if traces: # the list could be empty due to gaps
window_start = traces[0].wmin
timestring = util.time_to_str(window_start, format='%Y-%m-%d_%H')

@ -6,17 +6,18 @@ set -e
[ ! -z "$DRONE" ] || exit 1
keypath=/tmp/rsync-key-$$
keypath="/tmp/rsync-key-$$"
umask 0077
echo "$RSYNC_KEY" > $keypath
echo "$RSYNC_KEY" > "$keypath"
umask 0022
temppath=/tmp/rsync-temp-$$
temppath="/tmp/rsync-temp-$$"
mkdir -p $temppath/$2
target="${@: -1}"
mkdir -p "$temppath/$target"
rsync -av $1 $temppath/$2
rsync -rv -e "ssh -o StrictHostKeyChecking=no -i $keypath" $temppath/ ${RSYNC_USER}@${RSYNC_HOST}:
rsync -av "${@:1:$#-1}" "$temppath/$target"
rsync -rv -e "ssh -o StrictHostKeyChecking=no -i $keypath" "$temppath/" "${RSYNC_USER}@${RSYNC_HOST}:"
rm $keypath
rm "$keypath"

@ -362,11 +362,15 @@ class CustomBuildPyCommand(build_py):
module_code = '''
import sys
import pyrocko
if pyrocko.grumpy:
if pyrocko.grumpy == 1:
sys.stderr.write('using renamed pyrocko module: %s.%s\\n')
sys.stderr.write(' -> should now use: %s\\n\\n')
elif pyrocko.grumpy == 2:
sys.stderr.write('pyrocko module has been renamed: %s.%s\\n')
sys.stderr.write(' -> should now use: %s\\n\\n')
raise ImportError('Pyrocko module "%s.%s" has been renamed to "%s".')
''' % (package, compat_module, ', '.join(import_modules)) + ''.join(
''' % ((package, compat_module, ', '.join(import_modules))*3) + ''.join(
['from %s import *\n' % module for module in import_modules])
outfile = self.get_module_outfile(

@ -7,7 +7,7 @@ from __future__ import absolute_import
from .info import * # noqa
__version__ = version # noqa
grumpy = False # noqa
grumpy = 0 # noqa
class ExternalProgramMissing(Exception):

@ -9,7 +9,7 @@ import calendar
import re
import logging
from pyrocko import model
from pyrocko import model, util
from pyrocko.moment_tensor import MomentTensor
from pyrocko.util import Request, urlopen
from .base_catalog import EarthquakeCatalog
@ -108,9 +108,10 @@ class GlobalCMT(EarthquakeCatalog):
def complete(data):
try:
t = calendar.timegm((
data.year, data.month, data.day,
data.hour, data.minute, data.seconds))
t = util.to_time_float(
calendar.timegm((
data.year, data.month, data.day,
data.hour, data.minute, data.seconds)))
m = num.array(
[data.mrr, data.mrt, data.mrp,
@ -222,5 +223,6 @@ class GlobalCMT(EarthquakeCatalog):
else:
y, m, d = time.strptime(name[:8], '%Y%m%d')[:3]
t = calendar.timegm((y, m, d, 0, 0, 0))
t = util.to_time_float(
calendar.timegm((y, m, d, 0, 0, 0)))
return t

@ -106,6 +106,7 @@ class PyrockoConfig(ConfigBase):
gui_toolkit = StringChoice.T(
choices=['auto', 'qt4', 'qt5'],
default='auto')
use_high_precision_time = Bool.T(default=False)
config_cls = {

@ -67,7 +67,7 @@ class SeismosizerTrace(Object):
help='sampling interval [s]')
tmin = Timestamp.T(
default=0.0,
default=Timestamp.D('1970-01-01 00:00:00'),
help='time of first sample as a system timestamp [s]')
def pyrocko_trace(self):

@ -1046,7 +1046,7 @@ class Source(Location, Cloneable):
name = String.T(optional=True, default='')
time = Timestamp.T(
default=0.,
default=Timestamp.D('1970-01-01 00:00:00'),
help='source origin time.')
stf = STF.T(

@ -91,8 +91,8 @@ class Marker(object):
'Unable to create marker from string: "%s"' % line)
def parsedate(ymd, hms, sfs):
return calendar.timegm(
time.strptime(ymd+' '+hms, '%Y-%m-%d %H:%M:%S')) + float(sfs)
return util.to_time_str(calendar.timegm(
time.strptime(ymd+' '+hms, '%Y-%m-%d %H:%M:%S'))) + float(sfs)
try:
toks = line.split()

@ -215,32 +215,8 @@ acceptable_tincs = num.array([
60*60*6, 60*60*12, sday, smonth, syear], dtype=num.float)
def get_working_system_time_range():
now = time.time()
hi = now
for ignore in range(200):
now += syear
try:
tt = time.gmtime(now)
time.strftime('', tt)
hi = now
except Exception:
break
now = time.time()
lo = now
for ignore in range(200):
now -= syear
try:
tt = time.gmtime(now)
time.strftime('', tt)
lo = now
except Exception:
break
return lo, hi
working_system_time_range = get_working_system_time_range()
working_system_time_range = \
pyrocko.util.working_system_time_range(1950, 2030)
def is_working_time(t):

@ -270,6 +270,14 @@ def snuffler_from_commandline(args=None):
help='disable sorting in marker table for improved performance with '
'100000+ markers')
parser.add_option(
'--hptime',
choices=('on', 'off', 'config'),
dest='hp_time',
default='config',
metavar='on|off|config',
help='set high precision time mode [default: %default]')
parser.add_option(
'--opengl',
dest='opengl',
@ -305,6 +313,9 @@ def snuffler_from_commandline(args=None):
else:
util.setup_logging('snuffler', 'warning')
if options.hp_time in ('on', 'off'):
util.use_high_precision_time(options.hp_time == 'on')
if options.gui_toolkit_qt4:
config.override_gui_toolkit = 'qt4'

@ -28,7 +28,8 @@ try:
except ImportError:
from yaml import SafeLoader, SafeDumper
from .util import time_to_str, str_to_time, TimeStrError
from .util import time_to_str, str_to_time, TimeStrError, hpfloat, \
get_time_float
try:
newstr = unicode
@ -233,6 +234,13 @@ def make_content_name(name):
return name
def classnames(cls):
if isinstance(cls, tuple):
return '(%s)' % ', '.join(x.__name__ for x in cls)
else:
return cls.__name__
def expand_stream_args(mode):
def wrap(f):
'''Decorator to enhance functions taking stream objects.
@ -343,10 +351,6 @@ class TBase(object):
g_iprop += 1
self._default = default
if isinstance(self._default, DefaultMaker):
self._default_cmp = self._default.make()
else:
self._default_cmp = self._default
self.optional = optional
self.name = None
@ -360,10 +364,10 @@ class TBase(object):
return make_default(self._default)
def is_default(self, val):
if self._default_cmp is None:
if self._default is None:
return val is None
else:
return self._default_cmp == val
return self._default == val
def has_default(self):
return self._default is not None
@ -540,18 +544,25 @@ class TBase(object):
except ValueError:
raise ValidationError(
'%s: could not convert "%s" to type %s' % (
self.xname(), val, self.cls.__name__))
self.xname(), val, classnames(self.cls)))
else:
raise ValidationError(
'%s: "%s" (type: %s) is not of type %s' % (
self.xname(), val, type(val), self.cls.__name__))
self.xname(), val, type(val), classnames(self.cls)))
validator = self
if type(val) != self.cls \
and isinstance(val, self.cls) and \
hasattr(val, 'T'):
# derived classes only: validate with derived class validator
validator = val.T.instance
if isinstance(self.cls, tuple):
clss = self.cls
else:
clss = (self.cls,)
for cls in clss:
try:
if type(val) != cls and isinstance(val, cls):
validator = val.T.instance
except AttributeError:
pass
validator.validate_extra(val)
@ -593,9 +604,11 @@ class TBase(object):
def classname_for_help(self, strip_module=''):
if self.dummy_cls in guts_plain_dummy_types:
return '``%s``' % self.cls.__name__
elif self.dummy_cls.dummy_for_description:
return self.dummy_cls.dummy_for_description
else:
mod = self.cls.__module__
cls = self.cls.__name__
if self.dummy_cls is not self.cls:
if self.dummy_cls.__module__ == strip_module:
sadd = ' (:py:class:`%s`)' % (
@ -606,14 +619,23 @@ class TBase(object):
else:
sadd = ''
if mod == '__builtin__':
return '``%s``%s' % (cls, sadd)
def sclass(cls):
mod = cls.__module__
clsn = cls.__name__
if mod == '__builtin__' or mod == 'builtins':
return '``%s``' % clsn
elif self.cls.__module__ == strip_module:
return ':py:class:`%s`%s' % (cls, sadd)
elif mod == strip_module:
return ':py:class:`%s`' % clsn
else:
return ':py:class:`%s.%s`' % (mod, clsn)
if isinstance(self.cls, tuple):
return '(%s)%s' % (
' | '.join(sclass(cls) for cls in self.cls), sadd)
else:
return ':py:class:`%s.%s`%s' % (mod, cls, sadd)
return '%s%s' % (sclass(cls), sadd)
@classmethod
def props_help_string(cls):
@ -634,9 +656,12 @@ class TBase(object):
if prop.optional:
descr.append('*optional*')
d = prop.default()
if d is not None:
descr.append('*default:* ``%s``' % repr(d))
if isinstance(prop._default, DefaultMaker):
descr.append('*default:* ``%s``' % repr(prop._default))
else:
d = prop.default()
if d is not None:
descr.append('*default:* ``%s``' % repr(d))
hlp.append(' .. py:gattribute:: %s' % prop.name)
hlp.append('')
@ -805,16 +830,52 @@ def make_default(x):
class DefaultMaker(object):
def make(self):
raise NotImplementedError('Schould be implemented in subclass.')
class ObjectDefaultMaker(DefaultMaker):
def __init__(self, cls, args, kwargs):
DefaultMaker.__init__(self)
self.cls = cls
self.args = args
self.kwargs = kwargs
self.instance = None
def make(self):
return self.cls(
*[make_default(x) for x in self.args],
**dict((k, make_default(v)) for (k, v) in self.kwargs.items()))
def __eq__(self, other):
if self.instance is None:
self.instance = self.make()
return self.instance == other
def __repr__(self):
sargs = []
for arg in self.args:
sargs.append(repr(arg))
for k, v in self.kwargs.items():
sargs.append('%s=%s' % (k, repr(v)))
return '%s(%s)' % (self.cls.__name__, ', '.join(sargs))
class TimestampDefaultMaker(DefaultMaker):
def __init__(self, s, format='%Y-%m-%d %H:%M:%S.OPTFRAC'):
DefaultMaker.__init__(self)
self._stime = s
self._format = format
def make(self):
return str_to_time(self._stime, self._format)
def __repr__(self):
return "str_to_time(%s)" % repr(self._stime)
def with_metaclass(meta, *bases):
# inlined py2/py3 compat solution from python-future
@ -832,6 +893,7 @@ def with_metaclass(meta, *bases):
class Object(with_metaclass(ObjectMetaClass, object)):
dummy_for = None
dummy_for_description = None
def __init__(self, **kwargs):
if not kwargs.get('init_props', True):
@ -854,7 +916,7 @@ class Object(with_metaclass(ObjectMetaClass, object)):
@classmethod
def D(cls, *args, **kwargs):
return DefaultMaker(cls, args, kwargs)
return ObjectDefaultMaker(cls, args, kwargs)
def validate(self, regularize=False, depth=-1):
self.T.instance.validate(self, regularize, depth)
@ -1218,18 +1280,22 @@ re_tz = re.compile(r'(Z|([+-][0-2][0-9])(:?([0-5][0-9]))?)$')
class Timestamp(Object):
dummy_for = float
dummy_for = (hpfloat, float)
dummy_for_description = 'time_float'
class __T(TBase):
def regularize_extra(self, val):
time_float = get_time_float()
if isinstance(val, datetime.datetime):
tt = val.utctimetuple()
val = calendar.timegm(tt) + val.microsecond * 1e-6
val = time_float(calendar.timegm(tt)) + val.microsecond * 1e-6
elif isinstance(val, datetime.date):
tt = val.timetuple()
val = float(calendar.timegm(tt))
val = time_float(calendar.timegm(tt))
elif isinstance(val, (str, newstr)):
val = val.strip()
@ -1253,41 +1319,52 @@ class Timestamp(Object):
raise ValidationError(
'%s: cannot parse time/date: %s' % (self.xname(), val))
elif isinstance(val, int):
val = float(val)
elif isinstance(val, (int, float)):
val = time_float(val)
else:
raise ValidationError(
'%s: cannot convert "%s" to float' % (self.xname(), val))
'%s: cannot convert "%s" to type %s' % (
self.xname(), val, time_float))
return val
def to_save(self, val):
return datetime.datetime.utcfromtimestamp(val)
return time_to_str(val, format='%Y-%m-%d %H:%M:%S.9FRAC')\
.rstrip('0').rstrip('.')
def to_save_xml(self, val):
return datetime.datetime.utcfromtimestamp(val).isoformat() + 'Z'
return time_to_str(val, format='%Y-%m-%dT%H:%M:%S.9FRAC')\
.rstrip('0').rstrip('.') + 'Z'
@classmethod
def D(self, s):
return TimestampDefaultMaker(s)
class DateTimestamp(Object):
dummy_for = float
dummy_for = (hpfloat, float)
dummy_for_description = 'time_float'
class __T(TBase):
def regularize_extra(self, val):
time_float = get_time_float()
if isinstance(val, datetime.datetime):
tt = val.utctimetuple()
val = calendar.timegm(tt) + val.microsecond * 1e-6
val = time_float(calendar.timegm(tt)) + val.microsecond * 1e-6
elif isinstance(val, datetime.date):
tt = val.timetuple()
val = float(calendar.timegm(tt))
val = time_float(calendar.timegm(tt))
elif isinstance(val, (str, newstr)):
val = str_to_time(val, format='%Y-%m-%d')
elif isinstance(val, int):
val = float(val)
val = time_float(val)
return val
@ -1297,6 +1374,10 @@ class DateTimestamp(Object):
def to_save_xml(self, val):
return time_to_str(val, format='%Y-%m-%d')
@classmethod
def D(self, s):
return TimestampDefaultMaker(s, format='%Y-%m-%d')
class StringPattern(String):
@ -1466,16 +1547,27 @@ class Choice(Object):
raise ValidationError(
'%s: could not convert "%s" to any type out of '
'(%s)' % (self.xname(), val, ','.join(
x.cls.__name__ for x in self.choices)))
classnames(x.cls) for x in self.choices)))
else:
raise ValidationError(
'%s: "%s" (type: %s) is not of any type out of '
'(%s)' % (self.xname(), val, type(val), ','.join(
x.cls.__name__ for x in self.choices)))
classnames(x.cls) for x in self.choices)))
validator = t
if type(val) != t.cls and isinstance(val, t.cls):
validator = val.T.instance
if isinstance(t.cls, tuple):
clss = t.cls
else:
clss = (t.cls,)
for cls in clss:
try:
if type(val) != cls and isinstance(val, cls):
validator = val.T.instance
except AttributeError:
pass
validator.validate_extra(val)
@ -1669,10 +1761,13 @@ class Constructor(object):
cls = self.stack[-1][1].T.xmltagname_to_class.get(
ns_name, None)
if cls is not None and (
not issubclass(cls, Object)
or issubclass(cls, SObject)):
if isinstance(cls, tuple):
cls = None
else:
if cls is not None and (
not issubclass(cls, Object)
or issubclass(cls, SObject)):
cls = None
else:
cls = g_xmltagname_to_class.get(ns_name, None)

@ -59,12 +59,12 @@ class Array(Object):
self.serialize_dtype = serialize_dtype
def is_default(self, val):
if self._default_cmp is None:
if self._default is None:
return val is None
elif val is None:
return False
else:
return array_equal(self._default_cmp, val)
return array_equal(self._default, val)
def regularize_extra(self, val):
if isinstance(val, (str, newstr)):

@ -15,7 +15,16 @@ from collections import namedtuple
from pyrocko import util, trace
from .io_common import FileLoadError
guralp_zero = util.str_to_time('1989-11-17 00:00:00')
g_guralp_zero = None
def get_guralp_zero():
global g_guralp_zero
if g_guralp_zero is None:
g_guralp_zero = util.str_to_time('1989-11-17 00:00:00')
return g_guralp_zero
class GCFLoadError(FileLoadError):
@ -79,7 +88,7 @@ def read_header(f, endianness='>'):
i_day_second = struct.unpack(e+'I', data[8:12])[0]
iday = i_day_second >> 17
isecond = i_day_second & 0x1ffff
time = (iday*24*60*60) + guralp_zero + isecond
time = (iday*24*60*60) + get_guralp_zero() + isecond
ittl, israte, compression, nrecords = struct.unpack(e+'BBBB', data[12:])
if nrecords > 250:
@ -236,7 +245,6 @@ def detect(first512):
if __name__ == '__main__':
from pyrocko import util
util.setup_logging('warn')
all_traces = []

@ -68,7 +68,8 @@ class KanFile:
date = str(filedata[393:405].decode('ascii')).strip()
tim = str(filedata[405:415].decode('ascii')).strip()
microseconds = int(filedata[415:423])
ref_time = util.ctimegm('%s %s' % (date, tim)) + microseconds/1.0e6
ref_time = util.to_time_float(util.ctimegm('%s %s' % (date, tim))) \
+ microseconds/1.0e6
stationname = os.path.basename(filename)
stationname = stationname.replace('.kan', '')

@ -400,7 +400,8 @@ class SeedVolumeAccess(eventdata.EventDataAccess):
if len(toks) == 9:
datetime = toks[1].split('.')[0]
format = '%Y/%m/%d %H:%M:%S'
secs = calendar.timegm(time.strptime(datetime, format))
secs = util.to_time_float(calendar.timegm(
time.strptime(datetime, format)))
e = model.Event(
lat=float(toks[2]),
lon=float(toks[3]),

@ -13,7 +13,7 @@ import numpy as num
from calendar import timegm
from time import gmtime
from pyrocko import trace
from pyrocko import trace, util
from pyrocko.util import reuse
from .io_common import FileLoadError
@ -106,8 +106,9 @@ iqb1 iqb2 iqbx iqmt ieq ieq1 ieq2 ime iex inu inc io_ il ir it iu
self.nzsec, self.nzmsec):
raise SacError('Not all header values for reference time are set.')
return timegm((self.nzyear, 1, self.nzjday, self.nzhour, self.nzmin,
self.nzsec)) + self.nzmsec/1000.
return util.to_time_float(timegm(
(self.nzyear, 1, self.nzjday,
self.nzhour, self.nzmin, self.nzsec))) + self.nzmsec/1000.
def set_ref_time(self, timestamp):
'''

@ -8,7 +8,7 @@ import numpy as num
import struct
import calendar
from pyrocko import trace
from pyrocko import trace, util
from .io_common import FileLoadError
@ -163,8 +163,8 @@ def iload(filename, load_data, endianness='>'):
if year < 100:
year += 2000
tmin = calendar.timegm(
(year, 1, doy, hour, minute, second)) \
tmin = util.to_time_float(calendar.timegm(
(year, 1, doy, hour, minute, second))) \
+ msecs * 1.0e-3 + usecs * 1.0e-6
except Exception:

@ -54,8 +54,8 @@ class SeisanResponseFile(object):
line = str(line.decode('ascii'))
comment = line.strip()
tmin = calendar.timegm(
(century+deltayear, 1, doy, hr, mi, int(sec))) + sec-int(sec)
tmin = util.to_time_float(calendar.timegm(
(century+deltayear, 1, doy, hr, mi, int(sec)))) + sec-int(sec)
if filetype == 'gains-and-filters':

@ -35,7 +35,8 @@ def read_file_header(f, npad=4):
d)
year = 1900 + ear
tmin = calendar.timegm((year, mon, day, hr, min, secs))
tmin = util.to_time_float(
calendar.timegm((year, mon, day, hr, min, secs)))
header_infos.append(
(net_name, nchannels, util.time_to_str(tmin)))
@ -84,7 +85,8 @@ def read_channel_header(f, npad=4):
cha = cha1+cha2
loc = loc1+loc2
net = net1+net2
tmin = calendar.timegm((1900+ear, mon, day, hr, min, secs))
tmin = util.to_time_float(
calendar.timegm((1900+ear, mon, day, hr, min, secs)))
deltat = 1./rate
return (net, sta, loc, cha,

@ -18,6 +18,7 @@ from pyrocko.guts import (StringChoice, StringPattern, UnicodePattern, String,
Unicode, Int, Float, List, Object, Timestamp,
ValidationError, TBase, re_tz)
from pyrocko.guts import load_xml # noqa
from pyrocko.util import hpfloat, time_to_str, get_time_float
import pyrocko.model
from pyrocko import trace, util
@ -120,18 +121,21 @@ this_year = time.gmtime()[0]
class DummyAwareOptionalTimestamp(Object):
dummy_for = float
dummy_for = (hpfloat, float)
dummy_for_description = 'time_float'
class __T(TBase):
def regularize_extra(self, val):
time_float = get_time_float()
if isinstance(val, datetime.datetime):
tt = val.utctimetuple()
val = calendar.timegm(tt) + val.microsecond * 1e-6
val = time_float(calendar.timegm(tt)) + val.microsecond * 1e-6
elif isinstance(val, datetime.date):
tt = val.timetuple()
val = float(calendar.timegm(tt))
val = time_float(calendar.timegm(tt))
elif isinstance(val, (str, newstr)):
val = val.strip()
@ -147,7 +151,7 @@ class DummyAwareOptionalTimestamp(Object):
val = re_tz.sub('', val)
if val[10] == 'T':
if len(val) > 10 and val[10] == 'T':
val = val.replace('T', ' ', 1)
try:
@ -165,20 +169,23 @@ class DummyAwareOptionalTimestamp(Object):
raise
elif isinstance(val, int):
val = float(val)
elif isinstance(val, (int, float)):
val = time_float(val)
else:
raise ValidationError(
'%s: cannot convert "%s" to float' % (self.xname(), val))
'%s: cannot convert "%s" to type %s' % (
self.xname(), val, time_float))
return val
def to_save(self, val):
return datetime.datetime.utcfromtimestamp(val)
return time_to_str(val, format='%Y-%m-%d %H:%M:%S.9FRAC')\
.rstrip('0').rstrip('.')
def to_save_xml(self, val):
return datetime.datetime.utcfromtimestamp(val).isoformat() + 'Z'
return time_to_str(val, format='%Y-%m-%dT%H:%M:%S.9FRAC')\
.rstrip('0').rstrip('.') + 'Z'
class Nominal(StringChoice):
@ -1167,7 +1174,7 @@ class FDSNStationXML(Object):
'coordinates. Storing effective lat/lon for stations: %s' %
', '.join('.'.join(nsl) for nsl in sorted(have_offsets)))
timestamp = time.time()
timestamp = util.to_time_float(time.time())
network_list = []
for k, station_list in network_dict.items():

@ -14,7 +14,16 @@ from pyrocko import util, trace, model
from .io_common import FileLoadError
suds_tzero = util.str_to_time('1970-01-01 00:00:00')
g_suds_zero = None
def get_suds_zero():
global g_suds_zero
if g_suds_zero is None:
g_suds_zero = util.str_to_time('1970-01-01 00:00:00')
return g_suds_zero
logger = logging.getLogger('pyrocko.io.suds')
@ -105,7 +114,7 @@ class SudsDescriptrace(SudsStructBase, namedtuple(
return cls._make(v)
def to_trace(self, data):
tmin = self.begintime - suds_tzero
tmin = self.begintime - get_suds_zero()
deltat = 1.0 / self.rate
if data is None:

@ -60,7 +60,7 @@ class Event(Location):
:param lat: latitude of hypocenter (default 0.0)
:param lon: longitude of hypocenter (default 0.0)
:param time: origin time as float in seconds after '1970-01-01 00:00:00
:param time: origin time system timestamp
:param name: event identifier as string (optional)
:param depth: source depth (optional)
:param magnitude: magnitude of event (optional)
@ -74,7 +74,7 @@ class Event(Location):
Keys must be strings, values must be YAML serializable.
'''
time = Timestamp.T(default=util.str_to_time('1970-01-01 00:00:00'))
time = Timestamp.T(default=Timestamp.D('1970-01-01 00:00:00'))
depth = Float.T(optional=True)
name = String.T(default='', optional=True, yamlstyle="'")
magnitude = Float.T(optional=True)
@ -450,7 +450,7 @@ def load_kps_event_list(filename):
if len(toks) < 7:
continue
tim = util.ctimegm(toks[0]+' '+toks[1])
tim = util.to_time_float(util.ctimegm(toks[0]+' '+toks[1]))
lat, lon, depth, magnitude = [float(x) for x in toks[2:6]]
duration = float(toks[10])
region = toks[-1]

@ -288,6 +288,8 @@ class TracesFileCache(object):
if not os.path.isfile(fn):
del cache[fn]
time_float = util.get_time_float()
for v in cache.values():
v.trees_from_content(v.traces)
for tr in v.traces:
@ -302,6 +304,9 @@ class TracesFileCache(object):
str(tr.location),
str(tr.channel))
tr.tmin = time_float(tr.tmin)
tr.tmax = time_float(tr.tmax)
v.data_use_count = 0
v.data_loaded = False
v.fix_unicode_codes()

@ -12,7 +12,8 @@ import time
from pyrocko.guts import Object, Timestamp
from pyrocko import gf, guts, util, pile, gmtpy
from pyrocko import gf, guts, util, pile
from pyrocko.plot import gmtpy
from .scenario import draw_scenario_gmt
from .error import ScenarioError
@ -191,7 +192,7 @@ class ScenarioCollection(object):
scenario = ScenarioCollectionItem(
scenario_id=scenario_id,
time_created=time.time())
time_created=util.to_time_float(time.time()))
scenario_path = self.get_path(scenario_id, 'scenario.yaml')
guts.dump(scenario, filename=scenario_path)

@ -245,7 +245,8 @@ class ScenarioGenerator(LocationGenerator):
return lambda gen: [gen.get_time_range(self.get_sources())]
def get_time_range(self):
ranges = num.array(self._get_time_ranges())
ranges = num.array(
self._get_time_ranges(), dtype=util.get_time_dtype())
return ranges.min(), ranges.max()
def _time_range_fill_defaults(self, tmin, tmax):

@ -6,7 +6,7 @@ from __future__ import absolute_import, division, print_function
import os.path as op
from pyrocko import util, moment_tensor
from pyrocko import moment_tensor
from pyrocko.guts import Timestamp, Float, Int, Bool
from ..base import LocationGenerator
@ -23,8 +23,8 @@ class SourceGenerator(LocationGenerator):
default=False,
help='Avoid sources offshore under the ocean / lakes.')
time_min = Timestamp.T(default=util.str_to_time('2017-01-01 00:00:00'))
time_max = Timestamp.T(default=util.str_to_time('2017-01-03 00:00:00'))
time_min = Timestamp.T(default=Timestamp.D('2017-01-01 00:00:00'))
time_max = Timestamp.T(default=Timestamp.D('2017-01-03 00:00:00'))
magnitude_min = Float.T(
default=4.0,

@ -6,6 +6,7 @@ from __future__ import absolute_import, division, print_function
import numpy as num
from pyrocko import util
from pyrocko.guts import Float
from pyrocko import moment_tensor, gf
@ -28,7 +29,9 @@ class DCSourceGenerator(SourceGenerator):
def get_source(self, ievent):
rstate = self.get_rstate(ievent)
time = rstate.uniform(self.time_min, self.time_max)
time = self.time_min + rstate.uniform(
0., float(self.time_max - self.time_min)) # hptime aware
lat, lon, north_shift, east_shift, depth = self.get_coordinates(ievent)
depth = rstate.uniform(self.depth_min, self.depth_max)
magnitude = self.draw_magnitude(rstate)
@ -53,7 +56,7 @@ class DCSourceGenerator(SourceGenerator):
source = gf.DCSource(
name='ev%04i' % ievent,
time=float(time),
time=util.to_time_float(time),
lat=float(lat),
lon=float(lon),
north_shift=float(north_shift),
@ -67,7 +70,7 @@ class DCSourceGenerator(SourceGenerator):
return source
def add_map_artists(self, automap):
from pyrocko import gmtpy
from pyrocko.plot import gmtpy
for source in self.get_sources():
event = source.pyrocko_event()

@ -7,7 +7,7 @@ from __future__ import absolute_import, division, print_function
import numpy as num
from pyrocko.guts import Float, Int
from pyrocko import moment_tensor, gf
from pyrocko import moment_tensor, gf, util
from .base import SourceGenerator
@ -39,7 +39,8 @@ class RectangularSourceGenerator(SourceGenerator):
def get_source(self, ievent):
rstate = self.get_rstate(ievent)
time = rstate.uniform(self.time_min, self.time_max)
time = self.time_min + rstate.uniform(
0., float(self.time_max - self.time_min)) # hptime aware
lat, lon = self.get_latlon(ievent)
depth = rstate.uniform(self.depth_min, self.depth_max)
@ -68,7 +69,7 @@ class RectangularSourceGenerator(SourceGenerator):
rake = self.rake
source = gf.RectangularSource(
time=float(time),
time=util.to_time_float(time),
lat=float(lat),
lon=float(lon),
anchor='top',

@ -5,6 +5,7 @@
from __future__ import absolute_import, division, print_function
import numpy as num
from pyrocko import util
from ..base import Generator
@ -17,7 +18,7 @@ class TargetGenerator(Generator):
targets we have to consider vmin, vmax
'''
times = num.array([source.time for source in sources],
dtype=num.float)
dtype=util.get_time_dtype())
return num.min(times), num.max(times)

@ -428,8 +428,8 @@ class InSARGenerator(TargetGenerator):
tmin, tmax = self.get_time_range(sources)
for sc in scenes:
sc.meta.time_master = float(tmin)
sc.meta.time_slave = float(tmax)
sc.meta.time_master = util.to_time_float(tmin)
sc.meta.time_slave = util.to_time_float(tmax)
scenes_asc = [sc for sc in scenes
if sc.config.meta.orbital_node == 'Ascending']

@ -15,7 +15,7 @@ from functools import reduce
from pyrocko.guts import StringChoice, Float, List, Bool
from pyrocko.gui.marker import PhaseMarker, EventMarker
from pyrocko import gf, model, util, trace, io
from pyrocko.io_common import FileSaveError
from pyrocko.io.io_common import FileSaveError
from pyrocko import pile
from ..station import StationGenerator, RandomStationGenerator