alex_bn_lee

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

【871】netCDF数据处理

参考:How to convert NetCDF to GeoTIFF?

参考:利用Python(netCDF4库)读取.nc文件(NetCDF气象数据文件)的基本操作

参考:经度转换0°-360°与-180°-180°之间的相互转换

参考:https://blog.csdn.net/weixin_44237337/article/details/119707332

参考:xarray.Dataset.swap_dims

参考:xarray.Dataset.to_netcdf


NetCDF TO GeoTIFF

# The first step is to import the packages:
# packages
import xarray as xr
import rioxarray as rio
# The xarray package enables to load and open the NetCDF file to convert:
nc_file = xr.open_dataset('./mydirectory/medsea_tem_20200320_21.nc')
nc_file
# The next step is to extract the variable of your choice to convert into raster file.
# We want here the bottom temperature bottomT:
bT = nc_file['bottomT']
# It is suggested to provide spatial axis x and y for the GeoTIFF and check for Coordinate Reference System (CRS). No output is required:
bT = bT.rio.set_spatial_dims(x_dim='lon', y_dim='lat')
bT.rio.crs
# Copernicus Marine products have as standard CRS - the WGS 84 (EPSG 4326) - except for ARCTIC products. This projection system can be defined as follows:
# Define the CRS projection
bT.rio.write_crs("epsg:4326", inplace=True)
# Finally, save the GeoTIFF file:
bT.rio.to_raster(r"medsea_bottomT_raster.tiff")

经度转换0°-360°与-180°-180°之间的相互转换

0°-360°转换为-180°-180°

da1 = xr.open_dataset('file.nc') # 文件中坐标标签为‘longitude'
da1['lon'] = xr.where(da1['longitude'] > 180, da1['longitude'] - 360, da1['longitude'])
da1 = (da1
.swap_dims({'longitude': 'lon'})
.sel(**{'lon': sorted(da1.lon)})
.drop('longitude'))
da1 = da1.rename({'lon': 'longitude'})

-180°-180°转换为 0°-360°

da = xr.open_dataset("file.nc", engine="netcdf4")
lon_name = 'lon'
da['longitude'] = xr.where(da[lon_name] < 0, da[lon_name]+360, da[lon_name])
da = (da
.swap_dims({lon_name: 'longitude'})
.sel(**{'longitude': sorted(da.longitude)})
.drop(lon_name))
da = da.rename({'longitude': lon_name})

 

posted on   McDelfino  阅读(54)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
历史上的今天:
2021-08-17 【649】shapely strtree STRtree 构建 RTree
2021-08-17 【648】计算直线间的夹角
2016-08-17 【213】IDL函数汇总
2016-08-17 【212】HDF更新数据&HDF创建
2012-08-17 【072】◀▶ Android (IV) - 显示及后台
点击右上角即可分享
微信分享提示