def plotRTEMSpectra(resultsList): xData = np.array([result.source.wavelength for result in resultsList]) RTEData = np.array( [np.sq(np.abs(result.rTE)) for result in resultsList]) RTMData = np.array( [np.sq(np.abs(result.rTM)) for result in resultsList]) fig, ax = plt.subplots(2, 1) ax[0].plot(xData, RTEData) ax[1].plot(xData, RTMData) ax[0].set_title('RTE and RTM vs wavelength') ax[0].set_ylabel('RTE') ax[1].set_title('RTM vs wavelength') ax[1].set_xlabel('wavelength (um)') ax[1].set_ylabel('RTM') fig.suptitle('RTE / TM vs wavelength', fontsize=16)
def calculate_beta_squircle(self, a, ss, rr, identifier): """takes all points of the workspace, parameter s and radious r of squircle for every point and returns the value of beta, eventually all this wont be required. a: points ss: value of paramter s rr: radius of the sqauircle identifier: 0 if object is an obstacle and 1 if its workspace """ beta = [] for i in range(len(a)): x = a[:, 0][i] y = a[:, 1][i] z = a[:, 2][i] temp1 = (2 - 4 * ss**2) * (x * y)**2 temp2 = (x**2 + y**2 + np.sqrt(x**4 + y**4 + temp1)) / 2 beta_temp = (temp2 + z**2) / 2 + sq(temp2**2 + z**4 + (2 - 4 * ss**2) * temp2 * z**2) / 2 - rr**2 if identifier == 1 and beta_temp >= 0: beta_temp = 0 elif identifier == 0 and beta_temp <= 0: beta_temp = 0 if identifier == 1: beta.append(-beta_temp) else: beta.append(beta_temp) #if identifier == 1: # beta = [-i for i in beta] return beta
def calculate_beta_squircle (self, a, ss, rr, identifier): """takes all points of the workspace, parameter s and radious r of squircle for every point and returns the value of beta, eventually all this wont be required. a: points ss: value of paramter s rr: radius of the sqauircle identifier: 0 if object is an obstacle and 1 if its workspace """ #x = a[:,0]; y = a[:,1]; z = a[:,2] beta = []#; beta_has_nan = False #print ss, rr for i in range(len(a)): x = a[:,0][i]; y = a[:,1][i]; z = a[:,2][i] temp1 = (2 - 4 * ss**2) * (x * y)**2 temp2 = (x**2 + y**2 + np.sqrt(x**4 + y**4 + temp1)) / 2 beta_temp = (temp2 + z**2)/2 + sq(temp2**2 + z**4 + (2-4*ss**2)*temp2*z**2)/2 - rr**2 if identifier == 1 and beta_temp >= 0: beta.append(-beta_temp) else: beta.append(beta_temp) """TODO: cross-check if reverting sign of beta for boundary is needed""" #print beta_has_nan if identifier == 0: return beta else: beta = [-i for i in beta] return beta
def nav_func(self, q): Beta, b0, b1, b2, b3, b4 = self.get_beta(q) dBeta_dq = -2*(q-self.q0)*b1*b2*b3*b4 + \ 2*b0*((q-self.q1)*b2*b3*b4 + (q-self.q2)*b3*b4*b1 + \ (q-self.q3)*b4*b1*b2 + (q-self.q4)*b1*b2*b3) a = nrm(q - self.qd)**2 b = nrm(q - self.qd)**(2 * self.k) + Beta c = pwr(b, 1.0 / self.k) phi0 = a / c dphi_dq = 2 * (q - self.qd) / c - (a / (self.k * b * c)) * ( 2 * self.k * pwr(sq(a), 2 * self.k - 2) * (q - self.qd) + dBeta_dq) return phi0, dphi_dq
def calculate_beta(self, a, ss, rr, identifier): """takes all points of the workspace, parameter s and radious r of squircle for every point and returns the value of beta, eventually all this wont be required. a: points ss: value of paramter s rr: radius of the sqauircle identifier: 0 if object is an obstacle and 1 if its workspace """ x = a[:, 0] y = a[:, 1] z = a[:, 2] temp1 = (2 - 4 * ss**2) * (x * y)**2 temp2 = (x**2 + y**2 + np.sqrt(x**4 + y**4 + temp1)) / 2.0 beta_temp = (temp2 + z**2) / 2 + sq(temp2**2 + z**4 + (2 - 4 * ss**2) * temp2 * z**2) / 2 - rr**2 if identifier == 1: beta_temp = [i if i < 0 else 0 for i in beta_temp] beta = [-1.0 * j for j in beta_temp] else: beta_temp = [i if i > 0 else 0 for i in beta_temp] beta = [j for j in beta_temp] return np.asarray(beta)
def constraintEquation(qi, i): id = l2i(i, "t0") t0, t1, t2, t3 = qi[id], qi[id + 1], qi[id + 2], qi[id + 3] constraintVector = sq(t0) + sq(t1) + sq(t2) + sq(t3) - 1 return constraintVector
def __init__(self,path=None,root='./',format='nodc',verbose=True): self.pr=[] pr = profile() if path is not None: if type(path) is list: flist=[];prof_dict={} for file in path: if format == 'nodc': try: f=netCDF4.Dataset(root+file['path']) except: if verbose: print('Unable to open '+root+file['path']) continue date_string = file['date_string'] pr.data={} pr.data['DataType']=f.variables['data_type'][:].tostring() pr.data['ref_dateTime']=f.variables['reference_date_time'][:].tostring() pr.data['time']=f.variables['juld'][:][0] reft = pr.data['ref_dateTime'] reft = 'days since '+reft[0:4]+'-'+reft[4:6]+'-'+reft[6:9] pr.data['ncDateTime_indx']=parser.parse(date_string) pr.data['ncDateTime']=netCDF4.num2date(pr.data['time'],reft) pr.data['wmoid']=f.variables['platform_number'][:].tostring() pr.data['cycle_number']=f.variables['cycle_number'][:][0] pr.data['direction']=f.variables['direction'][:].tostring() pr.data['latitude']=f.variables['latitude'][:][0] pr.data['longitude']=f.variables['longitude'][:][0] pr.data['pressure']=sq(f.variables['pres'][:] ) try: pr.data['pressure_adj']=sq(f.variables['pres_adjusted'][:]) except: pr.data['pressure_adj']=None pr.data['temp']=sq(f.variables['temp'][:]) try: pr.data['temp_adj']=sq(f.variables['temp_adjusted'][:]) except: pr.data['temp_adj']=None try: pr.data['salt']=sq(f.variables['psal'][:]) except: pr.data['salt']=None try: pr.data['salt_adj']=sq(f.variables['psal_adjusted'][:]) except: pr.data['salt_adj']=None try: pr.data['positioning_system']=string.join(f.variables['positioning_system'][:]) except: pr.data['positioning_system']=None try: pr.data['position_qc']=string.join(f.variables['position_qc'][:]) except: pr.data['position_qc']=None # Avoid confusion with direction attribute try: dp = numpy.roll(pr.data['pressure'],shift=-1)-pr.data['pressure'] except: dp = [0] if numpy.isscalar(dp): dp=[dp] if len(dp) > 4: dp = dp[:-2] if len(dp) > 4: if numpy.all(dp>0): pr.data['direction']='A' pr.data['min_dp']=numpy.min(dp) elif numpy.all(dp<0): pr.data['direction']='D' pr.data['min_dp']=numpy.min(-dp) else: continue else: continue pr_new = copy.copy(pr) self.pr.append(pr_new) elif format == 'pangea': f=open(file['path']) pr.data={} pr.data['DataType']=None pr.data['ref_dateTime']=None pr.data['ncDateTime']=file['ncDateTime']
#%% import numpy #%% numpy.sqrt(5) #%% import numpy as np #%% np.sqrt(5) #%% from numpy import sqrt as sq, cbrt as cu #%% sq(5) #%% cu(8) #%% a = np.array([1,2,3]) print(a) type(a) #%% a.shape #%% b = np.array([[1,2],[3,4],[5,6]])
def build(fle, R, parameters): dforward = parameters[13] dstarboard = parameters[14] twtcorr = parameters[15] raycorr = parameters[16] # Fancy characters (degree sign, lower-case sigma, plus-minus) deg = u'\N{DEGREE SIGN}' sig = u'\u03C3' pm = u'\u00b1' # Anonymous functions and formatting strings to make things more tractable. dst = lambda rx, ry, rz, x, y, z: sq((M(rx) - x)**2 + (M(ry) - y)**2 + (M(rz) - z)**2) a = '{:>11} {: >11.5f} {}{: .5f}\n' fmt1 = lambda s1, f1, s2, f2: a.format(s1, f1, s2, f2) b = '{:>3} {: >11.5f} {: <11.5f} {: <11.5f} {: >8.5f} {: >8.5f} {: >8.5f}\n' fmt2 = lambda f1, f2, f3, f4, f5, f6, f7: b.format(f1, f2, f3, f4, f5, f6, f7) c = '{:>14} {:<11} {:<11} {:<10} {:<9} {}' # .txt header and main results. fle.write('Bootstrap Inversion Results (' + pm + ' 2' + sig + ' uncertainty)\n') fle.write('\n') fle.write('{:>11} {} \n'.format('Station:', R['sta'])) fle.write( fmt1('Lat:', M(R['lat_sta']), deg + ' ' + pm, 2 * S(R['lat_sta']))) fle.write( fmt1('Lon:', M(R['lon_sta']), deg + ' ' + pm, 2 * S(R['lon_sta']))) fle.write(fmt1('X:', M(R['x_sta']), 'm ' + pm, 2 * S(R['x_sta']))) fle.write(fmt1('Y:', M(R['y_sta']), 'm ' + pm, 2 * S(R['y_sta']))) fle.write(fmt1('Depth:', M(R['z_sta']), 'm ' + pm, 2 * S(R['z_sta']))) fle.write(fmt1('Water Vel.:', M(R['vpws']), 'm ' + pm, 2 * S(R['vpws']))) fle.write(fmt1('Drift:', M(R['drifts']), 'm ' + pm, 2 * S(R['drifts']))) fle.write(fmt1('Drift Az:', M(R['azs']), 'm ' + pm, 2 * S(R['azs']))) fle.write(fmt1('dz:', M(R['dzs']), 'm ' + pm, 2 * S(R['dzs']))) fle.write('\n') fle.write( fmt1('RMS:', M(R['E_rms'] * 1000), 'ms ' + pm, 2 * S(R['E_rms']) * 1000)) fle.write('\n') fle.write('{} {} \n'.format('Bad Pings Removed:', R['Nbad'])) fle.write('{} {} \n'.format('Doppler Correction:', ('YES' if twtcorr else 'NO'))) fle.write('{} {} \n'.format('Ray Bending Correction:', ('YES' if raycorr else 'NO'))) fle.write('{} {} {} \n'.format('GPS-Transponder Offset: dforward = ', dforward, ' m')) fle.write('{} {} {} \n'.format(' dstarboard = ', dstarboard, ' m')) fle.write('{:=<80} \n'.format('')) # .txt survey points. fle.write( c.format('Lat (' + deg + ')', 'Lon (' + deg + ')', 'Range (m)', 'Resid (s)', 'Vr (m/s)', 'TWT corr. (ms)\n')) for i in range(len(R['svy_lats'])): fle.write( fmt2( i + 1, R['svy_lats'][i], R['svy_lons'][i], dst(R['x_sta'][i], R['y_sta'][i], R['z_sta'][i], R['svy_xs'][i], R['svy_ys'][i], R['svy_zs'][i]), R['dtwts'][i] * 1000, R['vrs'][i], R['corrs'][i] * 1000)) fle.close()
def angleDiffGoodness(theta1, psi1, theta2, psi2, sigma): if (theta1 == theta2 and psi1 == psi2): return 1 temp = np.sq(np.sin((theta1-theta2)/2)) dist = 2*np.arcsin(np.sqrt( temp + np.cos(theta1)*np.cos(theta2)*np.sq(np.sin(psi1-psi2/2)))) return np.exp(-np.sq(dist)/np.sq(sigma))
def evaluate(vars): r = vars[0][2] goodness = np.exp(-np.sq(r - self.mean)/np.sq(self.sigma)) return -goodness*self.scale #the cost
def __init__(self, path=None, root='./', format='nodc', verbose=True): self.pr = [] pr = profile() if path is not None: if type(path) is list: flist = [] prof_dict = {} for file in path: if format == 'nodc': try: f = netCDF4.Dataset(root + file['path']) except: if verbose: print('Unable to open ' + root + file['path']) continue date_string = file['date_string'] pr.data = {} pr.data['DataType'] = f.variables[ 'data_type'][:].tostring() pr.data['ref_dateTime'] = f.variables[ 'reference_date_time'][:].tostring() pr.data['time'] = f.variables['juld'][:][0] reft = pr.data['ref_dateTime'] reft = 'days since ' + reft[0:4] + '-' + reft[ 4:6] + '-' + reft[6:9] pr.data['ncDateTime_indx'] = parser.parse(date_string) pr.data['ncDateTime'] = netCDF4.num2date( pr.data['time'], reft) pr.data['wmoid'] = f.variables[ 'platform_number'][:].tostring() pr.data['cycle_number'] = f.variables[ 'cycle_number'][:][0] pr.data['direction'] = f.variables[ 'direction'][:].tostring() pr.data['latitude'] = f.variables['latitude'][:][0] pr.data['longitude'] = f.variables['longitude'][:][0] pr.data['pressure'] = sq(f.variables['pres'][:]) try: pr.data['pressure_adj'] = sq( f.variables['pres_adjusted'][:]) except: pr.data['pressure_adj'] = None pr.data['temp'] = sq(f.variables['temp'][:]) try: pr.data['temp_adj'] = sq( f.variables['temp_adjusted'][:]) except: pr.data['temp_adj'] = None try: pr.data['salt'] = sq(f.variables['psal'][:]) except: pr.data['salt'] = None try: pr.data['salt_adj'] = sq( f.variables['psal_adjusted'][:]) except: pr.data['salt_adj'] = None try: pr.data['positioning_system'] = string.join( f.variables['positioning_system'][:]) except: pr.data['positioning_system'] = None try: pr.data['position_qc'] = string.join( f.variables['position_qc'][:]) except: pr.data['position_qc'] = None # Avoid confusion with direction attribute try: dp = numpy.roll(pr.data['pressure'], shift=-1) - pr.data['pressure'] except: dp = [0] if numpy.isscalar(dp): dp = [dp] if len(dp) > 4: dp = dp[:-2] if len(dp) > 4: if numpy.all(dp > 0): pr.data['direction'] = 'A' pr.data['min_dp'] = numpy.min(dp) elif numpy.all(dp < 0): pr.data['direction'] = 'D' pr.data['min_dp'] = numpy.min(-dp) else: continue else: continue pr_new = copy.copy(pr) self.pr.append(pr_new) elif format == 'pangea': f = open(file['path']) pr.data = {} pr.data['DataType'] = None pr.data['ref_dateTime'] = None pr.data['ncDateTime'] = file['ncDateTime']