home / github / issue_comments

Menu
  • GraphQL API
  • Search all tables

issue_comments: 300381278

This data as json

html_url issue_url id node_id user created_at updated_at author_association body reactions performed_via_github_app issue
https://github.com/pydata/xarray/issues/1391#issuecomment-300381278 https://api.github.com/repos/pydata/xarray/issues/1391 300381278 MDEyOklzc3VlQ29tbWVudDMwMDM4MTI3OA== 6980561 2017-05-10T05:52:21Z 2017-05-10T05:56:15Z NONE

I have an example that I just struggled through that might be relevant to this idea. I'm running a point model using some arbitrary number of experiments (for the below example there are 28 experiments). Each experiment is opened and then stored in a dictionary resultsDict. The below excerpt extracts all of my scalar variables, concatenates them along an experiment dimension, and finally combines all scalar variables into a DataSet. I often find myself struggling to combine data (for instance meteorological stations) into a DataSet and I can never remember how to use merge and/or concat.

``` resultsDataSet = xr.Dataset() for k in scalar_data_vars: if not 'scalar' in k: continue

# Assign scalar value to a dataArray
darray = xr.concat([resultsDict[scen][scalar_data_vars[0]] for scen in resultsDict], dim='expID')
# Remove hru dimension, as it is unused
darray = darray.squeeze('hru')

resultsDataSet[k] = darray

print(resultsDataSet) which yields <xarray.Dataset> Dimensions: (expID: 28, time: 8041) Coordinates: * time (time) datetime64[ns] 2008-10-01 ... hru int32 1 Dimensions without coordinates: expID Data variables: scalarRainPlusMelt (expID, time) float64 -9.999e+03 -9.999e+03 ... scalarSWE (expID, time) float64 -9.999e+03 -9.999e+03 ... scalarSnowSublimation (expID, time) float64 -9.999e+03 -9.999e+03 ... scalarInfiltration (expID, time) float64 -9.999e+03 -9.999e+03 ... scalarSurfaceRunoff (expID, time) float64 -9.999e+03 -9.999e+03 ... scalarSurfaceTemp (expID, time) float64 -9.999e+03 -9.999e+03 ... scalarSenHeatTotal (expID, time) float64 -9.999e+03 -9.999e+03 ... scalarLatHeatTotal (expID, time) float64 -9.999e+03 -9.999e+03 ... scalarSnowDepth (expID, time) float64 -9.999e+03 -9.999e+03 ... ```

And here is a helper function that can do this more generally, which I wrote a while back.

``` def combinevars(ds_in, dat_vars, new_dim_name='new_dim', combinevarname='new_var'): ds_out = xr.Dataset() ds_out = xr.concat([ds_in[dv] for dv in dat_vars], dim='new_dim') ds_out = ds_out.rename({'new_dim': new_dim_name}) ds_out.coords[new_dim_name] = dat_vars ds_out.name = combinevarname

return ds_out

```

{
    "total_count": 0,
    "+1": 0,
    "-1": 0,
    "laugh": 0,
    "hooray": 0,
    "confused": 0,
    "heart": 0,
    "rocket": 0,
    "eyes": 0
}
  225536793
Powered by Datasette · Queries took 0.721ms · About: xarray-datasette