Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom dates plot ccftime & start of brand new test suite for api.py #101

Open
wants to merge 24 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
853e0a8
Fixes bug with wrong return types of this method
heavelock Oct 11, 2017
6798a67
Removes bug workaround from test
heavelock Oct 11, 2017
2db0816
Refactors default.py
heavelock Oct 11, 2017
489e7f8
Minor pep8 changes
heavelock Oct 11, 2017
7679fea
Pep 8 changes
heavelock Oct 11, 2017
ade6567
Adds custom dates to ccftime plot
heavelock Oct 12, 2017
3750311
Updates doc, adds some pep8 changes
heavelock Oct 12, 2017
8f560ca
PEP8 polishing
heavelock Oct 12, 2017
6e01bc6
PEP8 refactoring
heavelock Oct 12, 2017
4353c90
PEP8 refactoring
heavelock Oct 12, 2017
9540dba
Adds test_api.py scratch
heavelock Oct 17, 2017
dda5fa1
Changes to docstring in API, minor changes to test
heavelock Oct 17, 2017
19d4a7a
Adds test cases, creates a test suite
heavelock Oct 17, 2017
014a5a5
Moves test file to proper directory
heavelock Oct 17, 2017
44c68d4
Adds a inmemory db to test it properly
heavelock Oct 18, 2017
63593b1
Reverts unnecessary changes
heavelock Oct 18, 2017
9086273
Changes a bit how test suite is created
heavelock Oct 20, 2017
bf5dbd1
Adds exception for non existing path
heavelock Oct 24, 2017
c45309d
Changes a bit an exception message
heavelock Oct 24, 2017
1f72a9d
Reworked path creation mechanism to make checking possible
heavelock Oct 24, 2017
a10525f
Adds to docs information about startdate and enddate
heavelock Oct 24, 2017
e0191b0
Rebuilts imports
heavelock Oct 24, 2017
c7049d7
Adds test method for get_config and update_config
heavelock Nov 9, 2017
350c197
Reverts get_results method
heavelock Nov 9, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 17 additions & 10 deletions msnoise/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ def update_config(session, name, value):
"""

config = session.query(Config).filter(Config.name == name).first()
if "NULL" in value:
if "NULL" in value:
config.value = None
else:
config.value = value
Expand Down Expand Up @@ -996,7 +996,7 @@ def export_mseed(db, filename, pair, components, filterid, corr, ncorr=0,
except:
pass
filename += ".MSEED"

if maxlag is None:
maxlag = float(get_config(db, "maxlag"))
if cc_sampling_rate is None:
Expand Down Expand Up @@ -1187,7 +1187,7 @@ def build_ref_datelist(session):
return start, end, datelist.tolist()


def build_movstack_datelist(session):
def build_movstack_datelist(session, startdate=None, enddate=None):
"""
Creates a date array for the analyse period.
The returned tuple contains a start and an end date, and a list of
Expand All @@ -1196,12 +1196,19 @@ def build_movstack_datelist(session):
:type session: :class:`sqlalchemy.orm.session.Session`
:param session: A :class:`~sqlalchemy.orm.session.Session` object, as
obtained by :func:`connect`

:rtype: tuple
:returns: (start, end, datelist)
"""
begin = get_config(session, "startdate")
end = get_config(session, "enddate")
:type startdate: str
:param startdate: a startdate for which method should create a date array.
Defaults to None, in this case method gets the value from database.
:type enddate: str
:param enddate: a enddate for which method should create a date array.
Defaults to None, in this case method gets the value from database.

:rtype: (datetime.date, datetime.date, list of datetime.date)
:returns: Returns startdate, enddate and a list of all days between those.
"""
begin = startdate if startdate is not None else get_config(session,
"startdate")
end = enddate if enddate is not None else get_config(session, "enddate")
if begin[0] == '-':
start = datetime.date.today() + datetime.timedelta(days=int(begin))
end = datetime.date.today() + datetime.timedelta(days=int(end))
Expand Down Expand Up @@ -1433,7 +1440,7 @@ def make_same_length(st):
# apply the mask to all traces
for tr in st:
tr.data.mask = mask

# remove the masks from the stream
st = st.split()
return st
Expand Down
36 changes: 18 additions & 18 deletions msnoise/msnoise_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,23 +182,23 @@ def mwcs_step(form, field):
if field.data > form.data['mwcs_wlen']:
raise ValidationError("'mwcs_step' should be smaller or equal to"
" 'mwcs_wlen'")

form_args = dict(
mwcs_low=dict(validators=[mwcs_low]),
mwcs_high=dict(validators=[mwcs_high]),
high=dict(validators=[high]),
mwcs_step=dict(validators=[mwcs_step]),
)

column_list = ('ref', 'low', 'mwcs_low', 'mwcs_high', 'high',
'rms_threshold', 'mwcs_wlen', 'mwcs_step', 'used')
form_columns = ('low', 'mwcs_low', 'mwcs_high', 'high',
'rms_threshold', 'mwcs_wlen', 'mwcs_step', 'used')

def __init__(self, session, **kwargs):
# You can pass name and other parameters if you want to
super(FilterView, self).__init__(Filter, session, **kwargs)

@action('used',
lazy_gettext('Toggle Used'),
lazy_gettext('Are you sure you want to update selected models?'))
Expand All @@ -211,7 +211,7 @@ def used(self, ids):
else:
s.used = True
self.session.commit()
return
return


MY_DEFAULT_FORMATTERS = dict(typefmt.BASE_FORMATTERS)
Expand All @@ -224,10 +224,10 @@ class StationView(ModelView):
view_title = "Station Configuration"
column_filters = ('net', 'used')
column_type_formatters = MY_DEFAULT_FORMATTERS

def __init__(self, session, **kwargs):
super(StationView, self).__init__(Station, session, **kwargs)

@action('used',
lazy_gettext('Toggle Used'),
lazy_gettext('Are you sure you want to update selected models?'))
Expand All @@ -240,7 +240,7 @@ def used(self, ids):
else:
s.used = True
self.session.commit()
return
return


class DataAvailabilityView(ModelView):
Expand All @@ -255,7 +255,7 @@ class DataAvailabilityView(ModelView):
def __init__(self, session, **kwargs):
super(DataAvailabilityView, self).__init__(DataAvailability, session,
**kwargs)

@action('modified',
lazy_gettext('Mark as (M)odified'),
lazy_gettext('Are you sure you want to update selected models?'))
Expand All @@ -271,7 +271,7 @@ def modified(self, ids):
'%(count)s models were successfully flagged (M)odified.',
count, count=count))
return


class JobView(ModelView):
view_title = "Jobs"
Expand All @@ -284,7 +284,7 @@ class JobView(ModelView):

def __init__(self, session, **kwargs):
super(JobView, self).__init__(Job, session, **kwargs)

@action('todo',
lazy_gettext('Mark as (T)odo'),
lazy_gettext('Are you sure you want to update selected models?'))
Expand All @@ -295,7 +295,7 @@ def todo(self, ids):
s.flag = 'T'
self.session.commit()
return

@action('done',
lazy_gettext('Mark as (D)one'),
lazy_gettext('Are you sure you want to update selected models?'))
Expand All @@ -306,7 +306,7 @@ def done(self, ids):
s.flag = 'D'
self.session.commit()
return

@action('deletetype',
lazy_gettext('Delete all Jobs of the same "Type"'),
lazy_gettext('Are you sure you want to delete all those models?'))
Expand All @@ -318,7 +318,7 @@ def deletetype(self, ids):
self.get_query().filter(Job.jobtype == type_to_delete).delete()
self.session.commit()
return

@action('massTodo',
lazy_gettext('Mark all Jobs of the same Type as (T)odo'),
lazy_gettext('Are you sure you want to update all those models?'))
Expand All @@ -327,7 +327,7 @@ def massTodo(self, ids):
query = self.get_query().filter(model_pk.in_(ids))
for s in query.all():
type_to_delete = s.jobtype

for s in self.get_query().filter(Job.jobtype == type_to_delete).all():
s.flag = 'T'
self.session.commit()
Expand Down Expand Up @@ -564,7 +564,7 @@ def allresults():
start, end, dates = build_ref_datelist(db)
i, result = get_results(db,station1, station2, filterid, components, dates,
format=format)

data = {}
if format == 'stack':
if i != 0:
Expand Down Expand Up @@ -603,7 +603,7 @@ def new_jobsTRIG():
o['count'] = count
o = json.dumps(o)
return flask.Response(o, mimetype='application/json')


@app.route('/admin/jobs_list.json')
def joblists():
Expand Down Expand Up @@ -666,7 +666,7 @@ def main(port=5000):
db.close()

admin = Admin(app, template_mode='bootstrap2')

if "msnoise_brand" in os.environ:
tmp = eval(os.environ["msnoise_brand"])
name, logo = tmp.split("|")
Expand Down
18 changes: 12 additions & 6 deletions msnoise/plots/ccftime.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
selectable too. The ``--ampli`` argument allows to increase the vertical scale
of the CCFs. The ``--seismic`` shows the up-going wiggles with a black-filled
background (very heavy !). Passing ``--refilter`` allows to bandpass filter
CCFs before plotting (new in 1.5).
CCFs before plotting (new in 1.5). Passing ``--startdate`` and ``--enddate``
parameters allows to specify which period of data should be plotted. By default
the plot uses dates determined in database.

.. include:: clickhelp/msnoise-plot-ccftime.rst

Expand All @@ -17,22 +19,26 @@
"""
# plot interferogram

import datetime

import matplotlib.dates as mdates
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.widgets import Cursor

from obspy.signal.filter import envelope as obspy_envelope
from obspy.signal.filter import bandpass
from ..api import *
from obspy.signal.filter import envelope as obspy_envelope

from msnoise.api import connect, get_config, get_maxlag_samples, \
build_movstack_datelist, get_filters, get_results


def main(sta1, sta2, filterid, components, mov_stack=1, ampli=5, seismic=False,
show=False, outfile=None, envelope=False, refilter=None):
show=False, outfile=None, envelope=False, refilter=None, startdate=None, enddate=None):
db = connect()
maxlag = float(get_config(db, 'maxlag'))
samples = get_maxlag_samples(db)
cc_sampling_rate = float(get_config(db, 'cc_sampling_rate'))
start, end, datelist = build_movstack_datelist(db)
start, end, datelist = build_movstack_datelist(db, startdate, enddate)
base = mdates.date2num(start)
plt.figure(figsize=(12, 9))
sta1 = sta1.replace('.', '_')
Expand Down
1 change: 0 additions & 1 deletion msnoise/s000installer.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
directory. It's not very safe, but until now we haven't thought of another
solution.
"""
import argparse
import sys
from getpass import getpass

Expand Down
8 changes: 6 additions & 2 deletions msnoise/scripts/msnoise.py
Original file line number Diff line number Diff line change
Expand Up @@ -562,17 +562,21 @@ def interferogram(ctx, sta1, sta2, filterid, comp, mov_stack, show, outfile,
@click.option('-r', '--refilter', default=None,
help='Refilter CCFs before plotting (e.g. 4:8 for filtering CCFs '
'between 4.0 and 8.0 Hz. This will update the plot title.')
@click.option('--startdate', help='Change startdate',
default=None, type=str)
@click.option('--enddate', help='Change enddate',
default=None, type=str)
@click.pass_context
def ccftime(ctx, sta1, sta2, filterid, comp, mov_stack,
ampli, seismic, show, outfile, envelope, refilter):
ampli, seismic, show, outfile, envelope, refilter, startdate, enddate):
"""Plots the ccf vs time between sta1 and sta2 (parses the dt/t results)\n
STA1 and STA2 must be provided with this format: NET.STA !"""
if ctx.obj['MSNOISE_custom']:
from ccftime import main
else:
from ..plots.ccftime import main
main(sta1, sta2, filterid, comp, mov_stack, ampli, seismic, show, outfile,
envelope, refilter)
envelope, refilter, startdate, enddate)


@click.command()
Expand Down