def mapData(family, variable, year, mode): """Retrieves map data. Service call: /mapdata/es/iepg/economic_presence/2013/0?filter=US,DK,ES&toolfilter=US,DK Modes are: 0: countries 1: countries+EU 2: blocks """ if mode==0: population = copy.deepcopy(datacache.countries) if mode==1: population = arrayops.arraySubstraction( copy.deepcopy(datacache.countriesAndEu), common.helpers.getBlockMembers("XBEU", year=year)) if mode==2: population = copy.deepcopy(datacache.blocksNoEu) filter = processFilter(request.args, "filter") toolFilter = processFilter(request.args, "toolfilter") if filter: c = arrayops.arraySubstraction(population, filter) else: c = population try: varData = [v for (k,v) in common.helpers.getData(datacache.dataSets[family].variables[variable], year=year, countryList=c).iteritems()] except: varData = [] return(jsonify({"results": varData}))
def mapData(family, variable, year, mode): """Retrieves map data. Service call: /mapdata/es/iepg/economic_presence/2013/0?filter=US,DK,ES&toolfilter=US,DK Modes are: 0: countries 1: countries+EU 2: blocks """ if mode == 0: population = copy.deepcopy(datacache.countries) if mode == 1: population = arrayops.arraySubstraction( copy.deepcopy(datacache.countriesAndEu), common.helpers.getBlockMembers("XBEU", year=year)) if mode == 2: population = copy.deepcopy(datacache.blocksNoEu) filter = processFilter(request.args, "filter") toolFilter = processFilter(request.args, "toolfilter") if filter: c = arrayops.arraySubstraction(population, filter) else: c = population try: varData = [ v for (k, v) in common.helpers.getData( datacache.dataSets[family].variables[variable], year=year, countryList=c).iteritems() ] except: varData = [] return (jsonify({"results": varData}))
def ranking(lang, currentYear, referenceYear, family, variable, mode): """Retrieves ranking for a year and a variable. Examples: /ranking/es/1995/1990/iepg/energy/0 /ranking/en/2012/1995/iepe/manufactures/1?filter=US,DE&entities=ES,NL,XBAP,XBSA Modes are: 0: country ranking 1: countries+UE ranking 2: block ranking """ f = processFilter(request.args, "filter") e = processFilter(request.args, "entities") if mode == 1 and currentYear < 2005: return (jsonify({"results": []})) if family == "iepg": if mode == 0: population = copy.deepcopy(datacache.countries) if mode == 1: population = copy.deepcopy(datacache.countriesAndEu) if mode == 2: population = copy.deepcopy(datacache.blocksNoEu) if family == "iepe": population = chelpers.getBlockMembers("XBEU", year=currentYear) v = datacache.dataSets[family].variables[variable] if f: currentC = arrayops.arraySubstraction(population, f) else: currentC = population referenceC = currentC if mode == 1: currentC = arrayops.arraySubstraction( currentC, chelpers.getBlockMembers("XBEU", year=currentYear)) referenceC = arrayops.arraySubstraction( referenceC, chelpers.getBlockMembers("XBEU", year=referenceYear)) currentRanking = OrderedDict( sorted(chelpers.getRanking(currentC, currentYear, v).items(), key=lambda t: t[1]["rank"])) referenceRanking = chelpers.getRanking(referenceC, referenceYear, v) out = [] allNull = True for k, v in currentRanking.iteritems(): d = { "code": k, "currentRanking": v["rank"], "currentValue": v["value"] } if v["rank"] is not None: allNull = False if k in referenceRanking: d["referenceRanking"] = referenceRanking[k]["rank"] d["referenceValue"] = referenceRanking[k]["value"] else: d["referenceRanking"] = None d["referenceValue"] = None out.append(d) if allNull: return (jsonify({"results": []})) else: return (jsonify({"results": out}))
def countrySheet(lang, family, countryCode): """Retrieves all the data, for all years, and for a single country, to render the country sheet. Only retrieves context and IEPG variable families. Service call: /countrysheet/es/iepe/US?filter=US,DK,ES&entities=NL,XBEU,NZ entities is mandatory, although it has no effect in this tool. """ m = iepgdatamodel.IepgDataModel() f = processFilter(request.args, "filter") # Check the type of country code (country, EU, or block) if countryCode in datacache.countries: rankType = 0 population = copy.deepcopy(datacache.countries) if countryCode in datacache.blocks: rankType = 2 population = copy.deepcopy(datacache.blocksNoEu) if countryCode=="XBEU": rankType = 1 population = copy.deepcopy(datacache.countriesAndEu) # Filter substraction if f: filteredPopulation = arrayops.arraySubstraction(population, f) else: filteredPopulation = population try: out = dict() # Iterate through the years involved in the variable for year in const.years: yearData = dict() famData = datacache.dataSets[family].getData(code=countryCode, year=year) famPercentage = datacache.dataSets[family+"_relative_contribution"].\ getData(code=countryCode, year=year) conData = datacache.dataSets["context"].getData(code=countryCode, year=year) famDict = dict() for k,v in famData.iteritems(): a = v.values()[0] d = { "code": a["code"], "value": a["value"], "variable": k, "unit": "INDEX", "year": year, "percentage": famPercentage[k].values()[0]["value"] if k not in ["global"] else None } # Check if countryCode is a block. If it is, substract its members from countries # This is expensive. Cache block members. if countryCode in datacache.blocks: c = arrayops.arraySubstraction(filteredPopulation, common.helpers.getBlockMembers(countryCode, year)) else: c = filteredPopulation d["globalranking"] = common.helpers.getRankingCode(population, year, datacache.dataSets[family].variables[k], countryCode) if f: d["relativeranking"] = common.helpers.getRankingCode(c, year, datacache.dataSets[family].variables[k], countryCode) else: d["relativeranking"] = d["globalranking"] famDict[k] = d conDict = dict() for k,v in conData.iteritems(): if family=="iepe" and year>=2005: population = common.helpers.getBlockMembers("XBEU", 2013) if f: filteredPopulation = arrayops.arraySubstraction(population, f) else: filteredPopulation = population a = v.values()[0] d = { "code": a["code"], "variable": k, "year": year, "percentage": None } if lang=="en": val,d["unit"] = adjustBigUnits(a["value"]) if a["value"] else (None, None) d["value"] = round(val, 2) if a["value"] else None if lang=="es": val,d["unit"] = adjustBigUnits(a["value"], format=1) if a["value"] else (None, None) d["value"] = round(val, 2) if a["value"] else None # Check if countryCode is a block. If it is, substract its members from countries if countryCode in datacache.blocks: c = arrayops.arraySubstraction(filteredPopulation, common.helpers.getBlockMembers(countryCode, year)) else: c = filteredPopulation d["globalranking"] = common.helpers.getRankingCode(population, year, datacache.dataSets["context"].variables[k], countryCode) d["relativeranking"] = common.helpers.getRankingCode(c, year, datacache.dataSets["context"].variables[k], countryCode) conDict[k] = d yearData["family"] = famDict yearData["context"] = conDict comment = m.getIepgComment(lang, countryCode, 2013) yearData["comment"] = comment[0] if comment else None out[year] = yearData return(jsonify({"results": out})) except ElcanoApiRestError as e: return(jsonify(e.toDict()))
def ranking(lang, currentYear, referenceYear, family, variable, mode): """Retrieves ranking for a year and a variable. Examples: /ranking/es/1995/1990/iepg/energy/0 /ranking/en/2012/1995/iepe/manufactures/1?filter=US,DE&entities=ES,NL,XBAP,XBSA Modes are: 0: country ranking 1: countries+UE ranking 2: block ranking """ f = processFilter(request.args, "filter") e = processFilter(request.args, "entities") if mode==1 and currentYear<2005: return(jsonify({"results": []})) if family=="iepg": if mode==0: population=copy.deepcopy(datacache.countries) if mode==1: population=copy.deepcopy(datacache.countriesAndEu) if mode==2: population=copy.deepcopy(datacache.blocksNoEu) if family=="iepe": population = chelpers.getBlockMembers("XBEU", year=currentYear) v = datacache.dataSets[family].variables[variable] if f: currentC = arrayops.arraySubstraction(population, f) else: currentC = population referenceC = currentC if mode==1: currentC = arrayops.arraySubstraction(currentC, chelpers.getBlockMembers("XBEU", year=currentYear)) referenceC = arrayops.arraySubstraction(referenceC, chelpers.getBlockMembers("XBEU", year=referenceYear)) currentRanking = OrderedDict(sorted(chelpers.getRanking(currentC, currentYear, v).items(), key=lambda t: t[1]["rank"])) referenceRanking = chelpers.getRanking(referenceC, referenceYear, v) out = [] allNull = True for k,v in currentRanking.iteritems(): d = { "code": k, "currentRanking": v["rank"], "currentValue": v["value"] } if v["rank"] is not None: allNull = False if k in referenceRanking: d["referenceRanking"]=referenceRanking[k]["rank"] d["referenceValue"]=referenceRanking[k]["value"] else: d["referenceRanking"]=None d["referenceValue"]=None out.append(d) if allNull: return(jsonify({"results": []})) else: return(jsonify({"results": out}))
def countrySheet(lang, family, countryCode): """Retrieves all the data, for all years, and for a single country, to render the country sheet. Only retrieves context and IEPG variable families. Service call: /countrysheet/es/iepe/US?filter=US,DK,ES&entities=NL,XBEU,NZ entities is mandatory, although it has no effect in this tool. """ m = iepgdatamodel.IepgDataModel() f = processFilter(request.args, "filter") # Check the type of country code (country, EU, or block) if countryCode in datacache.countries: rankType = 0 population = copy.deepcopy(datacache.countries) if countryCode in datacache.blocks: rankType = 2 population = copy.deepcopy(datacache.blocksNoEu) if countryCode == "XBEU": rankType = 1 population = copy.deepcopy(datacache.countriesAndEu) # Filter substraction if f: filteredPopulation = arrayops.arraySubstraction(population, f) else: filteredPopulation = population try: out = dict() # Iterate through the years involved in the variable for year in const.years: yearData = dict() famData = datacache.dataSets[family].getData(code=countryCode, year=year) famPercentage = datacache.dataSets[family+"_relative_contribution"].\ getData(code=countryCode, year=year) conData = datacache.dataSets["context"].getData(code=countryCode, year=year) famDict = dict() for k, v in famData.iteritems(): a = v.values()[0] d = { "code": a["code"], "value": a["value"], "variable": k, "unit": "INDEX", "year": year, "percentage": famPercentage[k].values()[0]["value"] if k not in ["global"] else None } # Check if countryCode is a block. If it is, substract its members from countries # This is expensive. Cache block members. if countryCode in datacache.blocks: c = arrayops.arraySubstraction( filteredPopulation, common.helpers.getBlockMembers(countryCode, year)) else: c = filteredPopulation d["globalranking"] = common.helpers.getRankingCode( population, year, datacache.dataSets[family].variables[k], countryCode) if f: d["relativeranking"] = common.helpers.getRankingCode( c, year, datacache.dataSets[family].variables[k], countryCode) else: d["relativeranking"] = d["globalranking"] famDict[k] = d conDict = dict() for k, v in conData.iteritems(): if family == "iepe" and year >= 2005: population = common.helpers.getBlockMembers("XBEU", 2013) if f: filteredPopulation = arrayops.arraySubstraction( population, f) else: filteredPopulation = population a = v.values()[0] d = { "code": a["code"], "variable": k, "year": year, "percentage": None } if lang == "en": val, d["unit"] = adjustBigUnits( a["value"]) if a["value"] else (None, None) d["value"] = round(val, 2) if a["value"] else None if lang == "es": val, d["unit"] = adjustBigUnits( a["value"], format=1) if a["value"] else (None, None) d["value"] = round(val, 2) if a["value"] else None # Check if countryCode is a block. If it is, substract its members from countries if countryCode in datacache.blocks: c = arrayops.arraySubstraction( filteredPopulation, common.helpers.getBlockMembers(countryCode, year)) else: c = filteredPopulation d["globalranking"] = common.helpers.getRankingCode( population, year, datacache.dataSets["context"].variables[k], countryCode) d["relativeranking"] = common.helpers.getRankingCode( c, year, datacache.dataSets["context"].variables[k], countryCode) conDict[k] = d yearData["family"] = famDict yearData["context"] = conDict comment = m.getIepgComment(lang, countryCode, 2013) yearData["comment"] = comment[0] if comment else None out[year] = yearData return (jsonify({"results": out})) except ElcanoApiRestError as e: return (jsonify(e.toDict()))