<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Pyadjoint
Pyadjoint is a Python package to measure misfits and calculate adjoint sources. It aims to provide a comprehensive package and incorporate various types of measurements and associated adjoint sources.
Installation
Pyadjoint utilizes ObsPy (and some of its dependencies) for the processing and data handling. As a first step, please follow the installation instructions of ObsPy for your given platform (we recommend the installation with Anaconda as it will most likely result in the least amount of problems). Pyadjoint should work with Python versions 2.7, 3.4, and 3.5. To install it, best use pip (not working yet!)
Step1:  Pyadjoint expects the data to be fully preprocessed thus both observed and synthetic data are expected to have exactly the same length, sampling rate, and spectral content. Pyadjoint furthermore does not care about the actual components in question; it will use two traces and calculate misfit values and adjoint sources for them. To provide a familiar nomenclature we will always talk about observed and synthetic data Pyadjoint is independent of what the data actually represents.
Step2:  Usage Options
In case one just wants to calculate the misfit value, pass adjoint_src=False in which case the adjoint source will not be calculated. This sometimes is much faster and useful for line searches or similar endeavors.
Step3:  All adjoint source types can also be plotted during the calculation. The type of plot produced depends on the type of misfit measurement and adjoint source.
Step4:  Saving to Disc
<ASSISTANT_TASK:>
Python Code:
import obspy
import pyadjoint
Explanation: Pyadjoint
Pyadjoint is a Python package to measure misfits and calculate adjoint sources. It aims to provide a comprehensive package and incorporate various types of measurements and associated adjoint sources.
Installation
Pyadjoint utilizes ObsPy (and some of its dependencies) for the processing and data handling. As a first step, please follow the installation instructions of ObsPy for your given platform (we recommend the installation with Anaconda as it will most likely result in the least amount of problems). Pyadjoint should work with Python versions 2.7, 3.4, and 3.5. To install it, best use pip (not working yet!):
bash
$ pip install pyadjoint
If you want the latest development version, or want to work on the code, you will have to install with the help of git.
bash
$ git clone https://github.com/krischer/pyadjoint.git
$ cd pyadjoint
$ pip install -v -e .
Tests
To assure the installation is valid and everything works as expected, run the tests with
bash
$ python -m pyadjoint.tests
Usage
Basic Usage
The first step is to import ObsPy and Pyadjoint.
End of explanation
# Helper function to get some example data used for
# illustrative purposes.
obs, syn = pyadjoint.utils.get_example_data()
# Select the vertical components of both.
obs = obs.select(component="Z")[0]
syn = syn.select(component="Z")[0]
adj_src = pyadjoint.calculate_adjoint_source(
    # The type of misfit measurement and adjoint source.
    adj_src_type="waveform_misfit",
    # Pass observed and synthetic data traces.
    observed=obs, synthetic=syn,
    # The spectral content of the data.
    min_period=20.0, max_period=100.0,
    # The window borders in seconds since the first sample.
    left_window_border=800.0,
    right_window_border=900.0)
print(adj_src)
# Access misfit and adjoint sources. The misfit is a floating point number.
print(adj_src.misfit)
# The adjoint source is a a numpy array.
print(adj_src.adjoint_source)
Explanation: Pyadjoint expects the data to be fully preprocessed thus both observed and synthetic data are expected to have exactly the same length, sampling rate, and spectral content. Pyadjoint furthermore does not care about the actual components in question; it will use two traces and calculate misfit values and adjoint sources for them. To provide a familiar nomenclature we will always talk about observed and synthetic data Pyadjoint is independent of what the data actually represents.
End of explanation
print(pyadjoint.calculate_adjoint_source(
    adj_src_type="waveform_misfit", observed=obs, synthetic=syn,
    min_period=20.0, max_period=100.0,
    left_window_border=800.0, right_window_border=900.0, adjoint_src=False))
Explanation: Usage Options
In case one just wants to calculate the misfit value, pass adjoint_src=False in which case the adjoint source will not be calculated. This sometimes is much faster and useful for line searches or similar endeavors.
End of explanation
pyadjoint.calculate_adjoint_source(
    adj_src_type="waveform_misfit", observed=obs, synthetic=syn,
    min_period=20.0, max_period=100.0,
    left_window_border=800.0, right_window_border=900.0, plot=True);
print(pyadjoint.calculate_adjoint_source(
    adj_src_type="waveform_misfit", observed=obs, synthetic=syn,
    min_period=20.0, max_period=100.0,
    left_window_border=800.0, right_window_border=900.0,
    taper_percentage=0.3, taper_type="cosine"))
Explanation: All adjoint source types can also be plotted during the calculation. The type of plot produced depends on the type of misfit measurement and adjoint source.
End of explanation
adj_src.write(filename="NET.STA.CHA.adj_src",
              format="SPECFEM", time_offset=-10)
!head NET.STA.CHA.adj_src
!rm NET.STA.CHA.adj_src
Explanation: Saving to Disc
End of explanation
<END_TASK>