<?php

$needs = array
    (
     "fCorsikaInputCreated" => "MCRunProcessStatus.fMCRunNumber",
     "fCorsikaFileAvail"    => "MCRunProcessStatus.fCorsikaInputCreated",
     );


$timelimits = array
    (
     "fCorsikaInputCreated" => "1",
     "fCorsikaFileAvail"    => "60",
     );


$checkwhere = array
    (
     "fParticleTypeName"                 => CheckWhere("fParticleTypeKEY"),
     "fAtmosphericModelName"             => CheckWhere("fAtmosphericModelKEY"),
    );

$checkgroup = array
    (
     "fParticleTypeName"                 => CheckGroup("fParticleTypeKEY"),
     "fAtmosphericModelName"             => CheckGroup("fAtmosphericModelKEY"),
    );


$checkstatusgroup = array
    (
     "fCorsikaInputCreated" => CheckStatusGroup("fCorsikaInputCreatedStatus"),
     "fCorsikaFileAvail" => CheckStatusGroup("fCorsikaFileAvailStatus"),
    );



$alias = array
    (
//     "fRunNumber"                       => "Run#",
     "Run#"                    => "Run#",
     "fNumEvents"              => "#Evts",
     "SUM(fNumEvents)"         => "Evts",
     "fParticleTypeName"       => "Particle",
     "fAtmosphericModelName"   => "Atm.Model",
     "fCorsikaInputCreated"    => "Input",
     "fCorsikaFileAvail"       => "Corsika",
     "fStartTime"              => "Process",
     "fFailedTime"             => "Failed",
     "fReturnCode"             => "Ret<br>Code",
     "fProgramId"              => "Prgr<br>Id",
     "fZenithDistanceMin"      => "Zd<br>Min",
     "fZenithDistanceMax"      => "Zd<br>Max",
     "fAzimuthMin"             => "Az<br>Min",
     "fAzimuthMax"             => "Az<br>Max",
     "Min(fZenithDistanceMin)" => "ZDMin",
     "Max(fZenithDistanceMax)" => "ZDMax",
     "fEnergyMin"              => "EMin<br>[GeV]",
     "fEnergyMax"              => "EMax<br>[GeV]",
     "fImpactMax"              => "Impact<br>[cm]",
     "fViewConeMax"            => "View<br>cone<br>[deg]",
     "fEnergySlope"            => "Slope",
     "fNumReUseShower"         => "#ShowerReUse",
     "fStartingAltitude"       => "Altitude<br>[g/sqcm]",
     "fMirrorDiameter"         => "Mirror<br>diam.<br>[cm]",
    );


$rightalign = array
    (
//     $alias["fRunNumber"]                => "1",
     $alias["fNumEvents"]                => "1",
     $alias["fZenithDistanceMin"]        => "1",
     $alias["fZenithDistanceMax"]        => "1",
     $alias["fAzimuthMin"]               => "1",
     $alias["fAzimuthMax"]               => "1",
     $alias["fEnergyMin"]                => "1",
     $alias["fEnergyMax"]                => "1",
     $alias["fImpactMax"]                => "1",
     $alias["fViewConeMax"]              => "1",
     $alias["fEnergySlope"]              => "1",
     $alias["fNumReUseShower"]           => "1",
     $alias["fStartingAltitude"]         => "1",
     $alias["fMirrorDiameter"]           => "1",
     "Time"                              => "1",
     "Runs"                              => "1",
     "Evts"                              => "1",
);

function GetCheck($fromtable, $val)
{
    $checks = array
        (
         "fParticleTypeName"         => $fromtable . ".fParticleTypeKEY",
         "fAtmosphericModelName"     => $fromtable . ".fAtmosphericModelKEY",
        );

    $check="";
    if (!empty($checks[$val]))
        $check=$checks[$val];
    return $check;
}


function GetTable($fromtable, $val)
{
    $tables = array
        (
         "fNumEvents"                        => $fromtable . ".fNumEvents",
         "fMCRunNumber"                      => "MCCorsikaRunData.fMCRunNumber",
         "fRawFileAvail"                     => "RunProcessStatus.fRawFileAvail",
         "SUM(fRunTime)/3600"                => "'Time [h]'",
         "SUM(fNumEvents)"                   => "'Evts'",
         "fStartTime"                        => "fStartTime",
         "fFailedTime"                       => "fFailedTime",
         "fReturnCode"                       => "fReturnCode",
         "fProgramId"                        => "fProgramId",
         "fParticleTypeName"                 => "ParticleType.fParticleTypeName",
         "fAtmosphericModelName"             => "AtmosphericModel.fAtmosphericModelName",
         "fAzimuthMin"                       => $fromtable . ".fAzimuthMin",
         "fAzimuthMax"                       => $fromtable . ".fAzimuthMax",
         "fZenithDistanceMin"                => $fromtable . ".fZenithDistanceMin",
         "fZenithDistanceMax"                => $fromtable . ".fZenithDistanceMax",
         "fEnergyMin"                        => $fromtable . ".fEnergyMin",
         "fEnergyMax"                        => $fromtable . ".fEnergyMax",
         "fImpactMax"                        => $fromtable . ".fImpactMax",
         "fViewConeMax"                      => $fromtable . ".fViewConeMax",
         "fEnergySlope"                      => $fromtable . ".fEnergySlope",
         "fNumReUseShower"                   => $fromtable . ".fNumReUseShower",
         "fStartingAltitude"                 => $fromtable . ".fStartingAltitude",
         "fMirrorDiameter"                   => $fromtable . ".fMirrorDiameter",
         "Min(fZenithDistanceMin)"           => "'ZDMin'",
         "Max(fZenithDistanceMax)"           => "'ZDMax'",
        );

    $table="";
    if (!empty($tables[$val]))
        $table=$tables[$val];
    if ($val=="Tel:Run/File")
        $table="'Tel:Run/File'";
    if ($val=="Tel:Sequ")
        $table="CONCAT('M', Sequences.fTelescopeNumber, ':', LPAD(Sequences.fSequenceFirst, 8, '0'))";
    if ($val=="NumRuns")
        $table="'# Runs'";
    if ($val=="NumSequ")
        $table="'# Sequ'";
    if ($val=="NumDS")
        $table="'# Datasets'";
    if ($val=="NumDays")
        $table="'# days'";
    if (empty($table))
        $table=$val;
    return $table;
}


function GetJoin($fromtable, $val)
{
    $joins = array
        (
         "fParticleTypeName"           => " LEFT JOIN ParticleType         USING(fParticleTypeKEY) ",
         "fAtmosphericModelName"       => " LEFT JOIN AtmosphericModel     USING(fAtmosphericModelKEY) ",
        );


    $join="";
    if (!empty($joins[$val]))
        $join=$joins[$val];
    return $join;
}


?>
