python怎么批量nc4转tiff的
时间: 2024-05-06 21:19:26 浏览: 85
你可以使用Python中的netCDF4库和GDAL库来实现nc4文件批量转换为tiff文件。
下面是一个示例代码:
```
import os
import netCDF4
import gdal
nc_folder = "path/to/nc/files"
tiff_folder = "path/to/output/folder"
for nc_file in os.listdir(nc_folder):
if nc_file.endswith(".nc") or nc_file.endswith(".nc4"):
nc_path = os.path.join(nc_folder, nc_file)
with netCDF4.Dataset(nc_path, "r") as nc:
variable_name = "variable_name" # 替换成你的变量名称
data = nc.variables[variable_name][:]
geotransform = (lon_min, res_lon, 0, lat_max, 0, -res_lat) # 替换成你的经纬度范围、分辨率
projection = "GEOGCS[\"WGS 84\",DATUM[\"WGS_1984\",SPHEROID[\"WGS 84\",6378137,298.257223563,AUTHORITY[\"EPSG\",\"7030\"]],AUTHORITY[\"EPSG\",\"6326\"]],PRIMEM[\"Greenwich\",0],UNIT[\"degree\",0.0174532925199433],AUTHORITY[\"EPSG\",\"4326\"]]" # 替换成你的投影信息
tiff_path = os.path.join(tiff_folder, nc_file[:-3] + "tiff")
driver = gdal.GetDriverByName("GTiff")
dataset = driver.Create(tiff_path, data.shape[1], data.shape[0], 1, gdal.GDT_Float32)
dataset.SetGeoTransform(geotransform)
dataset.SetProjection(projection)
dataset.GetRasterBand(1).WriteArray(data)
dataset.FlushCache()
```
注意要根据你的具体情况替换变量名称、经纬度范围、分辨率、投影信息等参数。
阅读全文