import sys; sys.path[:0] = [".", ".."]
import json, numpy as np
from figstyle import *
M=json.load(open('modes.json')); E=json.load(open('energy.json'))
names={'Ashburn_VA':'Ashburn, VA','Dallas_TX':'Dallas, TX','Abilene_TX':'Abilene, TX','Phoenix_AZ':'Phoenix, AZ','Hillsboro_OR':'Hillsboro, OR','Columbus_OH':'Columbus, OH','Council_Bluffs_IA':'Council Bluffs, IA'}
cols=[BLUE,RED,ORANGE,PURPLE,GREEN,'#555555',AMBER]
mk=['o','s','^','D','v','P','X']
cls=[17,27,32,40,45]
fig,ax=plt.subplots(figsize=(5.6,3.3))
for (s,r),c,m in zip(M.items(),cols,mk):
    h=[x[1] for x in r]
    ax.plot(cls,h,'-',marker=m,color=c,ms=4.5,lw=1.2,label=names[s])
ax.set_yscale('symlog',linthresh=1); ax.set_ylim(-0.2,12000)
ax.minorticks_off(); ax.set_yticks([0,1,10,100,1000,8760]); ax.set_yticklabels(['0','1','10','100','1,000','8,760'])
ax.set_xticks(cls); ax.set_xticklabels([f'W{c}' for c in cls])
ax.set_xlabel('Facility water supply class (upper limit, $^{\\circ}$C)'); ax.set_ylabel('Hours a year needing a chiller')
ax.legend(loc='upper center',bbox_to_anchor=(0.5,-0.2),ncol=4,fontsize=7.5,handletextpad=0.4)
save(fig,'fig_hours.pdf')

fig,axs=plt.subplots(1,2,figsize=(TEXTW,3.0))
for (s,c,m) in zip(E,cols,mk):
    axs[0].plot(cls,[E[s][str(k)]['e_dry'] for k in cls],'-',marker=m,color=c,ms=4,lw=1.1,label=names[s])
    axs[1].plot(cls,[E[s][str(k)]['w_evap'] for k in cls],'-',marker=m,color=c,ms=4,lw=1.1)
axs[0].set_title('(a) Cooling energy, waterless plant'); axs[0].set_ylabel('kWh per kWh of IT load')
axs[1].set_title('(b) Water use, evaporative plant'); axs[1].set_ylabel('Litres per kWh of IT load')
for a in axs: a.set_xticks(cls); a.set_xticklabels([f'W{c}' for c in cls]); a.set_ylim(bottom=0)
h,l=axs[0].get_legend_handles_labels()
fig.tight_layout(w_pad=2.0)
fig.legend(h,l,loc='upper center',bbox_to_anchor=(0.5,0.02),ncol=4,fontsize=7.5,handletextpad=0.4)
save(fig,'fig_energy.pdf')
