0

I am new to python and trying to read in nc file and get the variables but I get the error:

NameError: name 'fh' is not defined

Here is my code:

from netCDF4 import Dataset 
import numpy as np  
my_example_nc_file = r'''\D:\UoR\Practice data\cru10min30_tmp.nc'
fh = (my_example_nc_file, mode='r')'''
lons = fh.variables['lon'][:]
lats = fh.variables['lat'][:]
tmax = fh.variables['Tmax'][:]
tmax_units = fh.variables['Tmax'].units
Til
  • 5,150
  • 13
  • 26
  • 34
Reen
  • 1
  • Possible duplicate of [Read .nc (netcdf) files using python](https://stackoverflow.com/questions/36360469/read-nc-netcdf-files-using-python) – Joao Vitorino Jun 19 '19 at 19:21

1 Answers1

0
from netCDF4 import Dataset 
import numpy as np  
my_example_nc_file = r'''\D:\UoR\Practice data\cru10min30_tmp.nc'''
fh = Dataset(my_example_nc_file, mode='r')
lons = fh.variables['lon'][:]
lats = fh.variables['lat'][:]
tmax = fh.variables['Tmax'][:]
tmax_units = fh.variables['Tmax'].units

try this.

you converted the line where fh was defined by closing it in strings

static const
  • 953
  • 4
  • 16
  • Hi Sharma, Thanks for your answer but when Itry this it complains showing this fh = (my_example_nc_file, mode ='r') ^ SyntaxError: invalid syntax – Reen Jun 19 '19 at 20:21
  • I think you're missing out on the syntax, which library are you using to create fh? I would double check for the initialization of fh – static const Jun 19 '19 at 20:37
  • I cant figure out how to do this, could you please help me here – Reen Jun 19 '19 at 21:05
  • but it brings back this error tmax_units = fh.variables['Tmax'].units ^ SyntaxError: EOF while scanning triple-quoted string literal – Reen Jun 19 '19 at 21:31