python读取并裁剪nc⽂件
1.⽂件读取与变量信息提取
(1)⽅法⼀:from netCDF4 import Dataset
from netCDF4 import Dataset
nc_file ='./file.c'
nc= Dataset(nc_file, mode='r')
#variable name
all_vars = nc.variables.keys()
# print(len(all_vars))
#Viewing a single variable
# var_info = f.variables['pwr_waveform_20_ku']
lon = nc['lon'][:]
lat = nc['lat'][:]
tim = nc['time'][:]
power = nc['power'][:]
# print(var_info)
# print(var_data.shape)
#covert to np.array
power = np.array(power )
lon = np.array(lon)
lat = np.array(lat)
tim = np.array(tim)
# print(tim.shape)#row
(2)⽅法⼆:import xarray as xr
import xarray as xr
data = xr.open_dataset(input_data_path)
2.
前提是所需要裁剪的变量是关于时间、坐标信息的变量,即variable_need = function (time,lon,lat)
#read nc file with xr
with xr.open_dataset('./') as file_nc:
waveform_xr=file_nc
print(waveform_xr)
# read selin boundary shp
lake_path ="./data/boundary_shp"
lake_path = os.path.join(
lake_path,"test.shp"
)
lake_ad_file(lake_path)
# print(lake_gdf.head())
#select test
test_aoi=lake_gdf[lake_gdf.name=="test"]
#get the outer envelop rectangular coordinates
# print(al_bounds)
#The NC data were sectioned according to the latitude and longitude of the outer envelope rectangle. # 获取外包络矩形的左下⾓和右上⾓经纬度坐标
aoi_lat =[float(al_bounds[1]), float(al_bounds[3])]
aoi_lon =[float(al_bounds[0]), float(al_bounds[2])]
# print(aoi_lat, aoi_lon)
# 将坐标转换为标准经度,即去掉正负号
aoi_lon[0]= aoi_lon[0] + 360
aoi_lon[1]= aoi_lon[1] + 360
python怎么读取桌面上的文件
# print(aoi_lon)
# 根据指定的时间和空间范围进⾏切⽚
start_date ="2011-02-02"
end_date ="2011-02-03"
waveform_test = waveform_xr["waveform"].sel(
time=slice(start_date, end_date),
lon=slice(aoi_lon[0], aoi_lon[1]),
lat=slice(aoi_lat[0], aoi_lat[1]))
waveform_test .plot()
plt.show()
如果所选的变量只与时间有关系,我们如何根据坐标信息进⾏裁剪呢?即已知variable1 = function1(time);
lon = function2(time);lat = function3 (time);
⼀种思路是根据经纬度坐标信息,计算时间范围,按照时间范围切⽚。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。