示例#1
0
def plot_maps_ts_from_path(path_nc, var_name, lon, lat, out_path, xaxis_min=0.0, xaxis_max=1.1, xaxis_step=0.1,
                           save_name='fig', xlabel='', start_movie_yr=-1, title='', do_jenks=True,
                           tme_name='time', land_bg=True, cmap=plt.cm.RdBu, grid=False):
    """
    Plot map for var_name variable from netCDF file
    :param path_nc: Name of netCDF file
    :param var_name: Name of variable in netCDF file to plot on map
    :param xaxis_min:
    :param xaxis_max:
    :param xaxis_step:
    :param lon: List of lon's
    :param lat: List of lat's
    :param out_path: Output directory path + file name
    :return: List of paths of images produced, side-effect: save an image(s)
    """
    logger.info('Plotting ' + var_name + ' in ' + path_nc)

    util.make_dir_if_missing(out_path)

    # Read netCDF file and get time dimension
    nc = util.open_or_die(path_nc)

    if start_movie_yr > 0:
        ts = nc.variables[tme_name][:].astype(int)  # time-series
        ts = ts[start_movie_yr - ts[0]:]
    else:
        ts = nc.variables[tme_name][:]  # time-series

    nc.close()

    return plot_maps_ts(path_nc, ts, lon, lat, out_path, var_name=var_name,
                        xaxis_min=xaxis_min, xaxis_max=xaxis_max, xaxis_step=xaxis_step,
                        save_name=save_name, xlabel=xlabel, do_jenks=do_jenks,
                        start_movie_yr=start_movie_yr, title=title, tme_name=tme_name, land_bg=land_bg, cmap=cmap,
                        grid=grid)
示例#2
0
def make_movie(list_images, out_path, out_fname):
    """

    :param list_images:
    :param out_path:
    :return:
    """
    util.make_dir_if_missing(out_path)

    convert_cmd = 'convert -delay 50 -loop 1 '+' '.join(list_images) + ' ' + out_path + os.sep + out_fname
    subprocess.call(convert_cmd, shell=True)
示例#3
0
def make_movie(list_images, out_path, out_fname):
    """

    :param list_images:
    :param out_path:
    :return:
    """
    util.make_dir_if_missing(out_path)

    convert_cmd = 'convert -delay 50 -loop 1 ' + ' '.join(
        list_images) + ' ' + out_path + os.sep + out_fname
    subprocess.call(convert_cmd, shell=True)
示例#4
0
    def __init__(self, out_path, ver='3.2', lon_name='lon', lat_name='lat', tme_name='time', area_name='cell_area'):
        """
        Constructor
        """
        self.list_files_to_del = []  # List of file paths to delete when done

        self.ver = ver
        if self.ver == '3.2':
            self.HYDE_crop_path = constants.HYDE32_CROP_PATH
            self.HYDE_othr_path = constants.HYDE32_OTHR_PATH
            self.HYDE_urbn_path = constants.HYDE32_URBN_PATH
            self.HYDE_past_path = constants.HYDE32_PAST_PATH
            self.HYDE_graz_path = constants.HYDE32_GRAZ_PATH
            self.HYDE_rang_path = constants.HYDE32_RANG_PATH
            self.HYDE_urbn_path = constants.HYDE32_URBN_PATH

            # Land use variables
            self.lus = {self.HYDE_crop_path: ['cropland', 'cropland fraction'],
                        self.HYDE_othr_path: ['other', 'other vegetation fraction'],
                        self.HYDE_urbn_path: ['urban', 'urban fraction'],
                        self.HYDE_graz_path: ['grazing', 'grazing land fraction'],
                        self.HYDE_past_path: ['pasture', 'managed pasture fraction'],
                        self.HYDE_rang_path: ['rangeland', 'rangeland fraction']}
        elif self.ver == '3.1':
            self.HYDE_crop_path = constants.HYDE31_CROP_PATH
            self.HYDE_othr_path = constants.HYDE31_OTHR_PATH
            self.HYDE_past_path = constants.HYDE31_PAST_PATH
            self.HYDE_urbn_path = constants.HYDE31_URBN_PATH

            # Land use variables
            self.lus = {self.HYDE_crop_path: ['cropland', 'cropland'],
                        self.HYDE_othr_path: ['primary', 'primary'],
                        self.HYDE_urbn_path: ['urban', 'urban fraction'],
                        self.HYDE_past_path: ['pasture', 'pasture']}
        elif self.ver == '3.2_v03':
            self.HYDE_crop_path = constants.HYDE32_v03_CROP_PATH
            self.HYDE_othr_path = constants.HYDE32_v03_OTHR_PATH
            self.HYDE_urbn_path = constants.HYDE32_v03_URBN_PATH
            self.HYDE_past_path = constants.HYDE32_v03_PAST_PATH
            self.HYDE_graz_path = constants.HYDE32_v03_GRAZ_PATH
            self.HYDE_rang_path = constants.HYDE32_v03_RANG_PATH
            self.HYDE_urbn_path = constants.HYDE32_v03_URBN_PATH

            # Land use variables
            self.lus = {self.HYDE_crop_path: ['cropland', 'cropland fraction'],
                        self.HYDE_othr_path: ['other', 'other vegetation fraction'],
                        self.HYDE_urbn_path: ['urban', 'urban fraction'],
                        self.HYDE_graz_path: ['grazing', 'grazing land fraction'],
                        self.HYDE_past_path: ['pasture', 'pasture fraction'],
                        self.HYDE_rang_path: ['rangeland', 'rangeland fraction']}
        elif self.ver == '3.2_v1hb':
            self.HYDE_crop_path = constants.HYDE32_v1hb_CROP_PATH
            self.HYDE_othr_path = constants.HYDE32_v1hb_OTHR_PATH
            self.HYDE_urbn_path = constants.HYDE32_v1hb_URBN_PATH
            self.HYDE_past_path = constants.HYDE32_v1hb_PAST_PATH
            self.HYDE_graz_path = constants.HYDE32_v1hb_GRAZ_PATH
            self.HYDE_rang_path = constants.HYDE32_v1hb_RANG_PATH

            # Land use variables
            self.lus = {self.HYDE_crop_path: ['cropland', 'cropland fraction'],
                        self.HYDE_othr_path: ['other', 'other vegetation fraction'],
                        self.HYDE_urbn_path: ['urban', 'urban fraction'],
                        self.HYDE_graz_path: ['grazing', 'grazing land fraction'],
                        self.HYDE_past_path: ['pasture', 'pasture fraction'],
                        self.HYDE_rang_path: ['rangeland', 'rangeland fraction']}
        elif self.ver == '3.2_march':
            self.HYDE_crop_path = constants.HYDE32_march_CROP_PATH
            self.HYDE_othr_path = constants.HYDE32_march_OTHR_PATH
            self.HYDE_urbn_path = constants.HYDE32_march_URBN_PATH
            self.HYDE_past_path = constants.HYDE32_march_PAST_PATH
            self.HYDE_graz_path = constants.HYDE32_march_GRAZ_PATH
            self.HYDE_rang_path = constants.HYDE32_march_RANG_PATH

            # Land use variables
            self.lus = {self.HYDE_crop_path: ['cropland', 'cropland fraction'],
                        self.HYDE_othr_path: ['other', 'other vegetation fraction'],
                        self.HYDE_urbn_path: ['urban', 'urban fraction'],
                        self.HYDE_graz_path: ['grazing', 'grazing land fraction'],
                        self.HYDE_past_path: ['pasture', 'pasture fraction'],
                        self.HYDE_rang_path: ['rangeland', 'rangeland fraction']}

        # Lat, Lon and time dimensions
        self.lon_name = lon_name
        self.lat_name = lat_name
        self.tme_name = tme_name
        self.area_name = area_name
        self.out_path = out_path
        self.movies_path = out_path + os.sep + 'movies'

        util.make_dir_if_missing(self.out_path)
        util.make_dir_if_missing(self.movies_path)

        # Open up netCDF and get dimensions
        ds = util.open_or_die(self.HYDE_crop_path)
        self.lat = ds.variables[lat_name][:]
        self.lon = ds.variables[lon_name][:]
        self.time = ds.variables[tme_name][:]
        ds.close()

        # GLM static data
        self.path_glm_stat = constants.path_glm_stat  # Static data, contains grid cell area (carea)
        self.path_glm_carea = constants.path_glm_carea

        # Get cell area (after subtracting ice/water fraction)
        icwtr_nc = util.open_or_die(self.path_glm_stat)
        icwtr = icwtr_nc.variables[constants.ice_water_frac][:, :]
        self.carea = util.open_or_die(self.path_glm_carea)
        self.carea_wo_wtr = util.open_or_die(self.path_glm_carea) * (1.0 - icwtr)

        # Movie frames
        self.yrs = np.arange(int(min(self.time)), int(max(self.time)), constants.MOVIE_SEP)
        # Get colors for plotting
        self.cols = plot.get_colors(palette='tableau')
示例#5
0
    def plot_HYDE_diff_maps(self, ver31='3.1', ver32alt='3.2'):
        """
        Maps showing spatial differences between HYDE versions
        :param ver31:
        :param ver32alt:
        :return:
        """
        obj_hyde31 = HYDE(constants.HYDE31_OUT_PATH, ver=ver31)
        obj_hyde32a = HYDE(constants.HYDE32_OUT_PATH, ver=ver32alt)

        # Create a reverse look-up table to access HYDE 3.2 paths based on HYDE 3.1 variables
        rev_dict = dict((v[0], k) for k, v in self.lus.iteritems())

        # Create difference map and movie of difference maps HYDE3.2 and HYDE3.2alt
        for path, lu in obj_hyde32a.lus.iteritems():
            imgs_diff_movie = []
            var_hyde32 = lu[0]

            out_dir = self.out_path + os.sep + 'HYDE_diff_maps'
            util.make_dir_if_missing(out_dir)

            for yr in obj_hyde32a.time.tolist()[::constants.MOVIE_SEP]:
                logger.info('Create difference map for year ' + str(int(yr)) + ' for variable ' + var_hyde32)
                out_img_path = out_dir + os.sep + 'hyde_diff_' + var_hyde32 + '_' + str(int(yr)) + '.png'

                cmap = palettable.colorbrewer.diverging.RdYlBu_11_r.mpl_colormap

                diff_nc = util.subtract_netcdf(path, rev_dict[var_hyde32], left_var=lu[0], right_var=var_hyde32,
                                               date=int(yr))
                plot.plot_arr_to_map(diff_nc, self.lon, self.lat, out_path=out_img_path,
                                     var_name='diff_' + var_hyde32 + '_' + str(int(yr)),
                                     xaxis_min=-1.0, xaxis_max=1.1, xaxis_step=0.1, annotate_date=True, yr=int(yr),
                                     xlabel='Difference in fraction of grid cell area \n HYDE 3.2 and ' + ver32alt +
                                            ': ' + var_hyde32, title='',
                                     cmap=cmap, any_time_data=False, land_bg=False, grid=True)

                imgs_diff_movie.append(out_img_path)

            plot.make_movie(imgs_diff_movie, out_dir + os.sep + 'movies', out_fname='Diff_HYDE32Alt_' + lu[0] + '.gif')

        # Create difference map and movie of difference maps HYDE3.2 and HYDE3.1
        for path, lu in obj_hyde31.lus.iteritems():
            imgs_diff_movie = []

            # Variable 'primary' in HYDE 3.1 corresponds to 'other' in HYDE 3.2
            var_hyde32 = lu[0]
            if lu[0] == 'primary':
                var_hyde32 = 'other'
            elif lu[0] == 'pasture':
                var_hyde32 = 'grazing'

            out_dir = self.out_path + os.sep + 'HYDE31_diff_maps'
            util.make_dir_if_missing(out_dir)

            for yr in obj_hyde31.time.tolist()[::constants.MOVIE_SEP]:
                logger.info('Create difference map for year ' + str(int(yr)) + ' for variable ' + var_hyde32)
                out_img_path = out_dir + os.sep + 'hyde31_diff_' + var_hyde32 + '_' + str(int(yr)) + '.png'

                cmap = palettable.colorbrewer.diverging.RdYlBu_11_r.mpl_colormap

                diff_nc = util.subtract_netcdf(rev_dict[var_hyde32], path, left_var=var_hyde32, right_var=lu[0],
                                               date=int(yr))
                plot.plot_arr_to_map(diff_nc, self.lon, self.lat, out_path=out_img_path,
                                     var_name='diff_' + var_hyde32 + '_' + str(int(yr)),
                                     xaxis_min=-1.0, xaxis_max=1.1, xaxis_step=0.1, annotate_date=True, yr=int(yr),
                                     xlabel='Difference in fraction of grid cell area \n HYDE 3.2 and HYDE 3.1: ' +
                                            var_hyde32, title='',
                                     cmap=cmap,
                                     any_time_data=False, land_bg=False, grid=True)

                imgs_diff_movie.append(out_img_path)

            plot.make_movie(imgs_diff_movie, out_dir + os.sep + 'movies', out_fname='Diff_HYDE31_' + lu[0] + '.gif')
示例#6
0
GLM_STRT_YR = parser.getint('PARAMETERS', 'GLM_STRT_YR')
GLM_END_YR = parser.getint('PARAMETERS', 'GLM_END_YR')
FILL_ZEROS = parser.getboolean('PARAMETERS', 'FILL_ZEROS')
TEST_CFT = parser.getboolean('PROJECT', 'TEST_CFT')

# Directories
data_dir = dir_prj + os.sep + parser.get('PATHS', 'data_dir') + os.sep
out_dir = dir_prj + os.sep + parser.get(
    'PATHS', 'out_dir') + os.sep + PROJ_NAME + os.sep
log_dir = out_dir + os.sep + 'Logs'
fao_dir = data_dir + os.sep + parser.get('PATHS', 'fao_dir') + os.sep
hyde_dir = data_dir + os.sep + parser.get('HYDE', 'hyde32_crop_path')

# FAO CONSTANTS
FAO_REGION_CODE = 1000  # FAO country codes maximum limit
RAW_FAO = fao_dir + os.sep + parser.get('PROJECT', 'RAW_FAO')
RAW_FAO_SHT = parser.get('PROJECT', 'RAW_FAO_SHT')
CROP_LUP = fao_dir + os.sep + parser.get('PROJECT', 'CROP_LUP')
CROP_LUP_SHT = parser.get('PROJECT', 'CROP_LUP_SHT')
FAO_CONCOR = fao_dir + os.sep + parser.get('PROJECT', 'FAO_CONCOR')

# Crop rotations csv file
csv_rotations = data_dir + os.sep + parser.get('PROJECT', 'CSV_ROTATIONS')

# Create directories
util.make_dir_if_missing(data_dir)
util.make_dir_if_missing(out_dir)
util.make_dir_if_missing(log_dir)

max_threads = multiprocessing.cpu_count() - 1
示例#7
0
import numpy as np
import pandas as pd

import pygeoutil.util as util
import GLM.constants as constants_glm
import constants
import plots

reload(sys)
sys.setdefaultencoding('utf-8')
pd.options.mode.chained_assignment = None  # default='warn'

# Logging
cur_flname = os.path.splitext(os.path.basename(__file__))[0]
LOG_FILENAME = constants.log_dir + os.sep + 'Log_' + cur_flname + '.txt'
util.make_dir_if_missing(constants.log_dir)
logging.basicConfig(
    filename=LOG_FILENAME,
    level=logging.INFO,
    filemode='w',
    format='%(asctime)s %(levelname)s %(module)s - %(funcName)s: %(message)s',
    datefmt="%m-%d %H:%M"
)  # Logging levels are DEBUG, INFO, WARNING, ERROR, and CRITICAL
# Output to screen
logger = logging.getLogger(cur_flname)
logger.addHandler(logging.StreamHandler(sys.stdout))


class CropStats:
    """
    1. read_raw_FAO_data:
示例#8
0
dir_prj = str(Path(__file__).parents[3])

TAG = parser.get('PROJECT', 'TAG')

# AgNPP
prj_name = parser.get('PROJECT', 'project_name')
base_dir = parser.get('PATHS', 'input_dir')
out_dir = parser.get('PATHS', 'out_dir') + os.sep + prj_dir + os.sep
log_dir = out_dir + os.sep + 'Logs'
inp_dir = prj_dir + os.sep + base_dir + os.sep
AgNPP_fname = parser.get('AgNPP', 'AgNPP_file')
AgNPP_file = inp_dir + os.sep + AgNPP_fname

# Create directories
import pygeoutil.util as util
util.make_dir_if_missing(base_dir)
util.make_dir_if_missing(out_dir)
util.make_dir_if_missing(log_dir)

# Logging
LOG_FILENAME = out_dir + os.sep + 'Log_' + TAG + '.txt'
logging.basicConfig(filename=LOG_FILENAME,level=logging.INFO,\
                    format='%(asctime)s %(levelname)s %(module)s - %(funcName)s: %(message)s',\
                    datefmt="%m-%d %H:%M") # Logging levels are DEBUG, INFO, WARNING, ERROR, and CRITICAL
# Add a rotating handler
logging.getLogger().addHandler(
    logging.handlers.RotatingFileHandler(LOG_FILENAME,
                                         maxBytes=50000,
                                         backupCount=5))
# Output to screen
logging.getLogger().addHandler(logging.StreamHandler())
示例#9
0
import matplotlib.pyplot as plt
import matplotlib.ticker as mtick
import numpy as np
import pandas as pd

import pygeoutil.util as util
import constants
import crop_stats
import plots


# Logging
cur_flname = os.path.splitext(os.path.basename(__file__))[0]
LOG_FILENAME = constants.log_dir + os.sep + 'Log_' + cur_flname + '.txt'
util.make_dir_if_missing(constants.log_dir)
logging.basicConfig(filename=LOG_FILENAME, level=logging.INFO, filemode='w',
                    format='%(asctime)s %(levelname)s %(module)s - %(funcName)s: %(message)s',
                    datefmt="%m-%d %H:%M")  # Logging levels are DEBUG, INFO, WARNING, ERROR, and CRITICAL
# Output to screen
logger = logging.getLogger(cur_flname)
logger.addHandler(logging.StreamHandler(sys.stdout))


class CropRotations:
    def __init__(self):
        self.name_country_col = 'Country_FAO'
        self.cft_type = 'functional crop type'

    @staticmethod
    def get_list_decades(lyrs):
示例#10
0
def output_kml(trans, lon, lat, path_out, xmin, xmax, step, cmap, fname_out='out', name_legend='', label='',
               do_log_cb=False):
    """

    :param trans:
    :param lon:
    :param lat:
    :param path_out:
    :param xmin:
    :param xmax:
    :param step:
    :param cmap:
    :param fname_out:
    :param name_legend:
    :param label:
    :param do_log_cb:
    :return:
    """
    # Return if xmin == xmax
    if xmin == xmax:
        return

    logging.info('output_kml' + fname_out)

    dir_output = path_out + os.sep + 'kml'
    util.make_dir_if_missing(dir_output)

    fig, ax = gearth_fig(llcrnrlon=lon.min(), llcrnrlat=lat.min(), urcrnrlon=lon.max(), urcrnrlat=lat.max())
    lons, lats = np.meshgrid(lon, lat)

    m = Basemap(projection='cyl', resolution='c')
    x, y = m(lons, lats)
    mask_data = maskoceans(lons, lats, trans)
    m.etopo()

    if do_log_cb and np.nanmin(mask_data) > 0.0:
        # manually set log levels e.g. http://matplotlib.org/examples/images_contours_and_fields/contourf_log.html
        lev_exp = np.arange(np.floor(np.log10(np.nanmin(mask_data)) - 1), np.ceil(np.log10(np.nanmax(mask_data)) + 1))
        levs = np.power(10, lev_exp)
        cs = m.contourf(x, y, mask_data, levs, norm=colors.LogNorm(), cmap=cmap)
    else:
        cs = m.contourf(x, y, mask_data, np.arange(xmin, xmax, step), cmap=cmap)
        if abs(xmax - xmin) > 10000.0:
            format = '%.1e'
        elif abs(xmax - xmin) > 100.0:
            format = '%.0f'
        elif abs(xmax - xmin) > 1.0:
            format = '%.1f'
        elif abs(xmax - xmin) > 0.1:
            format = '%.3f'
        else:
            format = '%.4f'
    ax.set_axis_off()
    fig.savefig(dir_output + os.sep + 'kml_' + fname_out + '.png', transparent=False, format='png', dpi=800)

    # Colorbar
    fig = plt.figure(figsize=(1.0, 4.0), facecolor=None, frameon=False)

    # Colorbar legend, numbers represent: [bottom_left_x_coord, bottom_left_y_coord, width, height]
    ax = fig.add_axes([0.02, 0.05, 0.2, 0.9])
    if do_log_cb and np.nanmin(mask_data) >= 0.0:
        cb = fig.colorbar(cs, cax=ax, spacing='uniform')
    else:
        cb = fig.colorbar(cs, cax=ax, format=format, spacing='uniform')
    cb.set_label(name_legend + '\n' + label, rotation=-90, color='k', labelpad=25, size=7)
    cb.ax.tick_params(labelsize=6)
    fig.savefig(dir_output + os.sep + name_legend + '.png', transparent=False, format='png', dpi=125)

    make_kml(llcrnrlon=lon.min(), llcrnrlat=lat.min(), urcrnrlon=lon.max(), urcrnrlat=lat.max(),
             figs=[dir_output + os.sep + 'kml_' + fname_out + '.png'],
             colorbar=dir_output + os.sep + name_legend + '.png',
             kmzfile=dir_output + os.sep + fname_out + '.kmz', name=fname_out)

    # Delte temp files
    # os.remove(dir_output + os.sep + 'kml_' + fname_out + '.png')
    # os.remove(dir_output + os.sep + name_legend + '.png')

    plt.close('all')
示例#11
0
parser.read('config_AgNPP.txt')

# Get directory path (3 levels up is the parent directory)
dir_prj = str(Path(__file__).parents[3])

TAG = parser.get('PROJECT', 'TAG')

# AgNPP
prj_name = parser.get('PROJECT', 'project_name')
base_dir = parser.get('PATHS', 'input_dir')
out_dir = parser.get('PATHS', 'out_dir') + os.sep + prj_dir + os.sep
log_dir = out_dir + os.sep + 'Logs'
inp_dir = prj_dir + os.sep + base_dir + os.sep
AgNPP_fname = parser.get('AgNPP', 'AgNPP_file')
AgNPP_file = inp_dir + os.sep + AgNPP_fname

# Create directories
import pygeoutil.util as util
util.make_dir_if_missing(base_dir)
util.make_dir_if_missing(out_dir)
util.make_dir_if_missing(log_dir)

# Logging
LOG_FILENAME   = out_dir + os.sep + 'Log_' + TAG + '.txt'
logging.basicConfig(filename=LOG_FILENAME,level=logging.INFO,\
                    format='%(asctime)s %(levelname)s %(module)s - %(funcName)s: %(message)s',\
                    datefmt="%m-%d %H:%M") # Logging levels are DEBUG, INFO, WARNING, ERROR, and CRITICAL
# Add a rotating handler
logging.getLogger().addHandler(logging.handlers.RotatingFileHandler(LOG_FILENAME, maxBytes=50000, backupCount=5))
# Output to screen
logging.getLogger().addHandler(logging.StreamHandler())
示例#12
0
CFT_FRAC_YR = parser.getint('PARAMETERS', 'CFT_FRAC_YR')
GLM_STRT_YR = parser.getint('PARAMETERS', 'GLM_STRT_YR')
GLM_END_YR = parser.getint('PARAMETERS', 'GLM_END_YR')
FILL_ZEROS = parser.getboolean('PARAMETERS', 'FILL_ZEROS')
TEST_CFT = parser.getboolean('PROJECT', 'TEST_CFT')

# Directories
data_dir = dir_prj + os.sep + parser.get('PATHS', 'data_dir') + os.sep
out_dir = dir_prj + os.sep + parser.get('PATHS', 'out_dir') + os.sep + PROJ_NAME + os.sep
log_dir = out_dir + os.sep + 'Logs'
fao_dir = data_dir + os.sep + parser.get('PATHS', 'fao_dir') + os.sep
hyde_dir = data_dir + os.sep + parser.get('HYDE', 'hyde32_crop_path')

# FAO CONSTANTS
FAO_REGION_CODE = 1000 # FAO country codes maximum limit
RAW_FAO = fao_dir + os.sep + parser.get('PROJECT', 'RAW_FAO')
RAW_FAO_SHT = parser.get('PROJECT', 'RAW_FAO_SHT')
CROP_LUP = fao_dir + os.sep + parser.get('PROJECT', 'CROP_LUP')
CROP_LUP_SHT = parser.get('PROJECT', 'CROP_LUP_SHT')
FAO_CONCOR = fao_dir + os.sep + parser.get('PROJECT', 'FAO_CONCOR')

# Crop rotations csv file
csv_rotations = data_dir + os.sep + parser.get('PROJECT', 'CSV_ROTATIONS')

# Create directories
util.make_dir_if_missing(data_dir)
util.make_dir_if_missing(out_dir)
util.make_dir_if_missing(log_dir)

max_threads = multiprocessing.cpu_count() - 1
示例#13
0
    def __init__(self, res='q'):
        """
        :param res: Resolution of output dataset: q=quarter, h=half, o=one
        :return:
        """
        # Dictionary of crop functional types
        self.cft = {
            'C4Annual': ['maize.asc', 'millet.asc', 'sorghum.asc'],
            'C4Perren': ['sugarcane.asc'],
            'C3Perren': [
                'banana.asc', 'berry.asc', 'citrus.asc', 'fruittree.asc',
                'grape.asc', 'palm.asc', 'tropevrgrn.asc'
            ],
            'Ntfixing': [
                'alfalfa.asc', 'bean.asc', 'legumehay.asc', 'peanut.asc',
                'soybean.asc'
            ],
            'C3Annual': [
                'beet.asc', 'cassava.asc', 'cotton.asc', 'flax.asc',
                'hops.asc', 'mixedcover.asc', 'nursflower.asc', 'oat.asc',
                'potato.asc', 'rapeseed.asc', 'rice.asc', 'rye.asc',
                'safflower.asc', 'sunflower.asc', 'tobacco.asc',
                'vegetable.asc', 'wheat.asc'
            ],
            'TotlRice': ['rice.asc', 'xrice.asc']
        }

        # Get shape of file
        self.skiprows = 6
        self.res = res
        self.tmpdata = util.open_or_die(path_file=GLM.constants.MFD_DATA_DIR +
                                        os.sep + 'maize.asc',
                                        skiprows=self.skiprows,
                                        delimiter=' ')
        self.asc_hdr = util.get_ascii_header(
            path_file=GLM.constants.MFD_DATA_DIR + os.sep + 'maize.asc',
            getrows=self.skiprows)
        self.yshape = self.tmpdata.shape[0]
        self.xshape = self.tmpdata.shape[1]

        # Create empty numpy arrays
        self.c4annual = numpy.zeros(shape=(self.yshape, self.xshape))
        self.c4perren = numpy.zeros(shape=(self.yshape, self.xshape))
        self.c3perren = numpy.zeros(shape=(self.yshape, self.xshape))
        self.ntfixing = numpy.zeros(shape=(self.yshape, self.xshape))
        self.c3annual = numpy.zeros(shape=(self.yshape, self.xshape))
        self.totlrice = numpy.zeros(shape=(self.yshape, self.xshape))
        self.totlcrop = numpy.zeros(shape=(self.yshape, self.xshape))

        self.c4anarea = numpy.zeros(shape=(self.yshape, self.xshape))
        self.c4prarea = numpy.zeros(shape=(self.yshape, self.xshape))
        self.c3prarea = numpy.zeros(shape=(self.yshape, self.xshape))
        self.ntfxarea = numpy.zeros(shape=(self.yshape, self.xshape))
        self.c3anarea = numpy.zeros(shape=(self.yshape, self.xshape))
        self.croparea = numpy.zeros(shape=(self.yshape, self.xshape))

        # Area of each cell in Monfreda dataset
        self.mfd_area = numpy.zeros(shape=(self.yshape, self.xshape))

        # Ice-water fraction and other static data
        self.icwtr = util.open_or_die(GLM.constants.path_GLM_stat)

        # Read in area file based on res
        if res == 'q':
            self.area_data = util.open_or_die(
                path_file=GLM.constants.CELL_AREA_Q)
        elif res == 'h':
            self.area_data = util.open_or_die(
                path_file=GLM.constants.CELL_AREA_H)
        elif res == 'o':
            self.area_data = util.open_or_die(
                path_file=GLM.constants.CELL_AREA_O)
        else:
            logging.info('Incorrect resolution for output of Monfreda')

        # Compute cell area (excluding ice-water fraction)
        self.cell_area = util.open_or_die(GLM.constants.path_GLM_carea)
        self.land_area = self.cell_area * (
            1.0 - self.icwtr.variables[GLM.constants.ice_water_frac][:, :])

        # Get FAO country concordance list
        self.fao_id = pandas.read_csv(
            GLM.constants.FAO_CONCOR)[['Country_FAO', 'ISO']]

        # Output path
        self.out_path = GLM.constants.out_dir + os.sep + 'Monfreda'
        util.make_dir_if_missing(self.out_path)
示例#14
0
def plot_maps_ts_from_path(path_nc,
                           var_name,
                           lon,
                           lat,
                           out_path,
                           xaxis_min=0.0,
                           xaxis_max=1.1,
                           xaxis_step=0.1,
                           save_name='fig',
                           xlabel='',
                           start_movie_yr=-1,
                           title='',
                           do_jenks=True,
                           tme_name='time',
                           land_bg=True,
                           cmap=plt.cm.RdBu,
                           grid=False):
    """
    Plot map for var_name variable from netCDF file
    :param path_nc: Name of netCDF file
    :param var_name: Name of variable in netCDF file to plot on map
    :param xaxis_min:
    :param xaxis_max:
    :param xaxis_step:
    :param lon: List of lon's
    :param lat: List of lat's
    :param out_path: Output directory path + file name
    :return: List of paths of images produced, side-effect: save an image(s)
    """
    logger.info('Plotting ' + var_name + ' in ' + path_nc)

    util.make_dir_if_missing(out_path)

    # Read netCDF file and get time dimension
    nc = util.open_or_die(path_nc)

    if start_movie_yr > 0:
        ts = nc.variables[tme_name][:].astype(int)  # time-series
        ts = ts[start_movie_yr - ts[0]:]
    else:
        ts = nc.variables[tme_name][:]  # time-series

    nc.close()

    return plot_maps_ts(path_nc,
                        ts,
                        lon,
                        lat,
                        out_path,
                        var_name=var_name,
                        xaxis_min=xaxis_min,
                        xaxis_max=xaxis_max,
                        xaxis_step=xaxis_step,
                        save_name=save_name,
                        xlabel=xlabel,
                        do_jenks=do_jenks,
                        start_movie_yr=start_movie_yr,
                        title=title,
                        tme_name=tme_name,
                        land_bg=land_bg,
                        cmap=cmap,
                        grid=grid)
示例#15
0
def output_kml(trans,
               lon,
               lat,
               path_out,
               xmin,
               xmax,
               step,
               cmap,
               fname_out='out',
               name_legend='',
               label='',
               do_log_cb=False):
    """

    :param trans:
    :param lon:
    :param lat:
    :param path_out:
    :param xmin:
    :param xmax:
    :param step:
    :param cmap:
    :param fname_out:
    :param name_legend:
    :param label:
    :param do_log_cb:
    :return:
    """
    # Return if xmin == xmax
    if xmin == xmax:
        return

    logging.info('output_kml' + fname_out)

    dir_output = path_out + os.sep + 'kml'
    util.make_dir_if_missing(dir_output)

    fig, ax = gearth_fig(llcrnrlon=lon.min(),
                         llcrnrlat=lat.min(),
                         urcrnrlon=lon.max(),
                         urcrnrlat=lat.max())
    lons, lats = np.meshgrid(lon, lat)

    m = Basemap(projection='cyl', resolution='c')
    x, y = m(lons, lats)
    mask_data = maskoceans(lons, lats, trans)
    m.etopo()

    if do_log_cb and np.nanmin(mask_data) > 0.0:
        # manually set log levels e.g. http://matplotlib.org/examples/images_contours_and_fields/contourf_log.html
        lev_exp = np.arange(np.floor(np.log10(np.nanmin(mask_data)) - 1),
                            np.ceil(np.log10(np.nanmax(mask_data)) + 1))
        levs = np.power(10, lev_exp)
        cs = m.contourf(x,
                        y,
                        mask_data,
                        levs,
                        norm=colors.LogNorm(),
                        cmap=cmap)
    else:
        cs = m.contourf(x,
                        y,
                        mask_data,
                        np.arange(xmin, xmax, step),
                        cmap=cmap)
        if abs(xmax - xmin) > 10000.0:
            format = '%.1e'
        elif abs(xmax - xmin) > 100.0:
            format = '%.0f'
        elif abs(xmax - xmin) > 1.0:
            format = '%.1f'
        elif abs(xmax - xmin) > 0.1:
            format = '%.3f'
        else:
            format = '%.4f'
    ax.set_axis_off()
    fig.savefig(dir_output + os.sep + 'kml_' + fname_out + '.png',
                transparent=False,
                format='png',
                dpi=800)

    # Colorbar
    fig = plt.figure(figsize=(1.0, 4.0), facecolor=None, frameon=False)

    # Colorbar legend, numbers represent: [bottom_left_x_coord, bottom_left_y_coord, width, height]
    ax = fig.add_axes([0.02, 0.05, 0.2, 0.9])
    if do_log_cb and np.nanmin(mask_data) >= 0.0:
        cb = fig.colorbar(cs, cax=ax, spacing='uniform')
    else:
        cb = fig.colorbar(cs, cax=ax, format=format, spacing='uniform')
    cb.set_label(name_legend + '\n' + label,
                 rotation=-90,
                 color='k',
                 labelpad=25,
                 size=7)
    cb.ax.tick_params(labelsize=6)
    fig.savefig(dir_output + os.sep + name_legend + '.png',
                transparent=False,
                format='png',
                dpi=125)

    make_kml(llcrnrlon=lon.min(),
             llcrnrlat=lat.min(),
             urcrnrlon=lon.max(),
             urcrnrlat=lat.max(),
             figs=[dir_output + os.sep + 'kml_' + fname_out + '.png'],
             colorbar=dir_output + os.sep + name_legend + '.png',
             kmzfile=dir_output + os.sep + fname_out + '.kmz',
             name=fname_out)

    # Delte temp files
    # os.remove(dir_output + os.sep + 'kml_' + fname_out + '.png')
    # os.remove(dir_output + os.sep + name_legend + '.png')

    plt.close('all')