英文:
Converting NOAA netcdf file to geo tif
问题
我正在尝试使用Python或GDAL将此netcdf文件转换为GeoTIFF,但没有成功。
以下是我在Python中的方法:
import rioxarray
rds = rioxarray.open_rasterio('path/to/nc')
rds.newhail.rio.to_raster('path/to/tif')
然后我尝试在QGIS中可视化它,但看不到任何内容。
接下来,我尝试使用gdalwarp设置crs(存储在nc文件的全局属性中 - map_proj = "+proj=lcc +lat_0=25 +lon_0=265 +lat_1=25 +lat_2=25 +x_0=0 +y_0=0 +R=6371200 +units=m +no_defs"):
gdalwarp -to SRC_METHOD=NO_GEOTRANSFORM -t_srs '+proj=lcc +lat_0=25 +lon_0=265 +lat_1=25 +lat_2=25 +x_0=0 +y_0=0 +R=6371200 +units=m +no_defs' newhail.tif newhail_crs.tif
现在我可以在QGIS中看到tif文件,但坐标完全错误。请参考附图 - 数据应该是美国的,而不是非洲。
我错过了什么或者做错了什么?
英文:
SI'm trying to convert this netcdf file to geo tif using python or gdal, but with no succes.
Here is my approach in python:
import rioxarray
rds = rioxarray.open_rasterio('path/to/nc')
rds.newhail.rio.to_raster('path/to/tif')
Then I try to visualize it in QGIS but can't see anything.
Next I tried to set crs (stored in global attributes of nc file - map_proj = "+proj=lcc +lat_0=25 +lon_0=265 +lat_1=25 +lat_2=25 +x_0=0 +y_0=0 +R=6371200 +units=m +no_defs") with gdalwarp:
gdalwarp -to SRC_METHOD=NO_GEOTRANSFORM -t_srs '+proj=lcc +lat_0=25 +lon_0=265 +lat_1=25 +lat_2=25 +x_0=0 +y_0=0 +R=6371200 +units=m +no_defs' newhail.tif newhail_crs.tif
Now I can see the tif in QGIS but the coordinates are compeltly wrong. See attached picture - data should be for US, not in Africa.
What am I missing or doing wrong?
答案1
得分: 1
通常只有在想要更改一些内容(如投影)时才会使用gdalwarp
,对于转换或分配元数据,gdal_translate
可能更好,因为它不会改变实际数据本身。
理想情况下,您对源文件有更多的信息,因为该文件有点混乱。它将坐标存储为纬度/经度,但网格(数据)位于此LCC投影中。
您可以将纬度/经度转换回该投影。假设这些坐标是像素的中心,通过分辨率的一半(40km)扩展以获得外部范围,并在转换为tiff时使用gdal_translate
进行分配。
类似以下命令将实现这一目标:
gdal_translate -a_srs "+proj=lcc +lat_0=25 +lon_0=265 +lat_1=25 +lat_2=25 +x_0=0 +y_0=0 +R=6371200 +units=m +no_defs" -a_ullr -2853244.2412856636 3284372.1657427647 2746755.7587143364 -315627.83425723313 NETCDF:"newhail.nc":newhail newhail.tif
这将大致符合要求,但仍然有些混乱,而且数据分辨率太低,无法直观地查看。边界框似乎不能近似地表示为一个漂亮的圆形,这通常是情况(但不一定是)。
英文:
You normally only use gdalwarp
if you want to change something (like projection), for converting or assigning metadata gdal_translate
is probably better since it doesn't change the actual data itself.
Ideally you have a bit more information about the source, because this file is a little messy. It stores the coordinates as lat/lon, but the grid (the data) is in this LCC projection.
One thing you can do is convert the lat/lon back to that projection. Assuming those coordinates are the center of the pixels, expand by half the resolution (40km) to get the outer extent, and assign that using gdal_translate
in the conversion to tiff.
Something like that would give:
gdal_translate -a_srs "+proj=lcc +lat_0=25 +lon_0=265 +lat_1=25 +lat_2=25 +x_0=0 +y_0=0 +R=6371200 +units=m +no_defs" -a_ullr -2853244.2412856636 3284372.1657427647 2746755.7587143364 -315627.83425723313 NETCDF:"newhail.nc":newhail newhail.tif
That puts it in the right ballpark, but it remains messy, and the data is too low resolution to eyeball it. The boundingbox doesn't seem to approximate something nicely rounded, which is often the case (but doesn't have to be of course).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论