<commit_before>#!/usr/bin/env python

# import modules
import matplotlib
from re import findall
from numpy import zeros, ones
from itertools import product
import matplotlib.pyplot as plt
from optparse import OptionParser
from netCDF4 import Dataset as nc
from matplotlib.patches import Polygon
from numpy.ma import masked_array, reshape, arange, masked_where, median

# parse inputs
parser = OptionParser()
parser.add_option("-i", "--infile", dest = "infile", default = "metrics.nc4", type = "string",
                  help = "Metrics file", metavar = "FILE")
parser.add_option("-c", "--crop", dest = "crop", default = "maize", type = "string",
                  help = "Crop (or all)")
parser.add_option("-r", "--hareafile", dest = "hareafile", default = "all.global.nc4", type = "string",
                  help = "Harvested area file", metavar = "FILE")
parser.add_option("-v", "--variable", dest = "variable", default = "beta", type = "string",
                  help = "Variable to plot")
parser.add_option("-b", "--boxfile", dest = "boxfile", default = "box.png", type = "string",
                  help = "Output boxplot file", metavar = "FILE")
parser.add_option("-n", "--ncfile", dest = "ncfile", default = "box.nc4", type = "string",
                  help = "Output netcdf data file", metavar = "FILE")
options, args = parser.parse_args()

infile    = options.infile
crop      = options.crop
hareafile = options.hareafile
variable  = options.variable
boxfile   = options.boxfile
ncfile    = options.ncfile

cals = {'maize': 3.60, 'wheat': 3.34, 'soy': 3.35, 'rice': 2.80}

with nc(infile) as f:
    decade = f.variables['decade'][:]
    dunits = f.variables['decade'].units
    decade = decade * 10 + int(findall(r'\d+', dunits)[0])

careas = {}
with nc(hareafile) as f:
    for c in ['maize', 'wheat', 'soy', 'rice']:
        careas[c] = f.variables['area_' + c][:]

models = ['epic', 'gepic', 'lpj-guess', 'lpjml', 'pdssat', 'pegasus'] # exclude image
gcms   = ['gfdl-esm2m', 'hadgem2-es', 'ipsl-cm5a-lr', 'miroc-esm-chem', 'noresm1-m']
crops  = ['maize', 'wheat', 'soy', 'rice'] if crop == 'all' else [crop]
co2s   = ['co2', 'noco2']

hadgemidx = gcms.index('hadgem2-es')

nm, ng, ncr, nd, nco2 = len(models), len(gcms), len(crops), len(decade), len(co2s)

# variable
sh = (nm, ng, ncr, nd, nco2)
varr = masked_array(zeros(sh), mask = ones(sh))
with nc(infile) as f:
    for m, g, c, co in product(range(nm), range(ng), range(ncr), range(nco2)):
        var = '%s_global_%s_%s_%s_%s' % (variable, models[m], gcms[g], crops[c], co2s[co])
        if var in f.variables:
            varr[m, g, c, :, co] = f.variables[var][0, :] # all decades

# weights<commit_msg>Made pegasus/rice for each gcm median of other crops in box plotting<commit_after>#!/usr/bin/env python

# import modules
import matplotlib
from re import findall
from numpy import zeros, ones
from itertools import product
import matplotlib.pyplot as plt
from optparse import OptionParser
from netCDF4 import Dataset as nc
from matplotlib.patches import Polygon
from numpy.ma import masked_array, reshape, arange, masked_where, median

# parse inputs
parser = OptionParser()
parser.add_option("-i", "--infile", dest = "infile", default = "metrics.nc4", type = "string",
                  help = "Metrics file", metavar = "FILE")
parser.add_option("-c", "--crop", dest = "crop", default = "maize", type = "string",
                  help = "Crop (or all)")
parser.add_option("-r", "--hareafile", dest = "hareafile", default = "all.global.nc4", type = "string",
                  help = "Harvested area file", metavar = "FILE")
parser.add_option("-v", "--variable", dest = "variable", default = "beta", type = "string",
                  help = "Variable to plot")
parser.add_option("-b", "--boxfile", dest = "boxfile", default = "box.png", type = "string",
                  help = "Output boxplot file", metavar = "FILE")
parser.add_option("-n", "--ncfile", dest = "ncfile", default = "box.nc4", type = "string",
                  help = "Output netcdf data file", metavar = "FILE")
options, args = parser.parse_args()

infile    = options.infile
crop      = options.crop
hareafile = options.hareafile
variable  = options.variable
boxfile   = options.boxfile
ncfile    = options.ncfile

cals = {'maize': 3.60, 'wheat': 3.34, 'soy': 3.35, 'rice': 2.80}

with nc(infile) as f:
    decade = f.variables['decade'][:]
    dunits = f.variables['decade'].units
    decade = decade * 10 + int(findall(r'\d+', dunits)[0])

careas = {}
with nc(hareafile) as f:
    for c in ['maize', 'wheat', 'soy', 'rice']:
        careas[c] = f.variables['area_' + c][:]

models = ['epic', 'gepic', 'lpj-guess', 'lpjml', 'pdssat', 'pegasus'] # exclude image
gcms   = ['gfdl-esm2m', 'hadgem2-es', 'ipsl-cm5a-lr', 'miroc-esm-chem', 'noresm1-m']
crops  = ['maize', 'wheat', 'soy', 'rice'] if crop == 'all' else [crop]
co2s   = ['co2', 'noco2']

hadgemidx  = gcms.index('hadgem2-es')

nm, ng, ncr, nd, nco2 = len(models), len(gcms), len(crops), len(decade), len(co2s)

# variable
sh = (nm, ng, ncr, nd, nco2)
varr = masked_array(zeros(sh), mask = ones(sh))
with nc(infile) as f:
    for m, g, c, co in product(range(nm), range(ng), range(ncr), range(nco2)):
        var = '%s_global_%s_%s_%s_%s' % (variable, models[m], gcms[g], crops[c], co2s[co])
        if var in f.variables:
            varr[m, g, c, :, co] = f.variables[var][0, :] # all decades

if crop == 'all':
    # replace rice for pegasus with median over other crops
    pegasusidx = models.index('pegasus')
    riceidx    = crops.index('rice')
    notriceidx = range(len(crops))
    notriceidx.pop(riceidx);

    for g, co in product(range(ng), range(nco2)):
        varr[pegasusidx, g, riceidx, :, co] = median(varr[pegasusidx, g, notriceidx, :, co], axis = 0)

# weights
{'fraction_non_alphanumeric': 0.1786763435285986, 'fraction_numerical': 0.011750475203041299, 'mean_word_length': 3.543171114599686, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '740148', 'n_tokens_mistral': 2154, 'n_tokens_neox': 2123, 'n_words': 575}