Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
293 views
in Technique[技术] by (71.8m points)

python - How to avoid unsupported .nc file while reading from different directory

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 dont know which file is corrupted or unsupported. Is there anyways to read files by jumping into another supported file.

The code that I am using is:

import xarray as xr
import numpy as np
import pandas as pd


ncfile = glob.glob('mydata/****/se*')
frame = pd.DataFrame()
for i in np.arange(len(ncfile)):
    frame = frame
    for j in np.arange(len(ds.variables['time'])): 
        ds1 = xr.open_dataset(ncfile[i])
        prec = np.ravel(ds.variables['precipitation_amount'][j,:,:])
        frame[dates] = prec
    ds = xr.open_dataset(ncfile[i])
question from:https://stackoverflow.com/questions/66049237/how-to-avoid-unsupported-nc-file-while-reading-from-different-directory

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You could do this using exception handling. I've shown this with a simple example based on your code:

import xarray as xr
import numpy as np
import pandas as pd


ncfile = glob.glob('mydata/****/se*')
frame = pd.DataFrame()
errors = []
for i in np.arange(len(ncfile)):
    frame = frame
    try:
        ds = xr.open_dataset(ncfile[i])
    except:
        errors.append(ncfile[i])

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...