Skip to content

Commit

Permalink
refactor: update rm_resp from str to enum
Browse files Browse the repository at this point in the history
  • Loading branch information
anujsinha3 committed Nov 18, 2023
1 parent 92d86a9 commit 9f2387d
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 18 deletions.
3 changes: 2 additions & 1 deletion src/noisepy/seis/S0B_to_ASDF_2019.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from mpi4py import MPI

from . import noise_module
from .datatypes import RmResp

if not sys.warnoptions:
import warnings
Expand Down Expand Up @@ -57,7 +58,7 @@
input_fmt = "sac" # input file format between 'sac' and 'mseed'
samp_freq = 10 # targeted sampling rate
stationxml = False # station.XML file exists or not
rm_resp = "no" # select 'no' to not remove response and
rm_resp = RmResp.NO # select 'no' to not remove response and
# use 'inv','spectrum','RESP', or 'polozeros' to remove response
respdir = os.path.join(
rootpath, "resp"
Expand Down
12 changes: 11 additions & 1 deletion src/noisepy/seis/datatypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,14 @@ class TimeNorm(str, Enum):
RMA = "rma"


class RmResp(str, Enum):
NO = "no"
INV = "inv"
SPECTRUM = "spectrum"
POLES_ZEROS = "poleszeros"
RESP = "RESP"


class ConfigParameters(BaseModel):
model_config = ConfigDict(validate_default=True)

Expand Down Expand Up @@ -200,7 +208,9 @@ class ConfigParameters(BaseModel):
stationxml: bool = Field(
default=False, description="station.XML file used to remove instrument response for SAC/miniseed data"
)
rm_resp: str = Field(default="inv", description="select 'no' to not remove response and use 'inv','spectrum',")
rm_resp: RmResp = Field(
default=RmResp.INV.value, description="select 'no' to not remove response and use 'inv','spectrum',"
)
rm_resp_out: str = Field(default="VEL", description="output location from response removal")
respdir: Optional[str] = Field(default=None, description="response directory")
# some control parameters
Expand Down
13 changes: 7 additions & 6 deletions src/noisepy/seis/noise_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
ChannelData,
ConfigParameters,
FreqNorm,
RmResp,
StackMethod,
TimeNorm,
)
Expand Down Expand Up @@ -242,12 +243,12 @@ def preprocess_raw(
# remove traces of too small length

# options to remove instrument response
if rm_resp != "no":
if rm_resp != "inv":
if rm_resp != RmResp.NO:
if rm_resp != RmResp.INV:
if (respdir is None) or (not os.path.isdir(respdir)):
raise ValueError("response file folder not found! abort!")

if rm_resp == "inv":
if rm_resp == RmResp.INV:
# ----check whether inventory is attached----
if not inv[0][0][0].response:
raise ValueError("no response found in the inventory! abort!")
Expand All @@ -263,14 +264,14 @@ def preprocess_raw(
st = []
return st

elif rm_resp == "spectrum":
elif rm_resp == RmResp.SPECTRUM:
logger.info("remove response using spectrum")
specfile = glob.glob(os.path.join(respdir, "*" + station + "*"))
if len(specfile) == 0:
raise ValueError("no response sepctrum found for %s" % station)
st = resp_spectrum(st, specfile[0], samp_freq, pre_filt)

elif rm_resp == "RESP":
elif rm_resp == RmResp.RESP:
logger.info("remove response using RESP files")
resp = glob.glob(os.path.join(respdir, "RESP." + station + "*"))
if len(resp) == 0:
Expand All @@ -282,7 +283,7 @@ def preprocess_raw(
}
st.simulate(paz_remove=None, pre_filt=pre_filt, seedresp=seedresp)

elif rm_resp == "poleszeros":
elif rm_resp == RmResp.POLES_ZEROS:
logger.info("remove response using poles and zeros")
paz_sts = glob.glob(os.path.join(respdir, "*" + station + "*"))
if len(paz_sts) == 0:
Expand Down
10 changes: 8 additions & 2 deletions tests/test_cross_correlation.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@
_safe_read_data,
cross_correlate,
)
from noisepy.seis.datatypes import Channel, ChannelData, ConfigParameters, Station
from noisepy.seis.datatypes import (
Channel,
ChannelData,
ConfigParameters,
RmResp,
Station,
)
from noisepy.seis.scedc_s3store import SCEDCS3DataStore


Expand Down Expand Up @@ -57,7 +63,7 @@ def test_correlation_nodata():
def test_correlation():
config = ConfigParameters()
config.samp_freq = 1.0
config.rm_resp = "no" # since we are using a mock catalog
config.rm_resp = RmResp.NO # since we are using a mock catalog
path = os.path.join(os.path.dirname(__file__), "./data/cc")
raw_store = SCEDCS3DataStore(path, MockCatalog())
ts = raw_store.get_timespans()
Expand Down
4 changes: 2 additions & 2 deletions tutorials/get_started.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
"source": [
"from noisepy.seis import download, cross_correlate, stack_cross_correlations, plotting_modules, __version__\n",
"from noisepy.seis.asdfstore import ASDFRawDataStore, ASDFCCStore, ASDFStackStore\n",
"from noisepy.seis.datatypes import CCMethod, ConfigParameters, FreqNorm, TimeNorm\n",
"from noisepy.seis.datatypes import CCMethod, ConfigParameters, FreqNorm, RmResp, TimeNorm\n",
"from dateutil.parser import isoparse\n",
"import os\n",
"import shutil\n",
Expand Down Expand Up @@ -114,7 +114,7 @@
" # pre-processing parameters\n",
"config.step= 1800.0 # (float) overlapping between each cc_len (sec)\n",
"config.stationxml= False # station.XML file used to remove instrument response for SAC/miniseed data\n",
"config.rm_resp= \"inv\" # select 'no' to not remove response and use 'inv' if you use the stationXML,'spectrum',\n",
"config.rm_resp= RmResp.INV # select 'no' to not remove response and use 'inv' if you use the stationXML,'spectrum',\n",
"config.freqmin = 0.05\n",
"config.freqmax = 2.0\n",
"config.max_over_std = 10 # threshold to remove window of bad signals: set it to 10*9 if prefer not to remove them\n",
Expand Down
4 changes: 2 additions & 2 deletions tutorials/noisepy_pnwstore_tutorial.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
"from noisepy.seis.asdfstore import ASDFCCStore, ASDFStackStore # Object to store ASDF data within noisepy\n",
"from noisepy.seis.scedc_s3store import channel_filter\n",
"from noisepy.seis.pnwstore import PNWDataStore\n",
"from noisepy.seis.datatypes import CCMethod, ConfigParameters, Channel, ChannelData, ChannelType, FreqNorm, Station, TimeNorm # Main configuration object\n",
"from noisepy.seis.datatypes import CCMethod, ConfigParameters, Channel, ChannelData, ChannelType, FreqNorm, RmResp, Station, TimeNorm # Main configuration object\n",
"from noisepy.seis.channelcatalog import XMLStationChannelCatalog # Required stationXML handling object\n",
"import os\n",
"from datetime import datetime\n",
Expand Down Expand Up @@ -179,7 +179,7 @@
" # pre-processing parameters\n",
"config.step= 1800.0 # (float) overlapping between each cc_len (sec)\n",
"config.stationxml= False # station.XML file used to remove instrument response for SAC/miniseed data\n",
"config.rm_resp= \"inv\" # select 'no' to not remove response and use 'inv' if you use the stationXML,'spectrum',\n",
"config.rm_resp= RmResp.INV # select 'no' to not remove response and use 'inv' if you use the stationXML,'spectrum',\n",
"config.freqmin = 0.05\n",
"config.freqmax = 2.0\n",
"config.max_over_std = 10 # threshold to remove window of bad signals: set it to 10*9 if prefer not to remove them\n",
Expand Down
4 changes: 2 additions & 2 deletions tutorials/noisepy_scedc_tutorial.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
"from noisepy.seis import cross_correlate, stack_cross_correlations, __version__ # noisepy core functions\n",
"from noisepy.seis.asdfstore import ASDFCCStore, ASDFStackStore # Object to store ASDF data within noisepy\n",
"from noisepy.seis.scedc_s3store import SCEDCS3DataStore, channel_filter # Object to query SCEDC data from on S3\n",
"from noisepy.seis.datatypes import CCMethod, ConfigParameters, FreqNorm, StackMethod, TimeNorm # Main configuration object\n",
"from noisepy.seis.datatypes import CCMethod, ConfigParameters, FreqNorm, RmResp, StackMethod, TimeNorm # Main configuration object\n",
"from noisepy.seis.channelcatalog import XMLStationChannelCatalog # Required stationXML handling object\n",
"import os\n",
"from datetime import datetime, timezone\n",
Expand Down Expand Up @@ -196,7 +196,7 @@
"\n",
"config.stationxml= False # station.XML file used to remove instrument response for SAC/miniseed data\n",
" # If True, the stationXML file is assumed to be provided.\n",
"config.rm_resp= \"inv\" # select 'no' to not remove response and use 'inv' if you use the stationXML,'spectrum',\n",
"config.rm_resp= RmResp.INV # select 'no' to not remove response and use 'inv' if you use the stationXML,'spectrum',\n",
"\n",
"\n",
"############## NOISE PRE-PROCESSING ##################\n",
Expand Down
4 changes: 2 additions & 2 deletions tutorials/old/download_toASDF_cross_correlation.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
"import pandas as pd\n",
"from obspy import UTCDateTime\n",
"import matplotlib.pyplot as plt\n",
"from noisepy.seis.datatypes import CCMethod, FreqNorm, TimeNorm\n",
"from noisepy.seis.datatypes import CCMethod, FreqNorm, RmResp, TimeNorm\n",
"from obspy.clients.fdsn import Client\n",
"\n",
"\n",
Expand All @@ -95,7 +95,7 @@
"# download parameters\n",
"client = Client('IRIS') # client/data center. see https://docs.obspy.org/packages/obspy.clients.fdsn.html for a list\n",
"samp_freq = 1 # targeted sampling rate at X samples per seconds \n",
"rm_resp = 'no' # select 'no' to not remove response and use 'inv','spectrum','RESP', or 'polozeros' to remove response\n",
"rm_resp = RmResp.NO # select 'no' to not remove response and use 'inv','spectrum','RESP', or 'polozeros' to remove response\n",
"respdir = './' # directory where resp files are located (required if rm_resp is neither 'no' nor 'inv')\n",
"freqmin = 0.05 # pre filtering frequency bandwidth\n",
"freqmax = 0.4 # note this cannot exceed Nquist freq \n",
Expand Down

0 comments on commit 9f2387d

Please sign in to comment.