Import('env')
import gslab_scons as gs
from os import path

sim_r = env['CONFIG']['global']['release_large']['simulation']
est_r = env['CONFIG']['global']['release_large']['estimation']
wgt_r = env['CONFIG']['global']['release_large']['weights']
sim_s = env['CONFIG']['global']['source']['simulation']
est_s = env['CONFIG']['global']['source']['estimation']
lib   = env['CONFIG']['global']['source']['lib']
plt_r = env['CONFIG']['global']['release']['plot']
plt_s = env['CONFIG']['global']['source']['plot']

# ----------------------------------------------------------------------

target = []
source = [
    '#%s/plot_simulation.m'             % plt_s,
    '#%s/GetMainPlots.m'                % plt_s,
    '#%s/GetHTEPlots.m'                 % plt_s,
    '#%s/GetParams.m'                   % sim_s,
    '#%s/matlab/misc/loadparam.m'       % lib,
    '#%s/matlab/misc/loadfile.m'        % lib,
    '#%s/matlab/misc/MainPlotWrapper.m' % lib,
    '#%s/designs_main.csv'              % lib
]

for design in env['designs_main']:
    source += [
        '#%s/designs/%s.txt' % (sim_s, design),
        '#%s/%s_ep0.csv'     % (sim_r, design),
        '#%s/%s.mat'         % (est_r, design),
        '#%s/HTE_%s.mat'     % (est_r, design)
    ]

    target += [
        '#%s/%s/unconfounded.eps'       %  (plt_r, design),
        '#%s/%s/unconfounded.pdf'       %  (plt_r, design),
        '#%s/%s/unconfounded_line.eps'  %  (plt_r, design),
        '#%s/%s/unconfounded_line.pdf'  %  (plt_r, design),
        '#%s/%s/HTE_unconfounded.eps'   %  (plt_r, design),
        '#%s/%s/HTE_unconfounded.pdf'   %  (plt_r, design),
        '#%s/%s/confound.eps'           %  (plt_r, design),
        '#%s/%s/confound.pdf'           %  (plt_r, design),
        '#%s/%s/proxy.eps'              %  (plt_r, design),
        '#%s/%s/proxy.pdf'              %  (plt_r, design),
        '#%s/%s/outcome.eps'            %  (plt_r, design),
        '#%s/%s/outcome.pdf'            %  (plt_r, design),
        '#%s/%s/twoway_fe.eps'          %  (plt_r, design),
        '#%s/%s/twoway_fe.pdf'          %  (plt_r, design),
        '#%s/%s/HTE_twoway_fe.eps'      %  (plt_r, design),
        '#%s/%s/HTE_twoway_fe.pdf'      %  (plt_r, design),
        '#%s/%s/outcome.eps'            %  (plt_r, design),
        '#%s/%s/outcome.pdf'            %  (plt_r, design),
        '#%s/%s/controlx.eps'           %  (plt_r, design),
        '#%s/%s/controlx.pdf'           %  (plt_r, design),
        '#%s/%s/unit_linear_trend.eps'  %  (plt_r, design),
        '#%s/%s/unit_linear_trend.pdf'  %  (plt_r, design),
        '#%s/%s/extrapolation.eps'      %  (plt_r, design),
        '#%s/%s/extrapolation.pdf'      %  (plt_r, design),
        '#%s/%s/ife.eps'                %  (plt_r, design),
        '#%s/%s/ife.pdf'                %  (plt_r, design),
        '#%s/%s/fhs_strongest.eps'      %  (plt_r, design),
        '#%s/%s/fhs_strongest.pdf'      %  (plt_r, design),
        '#%s/%s/HTE_fhs_strongest.eps'  %  (plt_r, design),
        '#%s/%s/HTE_fhs_strongest.pdf'  %  (plt_r, design),
        '#%s/%s/me_iv.eps'              %  (plt_r, design),
        '#%s/%s/me_iv.pdf'              %  (plt_r, design)
    ]

env.BuildMatlab(target = target,
                source = source,
                log_ext = 'plot')

# ----------------------------------------------------------------------

target = []
source = ['#%s/SyntheticAndCannedPlots.m'  % plt_s]

for design in env['designs_main']:
    source += ['#%s/%s_ep0.csv' % (sim_r, design)]

    for type in ['bjs', 'sa', 'cdh']:
        source += ['#%s/HeterogeneityRobust/%s_%s_pctiles.csv' % (est_r, design, type)]

    for type in ['NotRecycling', 'NotRecycling_negW']:
        source += ["#%s/SyntheticControl/%s_NotRecycling.csv" % (est_r, design)]

    for type in ['HeteroBJS', 'HeteroSA', 'CdH']:
        target += [
            "#%s/%s/%s_dynamicresults_%s.pdf" % (plt_r, design, type, design),
            "#%s/%s/%s_dynamicresults_%s.eps" % (plt_r, design, type, design)
        ]

    for type in ['NotRecycling', 'NotRecycling_negW']:
        target += [
            "#%s/%s/SC_%s.pdf" % (plt_r, design, type),
            "#%s/%s/SC_%s.eps" % (plt_r, design, type)
        ]

env.BuildMatlab(target = target,
                source = source,
                log_ext = 'plot')

# ----------------------------------------------------------------------

target = ['#%s/BaselineV/weights_average_z_lead5_1minus.pdf'      % plt_r,
          '#%s/BaselineV/weights_average_deltaz_lag0_lag1.pdf'    % plt_r,
          '#%s/BaselineV/weights_average_z_lag5.pdf'              % plt_r,
          '#%s/BaselineV/weights_average_z_lead5_1minus.eps'      % plt_r,
          '#%s/BaselineV/weights_average_deltaz_lag0_lag1.eps'    % plt_r,
          '#%s/BaselineV/weights_average_z_lag5.eps'              % plt_r,
          '#%s/BaselineV/weights_sample_run_z_lead5_1minus.pdf'   % plt_r,
          '#%s/BaselineV/weights_sample_run_deltaz_lag0_lag1.pdf' % plt_r,
          '#%s/BaselineV/weights_sample_run_z_lag5.pdf'           % plt_r,
          '#%s/BaselineV/weights_sample_run_z_lead5_1minus.eps'   % plt_r,
          '#%s/BaselineV/weights_sample_run_deltaz_lag0_lag1.eps' % plt_r,
          '#%s/BaselineV/weights_sample_run_z_lag5.eps'           % plt_r]

source = ['#%s/plot_weights.m' % plt_s,
          '#%s/weights.txt'    % wgt_r]

env.BuildMatlab(target = target,
           source = source,
           log_ext = 'plot')

# ----------------------------------------------------------------------

