Skip to content

Commit d0e4593

Browse files
authored
Merge pull request #1698 from magnesim/radio-dev
Radio dev
2 parents e1f8480 + 48a1fe3 commit d0e4593

1 file changed

Lines changed: 61 additions & 7 deletions

File tree

opendrift/models/radionuclides.py

Lines changed: 61 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,7 @@ def set_init_diameter(self, num, idxs,diam):
290290
uncert_ln = uncert/diam * 3.
291291
else:
292292
uncert_ln = 0.
293+
uncert = 0.
293294

294295
rng = np.random.default_rng()
295296

@@ -1055,10 +1056,24 @@ def write_netcdf_radionuclide_density_map(self, filename, pixelsize_m='auto', zl
10551056
horizontal_smoothing=False,
10561057
smoothing_cells=0,
10571058
reader_sea_depth=None,
1059+
landmask_shapefile=None,
1060+
origin_marker=None,
10581061
):
10591062
'''Write netCDF file with map of radionuclide species densities and concentrations'''
10601063

10611064
from netCDF4 import Dataset, date2num #, stringtochar
1065+
if landmask_shapefile is not None:
1066+
if 'shape' in self.env.readers.keys():
1067+
# removing previously stored landmask
1068+
del self.env.readers['shape']
1069+
# Adding new landmask
1070+
from opendrift.readers import reader_shape
1071+
custom_landmask = reader_shape.Reader.from_shpfiles(landmask_shapefile)
1072+
self.add_reader(custom_landmask)
1073+
elif 'global_landmask' not in self.env.readers.keys():
1074+
from opendrift.readers import reader_global_landmask
1075+
global_landmask = reader_global_landmask.Reader()
1076+
self.add_reader(global_landmask)
10621077

10631078
logger.info('Postprocessing: Write density and concentration to netcdf file')
10641079

@@ -1133,7 +1148,9 @@ def write_netcdf_radionuclide_density_map(self, filename, pixelsize_m='auto', zl
11331148
zlevels = np.sort(zlevels)
11341149
z_array = np.append(np.append(-10000, zlevels) , max(0,np.nanmax(z)))
11351150
else:
1136-
z_array = [min(-10000,np.nanmin(z)), max(0,np.nanmax(z))]
1151+
z_array = self.depthintervals
1152+
z_array = np.append(np.append(-10000, z_array) , max(0,np.nanmax(z)))
1153+
# z_array = [min(-10000,np.nanmin(z)), max(0,np.nanmax(z))]
11371154
logger.info('z_array: {}'.format( [str(item) for item in z_array] ) )
11381155

11391156

@@ -1152,7 +1169,16 @@ def write_netcdf_radionuclide_density_map(self, filename, pixelsize_m='auto', zl
11521169
lon_array = (lon_array[:-1,:-1] + lon_array[1:,1:])/2
11531170
lat_array = (lat_array[:-1,:-1] + lat_array[1:,1:])/2
11541171

1172+
landmask = np.zeros_like(H[0,0,0,:,:])
1173+
if landmask_shapefile is not None:
1174+
landmask = self.env.readers['shape'].__on_land__(lon_array,lat_array)
1175+
else:
1176+
landmask = self.env.readers['global_landmask'].__on_land__(lon_array,lat_array)
1177+
11551178

1179+
print('landmask.shape: ', landmask.shape)
1180+
landmask = landmask.reshape(lat_array.shape[0],lat_array.shape[1])
1181+
print('landmask.shape: ', landmask.shape)
11561182

11571183
if horizontal_smoothing:
11581184
# Compute horizontally smoother field
@@ -1189,13 +1215,17 @@ def write_netcdf_radionuclide_density_map(self, filename, pixelsize_m='auto', zl
11891215

11901216

11911217
conc = np.zeros_like(H)
1218+
Landmask=np.zeros_like(H)
11921219
if horizontal_smoothing:
11931220
conc_sm = np.zeros_like(Hsm)
11941221
for ti in range(H.shape[0]):
11951222
for sp in range(self.nspecies):
11961223
conc[ti,sp,:,:,:] = H[ti,sp,:,:,:] / pixel_volume * activity_per_element
11971224
if horizontal_smoothing:
11981225
conc_sm[ti,sp,:,:,:] = Hsm[ti,sp,:,:,:] / pixel_volume * activity_per_element
1226+
for zi in range(len(z_array)-1):
1227+
Landmask[ti,sp,zi,:,:] = landmask
1228+
11991229

12001230

12011231

@@ -1215,6 +1245,12 @@ def write_netcdf_radionuclide_density_map(self, filename, pixelsize_m='auto', zl
12151245
logger.info ('ndt '+ str(ndt)) # number of time steps over which to average in conc file
12161246
logger.info ('odt '+ str(odt)) # number of average slices
12171247

1248+
Landmask=np.zeros_like(conc[0:odt,:,:,:,:])
1249+
for zi in range(len(z_array)-1):
1250+
for sp in range(self.nspecies):
1251+
for ti in range(odt):
1252+
Landmask[ti,sp,zi,:,:] = landmask
1253+
12181254

12191255
# This may probably be written more efficiently!
12201256
mean_conc = np.zeros( [odt,cshape[1],cshape[2],cshape[3],cshape[4]] )
@@ -1312,6 +1348,7 @@ def write_netcdf_radionuclide_density_map(self, filename, pixelsize_m='auto', zl
13121348
# Radionuclide concentration, horizontally smoothed
13131349
nc.createVariable('concentration', 'f8',
13141350
('time','specie','depth','y', 'x'),fill_value=1.e36)
1351+
conc = np.ma.masked_where(Landmask==1,conc)
13151352
conc = np.swapaxes(conc, 3, 4) #.astype('i4')
13161353
#conc = np.ma.masked_where(conc==0, conc)
13171354
nc.variables['concentration'][:] = conc
@@ -1325,6 +1362,7 @@ def write_netcdf_radionuclide_density_map(self, filename, pixelsize_m='auto', zl
13251362
# Radionuclide concentration, horizontally smoothed
13261363
nc.createVariable('concentration_smooth', 'f8',
13271364
('time','specie','depth','y', 'x'),fill_value=1.e36)
1365+
conc_sm = np.ma.masked_where(Landmask==1, conc_sm)
13281366
conc_sm = np.swapaxes(conc_sm, 3, 4) #.astype('i4')
13291367
# conc_sm = np.ma.masked_where(conc_sm==0, conc_sm)
13301368
nc.variables['concentration_smooth'][:] = conc_sm
@@ -1350,21 +1388,31 @@ def write_netcdf_radionuclide_density_map(self, filename, pixelsize_m='auto', zl
13501388
nc.createVariable('volume', 'f8',
13511389
('depth','y', 'x'),fill_value=0)
13521390
pixel_volume = np.swapaxes(pixel_volume, 1, 2) #.astype('i4')
1353-
pixel_volume = np.ma.masked_where(pixel_volume==0, pixel_volume)
1391+
for zi in range(len(z_array)-1):
1392+
pixel_volume[zi,:,:] = np.ma.masked_where(landmask.T==1, pixel_volume[zi,:,:])
1393+
#pixel_volume = np.ma.masked_where(pixel_volume==0, pixel_volume)
13541394
nc.variables['volume'][:] = pixel_volume
1355-
nc.variables['volume'].long_name = 'Volume of grid cell'
1395+
nc.variables['volume'].long_name = 'Volume of grid cell (' + str(pixelsize_m)+'x'+str(pixelsize_m)+'m)'
13561396
nc.variables['volume'].grid_mapping = 'projection_lonlat'
13571397
nc.variables['volume'].units = 'm3'
13581398

13591399

13601400
# Topography
13611401
nc.createVariable('topo', 'f8', ('y', 'x'),fill_value=0)
1362-
pixel_mean_depth = np.ma.masked_where(pixel_mean_depth==0, pixel_mean_depth)
1402+
#pixel_mean_depth = np.ma.masked_where(pixel_mean_depth==0, pixel_mean_depth)
1403+
pixel_mean_depth = np.ma.masked_where(landmask==1, pixel_mean_depth)
13631404
nc.variables['topo'][:] = pixel_mean_depth.T
13641405
nc.variables['topo'].long_name = 'Depth of grid point'
13651406
nc.variables['topo'].grid_mapping = 'projection_lonlat'
13661407
nc.variables['topo'].units = 'm'
13671408

1409+
# Binary mask
1410+
nc.createVariable('land', 'i4', ('y', 'x'),fill_value=-1)
1411+
#landmask = np.ma.masked_where(landmask==0, landmask)
1412+
nc.variables['land'][:] = np.swapaxes(landmask,0,1).astype('i4')
1413+
nc.variables['land'].long_name = 'Binary land mask'
1414+
nc.variables['land'].grid_mapping = 'projection_lonlat'
1415+
nc.variables['land'].units = 'm'
13681416

13691417
nc.close()
13701418
logger.info('Wrote to '+filename)
@@ -1376,7 +1424,7 @@ def write_netcdf_radionuclide_density_map(self, filename, pixelsize_m='auto', zl
13761424
def get_radionuclide_density_array(self, pixelsize_m, z_array,
13771425
density_proj=None, llcrnrlon=None,llcrnrlat=None,
13781426
urcrnrlon=None,urcrnrlat=None,
1379-
weight=None):
1427+
weight=None, origin_marker=None):
13801428
'''
13811429
compute a particle concentration map from particle positions
13821430
Use user defined projection (density_proj=<proj4_string>)
@@ -1409,8 +1457,10 @@ def get_radionuclide_density_array(self, pixelsize_m, z_array,
14091457
if weight is not None:
14101458
weight_array = self.result[weight]
14111459

1412-
status = self.result.status
1413-
specie = self.result.specie.T
1460+
status = self.get_property('status')[0]
1461+
specie = self.get_property('specie')[0]
1462+
if origin_marker is not None:
1463+
originmarker = self.get_property('origin_marker')[0]
14141464
Nspecies = self.nspecies
14151465
H = np.zeros((len(times),
14161466
Nspecies,
@@ -1423,13 +1473,16 @@ def get_radionuclide_density_array(self, pixelsize_m, z_array,
14231473
for i in range(len(times)):
14241474
if weight is not None:
14251475
weights = weight_array[i,:]
1476+
if origin_marker is not None:
1477+
weight_array[i,:] = weight_array[i,:] * (originmarker[i,:]==origin_marker)
14261478
else:
14271479
weights = None
14281480
for zi in range(len(z_array)-1):
14291481
kktmp = ( (specie[i,:]==sp) & (z[i,:]>z_array[zi]) & (z[i,:]<=z_array[zi+1]) )
14301482
H[i,sp,zi,:,:], dummy, dummy = \
14311483
np.histogram2d(x[i,kktmp], y[i,kktmp],
14321484
weights=weights, bins=bins)
1485+
# chem weights=weight_array[i,kktmp], bins=bins)
14331486

14341487
if density_proj is not None:
14351488
Y,X = np.meshgrid(y_array, x_array)
@@ -1455,6 +1508,7 @@ def get_pixel_mean_depth(self,lons,lats):
14551508

14561509
# Interpolate topography to new grid
14571510
h = interpolate.griddata((lon_grd[h_grd.mask==False].flatten(),lat_grd[h_grd.mask==False].flatten()), h_grd[h_grd.mask==False].flatten(), (lons, lats), method='linear')
1511+
# chem h = interpolate.griddata((lon_grd.flatten(),lat_grd.flatten()), h_grd.flatten(), (lons, lats), method='linear')
14581512

14591513
return h
14601514

0 commit comments

Comments
 (0)