-
Notifications
You must be signed in to change notification settings - Fork 80
/
msnoise.py
1531 lines (1354 loc) · 55.7 KB
/
msnoise.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import traceback
import logging
import os
import sys
import sqlalchemy
from sqlalchemy import text
import time
import click
from .._version import get_git_version
__version__ = get_git_version(dirty=True)
from .. import MSNoiseError, DBConfigNotFoundError
from ..api import connect, get_config, update_station, get_logger, get_job_types
from ..msnoise_table_def import DataAvailability
old = False
if os.path.isfile(".old"):
print("OK, showing old commands too")
old = True
class OrderedGroup(click.Group):
def list_commands(self, ctx):
return self.commands.keys()
def parse_extra_args(ctx, param, extra_args):
# extra_args = extra_args.split(" ")
kwargs = {}
for e in extra_args:
kw, value = e.split("=")
kwargs[kw.replace("--", "")] = eval(value)
return kwargs
def validate_verbosity(ctx, param, value):
"""
Validate the --quiet and --verbose options to have it conflict with one
another.
"""
if param.name == 'quiet':
excluded = 'verbose'
elif param.name == 'verbose':
excluded = 'quiet'
if value and excluded in ctx.params:
raise click.BadParameter('Cannot use both --quiet and --verbose option.')
return value
def show_config_values(db, names):
"""
Show configuration value of parameters provided in the 'names' list.
"""
from ..api import get_config
from ..default import default
for key in names:
display_value = value = get_config(db, key)
if value == '':
# Use a more explicit representation of the empty string
display_value = "''"
try:
default_value = default[key].default
except KeyError:
click.secho("Error: unknown parameter '%s'" % key)
continue
if value == default_value:
click.secho(" %s: %s" % (key, display_value))
else:
click.secho(" M %s: %s" % (key, display_value), fg='green')
def info_db_ini():
"""
Show information stored in the db.ini file.
"""
from ..api import read_db_inifile
dbini = read_db_inifile()
click.echo('Database information stored in the db.ini file:')
if dbini.tech == 1:
click.echo(' - database type: SQLite')
click.echo(' - filename: {}'.format(dbini.hostname))
elif dbini.tech == 2:
click.echo(' - database type: MySQL')
click.echo(' - hostname: {}'.format(dbini.hostname))
click.echo(' - database: {}'.format(dbini.database))
click.echo(' - username: {}'.format(dbini.username))
click.echo(' - password: {}'.format('*' * len(dbini.password)))
click.echo(' - table prefix: {}'.format(dbini.prefix if dbini.prefix else '(none)'))
def info_folders(db):
"""
Show information about folders used by MSNoise.
"""
from ..api import get_config
click.echo('')
click.echo('General:')
click.echo(' - the MSNoise module is installed in: %s'
% os.path.abspath(os.path.join(
os.path.dirname(__file__), '..', '..')))
if os.path.isfile('db.ini'):
click.echo(' - db.ini is present in the current directory')
else:
click.secho(' - db.ini not found in the present directory, is MSNoise installed here ?',
fg='red')
return
info_db_ini()
click.echo('')
click.echo('Configuration:')
data_folder = get_config(db, "data_folder")
if os.path.isdir(data_folder):
click.echo(" - %s exists" % data_folder)
else:
click.secho(" - %s does not exists !" % data_folder, fg='red')
output_folder = get_config(db, "output_folder")
if os.path.isdir(output_folder):
click.echo(" - %s exists" % output_folder)
else:
if get_config(db, 'keep_all') in ['Y', 'y']:
for job in get_job_types(db):
if job[1] == 'D':
if job[0] > 0:
click.secho(
" - %s does not exists and that is not normal"
" (%i CC jobs done)" % (output_folder, job[0]),
fg='red')
else:
click.secho(
" - %s does not exists and that is normal"
" (%i CC jobs done)" % (output_folder, job[0]))
else:
click.secho(
" - %s does not exists (and that is normal because"
" keep_all=False)" % output_folder)
def info_parameters(db):
"""
Show values of each configuration parameters.
"""
from ..api import get_filters
from ..default import default
click.echo('')
click.echo('Configuration values:'
' | Normal colour indicates that the default value is used'
' | Green indicates "M"odified values')
# TODO: add plugins params
show_config_values(db, default.keys())
click.echo('')
click.echo('Filters:')
click.echo(' ID: [low:high] [mwcs_low:mwcs_high] mwcs_wlen mwcs_step Used?')
for f in get_filters(db, all=True):
click.echo(' {:2d}: {:^15s} {:^20s} {:^9s} {:^9s} {:1s}'
.format(f.ref,
'[{:.3f}:{:.3f}]'.format(f.low, f.high),
'[{:.3f}:{:.3f}]'.format(f.mwcs_low, f.mwcs_high),
'{:.0f}'.format(f.mwcs_wlen),
'{:.0f}'.format(f.mwcs_step),
'Y' if f.used else 'N'))
def info_stations(db):
"""
Show information about configured stations.
"""
from ..api import get_stations
click.echo('')
click.echo('Stations:')
click.echo(' NET.STA Long. Lat. Alt. Coord. Used?')
s = None
na_sign = '-'
for s in get_stations(db, all=True):
click.echo(' {:>8s} {:>9s} {:>8s} {:>6s} {:3s} {:1s}'.format(
'{}.{}'.format(s.net, s.sta),
'{:.4f}'.format(s.X) if s.X is not None else na_sign,
'{:.4f}'.format(s.Y) if s.Y is not None else na_sign,
'{:.1f}'.format(s.altitude) if s.altitude is not None else na_sign,
s.coordinates or na_sign,
'Y' if s.used else 'N'))
click.echo(" | Location code(s): %s" % s.used_location_codes)
click.echo(" | Channel names(s): %s" % s.used_channel_names)
if s is None:
click.echo(' ')
def info_jobs(db):
"""
Show information about jobs registered in database.
"""
from ..api import get_job_types
jobtypes = {}
jobtypes["QC"] = ["PSD", "PSD2HDF", "HDF2RMS"]
jobtypes["CC"] = ["CC", "STACK", "MWCS", "DTT", "DVV"]
click.echo("Jobs:")
for category in ["QC", "CC"]:
click.echo(' %s:' % category)
for jobtype in jobtypes[category]:
click.echo(' %s:' % jobtype)
n = None
for (n, jobtype) in get_job_types(db, jobtype):
click.echo(" %s : %i" % (jobtype, n))
if n is None:
click.echo(' none defined')
def info_plugins(db):
"""
Show information about configured plugins.
"""
from ..api import get_config, get_job_types
plugins = get_config(db, "plugins")
if not plugins:
return
plugins = plugins.split(",")
import pkg_resources
for ep in pkg_resources.iter_entry_points(group='msnoise.plugins.jobtypes'):
module_name = ep.module_name.split(".")[0]
if module_name in plugins:
click.echo('')
click.echo('Plugin: %s' % module_name)
for row in ep.load()():
click.echo(' %s:' % row["name"])
for (n, jobtype) in get_job_types(db, row["name"]):
click.echo(" %s : %i" % (jobtype, n))
from pkg_resources import iter_entry_points
# import click
# from click_plugins import with_plugins
# @with_plugins(iter_entry_points('click_command_tree'))
@click.group(context_settings=dict(max_content_width=120), cls=OrderedGroup)
@click.option('-t', '--threads', default=1, help='Number of threads to use \
(only affects modules that are designed to do parallel processing)')
@click.option('-d', '--delay', default=1, help='In the case of multi-threading'
', defines the number of seconds to wait before lauching '
'the next thread. Defaults to [1] second ')
@click.option('-c', '--custom', default=False, is_flag=True, help='Use custom \
file for plots. To use this, copy the plot script here and edit it.')
@click.option('-v', '--verbose', is_flag=True, callback=validate_verbosity)
@click.option('-q', '--quiet', is_flag=True, default=False,
callback=validate_verbosity)
@click.version_option(__version__)
@click.pass_context
def cli(ctx, threads, delay, custom, verbose, quiet):
ctx.obj['MSNOISE_threads'] = threads
ctx.obj['MSNOISE_threadsdelay'] = delay
ctx.obj['MSNOISE_custom'] = custom
ctx.obj['MSNOISE_verbosity'] = "INFO"
if quiet:
ctx.obj['MSNOISE_verbosity'] = "WARNING"
elif verbose:
ctx.obj['MSNOISE_verbosity'] = "DEBUG"
global logger
if threads > 1:
logger = get_logger('msnoise', ctx.obj['MSNOISE_verbosity'], with_pid=True)
else:
logger = get_logger('msnoise', ctx.obj['MSNOISE_verbosity'])
# Is this really needed?
if custom:
sys.path.append(os.getcwd())
@cli.command()
@click.option('-p', '--port', default=5000, help='Port to open')
def admin(port):
"""Starts the Web Admin on http://localhost:5000 by default"""
from ..msnoise_admin import main
main(port)
@cli.group(cls=OrderedGroup)
def db():
"""Commands to interact with the database"""
pass
@db.command(name="init")
@click.option('--tech', help='Database technology: 1=SQLite 2=MySQL/MariaDB 3=PostgreSQL',
default=None)
def db_init(tech):
"""This command initializes the current folder to be a MSNoise Project
by creating a database and a db.ini file."""
click.echo('Launching the init')
from ..s000installer import main
main(tech)
@db.command(name="update_loc_chan")
@click.pass_context
def db_da_stations_update_loc_chan(ctx):
"""Populates the Location & Channel from the Data Availability
table. Warning: rewrites automatically, no confirmation."""
from msnoise.api import connect, get_stations
session = connect()
stations = get_stations(session)
for sta in stations:
data = session.query(DataAvailability). \
filter(text("net=:net")). \
filter(text("sta=:sta")). \
group_by(DataAvailability.net, DataAvailability.sta,
DataAvailability.loc, DataAvailability.chan). \
params(net=sta.net, sta=sta.sta).all()
locids = list(set(sorted([d.loc for d in data])))
chans = list(set(sorted([d.chan for d in data])))
logger.info("%s.%s has locids:%s and chans:%s" % (sta.net, sta.sta,
locids, chans))
sta.used_location_codes = ",".join(locids)
sta.used_channel_names = ",".join(chans)
session.commit()
@db.command(name="execute")
@click.argument('sql_command')
@click.option('-o', '--outfile', help='Output filename (?="request.csv")',
default=None, type=str)
@click.option('-s', '--show', help='Show output (in case of SELECT statement)?',
default=True, type=bool)
@click.pass_context
def db_execute(ctx, sql_command, outfile=None, show=True):
"""EXPERT MODE: Executes 'sql_command' on the database. Use this command
at your own risk!!"""
from msnoise.api import connect
db = connect()
for cmd in sql_command.split(";"):
if not len(cmd):
continue
logger.info("Executing '%s'" % cmd)
r = db.execute(text(cmd))
if cmd.count("select") or cmd.count("SELECT"):
result = r.fetchall()
if not len(result):
logger.info("The query returned no results, sorry.")
else:
import pandas as pd
df = pd.DataFrame(result, columns=r.keys())
if show:
pd.set_option('display.max_rows', None)
pd.set_option('display.max_columns', None)
pd.set_option('display.width', None)
pd.set_option('display.max_colwidth', None)
print(df)
if outfile:
if outfile == "?":
df.to_csv("request.csv")
else:
df.to_csv("%s" % outfile)
db.commit()
db.close()
@db.command(name="upgrade")
def db_upgrade():
"""Upgrade the database from previous to a new version.
This procedure adds new parameters with their default value
in the config database.
"""
from ..api import connect, Config, read_db_inifile
from ..default import default
db = connect()
dbini = read_db_inifile()
prefix = (dbini.prefix + '_') if dbini.prefix != '' else ''
for name in default.keys():
try:
db.add(Config(name=name, value=default[name].default))
db.commit()
except:
db.rollback()
# print("Passing %s: already in DB" % name)
continue
try:
db.execute(text("CREATE UNIQUE INDEX job_index ON %sjobs (day, pair, "
"jobtype)" %
prefix))
db.commit()
except:
logging.info("It looks like the v1.5 'job_index' is already in the DB")
db.rollback()
try:
db.execute(text("CREATE INDEX job_index2 ON %sjobs (jobtype, flag)" %
prefix))
db.commit()
except:
logging.info("It looks like the v1.6 'job_index2' is already in the DB")
db.rollback()
try:
db.execute(text("CREATE UNIQUE INDEX da_index ON %sdata_availability (path, "
"file, net, sta, loc, chan)" %
prefix))
db.commit()
except:
logging.info("It looks like the v1.5 'da_index' is already in the DB")
db.rollback()
db.close()
@db.command(name='clean_duplicates')
def db_clean_duplicates():
"""Checks the Jobs table and deletes duplicate entries"""
from msnoise.api import connect, read_db_inifile
dbini = read_db_inifile()
prefix = (dbini.prefix + '_') if dbini.prefix != '' else ''
db = connect()
if dbini.tech == 1:
query = 'DELETE FROM {0}jobs WHERE rowid NOT IN '\
'(SELECT MIN(rowid) FROM {0}jobs GROUP BY day,pair,jobtype)'\
.format(prefix)
else:
query = 'DELETE from {0}jobs USING {0}jobs as j1, {0}jobs as j2 '\
'WHERE (j1.ref > j2.ref) AND (j1.day=j2.day) '\
'AND (j1.pair=j2.pair) AND (j1.jobtype=j2.jobtype)'\
.format(prefix)
db.execute(text(query))
db.commit()
db.close()
@db.command(name="dump")
@click.option("--format", default="csv")
def db_dump(format):
"""Dumps the complete database in formatted files, defaults to CSV.
"""
from ..api import connect, get_engine
from sqlalchemy import MetaData
import pandas as pd
if format == "csv":
engine = get_engine(inifile=os.path.join(os.getcwd(), 'db.ini'))
meta = MetaData()
meta.reflect(bind=engine)
for table in meta.sorted_tables:
with engine.connect() as conn:
rows = conn.execute(table.select()).all()
df = pd.DataFrame(rows)
logger.info("Dumping table %s to %s.csv" % (table.name, table.name))
df.to_csv("%s.csv" % table.name, index=False)
else:
logger.error("Currently only the csv format is supported, sorry.")
@db.command(name="import")
@click.argument("table")
@click.option("--format", default="csv")
@click.option("--force", is_flag=True, default=False)
def db_import(table, format, force):
"""
Imports msnoise tables from formatted files (CSV).
"""
from ..api import connect, get_engine
from sqlalchemy import MetaData
import pandas as pd
if format == "csv":
engine = get_engine(inifile=os.path.join(os.getcwd(), 'db.ini'))
logger.info("Loading table %s from %s.csv" % (table, table))
df = pd.read_csv("%s.csv" % table)
if force:
df.to_sql(table, engine, if_exists="replace")
else:
try:
df.to_sql(table, engine)
except ValueError:
traceback.print_exc()
logger.info("You're probably getting the error above because the "
"table already exists, if you want to replace the table "
"with the imported data, then pass the --force option")
else:
logger.error("Currently only the csv format is supported, sorry.")
@cli.command()
@click.option('-j', '--jobs', is_flag=True, help='Jobs Info only')
def info(jobs):
"""Outputs general information about the current install and config, plus
information about jobs and their status."""
from ..api import connect
if not os.path.isfile('db.ini'):
click.secho(' - db.ini is not present, is MSNoise installed here ?',
fg='red')
return
db = connect()
if not jobs:
info_folders(db)
info_parameters(db)
info_stations(db)
info_jobs(db)
info_plugins(db)
db.close()
@cli.group()
def config():
"""
This command allows to set a parameter value in the database, show
parameter values, or synchronise station metadata, depending on the
invoked subcommands.
"""
pass
@config.command(name='sync')
def config_sync():
"""
Synchronise station metadata from inventory/dataless.
"""
import glob
from ..api import connect, get_config, get_stations, update_station,\
preload_instrument_responses
db = connect()
responses = preload_instrument_responses(db)
netsta = []
for id, row in responses.iterrows():
net, sta, loc, chan = row["channel_id"].split(".")
netsta.append("%s.%s"%(net,sta))
responses["netsta"] = netsta
for station in get_stations(db):
id = "%s.%s" % (station.net, station.sta)
coords = responses[responses["netsta"] == id]
if not len(coords):
logger.error("No coords for %s, skipping,..." % id)
continue
lon = float(coords["longitude"].values[0])
lat = float(coords["latitude"].values[0])
update_station(db, station.net, station.sta, lon, lat, 0, "DEG", )
logging.info("Added coordinates (%.5f %.5f) for station %s.%s" %
(lon, lat, station.net, station.sta))
db.close()
@config.command(name='set')
@click.argument('name_value')
def config_set(name_value):
"""
Set a configuration value. The argument should be of the form
'variable=value'.
"""
from ..default import default
if not name_value.count("="):
click.echo("!! format of the set command is name=value !!")
return
name, value = name_value.split("=")
if name not in default:
click.echo("!! unknown parameter %s !!" % name)
return
from ..api import connect, update_config
db = connect()
update_config(db, name, value)
db.commit()
db.close()
click.echo("Successfully updated parameter %s = %s" % (name, value))
@config.command(name='get')
@click.argument('names', nargs=-1)
def config_get(names):
"""
Display the value of the given configuration variable(s).
"""
from ..api import connect, get_config
db = connect()
show_config_values(db, names)
db.close()
@cli.command()
@click.argument('jobtype')
@click.option('-a', '--all', is_flag=True, help='Reset all jobs')
@click.option('-r', '--rule', help='Reset job that match this SQL rule')
def reset(jobtype, all, rule):
"""Resets the jobs to "T"odo. JOBTYPE is the acronym of the job type.
By default only resets jobs "I"n progress. --all resets all jobs, whatever
the flag value. Standard Job Types are CC, STACK, MWCS and DTT, but
plugins can define their own."""
from ..api import connect, reset_jobs, read_db_inifile
dbini = read_db_inifile()
prefix = (dbini.prefix + '_') if dbini.prefix != '' else ''
session = connect()
if jobtype == "DA":
session.execute(text("UPDATE {0}data_availability SET flag='M'"
.format(prefix)))
elif jobtype != jobtype.upper():
logging.info("The jobtype %s is not uppercase (usually jobtypes"
" are uppercase...)"%jobtype)
reset_jobs(session, jobtype, all, rule)
session.close()
@cli.command()
@click.option('--fromDA', help='Populates the station table '
'using network and station codes'
' found in the data_availability'
' table, overrides the default'
' workflow step.',
is_flag=True)
@click.pass_context
def populate(ctx, fromda):
"""Rapidly scan the archive filenames and find Network/Stations, only works
with known archive structures, or with a custom code provided by the user.
"""
loglevel = ctx.obj['MSNOISE_verbosity']
if fromda:
logger.info("Populating the Station table")
logger.info("Overriding workflow...")
db = connect()
stations = db.query(DataAvailability.net, DataAvailability.sta). \
group_by(DataAvailability.net, DataAvailability.sta)
for net, sta in stations:
logger.info('Adding: %s.%s' % (net, sta))
X = 0.0
Y = 0.0
altitude = 0.0
coordinates = 'UTM'
instrument = 'N/A'
update_station(db, net, sta, X, Y, altitude,
coordinates=coordinates, instrument=instrument)
logger.info("Checking the available loc ids and chans...")
ctx.invoke(db_da_stations_update_loc_chan)
else:
from ..s002populate_station_table import main
main(loglevel=loglevel)
@cli.command(name='scan_archive')
@click.option('-i', '--init', is_flag=True, help='First run ?')
@click.option('--path', help='Scan all files in specific folder, overrides the'
' default workflow step.')
@click.option('-r', '--recursively', is_flag=True,
help='When scanning a path, walk subfolders automatically ?')
@click.option('--crondays',
help='Number of past days to monitor, typically used in cron jobs '
"(overrides the 'crondays' configuration value). "
'Must be a float representing a number of days, or designate '
" weeks, days, and/or hours using the format 'Xw Xd Xh'.")
@click.pass_context
def scan_archive(ctx, init, crondays, path, recursively):
"""Scan the archive and insert into the Data Availability table."""
from .. import s01scan_archive
nthreads = ctx.obj['MSNOISE_threads']
if path:
if not os.path.isdir(path):
logging.critical('Cannot scan from %s: not such directory.' % path)
sys.exit(1)
logging.info('Overriding workflow: only scanning path'
' %s (%srecursively)'
% (path, '' if recursively else 'non-'))
s01scan_archive.main(init, nthreads, crondays, path, recursively)
else:
s01scan_archive.main(init, nthreads, crondays)
@cli.group(name="plot")
def plot():
"""Commands to trigger plots (data availability, station map)"""
pass
@plot.command(name='data_availability')
@click.option('-c', '--chan', default="?HZ", help="Channel, you can use the ? wildcard, e.g. '?HZ' (default) or 'HH?', etc.")
@click.option('-s', '--show', help='Show interactively?',
default=True, type=bool)
@click.option('-o', '--outfile', help='Output filename (?=auto). Defaults to PNG format, but can be anything matplotlib outputs, e.g. ?.pdf will save to PDF with an automatic file naming.',
default=None, type=str)
@click.pass_context
def plot_data_availability(ctx, chan, show, outfile):
"""Plots the Data Availability vs time"""
loglevel = ctx.obj['MSNOISE_verbosity']
if ctx.obj['MSNOISE_custom']:
from data_availability import main # NOQA
else:
from ..plots.data_availability import main
main(chan, show, outfile, loglevel=loglevel)
@plot.command(name='station_map')
@click.option('-s', '--show', help='Show interactively?',
default=True, type=bool)
@click.option('-o', '--outfile', help='Output filename (?=auto). Defaults to PNG format, but can be anything matplotlib outputs, e.g. ?.pdf will save to PDF with an automatic file naming.',
default=None, type=str)
@click.pass_context
def plot_station_map(ctx, show, outfile):
"""Plots the station map (very very basic)"""
if ctx.obj['MSNOISE_custom']:
from station_map import main # NOQA
else:
from ..plots.station_map import main
main(show, outfile)
@cli.command(name='new_jobs')
@click.option('-i', '--init', is_flag=True, help='First run ? This disables '
'the check for existing jobs.')
@click.option('--nocc', is_flag=True, default=False, help='Disable the creation'
' of CC jobs.')
@click.option('--hpc', help='Format PREVIOUS:NEXT. When running on HPC, '
'create the next jobs in the workflow based on the'
'previous step mentioned here. Example:'
'"msnoise new_jobs --hpc CC:STACK" will create '
'STACK jobs based on CC jobs marked "D"one.')
def new_jobs(init, nocc, hpc=""):
"""Determines if new CC/QC jobs are to be defined"""
if not hpc:
from ..s02new_jobs import main
main(init, nocc)
if hpc:
from ..api import connect, read_db_inifile
dbini = read_db_inifile()
prefix = (dbini.prefix + '_') if dbini.prefix != '' else ''
left, right = hpc.split(':')
db = connect()
db.execute(text("INSERT INTO {prefix}jobs (pair, day, jobtype, flag) "
"SELECT pair, day, '{right_type}', 'T' FROM {prefix}jobs "
"WHERE jobtype='{left_type}' AND flag='D';"
.format(prefix=prefix, right_type=right, left_type=left)))
db.commit()
db.close()
@cli.group(cls=OrderedGroup)
def cc():
"""Commands for the "Cross-Correlations" Workflow"""
pass
@cc.command(name='compute_cc')
@click.pass_context
def cc_compute_cc(ctx):
"""Computes the CC jobs (based on the "New Jobs" identified)"""
from ..s03compute_no_rotation import main
threads = ctx.obj['MSNOISE_threads']
delay = ctx.obj['MSNOISE_threadsdelay']
loglevel = ctx.obj['MSNOISE_verbosity']
if threads == 1:
main(loglevel=loglevel)
else:
from multiprocessing import Process
processes = []
for i in range(threads):
p = Process(target=main, kwargs={"loglevel": loglevel})
p.start()
processes.append(p)
time.sleep(delay)
for p in processes:
p.join()
@cc.command(name='compute_cc_rot')
@click.pass_context
def cc_compute_cc_rot(ctx):
"""Computes the CC jobs too (allows for R or T components)"""
from ..s03compute_cc import main
threads = ctx.obj['MSNOISE_threads']
delay = ctx.obj['MSNOISE_threadsdelay']
loglevel = ctx.obj['MSNOISE_verbosity']
if threads == 1:
main(loglevel=loglevel)
else:
from multiprocessing import Process
processes = []
for i in range(threads):
p = Process(target=main, kwargs={"loglevel": loglevel})
p.start()
processes.append(p)
time.sleep(delay)
for p in processes:
p.join()
if old:
@cc.command(name="stack_old")
@click.pass_context
@click.option('-r', '--ref', is_flag=True, help='Compute the REF Stack')
@click.option('-m', '--mov', is_flag=True, help='Compute the MOV Stacks')
@click.option('-s', '--step', is_flag=True, help='Compute the STEP Stacks')
def cc_stack_old(ctx, ref, mov, step):
"""Stacks the [REF] or [MOV] windows.
Computes the STACK jobs.
"""
click.secho('Lets STACK !', fg='green')
from ..s04stack import main
threads = ctx.obj['MSNOISE_threads']
delay = ctx.obj['MSNOISE_threadsdelay']
loglevel = ctx.obj['MSNOISE_verbosity']
if ref and mov:
click.secho("With MSNoise 1.6, you can't run REF & MOV stacks"
"simultaneously, please run them one after the other.")
sys.exit()
if threads == 1:
if ref:
main('ref', loglevel=loglevel)
if mov:
main('mov', loglevel=loglevel)
if step:
main('step', loglevel=loglevel)
else:
from multiprocessing import Process
processes = []
if ref:
for i in range(threads):
p = Process(target=main, args=["ref",],
kwargs={"loglevel": loglevel})
p.start()
processes.append(p)
time.sleep(delay)
for p in processes:
p.join()
if mov:
for i in range(threads):
p = Process(target=main, args=["mov",],
kwargs={"loglevel": loglevel})
p.start()
processes.append(p)
time.sleep(delay)
for p in processes:
p.join()
if step:
for i in range(threads):
p = Process(target=main, args=["step",],
kwargs={"loglevel": loglevel})
p.start()
processes.append(p)
time.sleep(delay)
for p in processes:
p.join()
@cc.command(name="stack")
@click.pass_context
@click.option('-r', '--ref', is_flag=True, help='Compute the REF Stack')
@click.option('-m', '--mov', is_flag=True, help='Compute the MOV Stacks')
@click.option('-s', '--step', is_flag=True, help='Compute the STEP Stacks')
def cc_stack(ctx, ref, mov, step):
"""Stacks the [REF] or [MOV] windows.
Computes the STACK jobs.
"""
click.secho('Lets STACK !', fg='green')
from ..s04_stack2 import main
threads = ctx.obj['MSNOISE_threads']
delay = ctx.obj['MSNOISE_threadsdelay']
loglevel = ctx.obj['MSNOISE_verbosity']
if ref and mov:
click.secho("With MSNoise 1.6, you can't run REF & MOV stacks"
"simultaneously, please run them one after the other.")
sys.exit()
if threads == 1:
if ref:
main('ref', loglevel=loglevel)
if mov:
main('mov', loglevel=loglevel)
if step:
main('step', loglevel=loglevel)
else:
from multiprocessing import Process
processes = []
if ref:
for i in range(threads):
p = Process(target=main, args=["ref",],
kwargs={"loglevel": loglevel})
p.start()
processes.append(p)
time.sleep(delay)
for p in processes:
p.join()
if mov:
for i in range(threads):
p = Process(target=main, args=["mov",],
kwargs={"loglevel": loglevel})
p.start()
processes.append(p)
time.sleep(delay)
for p in processes:
p.join()
if step:
for i in range(threads):
p = Process(target=main, args=["step",],
kwargs={"loglevel": loglevel})
p.start()
processes.append(p)
time.sleep(delay)
for p in processes:
p.join()
@cc.group(name="plot")
def cc_plot():
"""Commands to trigger different plots"""
pass
@cc_plot.command(name="distance",
context_settings=dict(ignore_unknown_options=True, ))
@click.option('-f', '--filterid', default=1, help='Filter ID')
@click.option('-c', '--comp', default="ZZ", help='Components (ZZ, ZE, NZ, 1E,...). Defaults to ZZ')
@click.option('-a', '--ampli', default=1.0, help='Amplification of the individual lines on the vertical axis (default=1)')
@click.option('-s', '--show', help='Show interactively?',
default=True, type=bool)
@click.option('-o', '--outfile', help='Output filename (?=auto). Defaults to PNG format, but can be anything matplotlib outputs, e.g. ?.pdf will save to PDF with an automatic file naming.',
default=None, type=str)
@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('--virtual-source', default=None,
help='Use only pairs including this station. Format must be '
'NET.STA')
@click.argument('extra_args', nargs=-1, type=click.UNPROCESSED,
callback=parse_extra_args)
@click.pass_context
def cc_plot_distance(ctx, filterid, comp, ampli, show, outfile, refilter,
virtual_source, extra_args):
"""Plots the REFs of all pairs vs distance"""
loglevel = ctx.obj['MSNOISE_verbosity']
if ctx.obj['MSNOISE_custom']:
from distance import main # NOQA
else:
from ..plots.distance import main
main(filterid, comp, ampli, show, outfile, refilter, virtual_source,
loglevel=loglevel, **extra_args)
@cc_plot.command(name="interferogram",
context_settings=dict(ignore_unknown_options=True, ))
@click.argument('sta1')
@click.argument('sta2')
@click.option('-f', '--filterid', default=1, help='Filter ID')
@click.option('-c', '--comp', default="ZZ", help='Components (ZZ, ZE, NZ, 1E,...). Defaults to ZZ')
@click.option('-m', '--mov_stack', default=1,
help='Mov Stack to read from disk. Defaults to 1.')
@click.option('-s', '--show', help='Show interactively?',
default=True, type=bool)
@click.option('-o', '--outfile', help='Output filename (?=auto). Defaults to PNG format, but can be anything matplotlib outputs, e.g. ?.pdf will save to PDF with an automatic file naming.',
default=None, type=str)
@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.argument('extra_args', nargs=-1, type=click.UNPROCESSED,
callback=parse_extra_args)
@click.pass_context
def cc_plot_interferogram(ctx, sta1, sta2, filterid, comp, mov_stack, show,
outfile,
refilter, extra_args):
"""Plots the interferogram between sta1 and sta2 (parses the CCFs)
STA1 and STA2 must be provided with this format: NET.STA !"""
loglevel = ctx.obj['MSNOISE_verbosity']
if ctx.obj['MSNOISE_custom']:
from interferogram import main # NOQA
else:
from ..plots.interferogram import main
main(sta1, sta2, filterid, comp, mov_stack, show, outfile, refilter,
loglevel=loglevel, **extra_args)
@cc_plot.command(name="ccftime",
context_settings=dict(ignore_unknown_options=True, ))
@click.argument('sta1')
@click.argument('sta2')
@click.option('-f', '--filterid', default=1, help='Filter ID')
@click.option('-c', '--comp', default="ZZ", help='Components (ZZ, ZE, NZ, 1E,...). Defaults to ZZ')
@click.option('-m', '--mov_stack', default=1,
help='Mov Stack to read from disk. Defaults to 1.')
@click.option('-a', '--ampli', default=5.0, help='Amplification of the individual lines on the vertical axis (default=1)')
@click.option('-S', '--seismic', is_flag=True, help='Seismic style: fill the space between the zero and the positive wiggles')
@click.option('-s', '--show', help='Show interactively?',
default=True, type=bool)
@click.option('-o', '--outfile', help='Output filename (?=auto). Defaults to PNG format, but can be anything matplotlib outputs, e.g. ?.pdf will save to PDF with an automatic file naming.',
default=None, type=str)
@click.option('-e', '--envelope', is_flag=True, help='Plot envelope instead of '
'time series')
@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.')