pydata / pydata/xarray

'Time' dimensions not consistently saved when using 'to_netcdf'

Open
#5,552 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
4.2k
Forks
1.4k
Avg merge
2d 15h
Merged PRs (30d)
14

Description

What happened:
When using a for loop to subset 3 datasets, the second dataset only preserves 1 Time dimension while the first and third preserve all 4 Time dimensions. However, when subsetting the datasets individually (i.e. not using a for loop), the Time dimensions are all preserved for each file. I'm not sure if this is a bug or user error, but could possibly be related to #5549.

What you expected to happen:
Expected all three files to subset in the same capacity with same dimensions for variables.

Minimal Complete Verifiable Example:
Python code for the for loop to subset:

# Put your MCVE code here

wrf_path = '/Users/misi1684/wrf_python_tutorial/wrf_tutorial_data'
os.chdir(wrf_path)
wrf_file0 = glob.glob('wrf*')

toinclude = ['HGT', 'QVAPOR', 'QRAIN', 'QCLOUD', 'P', 'PB', 'PHB', 'PH', 'T', 'U', 'V', 'W']

for i in range(0,3):
	wrf_file = wrf_file0[i]
	wrf_out = xr.open_dataset(wrf_file, decode_times=False)
	# print(wrf_out)
	wrf_out = wrf_out[toinclude].to_netcdf('wrf_output_subset' + str(i) + '.nc')

Anything else we need to know?:
Incase you are not familiar with a wrfout file, it is the same as a netcdf file. Generally, I used netCDF Dataset to read in these types of files, but netCDF does not have the same subsetting capabilities as xarray. If you need more information or printouts from these datasets, please let me know!

Environment:

Output of xr.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.8.10 | packaged by conda-forge | (default, May 11 2021, 06:39:48)
[Clang 11.1.0 ]
python-bits: 64
OS: Darwin
OS-release: 19.6.0
machine: x86_64
processor: i386
byteorder: little
LC_ALL: None
LANG: None
LOCALE: (None, 'UTF-8')
libhdf5: 1.10.6
libnetcdf: 4.8.0

xarray: 0.18.2
pandas: 1.2.4
numpy: 1.20.3
scipy: 1.6.3
netCDF4: 1.5.6
pydap: None
h5netcdf: None
h5py: None
Nio: None
zarr: None
cftime: 1.5.0
nc_time_axis: None
PseudoNetCDF: None
rasterio: None
cfgrib: None
iris: None
bottleneck: None
dask: None
distributed: None
matplotlib: 3.3.0
cartopy: 0.19.0.post1
seaborn: None
numbagg: None
pint: 0.17
setuptools: 49.6.0.post20210108
pip: 21.1.2
conda: None
pytest: None
IPython: 7.24.0
sphinx: None

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the provided Python loop with the three WRF/netCDF datasets and compare the dimensions in each generated file with individually subset files. Investigate the to_netcdf path involved in preserving dimensions. Done means all three subset files preserve the same expected Time dimensions.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
data
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.