def test_interp (self): input = self.var.get() # Interpolation along X axis for slope in (float('nan'),0,1,2): output = interpolate(self.var, inaxis=self.x, outaxis=self.x2, interp_type='linear', d_above=slope, d_below=slope).transpose(YAxis,XAxis).get() if np.isnan(slope): self.assertTrue(np.all(np.isnan(output[:,0])), output) self.assertTrue(np.all(np.isnan(output[:,-1])), output) self.assertTrue(np.all(np.isfinite(output[:,1:-1])), output) else: self.assertTrue(np.all(np.isfinite(output)), output) # Check extrapolation (out of range by half a coordinate unit) self.assertTrue(np.all(output[:,0] == input[:,0] - 0.5*slope), output) self.assertTrue(np.all(output[:,-1] == input[:,-1] + 0.5*slope), output) # Interpolation along Y axis for slope in (float('nan'),0,1,2): output = interpolate(self.var, inaxis=self.y, outaxis=self.y2, interp_type='linear', d_above=slope, d_below=slope).transpose(YAxis,XAxis).get() if np.isnan(slope): self.assertTrue(np.all(np.isnan(output[0,:])), output) self.assertTrue(np.all(np.isnan(output[-1,:])), output) self.assertTrue(np.all(np.isfinite(output[1:-1,:])), output) else: self.assertTrue(np.all(np.isfinite(output)), output) # Check extrapolation (out of range by half a coordinate unit) self.assertTrue(np.all(output[0,:] == input[0,:] - 0.5*slope), output) self.assertTrue(np.all(output[-1,:] == input[-1,:] + 0.5*slope), output)
def interp2theta(var, theta, values, interp='linear', **kwargs): from pygeode.interp import interpolate, sorted # inaxis axis iaxis = var.whichaxis(ZAxis) # # sort theta (must be monotonically increasing) # var = sorted(var, iaxis, reverse=False) # theta = sorted(theta, iaxis, reverse=False) # prepare input inaxis = var.axes[iaxis] assert theta.hasaxis( inaxis), 'vertical axis of var and theta are incompatible' # create new axis outaxis = Isentrope(values=values, **kwargs) # interpolate to isentropic levels ivar = interpolate(var, inaxis, outaxis, inx=theta, interp_type=interp) # return variable interpolated to isentropic level(s) return ivar ## Dynamical Tropopause # axis for PV iso-surfaces (dynamical tropopause) class DynamicalTP(ZAxis): name = 'PViso' # default name units = '(K m^2)/(s kg)' plotatts = ZAxis.plotatts.copy() plotatts['formatstr'] = '%3.1f' # one digit behind decimal # Formatting attributes for axis labels and ticks (see formatter method for application) plotatts[ 'plottitle'] = 'Dynamical TP' # name displayed in plots (axis label) plotatts[ 'plotunits'] = 'PVU' # displayed units (after offset and scalefactor have been applied) plotatts[ 'scalefactor'] = 1e6 # conversion factor; assumed units are meters
def verticalVelocity(w, z, interpType='linear', **kwargs): from pygeode.axis import Height, ZAxis from pygeode.interp import interpolate from warnings import warn # figure out axes oldZ = findZAxis(w) assert isinstance(z, ZAxis), 'interpolation only works along vertical axis' if (not isinstance(z, Height)) or (not isinstance(oldZ, Height)): warn( 'The current implementation of verticalVelocity is designed for interpolation between Height coordinates only.' ) assert oldZ.__class__ == z.__class__, 'old and new axes are not of the same type (class)' # interpolate values vv = interpolate(w, oldZ, z, interp_type=interpType) # attributes (defaults) vv.atts['name'] = 'w' vv.atts['units'] = 'm/s' vv.atts['long_name'] = 'vertical velocity on full model levels' vv.atts['standard_name'] = 'vertical velocity' vv.atts['interpolation'] = interpType # plot attributes (defaults) vv.plotatts = variablePlotatts['w'] # apply user-defined attributes (from kwargs; override defaults) vv.atts.update(kwargs) # assign short name vv.name = vv.atts['name'] vv.units = vv.atts['units'] return vv
def interp2theta(var, theta, values, interp='linear', **kwargs): from pygeode.interp import interpolate, sorted # inaxis axis iaxis = var.whichaxis(ZAxis) # # sort theta (must be monotonically increasing) # var = sorted(var, iaxis, reverse=False) # theta = sorted(theta, iaxis, reverse=False) # prepare input inaxis = var.axes[iaxis] assert theta.hasaxis(inaxis), 'vertical axis of var and theta are incompatible' # create new axis outaxis = Isentrope(values=values, **kwargs) # interpolate to isentropic levels ivar = interpolate(var, inaxis, outaxis, inx=theta, interp_type=interp) # return variable interpolated to isentropic level(s) return ivar ## Dynamical Tropopause # axis for PV iso-surfaces (dynamical tropopause) class DynamicalTP(ZAxis): name = 'PViso' # default name units = '(K m^2)/(s kg)' plotatts = ZAxis.plotatts.copy() plotatts['formatstr'] = '%3.1f' # one digit behind decimal # Formatting attributes for axis labels and ticks (see formatter method for application) plotatts['plottitle'] = 'Dynamical TP' # name displayed in plots (axis label) plotatts['plotunits'] = 'PVU' # displayed units (after offset and scalefactor have been applied) plotatts['scalefactor'] = 1e6 # conversion factor; assumed units are meters
def verticalVelocity(w, z, interpType="linear", **kwargs): from pygeode.axis import Height, ZAxis from pygeode.interp import interpolate from warnings import warn # figure out axes oldZ = findZAxis(w) assert isinstance(z, ZAxis), "interpolation only works along vertical axis" if (not isinstance(z, Height)) or (not isinstance(oldZ, Height)): warn( "The current implementation of verticalVelocity is designed for interpolation between Height coordinates only." ) assert oldZ.__class__ == z.__class__, "old and new axes are not of the same type (class)" # interpolate values vv = interpolate(w, oldZ, z, interp_type=interpType) # attributes (defaults) vv.atts["name"] = "w" vv.atts["units"] = "m/s" vv.atts["long_name"] = "vertical velocity on full model levels" vv.atts["standard_name"] = "vertical velocity" vv.atts["interpolation"] = interpType # plot attributes (defaults) vv.plotatts = variablePlotatts["w"] # apply user-defined attributes (from kwargs; override defaults) vv.atts.update(kwargs) # assign short name vv.name = vv.atts["name"] vv.units = vv.atts["units"] return vv
def verticalVelocity(w, z, interpType='linear', **kwargs): from pygeode.axis import Height, ZAxis from pygeode.interp import interpolate from warnings import warn # figure out axes oldZ = findZAxis(w) assert isinstance(z,ZAxis), 'interpolation only works along vertical axis' if (not isinstance(z,Height)) or (not isinstance(oldZ,Height)): warn('The current implementation of verticalVelocity is designed for interpolation between Height coordinates only.') assert oldZ.__class__ == z.__class__, 'old and new axes are not of the same type (class)' # interpolate values vv = interpolate(w, oldZ, z, interp_type=interpType) # attributes (defaults) vv.atts['name'] = 'w' vv.atts['units'] = 'm/s' vv.atts['long_name'] = 'vertical velocity on full model levels' vv.atts['standard_name'] = 'vertical velocity' vv.atts['interpolation'] = interpType # plot attributes (defaults) vv.plotatts = variablePlotatts['w'] # apply user-defined attributes (from kwargs; override defaults) vv.atts.update(kwargs) # assign short name vv.name = vv.atts['name'] vv.units = vv.atts['units'] return vv
def test_nointerp (self): input = self.var.get() output = interpolate(self.var, inaxis=self.x, outaxis=self.x, interp_type='linear').transpose(YAxis,XAxis).get() # GSL interoplates over nans, so we need to undo this for a direct comparison masked_output = np.array(output) masked_output[1,1] = float('nan') self.assertTrue(alleq(masked_output,input), output) self.assertEqual(output[1,1], 5.) # check how GSL interoplates over the nan # Repeat, but for the y axis output = interpolate(self.var, inaxis=self.y, outaxis=self.y, interp_type='linear').transpose(YAxis,XAxis).get() # GSL interoplates over nans, so we need to undo this for a direct comparison masked_output = np.array(output) masked_output[1,1] = float('nan') self.assertTrue(alleq(masked_output,input), output) self.assertEqual(output[1,1], 5.) # check how GSL interoplates over the nan
def test_2d_interp(self): input = self.var.get() output = interpolate(self.var, inaxis=self.x, outaxis=self.x6, inx=self.xfield, interp_type='linear').transpose(YAxis,XAxis).get() nan = float('nan') expected = np.array([[1,2,3,nan,nan],[nan,4,5,6,nan],[nan,nan,7,8,9],[nan,10,11,12,nan]]) self.assertTrue(alleq(output,expected), output)
def test_1(): from pygeode.axis import XAxis from pygeode.interp import interpolate # Input data inaxis = XAxis([3,2,1]) indata = inaxis ** 2 # Output axis outaxis = XAxis([1.5, 2.5]) outdata = interpolate(indata, inaxis=inaxis, outaxis=outaxis, interp_type='linear') outdata = list(outdata.get())
def test_2(): from pygeode.axis import XAxis, YAxis from pygeode.interp import interpolate # Input data inaxis = XAxis([3,2,1]) extra_axis = YAxis([2,4,6]) indata = inaxis ** 2 + extra_axis*0 # Output axis outaxis = XAxis([1.5, 2.5]) outx = outaxis + extra_axis*0 outdata = interpolate(indata, inaxis=inaxis, outaxis=outaxis, outx=outx, interp_type='linear') outdata = list(outdata.get())
def test_1(): from pygeode.axis import XAxis from pygeode.interp import interpolate # Input data inaxis = XAxis([3, 2, 1]) indata = inaxis**2 # Output axis outaxis = XAxis([1.5, 2.5]) outdata = interpolate(indata, inaxis=inaxis, outaxis=outaxis, interp_type='linear') outdata = list(outdata.get())
def test_2(): from pygeode.axis import XAxis, YAxis from pygeode.interp import interpolate # Input data inaxis = XAxis([3, 2, 1]) extra_axis = YAxis([2, 4, 6]) indata = inaxis**2 + extra_axis * 0 # Output axis outaxis = XAxis([1.5, 2.5]) outx = outaxis + extra_axis * 0 outdata = interpolate(indata, inaxis=inaxis, outaxis=outaxis, outx=outx, interp_type='linear') outdata = list(outdata.get())
def test_nm_interp (self): # Interpolation with non-monotonic source data - should fail with ValueError output = interpolate(self.var, inaxis=self.y, outaxis=self.y2, inx=self.y_nm, interp_type='linear').transpose(YAxis,XAxis).get()