Ejemplo n.º 1
0
 function execute($parUserId)
 {
     // Arrays
     require "arrays.php";
     //Verify item status
     if ($this->status != 'B' and $this->status != 'Q') {
         return 1;
     }
     // Flags
     $inproduction = true;
     // Searching Scene
     $sql = "SELECT * FROM Scene WHERE SceneId='" . $this->sceneId . "'";
     $this->bd->query($sql) or die("<br> Error on execution/result sql = {$sql} <br>");
     $itens = $this->bd->numRows();
     if ($itens == 0) {
         die("Erro nas Cenas");
     }
     $row = $this->bd->fetchRow();
     $this->bd->freeResult($results);
     // Scene data
     $res = explode(".", $row["SceneId"]);
     $sufix = $res[1];
     $satCod = $row["Satellite"];
     $sensor = $row["Sensor"];
     if ($satCod == "P6" and ($sensor == "LIS3" or $sensor == "AWIF")) {
         $date_IRS = $row["Date"];
         $path_IRS = $row["Path"];
         $row_IRS = $row["Row"];
     }
     for ($i = 0; $i < $matSatCod[0]; $i++) {
         if ($satCod == $matSatCod[$i]) {
             break;
         }
     }
     $satName = $matSatName[$i];
     $taName = $satName . "Scene";
     if ($GLOBALS["stationDebug"]) {
         echo "\n RequestClass - Satelite = " . $satName . " TabName = " . $taName . " UserName = "******"\n";
     }
     //Finding Gralha/Fred
     $sql = "SELECT * FROM {$taName} WHERE SceneId='" . $this->sceneId . "'";
     if ($GLOBALS["stationDebug"]) {
         echo "RequestClass - Sql Command: " . $sql . "\n";
     }
     $this->bd->query($sql);
     $row = $this->bd->fetchRow() or die("<br> Error on execution/result sql = {$sql} <br>");
     $file = $row["Gralha"];
     $this->bd->freeResult();
     if ($GLOBALS["stationDebug"]) {
         echo "RequestClass - File = " . $file . "\n";
     }
     // Creating p2u argument
     $argument = $parUserId . ";" . $this->reqId . ";" . $this->sceneId . ";" . $file . ";" . $this->numItem . ";" . $this->restoration;
     $procPar = 0;
     if ($GLOBALS["stationDebug"]) {
         echo "RequestClass - Argument = " . $argument . "\n";
     }
     // Instantiating P2 class
     searchMedia($this->bd, $objMedia, $this->media);
     $execClass = $objMedia[0]->getExecClass();
     if ($GLOBALS["stationDebug"]) {
         echo "1 - RequestClass - starting P2U or P2C = " . $execClass . "\n";
     }
     if ($satCod == "P6" and ($sensor == "LIS3" or $sensor == "AWIF")) {
         include_once "listFiles.php";
         if ($sensor == "LIS3") {
             $sensor = "LISS3";
         } else {
             $sensor = "AWIFS";
         }
         $dir_path = getTiffDir($satCod);
         $res = explode("-", $date_IRS);
         $date_dir = $res[0] . "_" . $res[1];
         $date_aux = $res[0] . $res[1] . $res[2];
         $tiff_dir = $dir_path . "{$date_dir}/{$satCod}" . "_{$sensor}" . "_{$date_aux}/{$path_IRS}" . "_{$row_IRS}";
         //       $tiffs = listFiles2($tiff_dir,"");
         //       $cmd = "ls -1 $tiff_dir";
         //       $res = shell_exec($cmd);
         //			 $tiffs = explode("\n",$res);
         $aux_tiffs = glob($tiff_dir . "/*");
         $index = 0;
         foreach ($aux_tiffs as $file) {
             $tiffs[$index] = basename($file);
             $index++;
         }
         print_r($tiffs);
         $argument = $parUserId . ";" . $this->reqId . ";" . $this->sceneId . ";" . $tiff_dir . ";" . $this->numItem . ";" . $satCod;
         if ($GLOBALS["stationDebug"]) {
             echo "RequestClass ====> starting P2U for P6 products - Argument = {$argument} \n";
         }
         if ($GLOBALS["stationDebug"]) {
             echo "\n RequestClass =====> tifdir = {$tiff_dir} \n";
         }
         $p2u = new p2u($argument);
     } else {
         // GLS
         // ===
         if ($sufix == "GLS") {
             $sql = "SELECT Gralha FROM LandsatScene WHERE SceneId='" . $this->sceneId . "'";
             $this->bd->query($sql) or die("<br> Error on execution/result sql = {$sql} <br>");
             $itens = $this->bd->numRows();
             if ($itens == 0) {
                 die("Erro nas Cenas");
             }
             $row = $this->bd->fetchRow();
             $this->bd->freeResult($results);
             $gralha = $row["Gralha"];
             decodeGralha($gralha, $satellite, $number, $instrument, $channel_gralha, $type, $date, $path, $row);
             if ($number == 5) {
                 $sat_prefix = "LT5";
             } else {
                 $sat_prefix = "LE7";
             }
             $year = substr($date, 0, 4);
             $timestamp = strtotime($date);
             $julian_date = strftime('%j', $timestamp);
             $sql = "SELECT Dataset FROM Scene WHERE SceneId='" . $this->sceneId . "'";
             if ($GLOBALS["stationDebug"]) {
                 echo "\n =====> RequestClass - Sql Command: " . $sql . "\n";
             }
             $this->bd->query($sql);
             $row_result = $this->bd->fetchRow() or die("\n =====> Request.Class - execute ====> Error on execution/result sql = {$sql} \n");
             $dataset = $row_result["Dataset"];
             $this->bd->freeResult();
             if ($dataset == "GLS2000") {
                 $target_tiffs = "*" . $path . "*" . $row . "*" . $date . "*.tar.gz";
             } else {
                 $target_tiffs = "*" . $path . "*" . $row . "*" . $year . $julian_date . "*.tar.gz";
             }
             $dir_path = getTiffDir($sufix);
             $tiff_dir = $dir_path . $satellite . $number;
             $cmd = "find {$tiff_dir} -name {$target_tiffs}";
             $cmd = addslashes($cmd);
             echo "\n =====> Request.Class - execute ====> com = {$cmd} \n";
             $output = shell_exec($cmd);
             $output = substr($output, 0, strlen($output) - 1);
             if ($output == "") {
                 die("\n =====> Request.Class - execute ====> No files : {$target_tiffs} !");
             }
             $tiffs = explode("\n", $output);
             sort($tiffs);
             echo "\ntiffs = \n";
             print_r($tiffs);
             echo "\n";
             $argument = $parUserId . ";" . $this->reqId . ";" . $this->sceneId . ";" . $tiffs[0] . ";" . $this->numItem . ";" . $sufix;
             echo "\nargument = \n";
             print_r($argument);
             echo "\n";
             $p2u = new p2u($argument);
             //      echo "\n<br> argument = $argument";
         } else {
             $p2u = new $execClass($argument);
             $tiffs = $p2u->getTiffs();
         }
     }
     //      print_r($tiffs);
     // For generation of a product
     if ($sensor == "CCD") {
         $bands_all = array(0 => "5", 1 => "BAND1", 2 => "BAND2", 3 => "BAND3", 4 => "BAND4", 5 => "BAND5");
         $bands_lacking = 0;
         foreach ($tiffs as $tiff_found) {
             $one_tiff = basename($tiff_found);
             for ($i = 1; $i <= $bands_all[0]; $i++) {
                 $found = strstr($one_tiff, $bands_all[$i]);
                 if ($found) {
                     $bands_all[$i] = 1;
                     $bands_lacking += 1;
                 }
             }
         }
         for ($i = 1; $i < 6; $i++) {
             if ($bands_all[$i] != 1) {
                 $bands_lacking += 1;
             }
         }
     }
     if ($sensor != "CCD") {
         $factor = 1;
     } else {
         if ($bands_lacking > 0) {
             $factor = 6;
         } else {
             $factor = 5;
         }
     }
     // We could have two repeated bands 3 (CCD1XS and CCD2XS), plus bands 1,2 and 4, and no PAN, and we would have 5 bands on directory!
     //      if(trim($this->productId)== "" and count($tiffs) < $factor)
     if (count($tiffs) < $factor) {
         // Tiff does not exist in disk, lets build it from gralha
         if ($GLOBALS["stationDebug"]) {
             echo "RequestClass - Tiff not found, looking for {$file}\n";
         }
         $gralhas = findSimilarGralhainDisk($file);
         $count_gralhas = count($gralhas);
         if ($count_gralhas > 0 or count($tiffs) > 0) {
             $gralhas_files = array(0 => "CCD1XS", 1 => "CCD2XS", 2 => "CCD2PAN");
             foreach ($gralhas as $filedir) {
                 if ($GLOBALS["stationDebug"]) {
                     echo "<br> \n RequestClass - {$filedir} found <br> \n";
                 }
                 if ($sensor == "CCD") {
                     decodeGralha(basename($filedir), $satellite, $number, $instrument, $channel, $type, $date, $path, $row);
                     $this_gralha_tiffs = findTiffinDiskFromGralha(basename($filedir), $this->restoration, $channel);
                     // look for specific (Channel) Tiff data
                     switch ($type) {
                         case "XS":
                             if ($channel == 1) {
                                 $gralhas_files[0] = "1";
                             } else {
                                 $gralhas_files[1] = "1";
                             }
                             // Gralha CCD2XS present
                             break;
                         case "PAN":
                             $gralhas_files[2] = "1";
                             // Gralha CCD2PAN present
                     }
                     if (count($this_gralha_tiffs) == 0) {
                         $procPar = $this->g2tStart($filedir, $procPar, $satCod);
                     }
                 } else {
                     if ($sensor == "ETM") {
                         $filepan = str_replace("XS", "PAN", $filedir);
                         $procPar = $this->g2tStart($filepan, $procPar, $satCod);
                     }
                     $procPar = $this->g2tStart($filedir, $procPar, $satCod);
                 }
             }
             if ($sensor == "CCD") {
                 for ($i = 0; $i < 3; $i++) {
                     if ($gralhas_files[$i] != "1") {
                         $gralha_name = str_replace("CCD1XS", $gralhas_files[$i], $file);
                         // CCD1XS is the reference for Gralha in CbersScene
                         decodeGralha($gralha_name, $satellite, $number, $instrument, $channel, $type, $date, $path, $row);
                         $tiffs_found = findTiffinDiskFromGralha($gralha_name, $this->restoration, $channel);
                         // look for specific (Channel) Tiff data
                         if (count($tiffs_found) == 0) {
                             $status = findDRDfromSceneId($this->sceneId, $drds, $tapeid, $tapepath, $skip);
                             if ($status == 0) {
                                 if ($gralhas_files[$i] == "CCD1XS") {
                                     $index = 0;
                                 } else {
                                     $index = 1;
                                 }
                                 // Corresponds to DRD[1] (CCD2XS and CCD2PAN)
                                 $fulldrd = findDRDinDisk($drds[$index]);
                                 if (strlen($fulldrd) > 1) {
                                     $d2g = new d2g($drds[$index]);
                                     $d2g->createWorkOrder();
                                     $d2g->start($procPar);
                                     $procPar = $this->g2tStart($gralha_name, $d2g->id(), $satCod);
                                     $gralhas_files[$i] = "1";
                                 } else {
                                     $inproduction = false;
                                 }
                             } else {
                                 $inproduction = false;
                             }
                         } else {
                             $gralhas_files[$i] = "1";
                         }
                         // Gralha not present but Tiffs are !!! That's what matters !!!
                     }
                 }
             }
         } else {
             if ($GLOBALS["stationDebug"]) {
                 echo "<br> RequestClass - Gralhas not found, looking for DRD\n";
             }
             $status = findDRDfromSceneId($this->sceneId, $drds, $tapeid, $tapepath, $skip);
             if ($GLOBALS["stationDebug"]) {
                 echo "<br> RequestClass - Nome DRD = {$drds['0']} <br> \n";
             }
             if ($status == 0) {
                 if ($sensor == "CCD") {
                     $limit = 2;
                 } else {
                     $limit = 1;
                 }
                 // Just one DRD to process
                 if ($GLOBALS["stationDebug"]) {
                     echo "<br> RequestClass - drd = {$drds} \n";
                 }
                 for ($i = 0; $i < $limit; $i++) {
                     $fulldrd = findDRDinDisk($drds[$i]);
                     if (strlen($fulldrd) > 1) {
                         if ($GLOBALS["stationDebug"]) {
                             echo "<br> RequestClass - drd = {$drds} full = {$fulldrd} \n";
                         }
                         if ($satCod == "T1" or $satCod == "A1") {
                             $d2g = new c2h($fulldrd);
                         } else {
                             $d2g = new d2g($drds[$i]);
                             $d2g->createWorkOrder();
                         }
                         $d2g->start($procPar);
                         if ($GLOBALS["stationDebug"]) {
                             echo "<br> RequestClass - After D2G, creating g2t para " . $gralha . "\n";
                         }
                         if ($satCod == "T1" or $satCod == "A1") {
                             decodeDRD($drds[$i], $satellite, $number, $instrument, $year, $month, $day, $hour, $minute, $second, $channel);
                             $datedir = $year . "_" . $month;
                             $datepath = getGralhaDir($satellite . $number) . $datedir;
                             $result = explode("_", $file);
                             $hdfName = $result[0] . "." . "hdf";
                             $hdfFullName = $datepath . "/" . $drds[$i] . "/" . $hdfName;
                             $arg = $hdfFullName;
                         } else {
                             $arg = $file;
                         }
                         if ($i == 1) {
                             $arg = str_replace("CCD1XS", "CCD2XS", $file);
                             $procPar = $this->g2tStart($arg, $d2g->id(), $satCod);
                             $arg = str_replace("CCD1XS", "CCD2PAN", $file);
                             $procPar = $this->g2tStart($arg, $procPar, $satCod);
                         } else {
                             $procPar = $this->g2tStart($arg, $d2g->id(), $satCod);
                         }
                     } else {
                         /*
                         #
                         # Old code segment - proceedings have been improved.
                         #
                         							if ($GLOBALS["stationDebug"]) echo "<br> RequestClass - DRD not found, looking for DRD on tape\n";
                         $targument = $tapeid.";".$tapepath.";;".$skip; // IMPORTANT
                         if ($GLOBALS["stationDebug"]) echo "<br> RequestClass - Argument = " . $targument . "\n";
                         $m2d = new m2d ($targument);
                         $m2d->setStatus ("OPERATOR");
                         if ($GLOBALS["stationDebug"]) echo "<br> RequestClass - Bd = Man " . $m2d->dbman . " Cat = " . $m2d->dbcat . "\n";
                         if ($GLOBALS["stationDebug"]) echo "<br> RequestClass - Bd = Man " . $GLOBALS["dbmanager"] . " Cat = " . $GLOBALS["dbcatalog"] . "\n";
                         $m2d->start ();
                         if ($GLOBALS["stationDebug"]) echo "<br> RequestClass - After M2D, creating d2g para " . $drds . "\n";
                         $d2g = new d2g ($drds[$i]);
                         $d2g->createWorkOrder();
                         $d2g->start ($m2d->id());
                         $procPar = $this->g2tStart($file, $d2g->id(),$satCod);
                         #
                         #
                         #
                         */
                         echo "<br> RequestClass - DRD data ({$drds[$i]}) not found ! <br>\n";
                         if ($sensor == "CCD") {
                             if ($i > 0) {
                                 $arg = str_replace("CCD1XS", "CCD2XS", $argument);
                             }
                             $m2d_warning = new m2d("{$arg}");
                             $m2d_warning->setStatus("OPERATOR");
                             $m2d_warning->start(0, "<font color=#FF0000>WARNING ====> </font> DRD data ({$drds[$i]}) not found on proper directory ! ");
                         } else {
                             $inproduction = false;
                         }
                     }
                 }
             } else {
                 $inproduction = false;
             }
         }
     }
     // Setting up status
     if ($sensor == "CCD" and $inproduction == false) {
         if ($gralhas_files[0] != "1") {
             $arg = str_replace("CCD2XS", "CCD1XS", $argument);
             $msg = "Can\\'t Generate GRALHA CCD1XS Data !";
             $p2u_warning = new p2u($arg);
             $p2u_warning->setStatus("OPERATOR");
             $p2u_warning->start(0, "<font color=#FF0000>WARNING ====>  </font>" . $msg);
         }
         if ($gralhas_files[1] != "1") {
             // Let's first check if CCD2XS Band 3 (that might be repeated due to CCD1XS) is the one that's lacking; if so, NO PROBLEMS !
             if ($bands_all[1] != "1" or $bands_all[3] != "1") {
                 $arg = str_replace("CCD1XS", "CCD2XS", $argument);
                 $msg = "Can\\'t Generate GRALHA CCD2XS Data !";
                 $p2u_warning = new p2u($arg);
                 $p2u_warning->setStatus("OPERATOR");
                 $p2u_warning->start(0, "<font color=#FF0000>WARNING ====>  </font>" . $msg);
             }
         }
         if ($gralhas_files[2] != "1") {
             $msg = "Can\\'t Generate GRALHA CCD2PAN Data !";
             $arg = str_replace("CCD1XS", "CCD2PAN", $argument);
             $p2u_warning = new p2u($arg);
             $p2u_warning->setStatus("OPERATOR");
             $p2u_warning->start(0, "<font color=#FF0000>WARNING ====>  </font>" . $msg);
         }
         $inproduction = true;
         // We previously know that we have at least one CCD - otherwise we would have been directed to the m2d operation above !!!
     }
     if ($this->status != 'Q') {
         $this->updateStatus($inproduction ? 'C' : 'Y');
     }
     if ($inproduction == true) {
         if ($GLOBALS["stationDebug"]) {
             echo "RequestClass - Everything OK - let's start p2u with this dependence " . $procPar . "\n";
         }
         if (trim($execClass) != "p2u") {
             if ($this->status != 'Q') {
                 // This clause has been added to provide a previous execution of a "p2c" request for allowing Control Quality
                 // assesment, before mailing product to user.
                 // Comment this statement if this policy be changed.
                 $p2u->setStatus("OPERATOR");
             }
         }
         $p2u->start($procPar);
     } else {
         $p2u->sendErrorMail($file);
         $p2u->sendFirstMail();
         $p2u->setStatus("OPERATOR");
         switch ($satCod) {
             case "P6":
                 $msg = "Tiff files no found !";
                 break;
             case "L5":
             case "L7":
                 if ($sufix == "GLS") {
                     $msg = "Tiff files no found !";
                 } else {
                     $msg = "DRD data not found !";
                 }
                 break;
             default:
                 $msg = "DRD data not found !";
         }
         $p2u->start(0, $msg);
     }
 }
function findSimilarGralhainDisk($gralha)
{
    decodeGralha($gralha, $satellite, $number, $instrument, $channel, $type, $date, $path, $row);
    if ($GLOBALS["stationDebug"]) {
        echo "findSimilarGralhainDisk: satellite = {$satellite} <br>\n";
    }
    // Build path based on Date
    //  if($satellite == "CREFL")
    if ($instrument == "MODIS") {
        include_once "globals.php";
        $dbcat = $GLOBALS["dbcatalog"];
        $sql = "SELECT DRD FROM ModisScene WHERE Gralha='{$gralha}'";
        $dbcat->query($sql) or $dbcat->error($sql);
        $row = $dbcat->fetchRow();
        $drd = $row["DRD"];
        $pieces = explode(".", $drd);
        $satellite = $pieces[0];
        if ($satellite == "aquadb") {
            $satellite = "AQUA";
        } else {
            $satellite = "TERRA";
        }
    }
    $dname = substr("{$date}", 0, 4) . "_" . substr("{$date}", 4, 2);
    $path = getGralhaDir($satellite . $number) . $dname;
    // Lets find the generic name for similar CCD gralhas (CCD1XS,CCD2XS,CCD2PAN) CBERS_2_CCD1XS_20040130_153_126.h5
    $target = $gralha;
    if ($instrument == "CCD") {
        $pieces = explode("_", $gralha);
        $target = $pieces[0] . "_" . $pieces[1] . "_CCD*_" . $pieces[3] . "_" . $pieces[4] . "_" . $pieces[5];
    }
    if ($instrument == "MODIS") {
        $target = "CREFL*.hdf";
        $path = $path . "/" . $drd;
    }
    if ($instrument == "ETM") {
        $pieces = explode("_", $gralha);
        $target = $pieces[0] . "_" . $pieces[1] . "_ETM*_" . $pieces[3] . "_" . $pieces[4] . "_" . $pieces[5];
    }
    $com = "find {$path} -name {$target}";
    if ($GLOBALS["stationDebug"]) {
        echo "findSimilarGralhainDisk - command = {$com} <br>\n";
    }
    $output = shell_exec($com);
    $output = substr($output, 0, strlen($output) - 1);
    // No file was found
    if (strlen($output) <= 1) {
        if ($GLOBALS["stationDebug"]) {
            echo "findSimilarGralhainDisk - No file found <br>\n";
        }
        $fullname = array();
        return $fullname;
    }
    $fullname = explode("\n", $output);
    if ($GLOBALS["stationDebug"]) {
        echo "findSimilarGralhainDisk - Files found : " . count($fullname) . " (" . strlen($output) . ")<br>\n";
        foreach ($fullname as $file) {
            echo "{$file} <br>\n";
        }
    }
    return $fullname;
}