{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Compute the H/V spectral ratio from the imaginary part of the CCFs\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Import & getting the data from the computed autocorrelations (ZZ, EE, NN)\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "source": [ "import os\nif \"SPHINX_DOC_BUILD\" in os.environ:\n if \"MSNOISE_DOC\" in os.environ:\n os.chdir(os.environ[\"MSNOISE_DOC\"])\n\nimport matplotlib\nmatplotlib.use(\"agg\")\n\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport pandas as pd\nfrom pandas.plotting import register_matplotlib_converters\nimport datetime\nregister_matplotlib_converters()\n\nplt.style.use(\"ggplot\")\n\nfrom msnoise.api import connect, get_results, build_movstack_datelist, get_params, get_t_axis, xr_get_ccf\n\n\n# connect to the database\ndb = connect()\n\n# Obtain a list of dates between ``start_date`` and ``enddate``\nstart, end, datelist = build_movstack_datelist(db)\n\n# Get the list of parameters from the DB:\nparams = get_params(db)\n\n# Get the time axis for plotting the CCF:\ntaxis = get_t_axis(db)\n\n# Get the first mov_stack configured:\nmov_stack = params.mov_stack[0]\n\n# Get the results for two station, filter id=1, ZZ component, mov_stack=1 and the results as a 2D array:\nZZ = xr_get_ccf(\"PF.FJS.00\", \"PF.FJS.00\", \"ZZ\", 2, mov_stack, taxis, format=\"dataframe\")\nEE = xr_get_ccf(\"PF.FJS.00\", \"PF.FJS.00\", \"EE\", 2, mov_stack, taxis, format=\"dataframe\")\nNN = xr_get_ccf(\"PF.FJS.00\", \"PF.FJS.00\", \"NN\", 2, mov_stack, taxis, format=\"dataframe\")" ], "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Checking the data has the same time base (index) and joining\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "source": [ "r = \"1D\"\nrZZ = ZZ.resample(r).mean()\nrEE = EE.resample(r).mean()\nrNN = NN.resample(r).mean()\n\nmerged = pd.concat({'ZZ': rZZ, 'EE': rEE, 'NN': rNN}, axis=0)\nmerged.index.names = ['channel', 'date']\n\n# Swap the levels of the MultiIndex\nresult = merged.swaplevel('channel', 'date').sort_index(level='date') #.iloc[:500]" ], "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Helper functions\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "source": [ "def get_imag(d, fs):\n NFFT = 1024*32\n iX = np.imag(np.fft.fft(d, n=NFFT)[:NFFT//2])\n freqs = np.fft.fftfreq(NFFT, d=1/fs)[:NFFT//2]\n\n return freqs, iX" ], "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Doing the job !\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "source": [ "HVSRs = {}\nfor date, group in result.groupby(level='date'):\n print(f\"Date: {date}\")\n group= group.droplevel(0)\n try:\n f, iZZ = get_imag(group.loc[\"ZZ\"].values, 20)\n f, iEE = get_imag(group.loc[\"EE\"].values, 20)\n f, iNN = get_imag(group.loc[\"NN\"].values, 20)\n except:\n continue\n hvsr = np.sqrt((iEE+iNN)/iZZ)\n HVSRs[date] = hvsr\n\nHVSR = pd.DataFrame(HVSRs, index=f)\nX = HVSR.copy().fillna(0.0)\nX = X.T.resample(r).mean().T" ], "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Plotting\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "source": [ "plt.subplots(figsize=(8,10))\nplt.pcolormesh(X.index, X.columns, X.T, rasterized=True, vmax=5)\nplt.xlabel(\"Frequency (Hz)\")\nplt.ylabel(\"Date\")\nplt.grid()\nplt.xlim(1,10)\nplt.show()" ], "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Plotting & zooming around 1-4 Hz\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "source": [ "plt.subplots(figsize=(8,10))\nplt.pcolormesh(X.index, X.columns, X.T, rasterized=True, vmax=3)\nplt.xlabel(\"Frequency (Hz)\")\nplt.ylabel(\"Date\")\nplt.grid()\nplt.xlim(1,4)\nplt.show()" ], "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Plotting the HVSR curve\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "source": [ "plt.subplots(figsize=(8,10))\nhvsr = X.mean(axis=1)\nplt.plot(hvsr.index, hvsr)\nplt.show()\n\n\n#EOF" ], "outputs": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.8" } }, "nbformat": 4, "nbformat_minor": 0 }