def popweight_outputs_for_input(custom_input): sim = ("RES" + str(np.round(custom_input[0][0], decimals=1)) + "_IND" + str(np.round(custom_input[0][1], decimals=1)) + "_TRA" + str(np.round(custom_input[0][2], decimals=1)) + "_AGR" + str(np.round(custom_input[0][3], decimals=1)) + "_ENE" + str(np.round(custom_input[0][4], decimals=1))) filename = f"ds_{sim}_{output}_popgrid_0.25deg_adjusted_scaled.nc" print(f"processing for {filename} ...") with xr.open_dataset( f"/nobackup/earlacoa/machinelearning/data_annual/predictions/{output}_adjusted_scaled/{filename}" ) as ds: ds_custom_output = ds[output].load() clip = rasterize(shapes, ds_custom_output.coords, longitude="lon", latitude="lat") ds_custom_output_clipped = ds_custom_output.where(clip == 0, other=np.nan) return filename, np.nansum( (ds_custom_output_clipped * pop_2015) / pop_2015_clipped.sum())
def crop_ds_to_shapefile(ds, shapefile): # load shapefile (single multipolygon) and extract shapes shapefile = gpd.read_file(shapefile) shapes = [(shape, index) for index, shape in enumerate(shapefile.geometry)] # apply shapefile to geometry, default: inside shapefile == 0, outside shapefile == np.nan ds['shapefile'] = rasterize(shapes, ds.coords, longitude='lon', latitude='lat') # change to more intuitive labelling of 1 for inside shapefile and np.nan for outside shapefile # if condition preserve (outside shapefile, as inside defaults to 0), otherwise (1, to mark in shapefile) ds['shapefile'] = ds.shapefile.where(cond=ds.shapefile != 0, other=1) # example: crop data to shapefile # if condition (not in shapefile) preserve, otherwise (in shapefile, and scale) ds = ds.where(cond=ds.shapefile == 1, other=np.nan) return ds
def popweight_outputs_for_input(custom_input): sim = 'RES' + str(np.round(custom_input[0][0], decimals=1)) \ + '_IND' + str(np.round(custom_input[0][1], decimals=1)) \ + '_TRA' + str(np.round(custom_input[0][2], decimals=1)) \ + '_AGR' + str(np.round(custom_input[0][3], decimals=1)) \ + '_ENE' + str(np.round(custom_input[0][4], decimals=1)) filename = f'ds_{sim}_{output}_popgrid.nc' print(f'processing for {filename} ...') try: with xr.open_dataset(path + output + '/' + filename) as ds: ds_custom_output = ds[output] clip = rasterize(shapes, ds_custom_output.coords, longitude='lon', latitude='lat') ds_custom_output_clipped = ds_custom_output.where(clip == 0, other=np.nan) return filename, np.nansum( (ds_custom_output_clipped * pop_2015) / pop_2015_clipped.sum()) except: RuntimeError
#!/usr/bin/env python3 import numpy as np import xarray as xr import salem from cutshapefile import transform_from_latlon, rasterize import geopandas as gpd # load shapefile (single multipolygon) and extract shapes shapefile = gpd.read_file( '/nobackup/earlacoa/emissions/shapefiles/CHN_adm0.shp') shapes = [(shape, index) for index, shape in enumerate(shapefile.geometry)] # load wrf dataset ds = salem.xr.open_dataset('/path/wrf.nc') # apply shapefile to geometry, default: inside shapefile == 0, outside shapefile == np.nan ds['shapefile'] = rasterize(shapes, ds.coords, longitude='lon', latitude='lat') # change to more intuitive labelling of 1 for inside shapefile and np.nan for outside shapefile # if condition preserve (outside shapefile, as inside defaults to 0), otherwise (1, to mark in shapefile) ds['shapefile'] = ds.shapefile.where(cond=ds.shapefile != 0, other=1) # example: scale data inside shapefile by 0.5 # if condition (not in shapefile) preserve, otherwise (in shapefile, and scale) ds = ds.where(cond=ds.shapefile != 1, other=ds * 0.5)
#region = "china_north" #shapefile = "/nobackup/earlacoa/health/data/CHN_north.shp" # region = 'china_north_east' # shapefile = '/nobackup/earlacoa/health/data/CHN_north_east.shp' # region = 'china_east' # shapefile = '/nobackup/earlacoa/health/data/CHN_east.shp' # region = 'china_south_central' # shapefile = '/nobackup/earlacoa/health/data/CHN_south_central.shp' # region = 'china_south_west' # shapefile = '/nobackup/earlacoa/health/data/CHN_south_west.shp' # region = 'china_north_west' # shapefile = '/nobackup/earlacoa/health/data/CHN_north_west.shp' shp = gpd.read_file(shapefile) shapes = [(shape, n) for n, shape in enumerate(shp.geometry)] clip = rasterize(shapes, pop_2015.coords, longitude="lon", latitude="lat") pop_2015_clipped = pop_2015.where(clip == 0, other=np.nan) def popweight_outputs_for_input(custom_input): sim = ("RES" + str(np.round(custom_input[0][0], decimals=1)) + "_IND" + str(np.round(custom_input[0][1], decimals=1)) + "_TRA" + str(np.round(custom_input[0][2], decimals=1)) + "_AGR" + str(np.round(custom_input[0][3], decimals=1)) + "_ENE" + str(np.round(custom_input[0][4], decimals=1))) filename = f"ds_{sim}_{output}_popgrid_0.25deg_adjusted_scaled.nc" print(f"processing for {filename} ...") with xr.open_dataset( f"/nobackup/earlacoa/machinelearning/data_annual/predictions/{output}_adjusted_scaled/{filename}" ) as ds: ds_custom_output = ds[output].load()
#region = 'china_north' #shapefile = '/nobackup/earlacoa/health/data/CHN_north.shp' #region = 'china_north_east' #shapefile = '/nobackup/earlacoa/health/data/CHN_north_east.shp' #region = 'china_east' #shapefile = '/nobackup/earlacoa/health/data/CHN_east.shp' #region = 'china_south_central' #shapefile = '/nobackup/earlacoa/health/data/CHN_south_central.shp' #region = 'china_south_west' #shapefile = '/nobackup/earlacoa/health/data/CHN_south_west.shp' #region = 'china_north_west' #shapefile = '/nobackup/earlacoa/health/data/CHN_north_west.shp' shp = gpd.read_file(shapefile) shapes = [(shape, n) for n, shape in enumerate(shp.geometry)] clip = rasterize(shapes, pop_2015.coords, longitude='lon', latitude='lat') pop_2015_clipped = pop_2015.where(clip == 0, other=np.nan) def popweight_outputs_for_input(custom_input): sim = 'RES' + str(np.round(custom_input[0][0], decimals=1)) \ + '_IND' + str(np.round(custom_input[0][1], decimals=1)) \ + '_TRA' + str(np.round(custom_input[0][2], decimals=1)) \ + '_AGR' + str(np.round(custom_input[0][3], decimals=1)) \ + '_ENE' + str(np.round(custom_input[0][4], decimals=1)) filename = f'ds_{sim}_{output}_popgrid.nc' print(f'processing for {filename} ...') try: with xr.open_dataset(path + output + '/' + filename) as ds: ds_custom_output = ds[output]