From 54ba7230d6c092ef874678c7160e6a1ac133618e Mon Sep 17 00:00:00 2001 From: Cameron Scott Bodine Date: Thu, 11 May 2023 08:42:46 -0700 Subject: [PATCH] Speedup wcp_src (#66) Thx @dbuscombe-usgs !! --- src/class_sonObj.py | 9 ++++++--- src/funcs_common.py | 1 + 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/class_sonObj.py b/src/class_sonObj.py index a39955e..f584fb7 100644 --- a/src/class_sonObj.py +++ b/src/class_sonObj.py @@ -1570,15 +1570,18 @@ def _WCR_SRC(self, sonMeta, son=True): #Iterate each ping for j in range(self.sonDat.shape[1]): depth = bedPick[j] # Get depth (in pixels) at nadir + dd = depth**2 # Create 1d array to store relocated bed pixels. Set to -9999 so we ## can later interpolate over gaps. - pingDat = (np.ones((self.sonDat.shape[0])).astype(np.float32)) * -9999 + # pingDat = (np.ones((self.sonDat.shape[0])).astype(np.float32)) * -9999 + pingDat = (np.ones((self.sonDat.shape[0])).astype(np.float32)) * np.nan dataExtent = 0 #Iterate each sonar/ping return for i in range(self.sonDat.shape[0]): if i >= depth: intensity = self.sonDat[i,j] # Get the intensity value - srcIndex = round(np.sqrt(i**2 - depth**2),0).astype(int) #Calculate horizontal range (in pixels) using pathagorean theorem + # srcIndex = round(np.sqrt(i**2 - depth**2),0).astype(int) #Calculate horizontal range (in pixels) using pathagorean theorem + srcIndex = int(round(math.sqrt(i**2 - dd),0)) #Calculate horizontal range (in pixels) using pathagorean theorem pingDat[srcIndex] = intensity # Store intensity at appropriate horizontal range dataExtent = srcIndex # Store range extent (max range) of ping else: @@ -1587,7 +1590,7 @@ def _WCR_SRC(self, sonMeta, son=True): # Process of relocating bed pixels will introduce across track gaps ## in the array so we will interpolate over gaps to fill them. - pingDat[pingDat==-9999] = np.nan + # pingDat[pingDat==-9999] = np.nan nans, x = np.isnan(pingDat), lambda z: z.nonzero()[0] pingDat[nans] = np.interp(x(nans), x(~nans), pingDat[~nans]) diff --git a/src/funcs_common.py b/src/funcs_common.py index a262235..5e30cf5 100644 --- a/src/funcs_common.py +++ b/src/funcs_common.py @@ -44,6 +44,7 @@ from numpy.lib.stride_tricks import as_strided as ast import pandas as pd +import math from collections import defaultdict from copy import deepcopy