I have several folders in a directory containing .nc
files. While reading, I am getting an error:
NETCDF can not read unsupported file
Since there are more than 5 thousand files, I don’t know which file is corrupted or unsupported. Is there any way to read files by jumping into another supported file?
The code that I am using is:
JavaScript
x
15
15
1
import xarray as xr
2
import numpy as np
3
import pandas as pd
4
5
6
ncfile = glob.glob('mydata/****/se*')
7
frame = pd.DataFrame()
8
for i in np.arange(len(ncfile)):
9
frame = frame
10
for j in np.arange(len(ds.variables['time'])):
11
ds1 = xr.open_dataset(ncfile[i])
12
prec = np.ravel(ds.variables['precipitation_amount'][j,:,:])
13
frame[dates] = prec
14
ds = xr.open_dataset(ncfile[i])
15
Advertisement
Answer
You could do this using exception handling. I’ve shown this with a simple example based on your code:
JavaScript
1
15
15
1
import xarray as xr
2
import numpy as np
3
import pandas as pd
4
5
6
ncfile = glob.glob('mydata/****/se*')
7
frame = pd.DataFrame()
8
errors = []
9
for i in np.arange(len(ncfile)):
10
frame = frame
11
try:
12
ds = xr.open_dataset(ncfile[i])
13
except:
14
errors.append(ncfile[i])
15