一、使用arcmap提取出第一期数据
1.使用工具箱中的“make netcdf raster layer”工具,提取出一个数据
可以发现该数据有正确的像元大小、坐标系等
2.导出该数据作为标准数据
二、使用python批量提取所有数据
1. 查看数据属性
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.5×0.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
为需要的结果
2.批量导出结果
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.根据需要处理缺失值
到此这篇关于python的netcdf4批量处理nc格式文件的操作方法的文章就介绍到这了,更多相关python netcdf4处理nc格式文件内容请搜索<计算机技术网(www.ctvol.com)!!>以前的文章或继续浏览下面的相关文章希望大家以后多多支持<计算机技术网(www.ctvol.com)!!>!
需要了解更多python教程分享python的netCDF4批量处理NC格式文件的操作方法,都可以关注python教程分享栏目—计算机技术网(www.ctvol.com)!
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/pythontutorial/1067927.html