*-------------- Mechanical SAMG reconciliation ---------------------------
$ontext
This routine simply removes very small differences in transactions so as
ensure a perfectly reconciled SAM
$offtext


 SAMGCHK(sacn,r)       = SAMG(sacn,"total",r) - SAMG("total",sacn,r) ;

* #### Mechanical Rebalancing of SAMG

 SAMG("saltax",c,r)  = SAMG("saltax",c,r) + SAMGCHK(c,r) ;

 SAMG("prodtax",a,r) = SAMG("prodtax",a,r) + SAMGCHK(a,r) ;

 SAMG("govt",tmr,r)  = SUM(c,SAMG(tmr,c,r)) ;

 SAMG("govt",tmrs,r) = SUM(c,SAMG(tmrs,c,r)) ;

 SAMG("govt",ter,r)  = SUM(c,SAMG(ter,c,r)) ;

 SAMG("govt",tff,r)  = SUM(a,SAMG(tff,a,r)) ;

* Calculating Household incomes from factors

 SAMG("hous",f,r)    = SUM(a,SAMG(f,a,r))
                           - SAMG("dirtax",f,r) - SAMG("kap",f,r) ;

* Calculating the import demand for each margin import

 SAMG("wglo",owatpmarg,r)  = SUM(c,SAMG(owatpmarg,c,r)) ;

* Calculating trade balance for each region with other regions

 SAMG("kap",w,r)    = SUM(c,SAMG(w,c,r)) - SUM(cp,SAMG(cp,w,r)) ;

* Calculating trade balance for each region with the GLOBE

 SAMG("kap","wglo",r)   =  SUM(owatpmarg,SAMG("wglo",owatpmarg,r))
                                 - SUM(c,SAMG(c,"wglo",r)) ;

* Calculating GLOBE's trade balance with each region

 SAMG("kap",w,"glo")     = SUM(c,SAMG(w,c,"glo"))
                             - SUM(cp,SAMG(cp,w,"glo")) ;

* Calculating Government tax revenues

 SAMG("govt","saltax",r) = SUM(c,SAMG("saltax",c,r)) ;

 SAMG("govt","vattax",r) = SUM(c,SAMG("vattax",c,r)) ;

 SAMG("govt","prodtax",r) = SUM(a,SAMG("prodtax",a,r)) ;

 SAMG("govt","dirtax",r) = SUM(f,SAMG("dirtax",f,r))
                               + SAMG("dirtax","hous",r) ;

* Calculating Institutional Savings

 SAMG("kap","hous",r)    = SUM(ff,SAMG("hous",ff,r))
                              - SUM(c,SAMG(c,"hous",r))
                              - SAMG("dirtax","hous",r) ;

 SAMG("kap","govt",r)    = SUM(tmr,SAMG("govt",tmr,r))
                               + SUM(tmrs,SAMG("govt",tmrs,r))
                               + SUM(ter,SAMG("govt",ter,r))
                               + SUM(tff,SAMG("govt",tff,r))
                               + SAMG("govt","saltax",r)
                               + SAMG("govt","vattax",r)
                               + SAMG("govt","prodtax",r)
                               + SAMG("govt","dirtax",r)
                               - SUM(c,SAMG(c,"govt",r)) ;

*----- Checking the reconciled SAMG

 SAMG("total",sacnp,r) = SUM(sacn,SAMG(sacn,sacnp,r)) ;
 SAMG(sacn,"total",r)  = SUM(sacnp,SAMG(sacn,sacnp,r)) ;

 SAMGCHK(sacn,r)       = SAMG("total",sacn,r) - SAMG(sacn,"total",r)  ;
* SAMGCHK(sacn,r)$(ABS(SAMGCHK(sacn,r) LT 0.000001))  = 0.0 ;

DISPLAY SAMGCHK ;