Skip to content

Instantly share code, notes, and snippets.

@chrisb13
Last active December 3, 2015 04:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisb13/62b159a374bb0763c569 to your computer and use it in GitHub Desktop.
Save chrisb13/62b159a374bb0763c569 to your computer and use it in GitHub Desktop.
function loadnc() #load netcdf file in python
{
if [[ ( $# -eq 0 ) || ( $1 == "--help" ) || ( $1 == "-h" ) ]] ; then
echo "Usage: loadnc FILENAME_PATHNAME"
echo "Purpose: loads path of file into ipython (creates loadvar.py in your current dir). Prints file info and available variables. 'ifile' is name of netCDF4 object."
echo " "
echo "Requires: python netCDF4"
echo " "
echo "Mandatory arguments: "
echo "FILENAME_PATHNAME: path to netCDF file"
echo " "
echo "Examples:"
echo "loadnc netcdffilename.nc"
echo "loadnc /path/to/netcdffilename.nc"
echo " "
return 1
fi
OUTPUT="$(readlink -e ${1}|tr -d '\n')"
cat << EOF > loadvar.py
import os
from netCDF4 import Dataset
infile='${OUTPUT}'
ifile=Dataset(infile, 'r')
#varone=ifile.variables['']
print ""
print "NetCDF file loaded with bash loadnc function..."
print os.path.basename(infile)
print ""
print "File Details"
print ifile
print ""
print "Variable Names: "
print ""
print ifile.variables.keys()
print ""
print ""
print "'ifile' is name of netCDF4 object... "
print ""
EOF
ipython -i loadvar.py
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment