Skip to content

Instantly share code, notes, and snippets.

@blaylockbk
Last active December 3, 2021 08:43
Show Gist options
  • Save blaylockbk/37dfcba1a6b31a4d6ccd350a5283c976 to your computer and use it in GitHub Desktop.
Save blaylockbk/37dfcba1a6b31a4d6ccd350a5283c976 to your computer and use it in GitHub Desktop.
Basics of opening NetCDF files with Python
#from netCDF4 import Dataset # use scipy instead
from scipy.io import netcdf #### <--- This is the library to import.
# Open file in a netCDF reader
directory = './'
wrf_file_name = directory+'filename'
nc = netcdf.netcdf_file(wrf_file_name,'r')
#Look at the variables available
nc.variables
#Look at the dimensions
nc.dimensions
#Look at a specific variable's dimensions
nc.variables['T2'].dimensions ## output is ('Time', 'south_north', 'west_east')
#Look at a specific variable's units
nc.variables['T2'].units ## output is ('K')
#######
#update: you should use xarray now to read netcdf files
@AdrienWehrle
Copy link

AdrienWehrle commented Mar 2, 2020

hi, I get this when I try to import NetCDF "No module named 'scipy'". how do I deal with it?

pip install scipy or conda install scipy in your terminal :-)

@fipoucat
Copy link

fipoucat commented Jul 5, 2020

cant't find coltbls?

@blaylockbk
Copy link
Author

@fipoucat You only need to import the scipy module.
But this is a really old gist. I would recommend using xarray to read netcdf files instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment