#!/usr/bin/env python3 """Replay: python alerte-thermique.py N2.xlsx weather.xlsm output.csv Requires numpy, scipy, openpyxl and sibling refroidissement-rejouer.py. Official Fraunhofer Annex 71 extended experiment, doi:10.24406/fordatis/76.2. """ import csv import sys import importlib.util from pathlib import Path from datetime import datetime as Date, timedelta import numpy as np def load_model(): spec=importlib.util.spec_from_file_location('thermal',Path(__file__).with_name('refroidissement-rejouer.py')) module=importlib.util.module_from_spec(spec) spec.loader.exec_module(module) module.BEGIN=Date(2019,4,1) module.END=Date(2019,6,18,16) return module H=6 DROP=2.0 MIN_HOURS=12 REHEAT=1.0 # kWh in previous 24 h FIT_END=Date(2019,4,20) CAL_END=Date(2019,4,26) TEST_END=Date(2019,5,26) LONG_ORIGIN=Date(2019,5,26,23) def zero_runs(powers): padded=np.r_[False,powers==0,False].astype(int) return zip(np.flatnonzero(np.diff(padded)==1),np.flatnonzero(np.diff(padded)==-1)) def main(): m=load_model() # Read using the original pinned workbook digests; only expand the date range. hh,house=m.get(sys.argv[1],m.SHAS[0]) wh,weather=m.get(sys.argv[2],m.SHAS[1]) dates=sorted(house) assert dates==sorted(weather) assert all(b-a==timedelta(hours=1) for a,b in zip(dates,dates[1:])) hi=[i for i,k in enumerate(hh) if k and k.startswith('n2_aroom_') and k.endswith('_heat_elP') and 'corrodor' not in k] ii=[i for i,k in enumerate(hh) if k and k.startswith('n2_aroom_') and k.endswith('_IHS_elP')] assert len(hi)==10 and len(ii)==7 t=np.array([house[d][hh.index('n2_aroom_living_110_AT')] for d in dates],float) hp=np.array([sum(float(house[d][i]) for i in hi)/1000 for d in dates]) ih=np.array([sum(float(house[d][i]) for i in ii)/1000 for d in dates]) tout=np.array([float(weather[d][wh.index('AmbientAirTemperature')]) for d in dates]) solar=np.array([max(0,float(weather[d][wh.index('Radiation_Global')]))/1000 for d in dates]) u=np.column_stack([tout,hp+ih,solar]) n=dates.index(FIT_END) p,_,mae=m.fit(u[:n],t[:n],False) print('fit',n,p,mae) def forecasts(i): # Only observed weather and internal gains up to i; assume they persist. q=np.repeat([[tout[i],ih[i],solar[i]]],H,axis=0) rc=m.predict(p,np.array([t[i]]),q,False)[:,0] slope=float(np.array([-3,-1,1,3])@t[i-3:i+1]/20) line=t[i]+min(0,slope)*np.arange(1,H+1) return rc,line def times(s,e): return range(s,e-H) runs=[] for a,b in zero_runs(hp): if b-a=TEST_END or energy0 margin=float(np.quantile(residuals,.90,method='higher')) print('margin',len(residuals),margin) with open(sys.argv[3],'w',newline='',encoding='utf-8') as f: writer=csv.writer(f) writer.writerow(['groupe','origine_cet','heure','T_C','seuil_C','franchissement_h', 'franchissement_dans_6h','pred_min_RC_C','marge_C','pred_min_pente_C', 'alerte_RC','alerte_marge','alerte_pente']) for grp,s,e in runs: th=t[s]-DROP crosses=np.flatnonzero(t[s+1:e]<=th+1e-9) cross=int(crosses[0])+1 if len(crosses) else None false=[0]*3 true=[[] for _ in false] for i in times(s,e): h=i-s if cross is not None and h>=cross: break rc,line=forecasts(i) r,l=float(min(rc)),float(min(line)) alerts=[int(x<=th+1e-9) for x in [r,r-margin,l]] due=cross is not None and h