def testKwargs(self): # This test makes sure that kwargs are passed through in each of the # types of operation arr = numpy.full((3, 3), .65) * [range(i, i + 3) for i in range(3)] # Test var_unary_operation v = MV2.around(arr, decimals=1) valid = numpy.array([ [0.0, 0.6, 1.3], [0.6, 1.3, 2.0], [1.3, 2.0, 2.6] ]) self.assertTrue(MV2.allequal(v, valid)) xs = [1, -1, -1, 1] ys = [1, 1, -1, -1] angles = [numpy.pi / 4, 3 * numpy.pi / 4, -3 * numpy.pi / 4, -numpy.pi / 4] test_arr = numpy.zeros(4) # Test var_binary_operation MV2.arctan2(ys, xs, out=test_arr) self.assertFalse(MV2.allclose(test_arr, 0)) self.assertTrue(MV2.allequal(test_arr, angles)) # Test var_unary_operation_with_axis values = numpy.tile(numpy.arange(2), 3) self.assertEqual( len(MV2.sometrue(values, axis=0, keepdims=True).shape), len(values.shape))
def vectoravg(hr1, hr2, clocktype): 'Function to test vector-averaging of two time values:' import MV2 sin_avg = (MV2.sin(hrs_to_rad(hr1, clocktype)) + MV2.sin(hrs_to_rad(hr2, clocktype))) / 2 cos_avg = (MV2.cos(hrs_to_rad(hr1, clocktype)) + MV2.cos(hrs_to_rad(hr2, clocktype))) / 2 return rad_to_hrs(MV2.arctan2(sin_avg, cos_avg), clocktype)
def vect2dir(u, v): return rad2deg(MV2.arctan2(v, u))
def spacevavg(tvarb1, tvarb2, sftlf, model): ''' Given a "root filename" and month/year specifications, vector-average lat/lon arrays in an (amplitude, phase) pair of input data files. Each input data file contains diurnal (24h), semidiurnal (12h) and terdiurnal (8h) Fourier harmonic components of the composite mean day/night cycle. Vector-averaging means we consider the input data to be readings on an 8-, 12- or 24-hour clock and separately average the Cartesian components (called "cosine" and "sine" below). Then the averaged components are combined back into amplitude and phase values and returned. Space-averaging is done globally, as well as separately for land and ocean areas. ''' glolf = cdutil.averager(sftlf, axis='xy') print(' Global mean land fraction = %5.3f' % glolf) outD = {} # Output dictionary to be returned by this function harmonics = [1, 2, 3] for harmonic in harmonics: ampl = tvarb1[harmonic - 1] tmax = tvarb2[harmonic - 1] # print ampl[:, :] # print tmax[:, :] clocktype = 24 / harmonic cosine = MV2.cos(hrs_to_rad(tmax, clocktype)) * ampl # X-component sine = MV2.sin(hrs_to_rad(tmax, clocktype)) * ampl # Y-component print( 'Area-averaging globally, over land only, and over ocean only ...' ) # Average Cartesian components ... cos_avg_glo = cdutil.averager(cosine, axis='xy') sin_avg_glo = cdutil.averager(sine, axis='xy') cos_avg_lnd = cdutil.averager(cosine * sftlf, axis='xy') sin_avg_lnd = cdutil.averager(sine * sftlf, axis='xy') cos_avg_ocn = cos_avg_glo - cos_avg_lnd sin_avg_ocn = sin_avg_glo - sin_avg_lnd # ... normalized by land-sea fraction: cos_avg_lnd /= glolf sin_avg_lnd /= glolf cos_avg_ocn /= (1 - glolf) sin_avg_ocn /= (1 - glolf) # Amplitude and phase: # * 86400 Convert kg/m2/s -> mm/d? amp_avg_glo = MV2.sqrt(sin_avg_glo**2 + cos_avg_glo**2) # * 86400 Convert kg/m2/s -> mm/d? amp_avg_lnd = MV2.sqrt(sin_avg_lnd**2 + cos_avg_lnd**2) # * 86400 Convert kg/m2/s -> mm/d? amp_avg_ocn = MV2.sqrt(sin_avg_ocn**2 + cos_avg_ocn**2) pha_avg_glo = MV2.remainder( rad_to_hrs(MV2.arctan2(sin_avg_glo, cos_avg_glo), clocktype), clocktype) pha_avg_lnd = MV2.remainder( rad_to_hrs(MV2.arctan2(sin_avg_lnd, cos_avg_lnd), clocktype), clocktype) pha_avg_ocn = MV2.remainder( rad_to_hrs(MV2.arctan2(sin_avg_ocn, cos_avg_ocn), clocktype), clocktype) if 'CMCC-CM' in model: # print '** Correcting erroneous time recording in ', rootfname pha_avg_lnd -= 3.0 pha_avg_lnd = MV2.remainder(pha_avg_lnd, clocktype) elif 'BNU-ESM' in model or 'CCSM4' in model or 'CNRM-CM5' in model: # print '** Correcting erroneous time recording in ', rootfname pha_avg_lnd -= 1.5 pha_avg_lnd = MV2.remainder(pha_avg_lnd, clocktype) print( 'Converting singleton transient variables to plain floating-point numbers ...' ) amp_avg_glo = float(amp_avg_glo) pha_avg_glo = float(pha_avg_glo) amp_avg_lnd = float(amp_avg_lnd) pha_avg_lnd = float(pha_avg_lnd) amp_avg_ocn = float(amp_avg_ocn) pha_avg_ocn = float(pha_avg_ocn) print( '%s %s-harmonic amplitude, phase = %7.3f mm/d, %7.3f hrsLST averaged globally' % (monthname, harmonic, amp_avg_glo, pha_avg_glo)) print( '%s %s-harmonic amplitude, phase = %7.3f mm/d, %7.3f hrsLST averaged over land' % (monthname, harmonic, amp_avg_lnd, pha_avg_lnd)) print( '%s %s-harmonic amplitude, phase = %7.3f mm/d, %7.3f hrsLST averaged over ocean' % (monthname, harmonic, amp_avg_ocn, pha_avg_ocn)) # Sub-dictionaries, one for each harmonic component: outD['harmonic' + str(harmonic)] = {} outD['harmonic' + str(harmonic)]['amp_avg_lnd'] = amp_avg_lnd outD['harmonic' + str(harmonic)]['pha_avg_lnd'] = pha_avg_lnd outD['harmonic' + str(harmonic)]['amp_avg_ocn'] = amp_avg_ocn outD['harmonic' + str(harmonic)]['pha_avg_ocn'] = pha_avg_ocn return outD
def spacevavg(tvarb1, tvarb2, sftlf, model): ''' Given a "root filename" and month/year specifications, vector-average lat/lon arrays in an (amplitude, phase) pair of input data files. Each input data file contains diurnal (24h), semidiurnal (12h) and terdiurnal (8h) Fourier harmonic components of the composite mean day/night cycle. Vector-averaging means we consider the input data to be readings on an 8-, 12- or 24-hour clock and separately average the Cartesian components (called "cosine" and "sine" below). Then the averaged components are combined back into amplitude and phase values and returned. Space-averaging is done globally, as well as separately for land and ocean areas. ''' glolf = cdutil.averager(sftlf, axis='xy') print(' Global mean land fraction = %5.3f' % glolf) outD = {} # Output dictionary to be returned by this function harmonics = [1, 2, 3] for harmonic in harmonics: ampl = tvarb1[harmonic - 1] tmax = tvarb2[harmonic - 1] # print ampl[:, :] # print tmax[:, :] clocktype = 24 / harmonic cosine = MV2.cos(hrs_to_rad(tmax, clocktype)) * ampl # X-component sine = MV2.sin(hrs_to_rad(tmax, clocktype)) * ampl # Y-component print('Area-averaging globally, over land only, and over ocean only ...') # Average Cartesian components ... cos_avg_glo = cdutil.averager(cosine, axis='xy') sin_avg_glo = cdutil.averager(sine, axis='xy') cos_avg_lnd = cdutil.averager(cosine * sftlf, axis='xy') sin_avg_lnd = cdutil.averager(sine * sftlf, axis='xy') cos_avg_ocn = cos_avg_glo - cos_avg_lnd sin_avg_ocn = sin_avg_glo - sin_avg_lnd # ... normalized by land-sea fraction: cos_avg_lnd /= glolf sin_avg_lnd /= glolf cos_avg_ocn /= (1 - glolf) sin_avg_ocn /= (1 - glolf) # Amplitude and phase: # * 86400 Convert kg/m2/s -> mm/d? amp_avg_glo = MV2.sqrt(sin_avg_glo**2 + cos_avg_glo**2) # * 86400 Convert kg/m2/s -> mm/d? amp_avg_lnd = MV2.sqrt(sin_avg_lnd**2 + cos_avg_lnd**2) # * 86400 Convert kg/m2/s -> mm/d? amp_avg_ocn = MV2.sqrt(sin_avg_ocn**2 + cos_avg_ocn**2) pha_avg_glo = MV2.remainder( rad_to_hrs( MV2.arctan2( sin_avg_glo, cos_avg_glo), clocktype), clocktype) pha_avg_lnd = MV2.remainder( rad_to_hrs( MV2.arctan2( sin_avg_lnd, cos_avg_lnd), clocktype), clocktype) pha_avg_ocn = MV2.remainder( rad_to_hrs( MV2.arctan2( sin_avg_ocn, cos_avg_ocn), clocktype), clocktype) if 'CMCC-CM' in model: # print '** Correcting erroneous time recording in ', rootfname pha_avg_lnd -= 3.0 pha_avg_lnd = MV2.remainder(pha_avg_lnd, clocktype) elif 'BNU-ESM' in model or 'CCSM4' in model or 'CNRM-CM5' in model: # print '** Correcting erroneous time recording in ', rootfname pha_avg_lnd -= 1.5 pha_avg_lnd = MV2.remainder(pha_avg_lnd, clocktype) print('Converting singleton transient variables to plain floating-point numbers ...') amp_avg_glo = float(amp_avg_glo) pha_avg_glo = float(pha_avg_glo) amp_avg_lnd = float(amp_avg_lnd) pha_avg_lnd = float(pha_avg_lnd) amp_avg_ocn = float(amp_avg_ocn) pha_avg_ocn = float(pha_avg_ocn) print('%s %s-harmonic amplitude, phase = %7.3f mm/d, %7.3f hrsLST averaged globally' % (monthname, harmonic, amp_avg_glo, pha_avg_glo)) print('%s %s-harmonic amplitude, phase = %7.3f mm/d, %7.3f hrsLST averaged over land' % (monthname, harmonic, amp_avg_lnd, pha_avg_lnd)) print('%s %s-harmonic amplitude, phase = %7.3f mm/d, %7.3f hrsLST averaged over ocean' % (monthname, harmonic, amp_avg_ocn, pha_avg_ocn)) # Sub-dictionaries, one for each harmonic component: outD['harmonic' + str(harmonic)] = {} outD['harmonic' + str(harmonic)]['amp_avg_lnd'] = amp_avg_lnd outD['harmonic' + str(harmonic)]['pha_avg_lnd'] = pha_avg_lnd outD['harmonic' + str(harmonic)]['amp_avg_ocn'] = amp_avg_ocn outD['harmonic' + str(harmonic)]['pha_avg_ocn'] = pha_avg_ocn return outD