* -------------- ANAR1 Structural Tables ----------------------------------

* ------- 2. Assign  Tables ------------------------------------

* --- Commodity Tables ---

 nIMPORTSH("stIMPregFOB",w,c,r,clos,simr)$SUM(wp,resPWMFOB(wp,c,r,clos,simr)* resQMR(wp,c,r,clos,simr))
          = (resPWMFOB(w,c,r,clos,simr)* resQMR(w,c,r,clos,simr))
              /SUM(wp,resPWMFOB(wp,c,r,clos,simr)* resQMR(wp,c,r,clos,simr)) ;

 nIMPORTSH("stIMPcomFOB",w,c,r,clos,simr)$SUM(cp,resPWMFOB(w,cp,r,clos,simr)* resQMR(w,cp,r,clos,simr))
          = (resPWMFOB(w,c,r,clos,simr)* resQMR(w,c,r,clos,simr))
              /SUM(cp,resPWMFOB(w,cp,r,clos,simr)* resQMR(w,cp,r,clos,simr)) ;

 nIMPORTSH("stIMPregCIF",w,c,r,clos,simr)$SUM(wp,resPWM(w,c,r,clos,simr)* resQMR(w,c,r,clos,simr))
          = (resPWM(w,c,r,clos,simr)* resQMR(w,c,r,clos,simr))
              /SUM(wp,resPWM(wp,c,r,clos,simr)* resQMR(wp,c,r,clos,simr)) ;

 nIMPORTSH("stIMPcomCIF",w,c,r,clos,simr)$SUM(cp,resPWM(w,cp,r,clos,simr)* resQMR(w,cp,r,clos,simr))
          = (resPWM(w,c,r,clos,simr)* resQMR(w,c,r,clos,simr))
              /SUM(cp,resPWM(w,cp,r,clos,simr)* resQMR(w,cp,r,clos,simr)) ;

 nIMPORTSH("stIMPregDOM",w,c,r,clos,simr)$SUM(wp,resPMR(w,c,r,clos,simr)* resQMR(w,c,r,clos,simr))
          = (resPMR(w,c,r,clos,simr)* resQMR(w,c,r,clos,simr))
              /SUM(wp,resPMR(wp,c,r,clos,simr)* resQMR(wp,c,r,clos,simr)) ;

 nIMPORTSH("stIMPcomDOM",w,c,r,clos,simr)$SUM(cp,resPMR(w,cp,r,clos,simr)* resQMR(w,cp,r,clos,simr))
          = (resPMR(w,c,r,clos,simr)* resQMR(w,c,r,clos,simr))
              /SUM(cp,resPMR(w,cp,r,clos,simr)* resQMR(w,cp,r,clos,simr)) ;


 nEXPORTSH("stEXPregFOB",c,w,r,clos,simr)$SUM(wp,resPWE(c,wp,r,clos,simr)* resQER(c,wp,r,clos,simr))
          = (resPWE(c,w,r,clos,simr)* resQER(c,w,r,clos,simr))
              /SUM(wp,resPWE(c,wp,r,clos,simr)* resQER(c,wp,r,clos,simr)) ;

 nEXPORTSH("stEXPcomFOB",c,w,r,clos,simr)$SUM(cp,resPWE(cp,w,r,clos,simr)* resQER(cp,w,r,clos,simr))
          = (resPWE(c,w,r,clos,simr)* resQER(c,w,r,clos,simr))
              /SUM(cp,resPWE(cp,w,r,clos,simr)* resQER(cp,w,r,clos,simr)) ;

 nEXPORTSH("stEXPregDOM",c,w,r,clos,simr)$SUM(wp,resPER(c,wp,r,clos,simr)* resQER(c,wp,r,clos,simr))
          = (resPER(c,w,r,clos,simr)* resQER(c,w,r,clos,simr))
              /SUM(wp,resPER(c,wp,r,clos,simr)* resQER(c,wp,r,clos,simr)) ;

 nEXPORTSH("stEXPcomDOM",c,w,r,clos,simr)$SUM(cp,resPER(cp,w,r,clos,simr)* resQER(cp,w,r,clos,simr))
          = (resPER(c,w,r,clos,simr)* resQER(c,w,r,clos,simr))
              /SUM(cp,resPER(cp,w,r,clos,simr)* resQER(cp,w,r,clos,simr)) ;

* Commodity account shares - indexed on stab3

* Demand Structures

 nCOMMODSH("stPRIVregDEM",c,r,clos,simr)$((resPQD(c,r,clos,simr) * (1 + resTV(c,r,clos,simr))*SUM(h,resQCD(c,h,r,clos,simr)))
                                          + (resPQD(c,r,clos,simr)* (resQGD(c,r,clos,simr)
                                          + resQINVD(c,r,clos,simr) + resQINTD(c,r,clos,simr))))
           = SUM(h,(resPQD(c,r,clos,simr) * (1 + resTV(c,r,clos,simr)) * resQCD(c,h,r,clos,simr)))
               /(resPQD(c,r,clos,simr)*(SUM(h,resQCD(c,h,r,clos,simr)) + resQGD(c,r,clos,simr)
                       + resQINVD(c,r,clos,simr) + resQINTD(c,r,clos,simr)))  ;

 nCOMMODSH("stPRIVcomDEM",c,r,clos,simr)$SUM((cp,h),resPQD(cp,r,clos,simr)* (1 + resTV(c,r,clos,simr))* resQCD(cp,h,r,clos,simr))
           = SUM(h,(resPQD(c,r,clos,simr)* (1 + resTV(c,r,clos,simr))* resQCD(c,h,r,clos,simr)))
               /SUM((cp,h),resPQD(cp,r,clos,simr)* (1 + resTV(c,r,clos,simr))* resQCD(cp,h,r,clos,simr)) ;

 nCOMMODSH("stGOVTregDEM",c,r,clos,simr)$(resPQD(c,r,clos,simr)*(SUM(h,resQCD(c,h,r,clos,simr)) + resQGD(c,r,clos,simr)
                                          + resQINVD(c,r,clos,simr) + resQINTD(c,r,clos,simr)))
           = (resPQD(c,r,clos,simr)* resQGD(c,r,clos,simr))
               /(resPQD(c,r,clos,simr)*(SUM(h,resQCD(c,h,r,clos,simr)) + resQGD(c,r,clos,simr)
                            + resQINVD(c,r,clos,simr) + resQINTD(c,r,clos,simr)))  ;

 nCOMMODSH("stGOVTcomDEM",c,r,clos,simr)$SUM(cp,resPQD(cp,r,clos,simr)* resQGD(cp,r,clos,simr))
           = (resPQD(c,r,clos,simr)* resQGD(c,r,clos,simr))
               /SUM(cp,resPQD(cp,r,clos,simr)* resQGD(cp,r,clos,simr)) ;

 nCOMMODSH("stINVDregDEM",c,r,clos,simr)$(resPQD(c,r,clos,simr)*(SUM(h,resQCD(c,h,r,clos,simr)) + resQGD(c,r,clos,simr)
                                          + resQINVD(c,r,clos,simr) + resQINTD(c,r,clos,simr)))
           = (resPQD(c,r,clos,simr)* resQINVD(c,r,clos,simr))
               /(resPQD(c,r,clos,simr)*(SUM(h,resQCD(c,h,r,clos,simr)) + resQGD(c,r,clos,simr)
                            + resQINVD(c,r,clos,simr) + resQINTD(c,r,clos,simr)))  ;

 nCOMMODSH("stINVDcomDEM",c,r,clos,simr)$SUM(cp,resPQD(cp,r,clos,simr)* resQINVD(cp,r,clos,simr))
           = (resPQD(c,r,clos,simr)* resQINVD(c,r,clos,simr))
               /SUM(cp,resPQD(cp,r,clos,simr)* resQINVD(cp,r,clos,simr)) ;

 nCOMMODSH("stINTDregDEM",c,r,clos,simr)$(resPQD(c,r,clos,simr)*(SUM(h,resQCD(c,h,r,clos,simr)) + resQGD(c,r,clos,simr)
                                          + resQINVD(c,r,clos,simr) + resQINTD(c,r,clos,simr)))
           = (resPQD(c,r,clos,simr)* resQINTD(c,r,clos,simr))
               /(resPQD(c,r,clos,simr)*(SUM(h,resQCD(c,h,r,clos,simr)) + resQGD(c,r,clos,simr)
                            + resQINVD(c,r,clos,simr) + resQINTD(c,r,clos,simr)))  ;

 nCOMMODSH("stINTDcomDEM",c,r,clos,simr)$SUM(cp,resPQD(cp,r,clos,simr)* resQINTD(cp,r,clos,simr))
           = (resPQD(c,r,clos,simr)* resQINTD(c,r,clos,simr))
               /SUM(cp,resPQD(cp,r,clos,simr)* resQINTD(cp,r,clos,simr)) ;

* Supply Structures

 nCOMMODSH("stMAKEcomSUP",c,r,clos,simr)$SUM(cp,resPXC(cp,r,clos,simr)* resQXC(cp,r,clos,simr))
           = (resPXC(c,r,clos,simr)* resQXC(c,r,clos,simr))
              /SUM(cp,resPXC(cp,r,clos,simr)* resQXC(cp,r,clos,simr)) ;

 nCOMMODSH("stQMcomSUP",c,r,clos,simr)$SUM(cp,resPM(cp,r,clos,simr)* resQM(cp,r,clos,simr))
           = (resPM(c,r,clos,simr)* resQM(c,r,clos,simr))
              /SUM(cp,resPM(cp,r,clos,simr)* resQM(cp,r,clos,simr)) ;

 nCOMMODSH("stQDcomSUP",c,r,clos,simr)$SUM(cp,resPD(cp,r,clos,simr)* resQD(cp,r,clos,simr))
           = (resPD(c,r,clos,simr)* resQD(c,r,clos,simr))
              /SUM(cp,resPD(cp,r,clos,simr)* resQD(cp,r,clos,simr)) ;

 nCOMMODSH("stSUPPcomSUP",c,r,clos,simr)$SUM(cp,(resPD(cp,r,clos,simr)* resQD(cp,r,clos,simr))
                                      + (resPM(c,r,clos,simr)* resQM(c,r,clos,simr)))
           = ((resPD(c,r,clos,simr)* resQD(c,r,clos,simr))+(resPM(c,r,clos,simr)* resQM(c,r,clos,simr)))
               /SUM(cp,(resPD(cp,r,clos,simr)* resQD(cp,r,clos,simr))
                 + (resPM(c,r,clos,simr)* resQM(c,r,clos,simr))) ;

 nCOMMODSH("stQEconSUP",c,r,clos,simr)$SUM(cp,resPE(cp,r,clos,simr)* resQE(cp,r,clos,simr))
           = (resPE(c,r,clos,simr)* resQE(c,r,clos,simr))
               /SUM(cp,resPE(cp,r,clos,simr)* resQE(cp,r,clos,simr)) ;

* --- Activity Tables ---

 nACTIVSH("stQINTQX",a,r,clos,simr)$(SUM(ap,resPX(ap,r,clos,simr)*resQX(ap,r,clos,simr)))
                    = (resPINT(a,r,clos,simr)*resQINT(a,r,clos,simr))/SUM(ap,resPX(ap,r,clos,simr)*resQX(ap,r,clos,simr)) ;

 nACTIVSH("stQINT",a,r,clos,simr)$(SUM(ap,resPINT(ap,r,clos,simr)*resQINT(ap,r,clos,simr)))
                    = (resPINT(a,r,clos,simr)*resQINT(a,r,clos,simr))/SUM(ap,resPINT(ap,r,clos,simr)*resQINT(ap,r,clos,simr)) ;

 nACTIVSH("stVAQX",a,r,clos,simr)$(SUM(ap,resPX(ap,r,clos,simr)*resQX(ap,r,clos,simr)))
                    = (resPVA(a,r,clos,simr)*resQVA(a,r,clos,simr))/SUM(ap,resPX(ap,r,clos,simr)*resQX(ap,r,clos,simr)) ;

 nACTIVSH("stVA",a,r,clos,simr)$(SUM(ap,resPVA(ap,r,clos,simr)*resQVA(ap,r,clos,simr)))
                    = (resPVA(a,r,clos,simr)*resQVA(a,r,clos,simr))/SUM(ap,resPVA(ap,r,clos,simr)*resQVA(ap,r,clos,simr)) ;

 nACTIVSH("stQX",a,r,clos,simr)$(SUM(ap,resPX(ap,r,clos,simr)*resQX(ap,r,clos,simr)))
                     = (resPX(a,r,clos,simr)*resQX(a,r,clos,simr))/SUM(ap,resPX(ap,r,clos,simr)*resQX(ap,r,clos,simr)) ;

 nACTIVSH("stLAB",a,r,clos,simr)$(resPVA(a,r,clos,simr)*resQVA(a,r,clos,simr))
                     = (SUM(l,resWF(l,r,clos,simr)*resWFDIST(l,a,r,clos,simr)*resFD(l,a,r,clos,simr)))
                           /(resPVA(a,r,clos,simr)*resQVA(a,r,clos,simr)) ;

 nACTIVSH("stUskLAB",a,r,clos,simr)$(resPVA(a,r,clos,simr)*resQVA(a,r,clos,simr))
                     = (SUM(lu,resWF(lu,r,clos,simr)*resWFDIST(lu,a,r,clos,simr)*resFD(lu,a,r,clos,simr)))
                           /(resPVA(a,r,clos,simr)*resQVA(a,r,clos,simr)) ;

 nACTIVSH("stSkLAB",a,r,clos,simr)$(resPVA(a,r,clos,simr)*resQVA(a,r,clos,simr))
                     = (SUM(ls,resWF(ls,r,clos,simr)*resWFDIST(ls,a,r,clos,simr)*resFD(ls,a,r,clos,simr)))
                           /(resPVA(a,r,clos,simr)*resQVA(a,r,clos,simr)) ;

 nACTIVSH("stCAP",a,r,clos,simr)$(resPVA(a,r,clos,simr)*resQVA(a,r,clos,simr))
                     = (SUM(k,resWF(k,r,clos,simr)*resWFDIST(k,a,r,clos,simr)*resFD(k,a,r,clos,simr)))
                           /(resPVA(a,r,clos,simr)*resQVA(a,r,clos,simr)) ;

 nACTIVSH("stLND",a,r,clos,simr)$(resPVA(a,r,clos,simr)*resQVA(a,r,clos,simr))
                     = (SUM(lnd,resWF(lnd,r,clos,simr)*resWFDIST(lnd,a,r,clos,simr)*resFD(lnd,a,r,clos,simr)))
                           /(resPVA(a,r,clos,simr)*resQVA(a,r,clos,simr)) ;

 nACTIVSH("stFTAX",a,r,clos,simr)$(resPVA(a,r,clos,simr)*resQVA(a,r,clos,simr))
              = (SUM(f,resTF(f,a,r,clos,simr)*resWF(f,r,clos,simr)*resWFDIST(f,a,r,clos,simr)*resFD(f,a,r,clos,simr)))
                          /(resPVA(a,r,clos,simr)*resQVA(a,r,clos,simr)) ;

 nACTIVSH("stFACTchk",a,r,clos,simr)
                     = nACTIVSH("stUskLAB",a,r,clos,simr) + nACTIVSH("stSkLAB",a,r,clos,simr)
                       + nACTIVSH("stCAP",a,r,clos,simr) + nACTIVSH("stLND",a,r,clos,simr)
                       + nACTIVSH("stFTAX",a,r,clos,simr) ;

* --- Factor Tables ---

* Assigning factor tables

 nFACTUSESH("stTRANS",f,a,r,clos,simr)
                    = (resWF(f,r,clos,simr) * resWFDIST(f,a,r,clos,simr) * resFD(f,a,r,clos,simr)) ;

 nFACTUSESH("stACTSH",f,a,r,clos,simr)$(SUM(fp,nFACTUSESH("stTRANS",fp,a,r,clos,simr)))
                    = nFACTUSESH("stTRANS",f,a,r,clos,simr)
                         /(SUM(fp,nFACTUSESH("stTRANS",fp,a,r,clos,simr))) ;

 nFACTUSESH("stFFSH",f,a,r,clos,simr)$(SUM(ap,nFACTUSESH("stTRANS",f,ap,r,clos,simr)))
                     = nFACTUSESH("stTRANS",f,a,r,clos,simr)
                          /(SUM(ap,nFACTUSESH("stTRANS",f,ap,r,clos,simr))) ;

* --- Factor adjustment

 FACTADJ(f,r,clos,simr)$resFS(f,r,clos,simr)
                   = (SUM(a, ABS(resFD(f,a,r,clos,simr)-FD0(f,a,r)))/2)
                          /resFS(f,r,clos,simr) ;

* ---- Aggregate export structure

 EXPORT_TOT(w,r,clos,simr)  = SUM(c,
           resPWE(c,w,r,clos,simr)*resQER(c,w,r,clos,simr)) ;

 EXPORT_DIF(w,r,clos,simr) = EXPORT_TOT(w,r,clos,simr) -
                            EXPORT_TOT(w,r,clos,"base");

 EXPORT_DIF_AGG(wagg,ragg,clos,simr)
                         = SUM((r,w)$(MAP_RAGG_R(ragg,r)and MAP_WAGG_W(wagg,w)),
                           (EXPORT_TOT(w,r,clos,simr) -
                            EXPORT_TOT(w,r,clos,"base")) );

 EXPORT_TOT_SH(wgn,r,clos,simr)$(SUM(wgnp, EXPORT_TOT(wgnp,r,clos,simr))) =
            EXPORT_TOT(wgn,r,clos,simr)/ SUM(wgnp, EXPORT_TOT(wgnp,r,clos,simr));

* intermediate aggregation steps to compute export totals over aggregate
* commodity, region and trade partner
* to compute shares of aggregate exports to aggregate non-globe regions
* use the totals and construct shares in EXCEL
* (or define a non-globe subset of rm and wm)

 EXP_CAGG_TOT(cagg,w,r,clos,simr)  = SUM(c$map_cagg_c(cagg,c),
           resPWE(c,w,r,clos,simr)*resQER(c,w,r,clos,simr)) ;

* ---- Aggregate import structure

 IMPORT_TOT(w,r,clos,simr)  = SUM(c,
           resPWM(w,c,r,clos,simr)*resQMR(w,c,r,clos,simr)) ;

 IMPORT_TOT_SH(wgn,r,clos,simr)$(SUM(wgnp, IMPORT_TOT(wgnp,r,clos,simr))) =
            IMPORT_TOT(wgn,r,clos,simr)/ SUM(wgnp, IMPORT_TOT(wgnp,r,clos,simr));

* intermediate aggregation steps to compute import totals over aggregate
* commodity, region and trade partner
* to compute shares of aggregate imports to aggregate non-globe regions
* use the totals and construct shares in EXCEL
* (or define a non-globe subset of rm and wm)

 IMP_CAGG_TOT(w,cagg,r,clos,simr)  = SUM(c$map_cagg_c(cagg,c),
           resPWM(w,c,r,clos,simr)*resQMR(w,c,r,clos,simr)) ;

* ---- Total Trade by aggregate commodities and/or regions


* ------- Exporting Structural Tables to GDX -----------------------------

* This writes the results data to GDX

Execute_Unload  'results/anar1_resstruct.gdx',

                       nIMPORTSH,
                       nEXPORTSH,
                       nCOMMODSH,
                       nACTIVSH,
                       nFACTUSESH,
                       FACTADJ,
                       EXPORT_TOT,
                       EXPORT_DIF,
*                       EXPORT_DIF_AGG,
                       EXPORT_TOT_SH,
                       EXP_CAGG_TOT,
                       IMPORT_TOT,
                       IMPORT_TOT_SH,
                       IMP_CAGG_TOT

   ;


* -------------- End of anar1 Structural Tables ---------------------------
