import holoviews as hv
from bokeh.models import HoverTool
from holoviews import opts
import xarray as xr
hv.extension("bokeh")
url = "https://thredds.met.no/thredds/dodsC/met.no/observations/stations/SN99710.nc"
rawdata = xr.load_dataset(url)
tmp = rawdata["air_temperature_2m"].where(rawdata["time.year"] >= 1979, drop=True)
data = tmp.where(tmp < 300)
grid_style = {
"grid_line_color": "black",
"grid_line_width": 1.1,
"minor_ygrid_line_color": "lightgray",
"minor_xgrid_line_color": "lightgray",
"xgrid_line_dash": [4, 4],
}
hv.Curve((data.time, data.variable), label='SN99710').opts(
tools=[
HoverTool(
tooltips=[
("Station", "SN99710"),
("Date", "$x{%F}"),
("Temp", "$y"),
],
toggleable=False,
formatters={"$x": "datetime"},
)
],
xrotation=45,
width=900,
height=400,
gridstyle=grid_style,
show_grid=True,
)
data
<xarray.DataArray 'air_temperature_2m' (time: 281939)> array([266.05, 267.25, 266.95, ..., nan, nan, nan], dtype=float32) Coordinates: * time (time) datetime64[ns] 1979-01-01 ... 2021-03-25T18:00:00 Attributes: long_name: Air temperature coverage_content_type: coordinate standard_name: air_temperature units: K _ChunkSizes: 1024
array([266.05, 267.25, 266.95, ..., nan, nan, nan], dtype=float32)
array(['1979-01-01T00:00:00.000000000', '1979-01-01T06:00:00.000000000', '1979-01-01T12:00:00.000000000', ..., '2021-03-25T16:00:00.000000000', '2021-03-25T17:00:00.000000000', '2021-03-25T18:00:00.000000000'], dtype='datetime64[ns]')
type(data)
xarray.core.dataarray.DataArray
data.resample(time='M').mean()
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-6-b337c68024d7> in <module> ----> 1 data.resample(time='M').mean() /opt/conda/lib/python3.8/site-packages/xarray/core/common.py in resample(self, indexer, skipna, closed, label, base, keep_attrs, loffset, restore_coord_dims, **indexer_kwargs) 1152 dim_coord, coords=dim_coord.coords, dims=dim_coord.dims, name=RESAMPLE_DIM 1153 ) -> 1154 resampler = self._resample_cls( 1155 self, 1156 group=group, /opt/conda/lib/python3.8/site-packages/xarray/core/resample.py in __init__(self, dim, resample_dim, *args, **kwargs) 174 self._resample_dim = resample_dim 175 --> 176 super().__init__(*args, **kwargs) 177 178 def map(self, func, shortcut=False, args=(), **kwargs): /opt/conda/lib/python3.8/site-packages/xarray/core/groupby.py in __init__(self, obj, group, squeeze, grouper, bins, restore_coord_dims, cut_kwargs) 349 if not index.is_monotonic: 350 # TODO: sort instead of raising an error --> 351 raise ValueError("index must be monotonic for resampling") 352 full_index, first_items = self._get_index_and_items(index, grouper) 353 sbins = first_items.values.astype(np.int64) ValueError: index must be monotonic for resampling
data.to_pandas().resample('M').mean()
time 1979-01-31 262.621765 1979-02-28 265.744629 1979-03-31 267.963715 1979-04-30 266.454987 1979-05-31 269.750000 ... 2020-11-30 NaN 2020-12-31 NaN 2021-01-31 270.839111 2021-02-28 269.285706 2021-03-31 267.659088 Freq: M, Length: 507, dtype: float32