Replies: 1 comment
-
Hi @benaplus1, Instead of doing oldcoords = ds.coords
newcoords = oldcoords.to_dataset().drop_dims(["patch", "soillev", "snowlev"]).coords is there anything that prevents you doing newcoords = ds.drop_dims(["patch", "soillev", "snowlev"]).coords which doesn't have the extra step? |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hi all,
Sorry if this question has already been answered somewhere else before. I'm trying to create a Dataset to hold data from processed output from a weather model.
There are several different variables from the weather model. Some are 3d variables (dimensions [z,y,x]), some are 2d (dimensions [y,x]), some are "2d-patch" (dimensions [patch,y,x]), some are "2d-soil" (dimensions [soillevel, y, x]), and some are "2d-snow" (dimensions [snowlevel, y, x]). These last three are surface variables run by the vegetation, soil, and snow code, respectively.
To hold all the variables in a single dataset, I would create a Dataset with the following coordinates: {"z": zvals, "y": yvals, "x": xvals, "lat2d": ([y,x], glats), "lon2d": ([y,x], glons), "soillev": soillevs, "snowlev": snowlevs, "pnum": np.arange(0,patchcount)}.
(These aren't the actual coordinate names, it's just an example)
Then, I want to use the coordinates from this dataset to hold data processed in a different way (so the two datasets will have data with different values). However, for this second dataset I just want the 3d data, not any of the surface variables. I'd like to reuse the coordinates by grabbing oldcoords = Dataset.coords, and then assigning the new Dataset just the 3d coordinates by doing
newDataset = xr.Dataset(coords = oldcoords.drop_dims(["patch", "soillev", "snowlev"])),
so I just have the ["z", "y", "x", "lat2d", "lon2d"] coordinates that apply to 3d data. However, there doesn't seem to be a "drop_dims" method for Coordinates objects, just for Datasets and DataArrays. So that means I have to do
newcoords = oldcoords.to_dataset().drop_dims(["patch", "soillev", "snowlev"]).coords,
which seems kind of silly! I guess it's only one line, but it seems like an unnecessary extra step if, for instance, you were going to make several different datasets data of different dimensions.
I'm using Python 3.10, and xarray version 2023.2.0
If anyone is familiar with another way of doing this, please let me know! Also let me know if any of the above explanation is unclear, and I can clarify it. Thanks a lot for your help!
Beta Was this translation helpful? Give feedback.
All reactions