Yu-Fen
Yu-Fen

Reputation: 51

python xarray open_dataset unable to read the second, third, or more nc file

I tried to read different NetCDF file by xarray, but it only read the first file. While the second, third, and more NetCDF files were read as the first file without any error. Anyone knows how to solve this problem?

### read files

## VI
VI_terra = xr.open_dataset(data_vi+"MOD13A1.006_500m_aid0001.nc")
VI_aqua = xr.open_dataset(data_vi+"MYD13A1.006_500m_aid0001.nc")

## LAI
LAI = xr.open_dataset(data_lai+"MCD15A2H.006_500m_aid0001.nc")

## ET
ET_terra = xr.open_dataset(data_et+"MOD16A2GF.006_500m_aid0001.nc")
ET_aqua = xr.open_dataset(data_et+"MYD16A2GF.006_500m_aid0001.nc")

## Surface temperature
Tsurf_terra = xr.open_dataset(data_tsurf+"MOD11A2.006_1km_aid0001.nc")
Tsurf_aqua = xr.open_dataset(data_tsurf+"MYD11A2.006_1km_aid0001.nc")

But the LAI is misread as VI_terra:

enter image description here

While when I use ncdump to check LAI, the file itself doesn't have problem (differ from VI): enter image description here

Upvotes: 0

Views: 1464

Answers (1)

turnerm
turnerm

Reputation: 1431

With xr.open_dataset, the .nc files remain open after reading.

The easiest solution is to use xr.load_dataset, which closes the .nc file automatically after reading.

If you need to stick with xr.open_dataset then you can place it in a with statement, or call .close() when finished.

Upvotes: 2

Related Questions