Skip to content

Commit

Permalink
bugfix in Stack
Browse files Browse the repository at this point in the history
Modifications in database_tools.py to solve the problem
  • Loading branch information
ThomasLecocq committed Oct 23, 2013
1 parent 5141734 commit 331a7c7
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 14 deletions.
18 changes: 12 additions & 6 deletions database_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from obspy.core import Stream, Trace, read
from obspy.sac import SacIO
import os

import logging
from msnoise_table_def import *


Expand Down Expand Up @@ -323,14 +323,17 @@ def export_sac(db, filename, pair, components, filterid, corr, ncorr=0, sac_form
return


def export_mseed(db, filename, pair, components, filterid, corr, ncorr=0):
def export_mseed(db, filename, pair, components, filterid, corr, ncorr=0, maxlag=None, cc_sampling_rate=None):
try:
os.makedirs(os.path.split(filename)[0])
except:
pass
filename += ".MSEED"
maxlag = float(get_config(db, "maxlag"))
cc_sampling_rate = float(get_config(db, "cc_sampling_rate"))

if maxlag is None:
maxlag = float(get_config(db, "maxlag"))
if cc_sampling_rate is None:
cc_sampling_rate = float(get_config(db, "cc_sampling_rate"))

mytrace = Trace(data=corr)
mytrace.stats['station'] = pair[:11]
Expand Down Expand Up @@ -445,9 +448,12 @@ def build_daystack_datelist(session):

def updated_days_for_dates(session, date1, date2, pair, type='CC', interval=datetime.timedelta(days=1), returndays=False):
lastmod = datetime.datetime.now() - interval
days = session.query(Job).filter(Job.day >= date1).filter(Job.day <= date2).filter(Job.type == type).filter(Job.lastmod >= lastmod).all()
if returndays:
days = session.query(Job).filter(Job.pair == pair).filter(Job.day >= date1).filter(Job.day <= date2).filter(Job.type == type).filter(Job.lastmod >= lastmod).group_by(Job.day).order_by(Job.day).all()
logging.debug('Found %03i updated days' % len(days))
if returndays and len(days) != 0:
return [datetime.datetime.strptime(day.day,'%Y-%m-%d').date() for day in days] ## RETURN DATE LIST !!!
elif returndays and len(days) == 0:
return []
else:
return True

Expand Down
20 changes: 12 additions & 8 deletions s04stack.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,10 @@ def stack(stype, interval=1):
elif export_format == "MSEED":
mseed = True
sac = False


maxlag = float(get_config(db, "maxlag"))
cc_sampling_rate = float(get_config(db, "cc_sampling_rate"))

if stype == "day":
start, end, datelist = build_daystack_datelist(db)
format = "stack"
Expand All @@ -131,13 +134,14 @@ def stack(stype, interval=1):
sta1 = "%s_%s" % (station1.net, station1.sta)
sta2 = "%s_%s" % (station2.net, station2.sta)
pair = "%s:%s" % (sta1, sta2)
if updated_days_for_dates(db, start, end, pair.replace('_', '.'), type='CC', interval=datetime.timedelta(days=interval)):
logging.debug('Processing %s-%s-%i' %
(pair, components, filterid))
updated_days = updated_days_for_dates(db, start, end, pair.replace('_', '.'), type='CC', interval=datetime.timedelta(days=interval),returndays=True)
if len(updated_days) != 0:
logging.debug("New Data for %s-%s-%i" %
(pair, components, filterid))
nstack, stack_total = get_results(
db, sta1, sta2, filterid, components, datelist, format=format)
updated_days = updated_days_for_dates(db, start, end, pair.replace(
'_', '.'), type='CC', interval=datetime.timedelta(days=interval), returndays=True)
if nstack > 0:
if stype == "mov":
for i, date in enumerate(datelist):
Expand All @@ -150,8 +154,8 @@ def stack(stype, interval=1):
low = i - mov_stack + 1
high = i + 1
newdata = False
for uday in updated_days:
if uday in datelist[low:high]:
for uday in datelist[low:high]:
if uday in updated_days:
newdata = True
break
if newdata:
Expand All @@ -170,10 +174,10 @@ def stack(stype, interval=1):
stack_path, str(date))
if mseed:
export_mseed(
db, filename, pair, components, filterid, corr)
db, filename, pair, components, filterid, corr, maxlag=maxlag, cc_sampling_rate=cc_sampling_rate)
if sac:
export_sac(
db, filename, pair, components, filterid, corr)
db, filename, pair, components, filterid, corr, maxlag=maxlag, cc_sampling_rate=cc_sampling_rate)
day_name = "%s:%s" % (
sta1, sta2)
if not jobadded:
Expand Down

0 comments on commit 331a7c7

Please sign in to comment.