时间:2022-11-06 09:22:41 | 栏目:Python代码 | 点击:次
可以发现该数据有正确的像元大小、坐标系等
from netCDF4 import Dataset,num2date infile = "../01Data/Runoff1992-2014/GRUN_v1_GSWP3_WGS84_05_1902_2014.nc" data_set = Dataset(infile) # 读取nc文件信息 print(data_set)
输出为
<class 'netCDF4._netCDF4.Dataset'>
root group (NETCDF3_CLASSIC data model, file format NETCDF3):
title: GRUN
version: GRUN 1.0
meteorological_forcing: GSWP3
temporal_resolution: monthly
spatial_resolution: 0.5x0.5
crs: WGS84
proj4: +proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs
EPSG: 4326
references: Ghiggi et al.,2019. GRUN: An observation-based global gridded runoff dataset from 1902 to 2014. ESSD, doi: https://doi.org/10.5194/essd-2019-32
authors: Gionata Ghiggi; Lukas Gudmundsson
contacts: gionata.ghiggi@gmail.com; lukas.gudmundsson@env.ethz.ch
institution: Land-Climate Dynamics, Institute for Atmospheric and Climate Science, ETH Zürich
institution_id: IAC ETHZ
dimensions(sizes): X(720), Y(360), time(1356)
variables(dimensions): float64 X(X), float64 Y(Y), float64 time(time), float32 Runoff(time, Y, X)
groups:
可以看到variables
变量X、Y为经纬度,time为时间,Runoff
为需要的结果
from osgeo import gdal from netCDF4 import Dataset,num2date import numpy as np def WriteTiff(im_data,inputdir, path): raster = gdal.Open(inputdir) im_width = raster.RasterXSize #栅格矩阵的列数 im_height = raster.RasterYSize #栅格矩阵的行数 im_bands = raster.RasterCount #波段数 im_geotrans = raster.GetGeoTransform()#获取仿射矩阵信息 im_proj = raster.GetProjection()#获取投影信息 if 'int8' in im_data.dtype.name: datatype = gdal.GDT_Byte elif 'int16' in im_data.dtype.name: datatype = gdal.GDT_UInt16 else: datatype = gdal.GDT_Float32 if len(im_data.shape) == 3: im_bands, im_height, im_width = im_data.shape elif len(im_data.shape) == 2: im_data = np.array([im_data]) im_bands, (im_height, im_width) = 1, im_data.shape # 创建文件 driver = gdal.GetDriverByName("GTiff") dataset = driver.Create(path, im_width, im_height, im_bands, datatype) if (dataset != None): dataset.SetGeoTransform(im_geotrans) # 写入仿射变换参数 dataset.SetProjection(im_proj) # 写入投影 for i in range(im_bands): dataset.GetRasterBand(i + 1).WriteArray(im_data[i]) del dataset infile = "../01Data/Runoff1992-2014/GRUN_v1_GSWP3_WGS84_05_1902_2014.nc" data_set = Dataset(infile) # 读取nc文件信息 time = data_set.variables["time"][:] # 获取时间一列 units = data_set.variables["time"].units # 获取第一期时间 #读取样本tif文件的地理信息 intif = "../03ProcessData/runoff_example.tif" for i in range(0,len(time)): yr = num2date(time[i],units).year # 提取年份 mon = num2date(time[i],units).month # 提取月份 value_data = data_set.variables['Runoff'][i] # 将缺失值改为0 data = value_data.data mask = value_data.mask data[np.where(mask == True)] = 0 outputname = "../01Data/Runoff1992-2014/tif/" + str(yr) + str(mon).zfill(2) + ".tif" WriteTiff(data,intif , outputname) print(outputname)
1.使用时候请自行修改修改输入输出文件路径与变量名称
2.根据需要处理缺失值