Skip to content

Instantly share code, notes, and snippets.

@joefutrelle
Created March 11, 2017 19:33
Show Gist options
  • Save joefutrelle/b80633f5b2e60df180aa115512babd6a to your computer and use it in GitHub Desktop.
Save joefutrelle/b80633f5b2e60df180aa115512babd6a to your computer and use it in GitHub Desktop.
Example DataFrame roundtripping for OrthogonalMultidimensionalTimeseriesProfile
import datetime
import numpy as np
import random
import pandas as pd
# construct fake dataframe
then = datetime.datetime.utcnow()
n_times = 2
n_levels = 4
n_stations = 3
times = [then + datetime.timedelta(hours=h) for h in np.cumsum(np.random.uniform(size=n_times))]
stations = ['station{}'.format(i) for i in range(n_stations)]
levels = range(n_levels)
zs = np.cumsum(np.random.uniform(size=n_levels))
lats = np.random.uniform(size=n_stations) * 180 - 90
lons = np.random.uniform(size=n_stations) * 360 - 180
ob = range(n_times * n_stations * n_levels)
time = np.repeat(times, n_stations * n_levels)
z = np.tile(np.repeat(zs, n_stations), n_times)
station = np.tile(stations, n_levels * n_times)
lat = np.tile(lats, n_times * n_levels)
lon = np.tile(lons, n_times * n_levels)
d = dict(station=station, t=time, ob=ob, z=z, y=lat, x=lon)
df = pd.DataFrame(d)
print(df)
from pocean.dsg.timeseriesProfile.om import OrthogonalMultidimensionalTimeseriesProfile
omtp = OrthogonalMultidimensionalTimeseriesProfile.from_dataframe(df, 'omtp.nc')
df2 = omtp.to_dataframe()
print('')
print(df2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment