コード例 #1
0
    def __init__(self, **kwargs):
        kwargs = basic.dict_set_default(kwargs, **default_dataset_attrs)

        super().__init__(**kwargs)

        self.database = kwargs.pop('database', 'Madrigal')
        self.facility = kwargs.pop('facility', 'DMSP')
        self.instrument = kwargs.pop('instrument', 'SSIES&SSM')
        self.product = kwargs.pop('product', 's1')
        self.allow_download = kwargs.pop('allow_download', False)
        self.force_download = kwargs.pop('force_download', False)
        self.add_AACGM = kwargs.pop('add_AACGM', False)
        self.add_APEX = kwargs.pop('add_APEX', False)
        self.calib_orbit = kwargs.pop('calib_orbit', False)
        self.replace_orbit = kwargs.pop('replace_orbit', False)

        self.sat_id = kwargs.pop('sat_id', None)

        self.metadata = None

        allow_load = kwargs.pop('allow_load', False)

        # self.config(**kwargs)

        if self.loader is None:
            self.loader = default_Loader

        if self.downloader is None:
            self.downloader = default_Downloader

        self._validate_attrs()

        if allow_load:
            self.load_data()
コード例 #2
0
ファイル: panels.py プロジェクト: JouleCai/geospacelab
    def overlay_bar(self, *args, ax=None, **kwargs):

        var = args[0]
        il = None
        data = self._retrieve_data_1d(var)
        x = data['x'].flatten()
        height = data['y'].flatten()

        bar_config = basic.dict_set_default(kwargs,
                                            **var.visual.plot_config.bar)

        color_by_value = bar_config.pop('color_by_value', False)
        colormap = bar_config.pop('colormap', 'viridis')
        vmin = bar_config.pop('vmin', np.nanmin(height))
        vmax = bar_config.pop('vmax', np.nanmax(height))
        if color_by_value:
            if isinstance(colormap, str):
                colormap = mpl.cm.get_cmap(colormap)
            norm = mpl_colors.Normalize(vmin=vmin, vmax=vmax)
            colors = colormap(norm(height))
            bar_config.update(color=colors)

        # plot_config = basic.dict_set_default(plot_config, **self._default_plot_config)
        ib = ax.bar(x, height, **bar_config)
        return [ib]
コード例 #3
0
ファイル: __init__.py プロジェクト: JouleCai/geospacelab
    def __init__(self, **kwargs):
        kwargs = basic.dict_set_default(kwargs, **default_dataset_attrs)

        super().__init__(**kwargs)

        self.database = kwargs.pop('database', 'SuperDARN')
        self.facility = kwargs.pop('facility', 'SuperDARN')
        self.product = kwargs.pop('product', 'POTMAP')
        self.allow_download = kwargs.pop('allow_download', False)

        self.pole = kwargs.pop('pole', 'N')
        self.load_append_support_data = kwargs.pop('append_support_data', True)

        self.metadata = None

        allow_load = kwargs.pop('allow_load', False)

        # self.config(**kwargs)

        if self.loader is None:
            self.loader = default_Loader

        if self.downloader is None:
            self.downloader = None

        self._validate_attrs()

        if allow_load:
            self.load_data()
コード例 #4
0
ファイル: __init__.py プロジェクト: JouleCai/geospacelab
    def __init__(self, **kwargs):
        kwargs = basic.dict_set_default(kwargs, **default_dataset_attrs)

        super().__init__(**kwargs)

        self.database = kwargs.pop('database', 'TUD')
        self.facility = kwargs.pop('facility', 'GRACE')
        self.instrument = kwargs.pop('instrument', 'ACC')
        self.product = kwargs.pop('product', 'WND-ACC')
        self.product_version = kwargs.pop('product_version', 'v01')
        self.local_latest_version = ''
        self.allow_download = kwargs.pop('allow_download', False)
        self.force_download = kwargs.pop('force_download', False)
        self.add_AACGM = kwargs.pop('add_AACGM', False)
        self.add_APEX = kwargs.pop('add_APEX', False)
        self._data_root_dir = self.data_root_dir  # Record the initial root dir

        self.sat_id = kwargs.pop('sat_id', 'A')

        self.metadata = None

        allow_load = kwargs.pop('allow_load', False)

        # self.config(**kwargs)

        if self.loader is None:
            self.loader = default_Loader

        if self.downloader is None:
            self.downloader = default_Downloader

        self._validate_attrs()

        if allow_load:
            self.load_data()
コード例 #5
0
ファイル: __init__.py プロジェクト: JouleCai/geospacelab
    def __init__(self, **kwargs):
        kwargs = basic.dict_set_default(kwargs, **default_dataset_attrs)

        super().__init__(**kwargs)

        self.database = cdaweb_database
        self.facility = kwargs.pop('facility', '')
        self.omni_type = kwargs.pop('omni_type', 'omni2')
        self.omni_res = kwargs.pop('omni_res', '1min')
        self.data_file_type = kwargs.pop('data_file_type', '')
        self.allow_download = kwargs.pop('allow_download', True)

        self.metadata = None

        allow_load = kwargs.pop('allow_load', False)

        # self.config(**kwargs)

        if self.loader is None:
            self.loader = default_Loader

        if self.downloader is None:
            self.downloader = default_Downloader

        self._set_default_variables(
            default_variable_names,
            configured_variables=var_config.configured_variables)

        self._validate_attrs()

        if allow_load:
            self.load_data()
コード例 #6
0
    def load_cdf_data(self):
        """
        load the data from the cdf file
        :return:
        """
        cdf_file = cdflib.CDF(self.file_path)
        cdf_info = cdf_file.cdf_info()
        variables = cdf_info['zVariables']

        if dict(self.variable_name_dict):
            new_dict = {vn: vn for vn in variables}
            self.variable_name_dict = pybasic.dict_set_default(
                self.variable_name_dict, **new_dict)

        for var_name, cdf_var_name in self.variable_name_dict.items():
            if var_name == 'CDF_EPOCH':
                epochs = cdf_file.varget(variable=cdf_var_name)
                epochs = cdflib.cdfepoch.unixtime(epochs)
                dts = [
                    datetime.timedelta(seconds=epoch) +
                    datetime.datetime(1970, 1, 1, 0, 0, 0) for epoch in epochs
                ]
                self.variables['SC_DATETIME'] = np.array(dts).reshape(
                    (len(dts), 1))
                continue
            var = cdf_file.varget(variable=cdf_var_name)
            var = np.array(var)
            vshape = var.shape
            if len(vshape) == 1:
                var = var.reshape(vshape[0], 1)
            self.variables[var_name] = var
コード例 #7
0
ファイル: __init__.py プロジェクト: JouleCai/geospacelab
    def __init__(self, **kwargs):
        kwargs = basic.dict_set_default(kwargs, **default_dataset_attrs)

        super().__init__(**kwargs)

        self.database = kwargs.pop('database', 'JHUAPL')
        self.facility = kwargs.pop('facility', 'DMSP')
        self.instrument = kwargs.pop('instrument', 'SSUSI')
        self.product = kwargs.pop('product', 'SDR-DISK')
        self.allow_download = kwargs.pop('allow_download', True)

        self.sat_id = kwargs.pop('sat_id', '')
        self.orbit_id = kwargs.pop('orbit_id', None)
        self.pole = kwargs.pop('pole', '')
        self.pp_type = kwargs.pop('pp_type', '')

        self.metadata = None

        allow_load = kwargs.pop('allow_load', False)

        # self.config(**kwargs)

        if self.loader is None:
            self.loader = default_Loader

        if self.downloader is None:
            self.downloader = default_Downloader

        self._validate_attrs()

        if allow_load:
            self.load_data()
コード例 #8
0
    def overlay_sites(self, site_ids=None, coords=None, cs=None, **kwargs):
        kwargs = pybasic.dict_set_default(kwargs, color='k', linestyle='', markersize=5, marker='.')

        cs_new = self.cs_transform(cs_fr=cs, coords=coords)
        
        isc = self().plot(cs_new['lon'], cs_new['lat'], transform=ccrs.PlateCarree(), **kwargs)
        return isc
コード例 #9
0
    def __init__(self, **kwargs):
        kwargs = basic.dict_set_default(kwargs, **default_dataset_attrs)

        super().__init__(**kwargs)

        self.database = kwargs.pop('database', '')
        self.product = kwargs.pop('product', '')
        self.allow_download = kwargs.pop('allow_download', True)

        self.metadata = None

        allow_load = kwargs.pop('allow_load', False)

        # self.config(**kwargs)

        if self.loader is None:
            self.loader = default_Loader

        if self.downloader is None:
            self.downloader = default_Downloader

        self._set_default_variables(
            default_variable_names,
            configured_variables=var_config.configured_variables
        )

        self._validate_attrs()

        if allow_load:
            self.load_data()
コード例 #10
0
ファイル: panels.py プロジェクト: JouleCai/geospacelab
    def overlay_line(self, var, ax=None, errorbar='off', **kwargs):
        """
        Overlay a line plot in the axes.
        :param var: A GeospaceLab Variable object
        :param ax: The axes to plot.
        :param errorbar: If 'on', show errorbar.
        :param kwargs: Other keyword arguments forwarded to ax.plot() or ax.errorbar()
        :return:
        """
        il = None

        data = self._retrieve_data_1d(var)
        x = data['x']
        y = data['y']
        y_err = data['y_err']

        plot_config = basic.dict_set_default(kwargs,
                                             **var.visual.plot_config.line)
        plot_config = basic.dict_set_default(plot_config,
                                             **self._default_plot_config)
        if self.axes_overview[ax]['twinx'] in ['on', 'self']:
            var.visual.axis[1].label = '@v.label'
        label = var.get_visual_axis_attr(axis=2, attr_name='label')
        plot_config = basic.dict_set_default(plot_config, label=label)
        if errorbar == 'off':
            il = ax.plot(x, y, **kwargs)
        elif errorbar == 'on':
            errorbar_config = dict(plot_config)
            errorbar_config.update(var.visual.plot_config.errorbar)
            il = ax.errorbar(x.flatten(),
                             y.flatten(),
                             yerr=y_err.flatten(),
                             **errorbar_config)
        if type(il) is not list:
            il = [il]
        self.axes_overview[ax]['lines'].extend(il)
        if self.axes_overview[ax]['legend'] is None:
            self.axes_overview[ax]['legend'] = 'on'
        elif self.axes_overview[ax]['legend'] == 'off':
            var.visual.axis[1].label = '@v.label'
        return il
コード例 #11
0
ファイル: panels.py プロジェクト: JouleCai/geospacelab
    def _check_legend(self, ax):
        ax_ov = self.axes_overview[ax]
        var_for_config = ax_ov['variables'][0]
        legend_config = var_for_config.visual.plot_config.legend
        # get color
        legend_config = basic.dict_set_default(legend_config,
                                               **self._default_legend_config)

        if len(ax_ov['lines']) == 1 and ax_ov['lines'][0]._label == '_line0':
            return

        ax.legend(handles=ax_ov['lines'], **legend_config)
コード例 #12
0
    def overlay_cross_track_vector(
            self, vector, unit_vector, sc_ut=None, sc_coords=None, cs=None, *,
            unit_vector_scale=0.1, vector_unit='',
            color='r', alpha=0.5, quiverkey_config={},
            **kwargs):

        cs_new = self.cs_transform(cs_fr=cs, coords=sc_coords, ut=sc_ut)

        if self.pole == 'N':
            ind_lat = np.where(cs_new['lat'] > self.boundary_lat)[0]
        else:
            ind_lat = np.where(cs_new['lat'] < self.boundary_lat)[0]

        lat_in = cs_new['lat'][ind_lat]
        lon_in = cs_new['lon'][ind_lat]
        dts_in = sc_ut[ind_lat]
        vector = vector[ind_lat]

        data = self.projection.transform_points(ccrs.PlateCarree(), lon_in, lat_in)
        xdata = np.float32(data[:, 0])
        ydata = np.float32(data[:, 1])
        slope = mathtool.calc_curve_tangent_slope(xdata, ydata, degree=3)

        width = np.float32(self._extent[1] - self._extent[0])
        vector = vector / unit_vector * width * unit_vector_scale
        sign = np.sign(xdata[1] - xdata[0])
        xq = xdata
        yq = ydata
        uq1 = - sign * vector * np.sin(slope)
        vq1 = sign * vector * np.cos(slope)

        iq = self.major_ax.quiver(
            xq, yq, uq1, vq1,
            units='xy', angles='xy', scale=1., scale_units='xy',
            width=0.001 * (self._extent[1] - self._extent[0]),
            headlength=0, headaxislength=0, pivot='tail', color=color, alpha=alpha, **kwargs
        )

        # Add quiverkey
        quiverkey_config = pybasic.dict_set_default(
            quiverkey_config, X=0.9, Y=0.95, U=width*unit_vector_scale,
            linewidth=0.01 * (self._extent[1] - self._extent[0]), label=str(unit_vector) + ' ' + vector_unit, color=color,
        )
        X = quiverkey_config.pop('X')
        Y = quiverkey_config.pop('Y')
        U = quiverkey_config.pop('U')
        label = quiverkey_config.pop('label')
        iqk = self.major_ax.quiverkey(
            iq, X, Y, U, label, **kwargs
        )

        return iq
コード例 #13
0
    def set_layout(self, num_rows=None, num_cols=None, **kwargs):
        """
        Set the layout of the dashboard in a canvas using matplotlib GridSpec.

        :param num_rows: The number of rows of the grid.
        :type num_rows: int.
        :param num_cols: The number of columns of the grid.
        :type num_cols: int.
        :param kwargs: Optional keyword arguments used in **matplotlib GridSpec**.
        """
        kwargs = pybasic.dict_set_default(kwargs,
                                          **self._default_layout_config)
        self.gs = self.figure.add_gridspec(num_rows, num_cols)
        self.gs.update(**kwargs)
コード例 #14
0
ファイル: __init__.py プロジェクト: JouleCai/geospacelab
    def __init__(self, **kwargs):
        kwargs = basic.dict_set_default(kwargs, **default_dataset_attrs)

        super().__init__(**kwargs)

        self.database = kwargs.pop('database', '')
        self.facility = kwargs.pop('facility', '')
        self.site = kwargs.pop('site', MillstoneHillSite('MillstoneHill'))
        self.antenna = kwargs.pop('antenna', '')
        self.experiment = kwargs.pop('experiment', '')
        self.exp_name_pattern = kwargs.pop('exp_name_pattern', '')
        self.exp_check = kwargs.pop('exp_check', False)
        self.pulse_code = kwargs.pop('pulse_code', '')
        self.pulse_length = kwargs.pop('pulse_length', '')
        self.data_file_type = kwargs.pop('data_file_type', '')
        self.affiliation = kwargs.pop('affiliation', '')
        self.allow_download = kwargs.pop('allow_download', True)
        self.beam_location = kwargs.pop('beam_location', True)
        self.metadata = None

        allow_load = kwargs.pop('allow_load', True)
        self.status_control = kwargs.pop('status_control', False)
        self.residual_control = kwargs.pop('residual_control', False)

        # self.config(**kwargs)

        if self.loader is None:
            self.loader = default_Loader
        if self.downloader is None:
            self.downloader = default_Downloader

        self._set_default_variables(
            default_variable_names,
            configured_variables=var_config.configured_variables)

        self._validate_attrs()

        if allow_load:
            self.load_data()
コード例 #15
0
    def __init__(self, **kwargs):
        kwargs = basic.dict_set_default(kwargs, **default_dataset_attrs)

        super().__init__(**kwargs)

        self.database = kwargs.pop('database', 'ESA/EarthOnline')
        self.facility = kwargs.pop('facility', 'SWARM')
        self.instrument = kwargs.pop('instrument', 'EFI-LP')
        self.product = kwargs.pop('product', 'HM02')
        self.product_version = kwargs.pop('product_version', '')
        self.local_latest_version = ''
        self.allow_download = kwargs.pop('allow_download', False)
        self.force_download = kwargs.pop('force_download', False)
        self.quality_control = kwargs.pop('quality_control', False)
        self.calib_control = kwargs.pop('calib_control', False)
        self.add_AACGM = kwargs.pop('add_AACGM', False)
        self.add_APEX = kwargs.pop('add_AACGM', False)
        self._data_root_dir_init = copy.deepcopy(
            self.data_root_dir)  # Record the initial root dir

        self.sat_id = kwargs.pop('sat_id', 'A')

        self.metadata = None

        allow_load = kwargs.pop('allow_load', False)

        # self.config(**kwargs)

        if self.loader is None:
            self.loader = default_Loader

        if self.downloader is None:
            self.downloader = default_Downloader

        self._validate_attrs()

        if allow_load:
            self.load_data()
コード例 #16
0
    def overlay_gridlines(self,
                      lat_res=None, lon_res=None,
                      lat_label_separator=None, lon_label_separator=None,
                      lat_label_format=None, lon_label_format=None,
                      lat_label=True, lat_label_clock=6.5, lat_label_config={},
                      lon_label=True, lon_label_config={},
                      gridlines_config={}):

        default_gridlines_label_config = {
            'lon-fixed': {
                'lat_res': 10.,
                'lon_res': 30.,
                'lat_label_separator': 0,
                'lon_label_separator': 0,
                'lat_label_format': '%d%D%N',
                'lon_label_format': '%d%D%E',
            },
            'mlon-fixed': {
                'lat_res': 10.,
                'lon_res': 30.,
                'lat_label_separator': 0,
                'lon_label_separator': 0,
                'lat_label_format': '%d%D%N',
                'lon_label_format': '%d%D%E',
            },
            'lst-fixed': {
                'lat_res': 10.,
                'lon_res': 15.,
                'lat_label_separator': 0,
                'lon_label_separator': 2,
                'lat_label_format': '%d%D%N',
                'lon_label_format': '%02d LT',
            },
            'mlt-fixed': {
                'lat_res': 10.,
                'lon_res': 15.,
                'lat_label_separator': 0,
                'lon_label_separator': 2,
                'lat_label_format': '%d%D%N',
                'lon_label_format': '%02d MLT',
            },
        }

        def label_formatter(value, fmt=''):
            ms = re.findall(r"(%[0-9]*[a-zA-Z])", fmt)
            fmt_new = copy.deepcopy(fmt)
            patterns = []
            if "%N" in ms:
                if value > 0:
                    lb_ns = 'N'
                elif value < 0:
                    lb_ns = 'S'
                else:
                    lb_ns = ''
                value = np.abs(value)

            if "%E" in ms:
                mod_value = np.mod(value - 180, 360) - 180.
                if mod_value == 0 or mod_value == 180:
                    lb_ew = ''
                elif mod_value < 0:
                    lb_ew = 'W'
                else:
                    lb_ew = 'E'
                value = np.abs(mod_value)

            for ind_m, m in enumerate(ms):
                if m == "%N":
                    fmt_new = fmt_new.replace(m, '{}')
                    patterns.append(lb_ns)
                elif m == "%E":
                    fmt_new = fmt_new.replace(m, '{}')
                    patterns.append(lb_ew)
                elif m == "%D":
                    continue
                else:
                    if 'd' in m:
                        value = int(value)
                    fmt_new = fmt_new.replace(m, '{:' + m[1:] + '}')
                    patterns.append(value)
            string_new = fmt_new.format(*patterns)
            if "%D" in string_new:
                splits = string_new.split("%D")
                string_new = splits[0] + r'$^{\circ}$' + splits[1]

            return string_new

        default_label_config = default_gridlines_label_config[self.style]
        if lat_res is None:
            lat_res = default_label_config['lat_res']
        if lon_res is None:
            lon_res = default_label_config['lon_res']
        if lat_label_separator is None:
            lat_label_separator = default_label_config['lat_label_separator']
        if lon_label_separator is None:
            lon_label_separator = default_label_config['lon_label_separator']
        if lat_label_format is None:
            lat_label_format = default_label_config['lat_label_format']
        if lon_label_format is None:
            lon_label_format = default_label_config['lon_label_format']

        # xlocator
        if self.style == 'lst-fixed':
            lon_shift = (self.ut - datetime.datetime(
                self.ut.year, self.ut.month, self.ut.day, self.ut.hour
            )).total_seconds() / 86400. * 360
        else:
            lon_shift = 0
        num_lons = 360. // lon_res + 1.
        lons = np.linspace(-180., 180., int(num_lons)) - lon_shift
        xlocator = mticker.FixedLocator(lons)

        # ylocator
        lat_fr = np.abs(self.boundary_lat) + np.mod(90 - np.abs(self.boundary_lat), lat_res)
        lat_to = 90.
        # num_lats = (lat_to - lat_fr) / lat_res + 1.
        lats = np.arange(lat_fr, lat_to, lat_res) * np.sign(self.lat_c)
        ylocator = mticker.FixedLocator(lats)

        pybasic.dict_set_default(gridlines_config, color='#331900', linewidth=0.5, linestyle=':',
                                     draw_labels=False)
        gl = self().gridlines(crs=ccrs.PlateCarree(), **gridlines_config)

        gl.xlocator = xlocator
        gl.ylocator = ylocator

        if lat_label:
            pybasic.dict_set_default(lat_label_config, fontsize=plt.rcParams['font.size'] - 2, fontweight='roman',
                    ha='center', va='center',
                    family='fantasy', alpha=0.9
                )
            if self.pole == 'S':
                clock = lat_label_clock / 12 * 360
                rotation = 180 - clock
                if self.mirror_south:
                    clock = - clock
                    rotation = 180 + clock
            else:
                clock = np.mod(180. - lat_label_clock / 12 * 360, 360)
                rotation = clock
            label_lon = self.lon_c + clock
            for ind, lat in enumerate(lats):
                if np.mod(ind, lat_label_separator+1) != 0:
                    continue
                # if lat > 0:
                #     label = r'' + '{:d}'.format(int(lat)) + r'$^{\circ}$N'
                # elif lat < 0:
                #     label = r'' + '{:d}'.format(int(-lat)) + r'$^{\circ}$S'
                # else:
                #     label = r'' + '{:d}'.format(int(0)) + r'$^{\circ}$S'
                label = label_formatter(lat, fmt=lat_label_format)
                self().text(
                    label_lon, lat, label, transform=ccrs.PlateCarree(),
                    rotation=rotation,
                    **lat_label_config
                )

        if lon_label:
            pybasic.dict_set_default(
                lon_label_config,
                va='center', ha='center',
                family='fantasy', fontweight='ultralight')
            lb_lons = np.arange(0, 360., lon_res)
            if self.pole == 'S':
                lon_shift_south = 180.
            else:
                lon_shift_south = 0
            if self.style == 'lon-fixed' or self.style == 'mlon-fixed':
                lb_lons_loc = lb_lons
            elif self.style == 'lst-fixed':
                lb_lons_loc = np.mod(self.lon_c + (lb_lons - self.lst_c / 24 * 360) + lon_shift_south, 360)
            elif self.style == 'mlt-fixed':
                lb_lons_loc = lb_lons

            if self.boundary_style == 'circle':
                lb_lats = np.empty_like(lb_lons)
                lb_lats[:] = self.boundary_lat
                data = self.projection.transform_points(ccrs.PlateCarree(), lb_lons_loc, lb_lats)
                xdata = data[:, 0]
                ydata = data[:, 1]
                scale = (self._extent[1] - self._extent[0]) * 0.03
                xdata = xdata + scale * np.sin((lb_lons_loc - self.lon_c) * np.pi / 180.)
                ydata = ydata - np.sign(self.lat_c) * scale * np.cos((lb_lons_loc - self.lon_c) * np.pi / 180.)
                for ind, lb_lon in enumerate(lb_lons):
                    if np.mod(ind, lon_label_separator+1) != 0:
                        continue
                    x = xdata[ind]
                    y = ydata[ind]
                    # if self.style in ['lon-fixed', 'mlon-fixed']:
                    #     lb_lon = np.mod(lb_lon + 180, 360) - 180
                    #     if lb_lon == 0 or np.abs(lb_lon) == 180.:
                    #         label = r'' + '{:d}'.format(int(np.abs(lb_lon))) + r'$^{\circ}$'
                    #     if lb_lon < 0:
                    #         label = r'' + '{:d}'.format(int(-lb_lon)) + r'$^{\circ}$W'
                    #     else:
                    #         label = r'' + '{:d}'.format(int(lb_lon)) + r'$^{\circ}$E'
                    # elif self.style == 'mlt-fixed':
                    #     label = '{:d}'.format(int(lb_lon / 15)) + ' MLT'
                    # elif self.style == 'lst-fixed':
                    #     label = '{:d}'.format(int(lb_lon / 15)) + ' LT'
                    if self.style in ['lon-fixed', 'mlon-fixed']:
                        lb_value = lb_lon
                    elif self.style in ['lst-fixed', 'mlt-fixed']:
                        lb_value = lb_lon / 15
                    label = label_formatter(lb_value, fmt=lon_label_format)
                    if self.pole == 'S':
                        rotation = self.lon_c - lb_lons_loc[ind]
                        if self.mirror_south:
                            rotation = -self.lon_c + lb_lons_loc[ind]
                    else:
                        rotation = (lb_lons_loc[ind] - self.lon_c) + 180
                    self().text(
                        x, y, label,
                        rotation=rotation,
                        **lon_label_config,
                    )
コード例 #17
0
    def add_colorbar(self,
                     im,
                     ax=None,
                     cax=None,
                     cax_position=None,
                     cax_scale=None,
                     cax_label=None,
                     cax_ticks=None,
                     cax_tick_labels=None,
                     cax_label_config=None,
                     cax_tick_label_step=1,
                     **kwargs):
        """
        Add a colorbar appended to the parent ax.

        :param im: the mappable
        :param cax: If None, create a colorbar axes using the default settings, see also matplotlib.colorbar.Colorbar.
        :type cax: axes instance or {None, 'new'}
        :param ax: The appended ax instance.
        :type ax: matplotlib.pyplot.Axes isntance.
        :param cax_position: If not None, set the colorbar ax position at [x, y, width, height], which are normalized to the main ax coordinates.
        :type cax_position: 4-tuple, default: [1.02, 0.01, 0.025, 0.85].
        :param cax_label_config: Optional keyword arguments for setting the colorbar label.
        :param kwargs: Other optional keyword arguments forwarded to matplotlib.colorbar.Colorbar.
        :return: The colarbar instance
        """

        if cax_label_config is None:
            cax_label_config = {}

        if cax == 'new':
            if cax_position is None:
                cax_position = [1.02, 0.01, 0.025, 0.85]

            pos_ax = ax.get_position()
            # convert from Axes coordinates to Figure coordinates
            pos_cax = [
                pos_ax.x0 + (pos_ax.x1 - pos_ax.x0) * cax_position[0],
                pos_ax.y0 + (pos_ax.y1 - pos_ax.y0) * cax_position[1],
                (pos_ax.x1 - pos_ax.x0) * cax_position[2],
                (pos_ax.y1 - pos_ax.y0) * cax_position[3],
            ]
            cax = self.add_axes(pos_cax)
            cax_label_config = {
                'rotation': 270,
                'va': 'bottom',
                'size': 'medium'
            }

        icb = self.figure.colorbar(im, cax=cax, **kwargs)

        self.sca(ax)

        # set colorbar label
        cax_label_config = pybasic.dict_set_default(cax_label_config,
                                                    rotation=270,
                                                    va='bottom',
                                                    size='small')
        if cax_label is not None:
            icb.set_label(cax_label, **cax_label_config)

        ylim = cax.get_ylim()
        # set cticks
        if cax_ticks is not None:
            icb.ax.yaxis.set_ticks(cax_ticks)
            if cax_tick_labels is not None:
                icb.ax.yaxis.set_ticklabels(cax_tick_labels)
        else:
            if cax_scale == 'log':
                num_major_ticks = int(np.ceil(np.diff(np.log10(ylim)))) * 2
                cax.yaxis.set_major_locator(
                    mpl.ticker.LogLocator(base=10.0, numticks=num_major_ticks))
                n = cax_tick_label_step
                [
                    l.set_visible(False)
                    for (i, l) in enumerate(cax.yaxis.get_ticklabels())
                    if i % n != 0
                ]
                # [l.set_ha('right') for (i, l) in enumerate(cax.yaxis.get_ticklabels()) if i % n != 0]
                minorlocator = mpl.ticker.LogLocator(base=10.0,
                                                     subs=(0.1, 0.2, 0.3, 0.4,
                                                           0.5, 0.6, 0.7, 0.8,
                                                           0.9),
                                                     numticks=12)
                cax.yaxis.set_minor_locator(minorlocator)
                cax.yaxis.set_minor_formatter(mpl.ticker.NullFormatter())
        cax.yaxis.set_tick_params(labelsize='x-small')