在python

时间:2017-04-06 11:34:48

标签: python netcdf geotiff

我正在尝试从netCDF文件中提取一组特定数据,然后将所述数据转换为GeoTIFF。

到目前为止,我已设法使用netCDF4提取我想要的数据,文件中的所有数据都存储为1d数组(lat,lon,我想要的数据)并将它们分配给2d数组。我正在使用的netcdf文件被子集化到特定区域。但是从这里我不知所措。

我对通过以下链接阅读的geotiff转换工作原理略有了解:

https://borealperspectives.wordpress.com/2014/01/16/data-type-mapping-when-using-pythongdal-to-write-numpy-arrays-to-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

我在上面创建地理基因的过程主要来自这里:

How do I write/create a GeoTIFF RGB image file in python?

我试过这个是基于我想要写入栅格的数组是一个2d数组,有3列,2个共同点和1个数据。我在snap中检查的结果是一个黑色页面,沿着LHS有一条白线。

所以我的问题如下:

如何从netcdf文件中提取必要的地理转换数据,适当调整地理转换参数,然后使用提取的lat long + dataw数组写入geotiff文件?

1 个答案:

答案 0 :(得分:1)

尝试使用gdal_translate命令行转换为geotiff文件

gdal_translate NETCDF:"<filename.nc>":<varible name> <required_file>.tif