try:
client.close()
cluster.close()
except:
pass
import bokeh.io
bokeh.io.output_notebook()
import holoviews as hv
hv.extension('bokeh')
import datashader as ds
from holoviews.operation.datashader import datashade,rasterize
from IPython.display import display, HTML
import socket,os
hostname = socket.gethostname()
from dask.distributed import Client, LocalCluster, progress
cluster = LocalCluster(n_workers=4,
threads_per_worker=4,
scheduler_port=0,
host=hostname,
dashboard_address='%s:0' % hostname,
memory_limit='8GB',
local_directory=os.path.join("/tmp",os.getenv("USER"),"dask")
)
client = Client(cluster)
client
import numpy as np
import saroumanedraft as sr
import logging
sr.logger.setLevel(logging.DEBUG)
chunks={'pol' : 1 , 'xtrack' : 500 , 'atrack' : 200 }
sar_ds = sr.open_dataset('/home/datawork-cersat-public/cache/project/mpc-sentinel1/data/esa/sentinel-1a/L1/IW/S1A_IW_GRDH_1S/2017/250/S1A_IW_GRDH_1SDV_20170907T103020_20170907T103045_018268_01EB76_992F.SAFE',chunks=chunks)
sar_ds
# take a slice : still no evaluation
#sliced_sar = sar_ds.sel(xtrack=slice(0,500),atrack=slice(150,300),pol='VV')
#sliced_sar = sar_ds.sel(xtrack=slice(0,500),atrack=slice(150,300),pol='VH')
#sliced_sar = sar_ds.sel(xtrack=slice(0,25186,100),atrack=slice(0,16777,100),pol='VH')
# eye
sliced_sar = sar_ds.sel(xtrack=slice(5000,9999,1),atrack=slice(7000,11999,1),pol='VH')
# slice with earth at high res
#sliced_sar = sar_ds.sel(xtrack=slice(24000,25186,1),atrack=slice(14500,15500,1),pol='VH')
sliced_sar
# dn at high res
future = client.compute(sliced_sar.digital_number)
progress(future)
dn_high = future.result()
display(dn_high)
rasterize(hv.Image(dn_high).opts(cmap='gray'))
# sigma0 at high res
future = client.compute(sliced_sar.sigma0)
progress(future)
sigma0_high = future.result()
display(sigma0_high)
rasterize(hv.Image(sigma0_high).opts(cmap='gray',colorbar=True,tools=['hover']))
# do some science : move a box of shape (20,20) across sigma0 array, and compute mean by box
def science_function(sigma0):
# here, sigma0 is a simple numpy array
return np.mean(sigma0),np.std(sigma0) # so the output size will be spatialy reduced, but expanding axis in a new dim
# shape_out is optionnal. It's the shape of returned values from science_function, with the count of returned
# values as the first dim. setting shape_out=None will compute this value for the user, but for better optimisation,
# the user should hard code the value.
sigma0_low,sigma0_std = sliced_sar.sarlib.map(science_function,sliced_sar.sigma0,shape_in=(20,10),shape_out=(2, 1, 1))
sigma0_low
# compute science
future = client.compute((sigma0_low,sigma0_std))
progress(future)
sigma0_low = future[0].result()
sigma0_std = future[1].result()
display(sigma0_low.shape)
rasterize(hv.Image(sigma0_low).opts(cmap='gray',colorbar=True,tools=['hover'])) + rasterize(hv.Image(sigma0_std).opts(cmap='gray',colorbar=True,tools=['hover']))
# sigma0 debug
#from importlib import reload
#reload(sr)
#sr.logger.setLevel(logging.DEBUG)
#import sys
#sys.path.append('/home3/homedir7/perso/oarcher/bin/')
#import pydev
lut = sar_ds.sarlib.metadata._pol[sliced_sar.pol.item()]['sigma0_lut']
xtrack_val = np.array(dn_high.xtrack).copy()
atrack_val = np.array(dn_high.atrack).copy()
sigma0 = sr._get_sigma0_from_lut(
np.asarray(dn_high).copy(),
xtrack_val,
atrack_val,
np.asarray(lut).copy(),
np.asarray(lut.xtrack).copy(),
np.asarray(lut.atrack).copy(),
block_info=((xtrack_val[0],xtrack_val[-1]),(atrack_val[0],atrack_val[-1]))
)
#rasterize(hv.Image(np.asarray(sigma0)).redim.range(z=(0, 1e-2))) + rasterize(hv.Image(np.asarray(dn_high)).opts(hv.opts.Image(tools=['hover'],colorbar=True)))
#[ sigma0 , np.array(dn_val) ]
#(np.asarray(dn_val),np.asarray(sliced_sar.xtrack),np.asarray(sliced_sar.atrack.data),np.asarray(lut),np.asarray(lut.xtrack),np.asarray(lut.atrack))
#hv.Image(np.asarray(sigma0))
rasterize(hv.Image(np.asarray(sigma0)).opts(cmap='gray',colorbar=True,tools=['hover']))
# manual interp from lut
import xarray as xr
import time
t1 = time.time()
from scipy.interpolate import RectBivariateSpline
xx , yy = np.mgrid[xtrack_val[0]:xtrack_val[-1]+1,atrack_val[0]:atrack_val[-1]+1]
interp_lut = RectBivariateSpline(lut.xtrack,lut.atrack,lut,kx=1,ky=1).ev(xx,yy)
interp_lut = xr.DataArray(interp_lut,dims=['xtrack','atrack'], coords = {'xtrack' : dn_high.xtrack.values , 'atrack' : dn_high.atrack.values})
#xarray interp is slowest than RectBivariateSpline
#interp_lut = lut.interp({'xtrack' : dn_high.xtrack , 'atrack' : dn_high.atrack},method='linear')
sigma0_1 = np.abs(dn_high)**2 / (interp_lut**2)
diff=np.max(np.abs(sigma0_1-sigma0))
print("computed in %2.1f secs. max diff : %f" % (time.time()-t1 , diff))
#(hv.HeatMap(lut) + hv.HeatMap(interp_lut)).opts(hv.opts.HeatMap(axiswise=True,tools=['hover'],colorbar=True)).redim.range(z=(500, 700))
#hv.Image(sigma0)