I created a geotiff from a netCDF file through the following code, but when I try to access the resulting file in python again, this gives me the following error message: 'NoneType' object has no attribute 'GetRasterBand' and I do not understand the reason for such! However, the file opens correctly in other software, such as QGIS. Can you help me?
ncdf=netCDF4.Dataset(file,'r')
lat=ncdf.variables["LAT"][:]
lon=ncdf.variables["LON"][:]
var =ncdf.variables["VAR"][:]
(code to convert LAT and LON in X and Y UTM coordinates)
X=...
Y=...
nx=len(X)
a=numpy.shape(var)
ny=a[1]
xres=30
yres=30
xmin, ymin, xmax, ymax = [X.min(), Y.min(), X.max(), Y.max()]
geotransform = (xmin, xres, 0, ymax, 0, -yres)
Wkt="""PROJCS["WGS 84 / UTM zone 29N",
GEOGCS["WGS 84",DATUM["WGS_1984",
SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],
AUTHORITY["EPSG","6326"]],
PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],
UNIT["degree",0.01745329251994328,AUTHORITY["EPSG","9122"]],
AUTHORITY["EPSG","4326"]],
UNIT["metre",1,
AUTHORITY["EPSG","9001"]],
PROJECTION["Transverse_Mercator"],
PARAMETER["latitude_of_origin",0],
PARAMETER["central_meridian",-9],
PARAMETER["scale_factor",0.9996],
PARAMETER["false_easting",500000],
PARAMETER["false_northing",0],
AUTHORITY["EPSG","32629"],
AXIS["Easting",EAST],
AXIS["Northing",NORTH]]"""
dst_ds = gdal.GetDriverByName('GTiff').Create('./B1_CATM.tif', ny, nx, 1, gdal.GDT_Float32)
dst_ds.GetRasterBand(1).WriteArray(refAerosol)
dst_ds.SetGeoTransform(geotransform)
srs = osr.SpatialReference()
srs.ImportFromWkt(Wkt)
srs.ImportFromEPSG(32629)
dst_ds.SetProjection(srs.ExportToWkt())
# AND IN OTHER SCRIPT OPEN THIS GEOTIFF File:
B=gdal.Open('directoryfile.tif')
Band=B.GetRasterBand(1)
# the following error message appears: 'NoneType' object has no attribute
'GetRasterBand'