问题是这样的,最近在处理CLDAS-V2.0的数据
这样的nc文件,我打算按照nc文件中的要素不同分文件夹转成tif文件并保存。
搞成这样的!!!!
但是我发现,不管什么怎么样,都智能处理54个nc文件
报错如下:
import os
import netCDF4 as nc
import numpy as np
from osgeo import gdal, osr
file_list = os.listdir(r'G:\date\CLDAS')
for i in file_list:
if i.split('-')[1] == 'GST':
NC_DS = nc.Dataset('G:/date/CLDAS/{}'.format(i))
name = i.split('.')[0]+'.tif'
OutTif = r'G:\date\CLDAS处理好的TIF影像\GST\{}'.format(name)
Lat = NC_DS.variables['LAT'][:]
Lon = NC_DS.variables['LON'][:]
PAIR_MEAN = NC_DS.variables['TG_MEAN'][:]
PAIR_MEAN = np.asarray(PAIR_MEAN)
print(PAIR_MEAN)
LonMin, LatMax, LonMax, LatMin = [Lon.min(), Lat.max(), Lon.max(), Lat.min()]
N_Lat = len(Lat)
N_Lon = len(Lon)
Lon_Res = (LonMax - LonMin) / (float(N_Lon) - 1)
Lat_Res = (LatMax - LatMin) / (float(N_Lat) - 1)
print(N_Lat,N_Lon,Lon_Res,Lat_Res)
spei_ds = gdal.GetDriverByName('Gtiff').Create(OutTif, N_Lon, N_Lat, 1, gdal.GDT_Float32)
geotransform = (LonMin, Lon_Res, 0, LatMin, 0, Lat_Res)
spei_ds.SetGeoTransform(geotransform)
srs = osr.SpatialReference()
srs.ImportFromEPSG(4326)
spei_ds.SetProjection(srs.ExportToWkt())
spei_ds.GetRasterBand(1).WriteArray(PAIR_MEAN)
spei_ds.FlushCache()
spei_ds = None
NC_DS.close()
这个就是我的代码!!!
编译环境是anaconda + pycharm