def take_average(self): anoms_by_grid = {} for key in self.obs_by_grid: anoms_by_grid[key] = [qc.winsorised_mean(self.obs_by_grid[key]), len(self.obs_by_grid[key])] self.obs_by_grid = {} return anoms_by_grid
def grid_super_obs(reps, intype): ''' Aggregate a list of :class:`.MarineReport` onto a 1x1x5-day grid using resistant averages :param reps: list of MarineReports to be gridded :param intype: one of either 'SST' for gridding SST or 'MAT' for gridding MAT :type reps: :class:`.MarineReport` :type intype: string :return: a dictionary whose keys are :class:`.Gridpt` instances and which contain two-element lists with the robust average and the number of observations contributing to it. :rtype: dictionary of list of floats. Keys are :class:`.Gridpt` The function grids anomalies from a list of marine reports. To save space and generally speed things up, the whole grid is never calculated. Instead, a dictionary is used. The keys are the populated grid boxes (as :class:`.Gridpt` objects) and lists are stored for each key. The output is in this format with the list under each key containing an average anomaly and a count of observations. ''' assert (intype == 'SST' or intype == 'AT'), intype # this does a simple gridding of observations of type "MarineReport" # with observations and averages for each grid cell held in dictionaries # it returns a dictionary with an entry for each occupied grid cell # and each entry is a list with two elements: # the gridcell average and the number of obs contributing to that average #make dictionary (obs_by_grid) of all anomalies referenced by grid point obs_by_grid = {} for this_report in reps: rep = Gridpt(this_report.getvar('LAT'), this_report.getvar('LON'), this_report.getvar('YR'), this_report.getvar('MO'), this_report.getvar('DY')) x = this_report.getanom(intype) if x != None: if rep in obs_by_grid: obs_by_grid[rep].append(x) else: obs_by_grid[rep] = [x] #calculate the trimmed mean of the super obs in each grid box #loop over all the keys in obs_by_grid. Each key is the #index for a different grid box #and each entry in obs_by_grid is a list of anomalies for #which we want to take the trimmed mean #and count the available observations anoms_by_grid = {} for key in obs_by_grid: anoms_by_grid[key] = [qc.winsorised_mean(obs_by_grid[key]), len(obs_by_grid[key])] return anoms_by_grid
def test_longer_ascending_run_two_outliers(self): inarr = [ 1., 2., 3., 4., 5., 6., 7., 8., 9., 10., 11., 12., 13., 14., 99., 1000. ] self.assertEqual(8.5, qc.winsorised_mean(inarr))
def test_longer_ascending_run_large_outlier(self): inarr = [1., 2., 3., 4., 5., 6., 700.] self.assertEqual(4.0, qc.winsorised_mean(inarr))
def test_ascending(self): inarr = [3., 4., 5.] self.assertEqual(4.0, qc.winsorised_mean(inarr))
def test_three_fours(self): inarr = [4.0, 4.0, 4.0] self.assertEqual(4.0, qc.winsorised_mean(inarr))
def test_all_zeroes(self): inarr = [0, 0, 0, 0, 0, 0, 0, 0, 0] self.assertEqual(0.0, qc.winsorised_mean(inarr))