cd1*_*123 5 python netcdf geotiff
我试图从 netCDF 文件中提取一组特定的数据,然后将所述数据转换为 GeoTIFF。
到目前为止,我已经设法使用 netCDF4 提取了我想要的数据,文件中的所有数据都存储为一维数组(lat、lon、我想要的数据)并将它们分配给一个二维数组。我正在使用的 netcdf 文件被子集化到特定区域。然而,从这里我不知所措。
通过我在这些链接中阅读的内容,我对 geotiff 转换的工作原理略有了解:
http://adventuresindevelopment.blogspot.co.uk/2008/12/create-geotiff-with-python-and-gdal.html
这是我目前所拥有的:
import netCDF4
import numpy as np
from osgeo import gdal
from osgeo import osr
#Reading in data from files and extracting said data
ncfile = netCDF4.Dataset("data.nc", 'r')
dataw = ncfile.variables["dataw"][:]
lat = ncfile.variables["Latitude"][:]
long = ncfile.variables["Longitude"][:]
n = len(dataw)
x = np.zeros((n,3), float)
x[:,0] = long[:]
x[:,1] = lat[:]
x[:,2] = dataw[:]
nx = len(long)
ny = len(lat)
xmin, ymin, xmax, ymax = [long.min(), lat.min(), long.max(), lat.max()]
xres = (xmax - xmin) / float(nx)
yres = (ymax - ymin) / float(ny)
geotransform = (xmin, xres, 0, ymax, 0, -yres)
#Creates 1 raster band file
dst_ds = gdal.GetDriverByName('GTiff').Create('myGeoTIFF.tif', ny, nx, 1, gdal.GDT_Float32)
dst_ds.SetGeoTransform(geotransform) # specify coords
srs = osr.SpatialReference() # establish encoding
srs.ImportFromEPSG(3857) # WGS84 lat/long
dst_ds.SetProjection(srs.ExportToWkt()) # export coords to file
dst_ds.GetRasterBand(1).WriteArray(x) # write r-band to the raster
dst_ds.FlushCache() # write to disk
dst_ds = None # save, close
Run Code Online (Sandbox Code Playgroud)
我上面的 geotiff 创建过程主要来自这里:
如何在 python 中编写/创建 GeoTIFF RGB 图像文件?
我尝试这样做是基于这样一种理解,即我想写入栅格的数组是一个 2d 数组,包含 3 列、2 个坐标和 1 个数据。我检查的结果是一个黑色的页面,沿着 LHS 有一条白线。
所以我的问题如下:
如何从我的 netcdf 文件中提取必要的地理变换数据,适当调整地理变换参数,然后使用提取的 lat long + dataw 数组写入 geotiff 文件?
小智 2
尝试使用 gdal_translate 命令行转换为 geotiff 文件
gdal_translate NETCDF:"<filename.nc>":<varible name> <required_file>.tif
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4212 次 |
| 最近记录: |