From fec662bb109422710b0a8b9ccac90dbbfa44a4e8 Mon Sep 17 00:00:00 2001 From: pcain-usgs <pcain@usgs.gov> Date: Thu, 11 Jun 2020 17:43:45 -0600 Subject: [PATCH] move project, add to console_scripts --- geomagio/processing/magproc.py | 124 +++++++++++++++++++++++++++++++++ setup.py | 3 + 2 files changed, 127 insertions(+) create mode 100644 geomagio/processing/magproc.py diff --git a/geomagio/processing/magproc.py b/geomagio/processing/magproc.py new file mode 100644 index 00000000..63d9762e --- /dev/null +++ b/geomagio/processing/magproc.py @@ -0,0 +1,124 @@ +from os import path +import os +import sys + +from datetime import datetime +from dateutil.relativedelta import relativedelta +from obspy.core import UTCDateTime +import typer + +from ..edge.EdgeFactory import EdgeFactory +from ..pcdcp import PCDCPFactory, PCDCP_FILE_PATTERN +from ..residual import WebAbsolutesFactory, CalFileFactory + +CAL_FILENAME_FORMAT = "{OBSERVATORY}/{OBSERVATORY}{YEAR}PCD.cal" +MIN_TEMPLATE = "file://c:/USGSDCP/%(OBS)s/" + PCDCP_FILE_PATTERN +RAW_TEMPLATE = "%(OBS)s/" + PCDCP_FILE_PATTERN + + +def main(): + typer.run(prepfiles) + + +def prepfiles(observatory: str, year: int, month: int): + starttime = datetime(year, month, 1) + endtime = starttime + relativedelta(months=+1) + + write_cal_file(starttime, endtime, observatory) + + +def write_cal_file( + starttime, endtime, observatory, base_directory="file://c:/Calibrat/" +): + filename = CAL_FILENAME_FORMAT.format(OBSERVATORY=observatory, YEAR=starttime.year) + starttime = starttime + relativedelta(months=-1) + endtime = endtime + relativedelta(months=+2) + starttime = UTCDateTime( + year=starttime.year, month=starttime.month, day=starttime.day + ) + endtime = UTCDateTime(year=endtime.year, month=endtime.month, day=endtime.day) + filename = CAL_FILENAME_FORMAT.format(OBSERVATORY=observatory, YEAR=starttime.year) + readings = WebAbsolutesFactory().get_readings( + observatory=observatory, + starttime=starttime, + endtime=endtime, + include_measurements=True, + ) + + calfile = CalFileFactory().format_readings(readings=readings) + + with open(base_directory + filename, "wb") as f: + f.write(calfile) + + +def write_raw_file(starttime, endtime, observatory, base_directory="file://c:/RAW/"): + starttime = UTCDateTime( + year=starttime.year, month=starttime.month, day=starttime.day + ) + endtime = UTCDateTime(year=endtime.year, month=endtime.month, day=endtime.day) + + channels = ["H", "E", "Z", "F"] + + edge_factory = EdgeFactory() + raw_timeseries = edge_factory.get_timeseries( + observatory=observatory, + starttime=starttime, + endtime=endtime, + channels=channels, + interval="second", + type="variation", + ) + + raw_factory = PCDCPFactory( + observatory=observatory, + channels=channels, + type="variation", + interval="second", + urlInterval=86400, + urlTemplate=base_directory + f"{RAW_TEMPLATE}", + ) + + raw_factory.put_timeseries( + timeseries=raw_timeseries, + starttime=starttime, + endtime=endtime, + interval="second", + type="variation", + channels=channels, + ) + + +def write_min_file(starttime, endtime, observatory, base_directory="file://c:/MIN/"): + starttime = UTCDateTime( + year=starttime.year, month=starttime.month, day=starttime.day + ) + endtime = UTCDateTime(year=endtime.year, month=endtime.month, day=endtime.day) + + channels = ["H", "E", "Z", "F"] + + min_timeseries = EdgeFactory().get_timeseries( + observatory=observatory, + starttime=starttime, + endtime=endtime, + channels=channels, + interval="minute", + type="variation", + ) + + min_factory = PCDCPFactory( + observatory=observatory, + channels=channels, + type="variation", + interval="minute", + urlInterval=86400, + urlTemplate=base_directory + f"{MIN_TEMPLATE}", + ) + + min_factory.put_timeseries( + timeseries=min_timeseries, + starttime=starttime, + endtime=endtime, + interval="minute", + type="variation", + channels=channels, + ) diff --git a/setup.py b/setup.py index 54917b5a..b923235e 100644 --- a/setup.py +++ b/setup.py @@ -21,4 +21,7 @@ setuptools.setup( scripts=["bin/geomag.py", "bin/geomag_webservice.py", "bin/make_cal.py"], setup_requires=["setuptools-pipfile",], use_pipfile=True, + entry_points={ + "console_scripts": ["magproc-prepfiles=geomagio.processing.magproc:main"], + }, ) -- GitLab