Write a `TimeSeriesBaseDict` to HDF5

    Each series in the dict is written as a dataset in the group
422539
def write_hdf5_dict(tsdict, h5f, group=None, **kwargs):
    
    # create group if needed
    if group and group not in h5f:
        h5g = h5f.create_group(group)
    elif group:
        h5g = h5f[group]
    else:
        h5g = h5f

    # write each timeseries
    kwargs.setdefault('format', 'hdf5')
    for key, series in tsdict.items():
        series.write(h5g, path=str(key), **kwargs)
825809
def create_dataset(self, group, chunk_size):
        """"""
        group.attrs['format'] = self.dformat
        group.attrs['dim'] = self.dim

        if chunk_size == 'auto':
            group.create_dataset(
                'coordinates', (0, 2), dtype=np.float64,
                chunks=True, maxshape=(None, 2))

            group.create_dataset(
                self.name, (0,), dtype=self.dtype,
                chunks=True, maxshape=(None,))

        else:
            # for storing sparse data we don't use the self.nb_per_chunk,
            # which is only used by the Writer to determine times chunking.
            per_chunk = nb_per_chunk(self.dtype.itemsize, 1, chunk_size)

            group.create_dataset(
                'coordinates', (0, 2), dtype=np.float64,
                chunks=(per_chunk, 2), maxshape=(None, 2))

            group.create_dataset(
                self.name, (0,), dtype=self.dtype,
                chunks=(per_chunk,), maxshape=(None,))

        dtype = np.int64
        if chunk_size == 'auto':
            chunks = True
            self.nb_per_chunk = 'auto'
        else:
            chunks = (nb_per_chunk(np.dtype(dtype).itemsize, 1, chunk_size),)
            # Needed by Times.create_dataset
            self.nb_per_chunk = nb_per_chunk(
                self.dtype.itemsize,
                int(round(self.sparsity*self.dim)),
                chunk_size)

        group.create_dataset(
            'frames', (0,), dtype=dtype,
            chunks=chunks, maxshape=(None,))
557771
def write(self, symbol, data, prune_previous_version=True, metadata=None, **kwargs):
        
        if data is not None:
            # We only write data if existing data is None or the Timeseries data has changed or metadata has changed
            if self.base_ts.data is None or not are_equals(data, self.base_ts.data) or metadata != self.base_ts.metadata:
                self._do_write = True
        self._write = partial(self._version_store.write, symbol, data, prune_previous_version=prune_previous_version,
                              metadata=metadata, **kwargs)
99760
def dump_dict_of_nested_lists_to_h5(fname, data):
    
    # Open file
    print('writing to file: %s' % fname)
    f = h5py.File(fname)
    # Iterate over values
    for i, ivalue in list(data.items()):
        igrp = f.create_group(str(i))
        for j, jvalue in enumerate(ivalue):
            jgrp = igrp.create_group(str(j))
            for k, kvalue in enumerate(jvalue):
                if kvalue.size > 0:
                    dset = jgrp.create_dataset(str(k), data=kvalue,
                                               compression='gzip')
                else:
                    dset = jgrp.create_dataset(str(k), data=kvalue,
                                               maxshape=(None, ),
                                               compression='gzip')
    # Close file
    f.close()
1273872
def WriteClientGraphSeries(self, graph_series,
                             client_label,
                             timestamp):
    """"""
    series_key = (client_label, graph_series.report_type, timestamp.Copy())
    self.client_graph_series[series_key] = graph_series.Copy()
787109
def timeseries(datasets):
    """"""
    expanded_ds = []
    for ds in datasets:
        tmp = ds.expand_dims("time")
        tmp.coords["time"] = pd.DatetimeIndex([ds.attrs["start_time"]])
        expanded_ds.append(tmp)

    res = xr.concat(expanded_ds, dim="time")
    res.attrs = combine_metadata(*[x.attrs for x in expanded_ds])
    return res
90797
def save_hdf_metadata(filename, metadata, groupname="data", mode="a"):
    """""""
    with _h5py.File(filename, mode) as f:
        for key, val in metadata.items():
            f[groupname].attrs[key] = val
697721
def timeseries():
    
    def method_builder(cls):
        method_doc = """Fetch the timeseries for this :class:`{0}`.

        Returns:

            The :class:`Timeseries` for this :class:`{0}`

        Keyword Args:

            **kwargs: The :class:`Timeseries` object constructor arguments.
        """.format(cls.__name__)

        def method(self, **kwargs):
            resource_id = None if self.is_singleton() else self.id
            return Timeseries(self._session, cls, resource_id, **kwargs)

        method.__doc__ = method_doc
        setattr(cls, 'timeseries', method)
        return cls
    return method_builder
290064
def _gdal_write_datasets(self, dst_ds, datasets):
        """"""
        for i, band in enumerate(datasets['bands']):
            chn = datasets.sel(bands=band)
            bnd = dst_ds.GetRasterBand(i + 1)
            bnd.SetNoDataValue(0)
            bnd.WriteArray(chn.values)
818878
def _setup_postprocess_hds_timeseries(hds_file, df, config_file, prefix=None, model=None):
    """"""
    warnings.warn(
        "Setting up post processing of hds or ucn timeseries obs. "
        "Prepending 'pp' to obs name may cause length to exceed 20 chars", PyemuWarning)
    if model is not None:
        t_str = df.index.map(lambda x: x.strftime("%Y%m%d"))
    else:
        t_str = df.index.map(lambda x: "{0:08.2f}".format(x))
    if prefix is not None:
        prefix = "pp{0}".format(prefix)
    else:
        prefix = "pp"
    ins_file = hds_file+"_timeseries.post_processed.ins"
    print("writing instruction file to {0}".format(ins_file))
    with open(ins_file,'w') as f:
        f.write('pif ~\n')
        f.write("l1 \n")
        for t in t_str:
            f.write("l1 w ")
            for site in df.columns:
                obsnme = "{0}{1}_{2}".format(prefix, site, t)
                f.write(" !{0}!".format(obsnme))
            f.write('\n')
    frun_line = "pyemu.gw_utils._apply_postprocess_hds_timeseries('{0}')\n".format(config_file)
    return frun_line
38335
def as_dataframe(self, absolute_time=False):
        

        import pandas as pd

        # When absolute_time is True,
        # use the wf_start_time as offset for the time_track()
        try:
            time = self.time_track(absolute_time)
        except KeyError:
            time = None
        if self.channel is None:
            return pd.DataFrame.from_items(
                [(ch.channel, pd.Series(ch.data))
                    for ch in self.tdms_file.group_channels(self.group)])
        else:
            return pd.DataFrame(self._data, index=time, columns=[self.path])
587463
def write_data_set(file_path, dataset, output_dir, color_array_info, new_name=None, compress=True):
    """"""
    fileName = new_name if new_name else os.path.basename(file_path)
    dataset_dir = os.path.join(output_dir, fileName)
    data_dir = os.path.join(dataset_dir, 'data')

    if not os.path.exists(data_dir):
        os.makedirs(data_dir)

    root = {}
    root['metadata'] = {}
    root['metadata']['name'] = fileName

    writer = writer_mapping[dataset.GetClassName()]
    if writer:
        writer(dataset_dir, data_dir, dataset, color_array_info, root, compress)
    else:
        print(dataObject.GetClassName(), 'is not supported')

    with open(os.path.join(dataset_dir, "index.json"), 'w') as f:
        f.write(json.dumps(root, indent=2))

    return dataset_dir
572938
def make_dataframe(result):
    
    import pandas as pd
    ret = {}
    if isinstance(result,dict):
        if 'timeseries' in result:
            result = result['timeseries']
    for uuid, data in result.items():
        df = pd.DataFrame(data)
        if len(df.columns) == 5: # statistical data
            df.columns = ['time','min','mean','max','count']
        else:
            df.columns = ['time','value']
        df['time'] = pd.to_datetime(df['time'],unit='ns')
        df = df.set_index(df.pop('time'))
        ret[uuid] = df
    return ret
453551
def to_pandas(self):
        """"""
        agedepthdf = pd.DataFrame(self.age, index=self.data.depth)
        agedepthdf.columns = list(range(self.n_members()))
        out = (agedepthdf.join(self.data.set_index('depth'))
               .reset_index()
               .melt(id_vars=self.data.columns.values, var_name='mciter', value_name='age'))
        out['mciter'] = pd.to_numeric(out.loc[:, 'mciter'])
        if self.n_members() == 1:
            out = out.drop('mciter', axis=1)
        return out
459213
def write_to(self, group, append=False):
        """"""
        data = self.data
        if append is True:
            try:
                # concatenate original and new properties in a single list
                original = read_properties(group)
                data = original + data
            except EOFError:
                pass  # no former data to append on

        # h5py does not support embedded NULLs in strings ('\x00')
        data = pickle.dumps(data).replace(b'\x00', b'__NULL__')
        group['properties'][...] = np.void(data)
1165459
def forecast(self, horizon, **kwargs):
        ''''''
        return DataFrame({ name: series.forecast(horizon, **kwargs) \
            for name, series in self.groups.iteritems() })
1306998
def to_frame(self, **kwargs):
        
        """
        df = export.write_dataframe(self._values, **kwargs)
        df.name = self.title
        return df