自用简单方法
参考代码:ERA5: How to calculate daily total precipitation - Copernicus Knowledge Base - ECMWF Confluence Wiki
1、先从官网(ERA5-Land hourly data from 1950 to present (copernicus.eu))下载全年的逐小时降水数据(nc格式)
2、运行代码
import time, sys from datetime import datetime, timedelta from netCDF4 import Dataset, date2num, num2date import numpy as np #设置起始日期和截止日期(逐月做,自行调整) startdate=20190701 enddate=20190731 for date in range(startdate,enddate+1): day = date d = datetime.strptime(str(day), '%Y%m%d') #输入下载好的逐小时nc数据(计算月末日期,如6月30日时,需用到7月1日0时的数据,可以直接先从官网下载全年的nc数据) f_in = 'tp2019.nc' # f_in = 'tp_%d-%s.nc' % (day, (d + timedelta(days = 1)).strftime('%Y%m%d')) f_out = 'daily-tp_%d.nc' % day time_needed = [] for i in range(1, 25): time_needed.append(d + timedelta(hours = i)) with Dataset(f_in) as ds_src: var_time = ds_src.variables['time'] time_avail = num2date(var_time[:], var_time.units, calendar = var_time.calendar) indices = [] for tm in time_needed: a = np.where(time_avail == tm)[0] if len(a) == 0: sys.stderr.write('Error: precipitation data is missing/incomplete - %s!\n' % tm.strftime('%Y%m%d %H:%M:%S')) sys.exit(200) else: print('Found %s' % tm.strftime('%Y%m%d %H:%M:%S')) indices.append(a[0]) var_tp = ds_src.variables['tp'] tp_values_set = False for idx in indices: if not tp_values_set: data = var_tp[idx, :, :] tp_values_set = True else: data += var_tp[idx, :, :] with Dataset(f_out, mode = 'w', format = 'NETCDF3_64BIT_OFFSET') as ds_dest: # Dimensions for name in ['latitude', 'longitude']: dim_src = ds_src.dimensions[name] ds_dest.createDimension(name, dim_src.size) var_src = ds_src.variables[name] var_dest = ds_dest.createVariable(name, var_src.datatype, (name,)) var_dest[:] = var_src[:] var_dest.setncattr('units', var_src.units) var_dest.setncattr('long_name', var_src.long_name) ds_dest.createDimension('time', None) var = ds_dest.createVariable('time', np.int32, ('time',)) time_units = 'hours since 1900-01-01 00:00:00' time_cal = 'gregorian' var[:] = date2num([d], units = time_units, calendar = time_cal) var.setncattr('units', time_units) var.setncattr('long_name', 'time') var.setncattr('calendar', time_cal) # Variables var = ds_dest.createVariable(var_tp.name, np.double, var_tp.dimensions) var[0, :, :] = data var.setncattr('units', var_tp.units) var.setncattr('long_name', var_tp.long_name) # Attributes ds_dest.setncattr('Conventions', 'CF-1.6') ds_dest.setncattr('history', '%s %s' % (datetime.now().strftime('%Y-%m-%d %H:%M:%S'), ' '.join(time.tzname))) print('Done! Daily total precipitation saved in %s' % f_out)
3、完成!
标签:src,name,python,tp,降水,ERA5,time,var,ds From: https://www.cnblogs.com/RobenLaw/p/16782649.html