read hydrophone data in .mseed format


In [1]:
import urllib
import obspy
import os

In [2]:
# read some hydrophone data from the Oregon Shelf Cabled Benthic Experiment Package
url='https://rawdata.oceanobservatories.org/files/CE02SHBP/LJ01D/11-HYDBBA106/2017/08/19/OO-HYEA2--YDH-2017-08-19T23:55:00.000000.mseed'
file_name='sample.mseed'

if not os.path.exists(file_name):
    urllib.request.urlretrieve(url, file_name)
    
st = obspy.read(file_name)

In [3]:
print(st)


1 Trace(s) in Stream:
OO.HYEA2..YDH | 2017-08-19T23:55:00.000000Z - 2017-08-19T23:59:59.998984Z | 64000.0 Hz, 19199936 samples

In [4]:
st.plot()


Select 1st stream


In [5]:
tr = st[0]

In [6]:
tr.stats


Out[6]:
         network: OO
         station: HYEA2
        location: 
         channel: YDH
       starttime: 2017-08-19T23:55:00.000000Z
         endtime: 2017-08-19T23:59:59.998984Z
   sampling_rate: 64000.0
           delta: 1.5625e-05
            npts: 19199936
           calib: 1.0
         _format: MSEED
           mseed: AttribDict({'dataquality': 'D', 'encoding': 'STEIM2', 'filesize': 33697792, 'record_length': 4096, 'byteorder': '>', 'number_of_records': 8227})

In [7]:
%matplotlib inline
import matplotlib.pyplot as plt
plt.plot(tr.times(), tr.data);



In [8]:
import numpy as np
start = np.datetime64(tr.stats.starttime)
print(start)


2017-08-19T23:55:00.000000

In [9]:
dt_nanosecs = 1.e9/tr.stats.sampling_rate
print(dt_nanosecs)


15625.0

In [10]:
import pandas as pd

In [11]:
jd = pd.date_range(start, 
              periods=tr.stats.npts, 
              freq='{}ns'.format(dt_nanosecs))

In [12]:
df = pd.DataFrame(tr.data, index=jd, columns=['stream1'])

In [13]:
df.head(3)


Out[13]:
stream1
2017-08-19 23:55:00.000000000 -15004
2017-08-19 23:55:00.000015625 -14783
2017-08-19 23:55:00.000031250 -14817

In [14]:
df.tail(3)


Out[14]:
stream1
2017-08-19 23:59:59.998953125 -14603
2017-08-19 23:59:59.998968750 -15246
2017-08-19 23:59:59.998984375 -14873

In [15]:
df.describe()


Out[15]:
stream1
count 1.919994e+07
mean -1.368198e+04
std 2.559722e+03
min -2.398500e+04
25% -1.547500e+04
50% -1.367300e+04
75% -1.186900e+04
max -3.901000e+03

Try writing data as netcdf file


In [ ]:
import xarray as xr
nc = xr.Dataset.from_dataframe(df)

In [28]:
nc.to_netcdf('foo.nc', format='netcdf4', 
             encoding={'stream1':{'complevel':4, 'chunksizes':{'index':10}}})


---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-28-d9712b317959> in <module>()
      1 nc.to_netcdf('foo.nc', format='netcdf4', 
----> 2              encoding={'stream1':{'complevel':4, 'chunksizes':{'index':10}}})

~\AppData\Local\Continuum\Miniconda3\envs\obspy\lib\site-packages\xarray\core\dataset.py in to_netcdf(self, path, mode, format, group, engine, encoding, unlimited_dims)
    975         return to_netcdf(self, path, mode, format=format, group=group,
    976                          engine=engine, encoding=encoding,
--> 977                          unlimited_dims=unlimited_dims)
    978 
    979     def __unicode__(self):

~\AppData\Local\Continuum\Miniconda3\envs\obspy\lib\site-packages\xarray\backends\api.py in to_netcdf(dataset, path_or_file, mode, format, group, engine, writer, encoding, unlimited_dims)
    571     try:
    572         dataset.dump_to_store(store, sync=sync, encoding=encoding,
--> 573                               unlimited_dims=unlimited_dims)
    574         if path_or_file is None:
    575             return target.getvalue()

~\AppData\Local\Continuum\Miniconda3\envs\obspy\lib\site-packages\xarray\core\dataset.py in dump_to_store(self, store, encoder, sync, encoding, unlimited_dims)
    914 
    915         store.store(variables, attrs, check_encoding,
--> 916                     unlimited_dims=unlimited_dims)
    917         if sync:
    918             store.sync()

~\AppData\Local\Continuum\Miniconda3\envs\obspy\lib\site-packages\xarray\backends\common.py in store(self, variables, attributes, *args, **kwargs)
    244         cf_variables, cf_attrs = cf_encoder(variables, attributes)
    245         AbstractWritableDataStore.store(self, cf_variables, cf_attrs,
--> 246                                         *args, **kwargs)
    247 
    248 

~\AppData\Local\Continuum\Miniconda3\envs\obspy\lib\site-packages\xarray\backends\common.py in store(self, variables, attributes, check_encoding_set, unlimited_dims)
    213         self.set_attributes(attributes)
    214         self.set_variables(variables, check_encoding_set,
--> 215                            unlimited_dims=unlimited_dims)
    216 
    217     def set_attributes(self, attributes):

~\AppData\Local\Continuum\Miniconda3\envs\obspy\lib\site-packages\xarray\backends\netCDF4_.py in set_variables(self, *args, **kwargs)
    286     def set_variables(self, *args, **kwargs):
    287         with self.ensure_open(autoclose=False):
--> 288             super(NetCDF4DataStore, self).set_variables(*args, **kwargs)
    289 
    290     def prepare_variable(self, name, variable, check_encoding=False,

~\AppData\Local\Continuum\Miniconda3\envs\obspy\lib\site-packages\xarray\backends\common.py in set_variables(self, variables, check_encoding_set, unlimited_dims)
    225             check = vn in check_encoding_set
    226             target, source = self.prepare_variable(
--> 227                 name, v, check, unlimited_dims=unlimited_dims)
    228             self.writer.add(source, target)
    229 

~\AppData\Local\Continuum\Miniconda3\envs\obspy\lib\site-packages\xarray\backends\netCDF4_.py in prepare_variable(self, name, variable, check_encoding, unlimited_dims)
    322             endian='native',
    323             least_significant_digit=encoding.get('least_significant_digit'),
--> 324             fill_value=fill_value)
    325         nc4_var.set_auto_maskandscale(False)
    326 

netCDF4\_netCDF4.pyx in netCDF4._netCDF4.Dataset.createVariable()

netCDF4\_netCDF4.pyx in netCDF4._netCDF4.Variable.__init__()

KeyError: 0

In [ ]: