Example #1
0
 def test_add_season_membership_invalid_spec(self):
     season = 'maj'  # not a season!
     with self.assertRaises(ValueError):
         ccat.add_season_membership(self.cube,
                                    'time',
                                    season,
                                    name='maj_season')
Example #2
0
 def test_add_season_membership(self):
     # season membership identifies correct seasons?
     season = "djf"
     ccat.add_season_membership(self.cube, "time", season, name="in_season")
     ccat.add_season(self.cube, "time")
     coord_season = self.cube.coord("season")
     coord_membership = self.cube.coord("in_season")
     season_locations = np.where(coord_season.points == season)[0]
     membership_locations = np.where(coord_membership.points)[0]
     self.assertArrayEqual(membership_locations, season_locations)
Example #3
0
 def test_add_season_membership(self):
     # season membership identifies correct seasons?
     season = 'djf'
     ccat.add_season_membership(self.cube, 'time', season, name='in_season')
     ccat.add_season(self.cube, 'time')
     coord_season = self.cube.coord('season')
     coord_membership = self.cube.coord('in_season')
     season_locations = np.where(coord_season.points == season)[0]
     membership_locations = np.where(coord_membership.points)[0]
     self.assertArrayEqual(membership_locations, season_locations)
 def test_add_season_membership(self):
     # season membership identifies correct seasons?
     season = 'djf'
     ccat.add_season_membership(self.cube, 'time', season,
                                name='in_season')
     ccat.add_season(self.cube, 'time')
     coord_season = self.cube.coord('season')
     coord_membership = self.cube.coord('in_season')
     season_locations = np.where(coord_season.points == season)[0]
     membership_locations = np.where(coord_membership.points)[0]
     self.assertArrayEqual(membership_locations, season_locations)
Example #5
0
def calc_plot_data(cube_data, season):
    '''
    Calculate data to plot for given season.
    Essentially, extract just what we need and collapse time dimension to mean
    :param cube_data: iris cube of source data
    :param season: season as string
    :return: seasonal mean of data as iris cube
    '''
    # extract domain
    cube_data = cube_data.extract(area_con)
    assert not cube_data is None
    # extract season
    # the season membership approach is used because iris doens't like adding a season co-ordinate that
    # includes single month seasons, or doesn't span the whole year
    if type(cube_data) == iris.cube.CubeList:
        for c in cube_data:
            iccat.add_season_membership(c, 'time', season=season)
    else:
        iccat.add_season_membership(cube_data, 'time', season=season)
    plot_data = cube_data.extract(iris.Constraint(season_membership=True))

    # Units conversion if necessary
    if VAR == 'MSLP':
        plot_data.convert_units('hPa')
    elif VAR == 'rainfall':
        if(plot_data.units != 'mm month -1'):
            # convert_units can't handle mm/month.. but we at least check we are converting what we think we are here
            assert plot_data.units == cf_units.Unit('kg m-2 s-1'), 'Expect units kg m-2 s-1, got {}'.format(plot_data.units)
            plot_data = plot_data * 86400 * 30
            plot_data.units = 'mm month -1'
    elif VAR == 'temperature':
        # ensure units are kelvin
        if plot_data.units == 'degrees Celsius':
            plot_data.units = 'celsius'
            plot_data.convert_units('K')
    # Collapse time dimension to mean
    if type(cube_data) == iris.cube.CubeList:
        for i in range(len(cube_data)):
            plot_data[i] = plot_data[i].collapsed('time', iris.analysis.MEAN)
            cube_data[i].remove_coord('season_membership')
    else:
        plot_data = plot_data.collapsed('time', iris.analysis.MEAN)
        cube_data.remove_coord('season_membership')

    # return
    return plot_data
 def test_add_season_membership_invalid_spec(self):
     season = 'maj'   # not a season!
     with self.assertRaises(ValueError):
         ccat.add_season_membership(self.cube, 'time', season,
                                    name='maj_season')
Example #7
0
 def test_add_season_membership_invalid_spec(self):
     season = "maj"  # not a season!
     with self.assertRaises(ValueError):
         ccat.add_season_membership(
             self.cube, "time", season, name="maj_season"
         )