* diagnostics-data.inc

DISPLAY "#### START: diagnostics-data.inc";    

PARAMETER
* errors in SAM

  samlegal(ac,acp)          parameter with values (of 1) in cells where entries are legal
  errsamflow(ac,acp)        illegal SAM payment from 2nd index to 1st (not in samlegal)
  samlegalneg(ac,acp)       parameter with values (of 1) in cells where NEGATIVE values are legal
  errnegsamflow(ac,acp)     if =UNDF illegal negative SAM payment from 2nd index to 1st (not in samlegalneg)
  errexpprd(c)              if =UNDF error exports > output
  errmapaggreg(ac)          if =UNDF error mapping mapaggreg
  samsmall(ac,acp)          0-1 parameter that signals cells with small values
  
  
* to diagnose missing data

  misssclaing(acscal)       if =UNDF missing scaling factor

  missprodelas(a,ac)        if =UNDF value not empty added without prodelas
  misstradelas(c,ac)        if =UNDF trade not empty without tradelas
  missleselas(c,h)          if =UNDF household consumption not empty without LES elasticity
  missfrisch(h)             if =UNDF household consumption not empty without Frisch parameter
  missmigrelas(f,f)         if =UNDF mffp not empty with without migrelas
  
  missmtfactf               if =UNDF taxfact with missing mapping mtfact
  missmsubcom               if =UNDF subcom with missing mapping msubcom
  errmsubcom(c)             if =UNDF msubcom defined for row            
            
  missqfacgrw(f)            if =UNDF missing qfacgrw
  missdeprcap(f)            if =UNDF missing deprcap
  missnetprfrat(f)          if =UNDF missing netprfrat
                           
  errtimp(c,r)              if =UNDF TM sin QMR
  errtexp(c,r)              if =UNDF TE sin QER
                           
  missmaprtacm(tacm)        if =UNDF tacm with missing mapping maprtacm
  missmaprtace(tace)        if =UNDF tace with missing mapping maprtace
  missmaprtimp(taximp)      if =UNDF taximp with missing mapping maprtimp
  missmaprtexp(taxexp)      if =UNDF taxexp with missing mapping maprtexp
  missmcapins(capins)       if =UNDF capins with missing mcapins
  
  misseta(c,r)              if =UNDF missing tradelasedem
  misseta_m(c,r)            if =UNDF missing tradelasmsup
  
  missinvng                 if =UNDF missing private investment SAM account
  errposeta_wf(f)         if =UNDF wage curve elasticity of the wrong sign
  errposeta_wfa(f,a)         if =UNDF capital rent curve elasticity of the wrong sign
  
  missmfcapinv1(fcap)       if =UNDF capital factor not linked to investment account
  missmfcapinv2(inv)      if =UNDF investment account not linked to capital factor
  missmfcapinv3(inv)      'if =UNDF one investment account linked to more than one capital factor; possible solution: use mapaggreg'
  
  missuerat00(f)            if =UNDF missing unemployment rate for factor with endogenous unemployment
  missexcaprat00(fcap,a)            if =UNDF missing excess capacity rate for factor with endogenous excess capacity 
  
  
  missdemvat(c)             'if =UNDF missing (intermediate and/or final) consumption and VAT'
  errdeprcap(f)             if =UNDF missing data for capital depreciation rate but capital demanded by more than one sector
  
  missssgrw(ac)             if =UNDF missing data in ssgrw
  
  errcdeexog(c)             if =UNDF commodity c is in cedexog and cddexog

  erremibase(ghg,ac,acp)    data in emibase without corresponding data in SAM
  errtaxco2(a)              tax data in SAM without corresponding data in emibase
  errothener(ghg,ac,acp)        'data in emibase not linked to c, f, othener, or AFOLU'

  erraqaexog(a)             if =UNDF aqaexog01 not empty and dmod eq 3  
  
  errtpovdata1              temporal prm to compute errtpovdata2
  errtpovdata2              if =UNDF tpovdata GT tmax
  
  emptyc1                if =UNDF c1 is empty
  missconelas1(c1,h)         if =UNDF missing conelas1 (household consumption without conelas1)
  errc1c2(c)             if =UNDF c is in c1 and c2
  
  errmapc2c1(c2)             if =UNDF produced commodity c2 not mapped to any household commodity c1

  errqfbase1(f,a)               if =UNDF data in SAM without corresponding data in qfbase
  errqfbase2(f,a)               if =UNDF data in qfbase without corresponding data in SAM
  errqfbaseqfor0(f,a)          if =UNDF data in qfbase NE to data in qfor0


  missqlandbase(fland)          if =UNDF missing data for fland in qfbase
   
  missqfor0(f,t)                  if =UNDF data in ffor wihout corresponding data in qfor0 
  errqffor(f)                   if =UNDF data in qfbase inconsistent with data in qfor0
  errffor(f)                    if =UNDF ffor in fland and ffor
   
* forest plant data
  missqforplant(lndforplant,t)    if =1 data in ffor wihout corresponding data in qfor0 
  missqforplant2(t)   if =UNDF data in ffor with empty qfor0 
  
* water data
  errwat1(c,ac)             demand in SAM without demand in watbase
  errwat2(c,ac)             demand in watbase data without demand in SAM
  errwat3(c,ac)             supply in SAM without supply in enviro data
  errwat4(c)                supply in enviro data without supply in SAM
  errwat51(c)               temporal prm to compute errwat52
  errwat52(c)               supply not equal to demand
  
  errmacacrep(ac)           if =UNDF SAM account ac not mapped to acrep
  
* social sec closure
  errssec                   if =UNDF there is social sec inst without transfers from gov to insssec
  
  
* land use
  errmlndcrolndagrtot(lndcro)    if =UNDF lndcro missing in mlndcrolndagrtot
  errmlndlivlndagrtot(lndliv)    if =UNDF lndliv missing in mlndcrolndagrtot
  
;

* start: display error for illegal sam payments ---------------------

* defining parameters where SAM entries are legal

samlegal(c,a)       = 1;
samlegal(f,a)       = 1;

samlegal(taxact,a)  = 1;
samlegal(taxco2,a)  = 1;
samlegal(taxfact,a) = 1;

samlegal(a,c)       = 1;
samlegal(insrow,c)  = 1;
samlegal(r,c) = 1;
samlegal(taxexp,c)  = 1;
samlegal(taximp,c)  = 1;
samlegal(taxcom,c)  = 1;
samlegal(taxvatc,c) = 1;
samlegal(subcom,c) = 1;

samlegal(insrow,r) = 1;

samlegal(tacd,c)$(SUM(a, SAM(a,c)) - (SUM(insrow, SAM(c,insrow)) - SUM(taxexp, SAM(taxexp,c)) - SUM(tace, SAM(tace,c))) GT 0) = 1;
samlegal(tace,c)$SUM(insrow, SAM(c,insrow)) = 1;
*!!
*samlegal(tace,c)$SUM(instrst, SAM(c,instrst)) = 1;
samlegal(tace,c)$SUM(r, SAM(c,r)) = 1;
samlegal(tacm,c)$SUM(insrow, SAM(insrow,c)) = 1;
samlegal(tacm,c)$SUM(r, SAM(r,c)) = 1;

samlegal(c,tacd)$SUM(cp, samlegal(tacd,cp)) = 1;
samlegal(c,tace)$SUM(cp, samlegal(tace,cp)) = 1;
samlegal(c,tacm)$SUM(cp, samlegal(tacm,cp)) = 1;

samlegal(ins,f)     = 1;
samlegal(taxfac,f)  = 1;

samlegal(c,h) = 1;

samlegal(c,insngo) = 1;
samlegal(c,insssec) = 1;
samlegal(c,insgov)      = 1;
samlegal(c,insrow)      = 1;
samlegal(c,r) = 1;
samlegal(c,h)         = 1;
samlegal(c,instrst)  = 1;
samlegal(f,insrow)      = 1;
samlegal(ins,insp)      = 1;
samlegal(instrst,insrow) = 1;

samlegal(taxdir,insdng) = 1;
samlegal(capins,ins)$mcapins(capins,ins) = 1; 
samlegal(capinsgov,capinsrow) = 1;
samlegal(capinsgov,capinsdng) = 1;
samlegal(capinsdng,capinsrow) = 1;


samlegal(invng,capinsdng)  = 1;
samlegal(invg,capinsdng)  = 1;
samlegal(invng,capinsrow) = 1;

samlegal(invg,capinsgov)  = 1;
samlegal(invng,capinsgov)  = 1;

samlegal(dstk,capinsdng) = 1;
samlegal(dstk,capinsgov) = 1;


samlegal(c,inv)  = 1;
samlegal(c,dstk) = 1;

samlegal(insgov,actax) = 1;
samlegal(insgov,taxfact) = 1;
*!!
samlegal(a,taxfact) = 1;

samlegal(insssec,taxfact) = 1;
samlegal(insgov,subcom) = 1;


samlegal(acnt,'total')$(SUM(acntp, samlegal(acnt,acntp)) AND SUM(acntp, samlegal(acntp,acnt))) = 1;
samlegal('total',acnt)$samlegal(acnt,'total') = 1;



* defining parameters where negative SAM entries are legal

samlegalneg(insrow,r) = 1;
samlegalneg(taxact,a) = 1;
samlegalneg(taxexp,c) = 1;
samlegalneg(taximp,c) = 1;
samlegalneg(taxcom,c) = 1; 
samlegalneg(taxfact,a) = 1;
samlegalneg(subcom,c) = 1;

samlegalneg(capins,ins)$mcapins(capins,ins) = 1;
samlegalneg(capinsdng,capinsrow) = 1;
samlegalneg(capinsgov,capinsrow) = 1;
samlegalneg(capinsgov,capinsdng) = 1;

samlegalneg(dstk,capinsdng) = 1;
samlegalneg(dstk,capinsgov) = 1;


samlegalneg(insgov,actax) = 1;
samlegalneg(insgov,subcom) = 1;

samlegalneg(c,dstk)      = 1;

* negative totals are permitted if negative entries are permitted.
samlegalneg(ac,'total')$SUM(acnt, samlegalneg(ac,acnt)) = 1;
samlegalneg('total',ac)$SUM(acnt, samlegalneg(acnt,ac)) = 1;

* illegal SAM payments or values
errsamflow(ac,acp)$(SAM(ac,acp) AND (NOT samlegal(ac,acp))) = 1/0;
DISPLAY errsamflow;

SET
  negcell(ac,acp)    the sam payment from acp (column) to ac (row) is negative
;

negcell(ac,acp)$(SAM(ac,acp) LT 0) = YES;

DISPLAY negcell;

errnegsamflow(ac,acp)$( (sam(ac,acp) LT 0) AND (NOT samlegalneg(ac,acp)) ) = 1;

errnegsamflow(ac,acp)$( (SAM(ac,acp) LT 0) AND (NOT samlegalneg(ac,acp)) ) = 1/0;
DISPLAY errnegsamflow;

* end: display error for illegal sam payments -----------------------



* errmapaggreg ------------------------------------------------------

errmapaggreg(ac)$( SUM(acp$mapaggreg(ac,acp), 1) > 1 ) = 1/0;
DISPLAY errmapaggreg;

errexpprd(c)$( (SAM(c,'row') - SUM(taxexp, SAM(taxexp,c)) - SUM(tace, SAM(tace,c)) ) > SUM(a, SAM(a,c))) = 1/0;
DISPLAY errexpprd;

* samsmall ----------------------------------------------------------

samsmall(ac,acp)$(ABS(SAM(ac,acp))<1e-5) = SAM(ac,acp);



missssgrw('gdp')$(NOT ssgrw('gdp')) = 1/0;
missssgrw('qlab')$(NOT ssgrw('qlab')) = 1/0;
DISPLAY missssgrw;

missqfacgrw(f)$(dmod NE 0 AND fncap(f) AND NOT fland(f) AND NOT ffor(f) AND NOT SUM(t, qfacgrw(f,t))) = 1/0;
DISPLAY missqfacgrw;

missmtfactf$(CARD(taxfact) NE 0 AND NOT SUM((taxfact,f), mtfactf(taxfact,f))) = 1/0;
DISPLAY missmtfactf;

missmsubcom$(CARD(subcom) NE 0 AND NOT SUM((c,ac), msubcom(c,ac))) = 1/0;
DISPLAY missmsubcom;


errmsubcom(c)$(SUM(insrow$msubcom(c,insrow), 1)) = 1/0;
DISPLAY errmsubcom;

* start: missing elasticities ---------------------------------------

emptyc1$(CARD(c1)=0) = 1/0;
DISPLAY emptyc1;

errc1c2(c)$(c1(c) AND c2(c)) = 1/0;
DISPLAY errc1c2;


missprodelas(a,'sigma_va')$(SUM(f, SAM(f,a)) AND NOT prodelas(a,'sigma_va')) = 1/0;
DISPLAY missprodelas;

misstradelas(c,'sigma_Q')$(SUM(r, SAM(r,c))>0 AND SUM(a, SAM(a,c)) AND NOT tradelas(c,'sigma_Q')) = 1/0;
misstradelas(c,'sigma_X')$(SUM(r, SAM(c,r))>0 AND SUM(a, SAM(a,c)) AND NOT tradelas(c,'sigma_X')) = 1/0;
DISPLAY misstradelas;

missleselas(c1,h)$(NOT leselas(c1,h)) = 1/0;
DISPLAY missleselas;
missfrisch(h)$(NOT frisch(h)) = 1/0;
DISPLAY missfrisch;

missconelas1(c1,h)$(cnsam(c1) AND NOT conelas1(c1,h)) = 1/0;
DISPLAY missconelas1;

misseta(c,r)$(mapcred(c,r) AND SAM(c,r) AND NOT tradelasedem(c,r)) = 1/0;
DISPLAY misseta;

misseta_m(c,r)$(mapcrms(c,r) AND SAM(c,r) AND NOT tradelasmsup(c,r)) = 1/0;
DISPLAY misseta_m;

missmigrelas(f,fp)$(mffp(f,fp) AND NOT migrelas(f,fp)) = 1/0;
DISPLAY missmigrelas;



* end: missing elasticities -----------------------------------------


* errposeta_wf ------------------------------------------------------

errposeta_wf(f)$(unemp(f,'eta_wf')>0) = 1/0;
DISPLAY errposeta_wf;

* errposeta_wfa -----------------------------------------------------

errposeta_wfa(f,a)$(excap(f,a,'eta_wfa')>0) = 1/0;
DISPLAY errposeta_wfa;

* missdeprcap -------------------------------------------------------

missdeprcap(fcap)$(NOT deprcap(fcap)) = 1/0;
DISPLAY missdeprcap;

* missnetprfrat -----------------------------------------------------

missnetprfrat(fcapng)$(NOT dinam('netprfrat',fcapng)) = 1/0;
DISPLAY missnetprfrat;


errtimp(c,r)$(NOT SAM(r,c) AND SUM(taximp$maprtimp(r,taximp), SAM(taximp,c))) = 1/0;
DISPLAY errtimp;

errtexp(c,r)$(NOT SAM(c,r) AND SUM(taxexp$maprtexp(r,taxexp), SAM(taxexp,c))) = 1/0;
DISPLAY errtexp;

missmaprtacm(tacm)$(NOT SUM(r$maprtacm(r,tacm), 1)) = 1/0;
DISPLAY missmaprtacm;

missmaprtace(tace)$(NOT SUM(r$maprtace(r,tace), 1)) = 1/0;
DISPLAY missmaprtace;

missmaprtimp(taximp)$(NOT SUM(r$maprtimp(r,taximp), 1)) = 1/0;
DISPLAY missmaprtimp;

missmaprtexp(taxexp)$(NOT SUM(r$maprtexp(r,taxexp), 1)) = 1/0;
DISPLAY missmaprtexp;
  

missmcapins(capins)$(NOT SUM(ins$mcapins(capins,ins), 1)) = 1/0;
DISPLAY missmcapins; 


missinvng$(CARD(invng) = 0) = 1/0;
DISPLAY missinvng;


missmfcapinv1(fcap)$(NOT SUM(inv$mfcapinv(fcap,inv), 1)) = 1/0;
DISPLAY missmfcapinv1;

missmfcapinv2(inv)$(NOT SUM(fcap$mfcapinv(fcap,inv), 1)) = 1/0;
DISPLAY missmfcapinv2;

missmfcapinv3(inv)$(SUM(fcap$mfcapinv(fcap,inv), 1) NE 1) = 1/0;
DISPLAY missmfcapinv3;

missuerat00(f)$(fuendog(f) AND NOT unemp(f,'UERAT00')) = 1/0;
DISPLAY missuerat00;

missexcaprat00(fcap,a)$(fexcapendog(fcap,a) AND NOT excap(fcap,a,'EXCAPRAT00')) = 1/0;
DISPLAY missexcaprat00;

missdemvat(c)$(SUM(taxvatc, SAM(taxvatc,c)) AND NOT SUM(ac, mtaxvatc(c,ac))) = 1/0;
DISPLAY missdemvat;


* misssclaing(acscal) -----------------------------------------------

misssclaing(acscal)$(NOT scaling(acscal)) = 1/0;
DISPLAY misssclaing;		


errdeprcap(fcap)$(NOT deprcap(fcap) AND SUM(a$SAM(fcap,a), 1)>1) = 1/0;
DISPLAY errdeprcap;

errcdeexog(cedexog)$SUM(cddexog$SAMEAS(cedexog,cddexog), 1) = 1/0;
DISPLAY errcdeexog;

erremibase(ghg,c,ac)$(emibase(ghg,c,ac) AND NOT SAM(c,ac)) = 1/0;
DISPLAY erremibase;

errtaxco2(a)$(SUM(taxco2, SAM(taxco2,a)) AND NOT SUM(c, emibase('CO2',c,a))) = 1/0;
DISPLAY errtaxco2;

errothener(ghg,ac,acp)$(emibase(ghg,ac,acp) 
  AND NOT call(ac) AND NOT f(ac) 
  AND NOT othener(ac) 
  AND NOT emibase(ghg,'AFOLU',acp)) = 1/0;
DISPLAY errothener;
  


* errqaexog ---------------------------------------------------------

erraqaexog(a)$(aqaexog01(a) NE 0 AND dmod=3) = 1/0;
DISPLAY erraqaexog;


errmapc2c1(c2)$(NOT SUM(c1$mc2c1(c2,c1), 1)) = 1/0;
DISPLAY errmapc2c1;



* errwat1 -----------------------------------------------------------

errwat1(c,acnt)$(cwat(c) AND SAM(c,acnt) AND NOT watbase(c,acnt)) = 1/0;
DISPLAY errwat1;

* errwat2 -----------------------------------------------------------

errwat2(c,acnt)$(cwat(c) AND NOT SAM(c,acnt) AND watbase(c,acnt)) = 1/0;
DISPLAY errwat2;

* errwat3 -----------------------------------------------------------

errwat3(c,a)$(cwat(c) AND SAM(a,c) AND NOT qxbase(c)) = 1/0;
DISPLAY errwat3;

* errwat4 -----------------------------------------------------------

errwat4(c)$(cwat(c) AND NOT SUM(a, SAM(a,c)) AND qxbase(c)) = 1/0;
DISPLAY errwat4;

* errwat5 -----------------------------------------------------------

errwat51(c)$cwat(c) = qxbase(c) - SUM(acnt, watbase(c,acnt));
errwat52(c)$( cwat(c) AND ABS(errwat51(c)) > 1e-10 ) = 1/0;
DISPLAY errwat51, errwat52;


errqfbase1(f,a)$(SUM(ap, qfbase(f,ap)) NE 0 AND SAM(f,a) AND NOT qfbase(f,a)) = 1/0;
errqfbase2(f,a)$(SUM(ap, qfbase(f,ap)) NE 0  AND NOT SAM(f,a) AND qfbase(f,a)) = 1/0;
DISPLAY errqfbase1, errqfbase2;

errqfbaseqfor0(ffor,afor)$(SAM(ffor,afor) AND qfbase(ffor,afor) NE SUM(lndforplant$mfforlndforplant(ffor,lndforplant), SUM(tmin, qfor0(lndforplant,tmin)))) = 1/0;
DISPLAY errqfbaseqfor0;

* missqlandbase

missqlandbase(fland)$(CARD(fland) NE 0 AND NOT SUM(a, qfbase(fland,a))) = 1/0;
DISPLAY missqlandbase;



* missqfor0 ---------------------------------------------------------

missqfor0(ffor,t)$(tsol(t) AND NOT SUM(lndforplant$mfforlndforplant(ffor,lndforplant), qfor0(lndforplant,t))) = 1/0;
DISPLAY missqfor0;

* errqffor

PARAMETER
  errqffor2(ffor)
;
errqffor2(ffor) = (SUM(a, qfbase(ffor,a)) - SUM(lndforplant$mfforlndforplant(ffor,lndforplant), SUM(tmin, qfor0(lndforplant,tmin)))); 
DISPLAY errqffor2;

errqffor(ffor)$(ABS(errqffor2(ffor)>1e-10)) = 1/0;
DISPLAY errqffor;

* errffor

errffor(f)$(fland(f) AND ffor(f)) = 1/0;
DISPLAY errffor;


* errmacacrep -------------------------------------------------------

errmacacrep(acsam)$(CARD(acrep) > 1 AND NOT SUM(acrep$macacrep(acsam,acrep), 1)) = 1/0;
DISPLAY errmacacrep;

* missqforplant -----------------------------------------------------

missqforplant(lndforplant,tsol)$(CARD(ffor)>=1 AND NOT qfor0(lndforplant,tsol)) = 1;
DISPLAY missqforplant;
missqforplant2(tsol)$(CARD(ffor)>=1 AND NOT SUM(lndforplant, qfor0(lndforplant,tsol))) = 1;

* errssec
errssec$(CARD(insssec) NE 0 AND SUM((insssec,insgov), SAM(insssec,insgov))=0) = 1/0;
DISPLAY errssec;



* errmlndcrolndagrtot

errmlndcrolndagrtot(lndcro)$(SUM(lndagrtot$mlndcrolndagrtot(lndcro,lndagrtot), 1) = 0) = 1/0;
DISPLAY errmlndcrolndagrtot;


* errmlndlivlndagrtot

errmlndlivlndagrtot(lndliv)$(SUM(lndagrtot$mlndlivlndagrtot(lndliv,lndagrtot), 1) = 0) = 1/0;
DISPLAY errmlndlivlndagrtot;


DISPLAY "#### END: diagnostics-data.inc";   