RequestMgr.php 101 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456
<?php
/**
 * @class RequestMgr
 * @version $Id: RequestMgr.php 2914 2015-05-19 10:31:38Z elena $
 *
 *
 */

class RequestMgr extends AmdaObjectMgr {

      public $obj;
      public $impex_prefix = array('impex___', 'spase__', 'CLWEB_');
      protected $searchChain;
      protected $jobXml, $jobXmlName;
      protected $parser, $paramBuilder;
      protected $derivedParamMgr;
      protected $request, $nObjects;
      protected $currentInt, $totalInt, $ttName;
      protected $paramsWithoutCode = false;
      protected $spectraParams = false;
      protected $dataBases;
      protected $types = array('request', 'condition');
      protected $argumentsByParam = array();
      protected $defaults = 'select...';
      protected $scatterOffset, $scatterOffsetX, $firstOffset;  
      protected $timeFormat = array('YYYY-MM-DDThh:mm:ss' => 0, 'YYYY MM DD hh mm ss' => 1, 'DD MM YYYY hh mm ss' => 2,  'YYYY DDD hh mm ss' => 3);
      protected $fileMgr;
      
      function __construct($type) {
 
        parent::__construct('Request.xml');
        $this->type = $type;

        $this->contentRootId = $type.'-treeRootNode';
        $this->contentRootTag = $type.'List';
        $this->objTagName = $type; //'request';
        $this->jobXmlName = USERDIR.'jobs.xml';
        $this->jobXml = new DomDocument("1.0");
        if (file_exists($this->jobXmlName)) {
           $this->jobXml->load($this->jobXmlName);
        }
        if ($type == 'request') {
           $this->id_prefix = 'req_';
           $this->attributes = array('name' => '');
           $this->optionalAttributes = array();
       }
       else {
           $this->id_prefix = 'cond_';
           $this->attributes = array('name' => '');
           //, 'timestep' => '', 'datagap' => '', 'buildchain' => '',
           //'StartTime' => '', 'TimeInt' => '');
           $this->optionalAttributes = array();
       }

       if (file_exists(paramListXml)) 
          $this->paramsWithoutCode = true;

       if (file_exists(spectraXml)) 
          $this->spectraParams = true;

       $this->parser = new Parser();
       $this->paramBuilder = new ParamBuilder();

       if (!file_exists($this->xmlName)) {
          $this->createDom();
          $this->xp = new domxpath($this->contentDom); 
       }
       //external bases       
       if (file_exists(RemoteData.'Bases.xml')) {
          $dataBasesDoc = new DomDocument("1.0");
          $dataBasesDoc->load(RemoteData.'Bases.xml');
          $this->dataBases = $dataBasesDoc->getElementsByTagName('dataCenter');
       }

       putenv("USER_DATA_PATH=".USERDATADIR);
       putenv("USER_WS_PATH=".USERWSDIR); 
       putenv("PHP_CLASSES_PATH=".CLASSPATH);
        
    }

/*********************************************************************
*   BEGIN : generic functions
**********************************************************************/
 protected function getWsdVars($vars){
   
    foreach($vars as $var) {           
            if(strpos($var, 'wsd_') === 0) 
            { 
                $wsdVars[] = $var;                
            }         
    }
 
    if (isset($wsdVars)) return $wsdVars;
    return false;
  }

  protected function getImpexVars($vars){
   
    foreach($vars as $var) {    
       foreach($this->impex_prefix as $prefix) {
            if(strpos($var, $prefix) === 0) 
            { 
                $impexVars[] = $var;   
            }
        }
    }
 
    if (isset($impexVars)) return $impexVars;
    return false;
  }

  protected function param2dd($paramID)
  {
      $pairs = array("-" => "_", "%" => "_","\\" => "_","$" => "_",":" => "_","+" =>" _","-" => "_","#" => "_","@" => "_", "." => "_",">" => "_", "<" => "_");    
      return strtr($paramID, $pairs); 
  }

  protected function param2dd_print($paramID)
  {
      $pairs = array("-" => "_", ":" => "_", "%" => "_","\\" => "_","$" => "_","+" =>" _","-" => "_","#" => "_","@" => "_", "." => "_",">" => "_", "<" => "_");    
      return strtr($paramID, $pairs); 
  }

  protected function setObject($obj) 
  {
      $this->obj = $obj;
  }

  /* temp name for output */
  protected function getRandomName() 
  {
      $length = 6;
      $characters = '0123456789abcdefghijklmnopqrstuvwxyz';
      $string = '';

      for ($p = 0; $p < $length; $p++) {
	  $string .= $characters[mt_rand(0, strlen($characters))];
      }

      return 'DD'.$string;
  }

  //DD notation 'DDDD:HH:MM:SS'  ATTENTION!!! DDDD => 4 digits!!!
  protected function getDdInterval() 
  {
      return $this->obj->durationDay.':'.$this->obj->durationHour.':'.
	      $this->obj->durationMin.':'.$this->obj->durationSec;
  }    

      
  //DD notation 'YYYY:DOY:HH:MM:SS' DOY => DOY-1
  protected function getDdTime($date)
  {
      $ddStart = getdate(strtotime($date));
      $DDTime = sprintf("%04d",$ddStart['year']).':'.sprintf("%03d", $ddStart['yday']).':'.
		  sprintf("%02d",$ddStart['hours']).':'.sprintf("%02d",$ddStart['minutes']).':'.
		  sprintf("%02d",$ddStart['seconds']);

      return $DDTime;
  }
       
/*
 * 
 */
/*public function isValidRequest()
{
 $usrMgr = new UserMgr();
 foreach ($this->obj->list as $param)
 {
  $usrMgr->isAvailableParamForUser();
 }
}*/
        protected function makeOrbitParam($stl) 
        {        
            $params = array( 'cluster1' => 'c1_xyz', 'cluster2' => 'c2_xyz', 'cluster3' => 'c3_xyz', 'cluster4' => 'c4_xyz',
                            'themisa'=> 'tha_xyz', 'themisb'=> 'thb_xyz', 'themisc'=> 'thc_xyz', 'themisd'=> 'thd_xyz', 'themise'=> 'the_xyz',
                            'geotail' => 'gtl_xyz',
                            'mex' => 'mex_xyz', 'mgs' => 'xyz_mgs_mso', 'maven' => 'mav_xyz_mso'); 

            $this->makeLocalParams(array($params[strtolower($stl)]));         
                    
        }

        protected function makeSWMonitorParam($swm) 
        {
            $params = array('omni' => array('omni5_sw_v', 'omni5_sw_n', 'omni5_imf'),
                            'amda' => array('ace_xyz', 'wnd_xyz', 'imp8_xyz', 'imf', 'wnd_b', 'imp8_b',
                                            'swp_n', 'swp_v', 'wnd_swe_n', 'wnd_swe_vmag', 'imp_mit_nfit', 'imp_mit_vfit'));
 
            $this->makeLocalParams($params[strtolower($swm)]);                             
        } 
          
        /* 
        *  plot objects - check Local Params without code
        */
        protected function isParamWithoutCode($id) 
        {           
            if (!$this->paramsWithoutCode) return false;            
            $localParams = new DomDocument('1.0');
            $localParams->load(paramListXml); 
            $xp = new domxpath($localParams);
            if ($xp->query('//PARAM[.="'.$id.'"]')->length != 0) {
                                $this->paramBuilder->paramLocalBuild($id);
                                return true;
            }
            return false;            
       }

        /* 
        *  plot objects - check and create Remote Params 
        */
        protected function isRemoteParam($object) 
        {
            if (!$this->dataBases) return false;
        
            foreach ($this->dataBases as $dataBase){
                        $dataBaseId = $dataBase->getAttribute('xml:id');
                        if (strncmp($object, $dataBaseId, strlen($dataBaseId)) === 0)  {
                                    return true;
                            }
                    }
            return false;
        }

        protected function makePlotRemoteParams($object, $file_name, $comp) 
        {
            require_once('paramBuild.php');
                    
            paramBuild($object);
            $SED = "sed 's/".$object."/".$this->param2dd($object)."/g' ".$file_name." > temp";
            system($SED);

            copy('temp',$file_name);

            if ($comp == 0) 
                        $comp = "0";
            if ($comp != -1) 
                        paramBuildComp($object, $comp);
            return true;

}

        /* 
        *  Process   Local Params without codes if exist
        */
        protected function makeLocalParams($realVars) 
        {     
            $localParams = new DomDocument('1.0');
            $localParams->load(paramListXml);
            $xp = new domxpath($localParams);
            foreach ($realVars as $var) {
            // process component
                        if (strpos($var, '(') !== false) {
                                    $var_arr = explode('(', $var);
                                    $var = $var_arr[0];
                        }
                        if ($xp->query('//PARAM[.="'.$var.'"]')->length  != 0) 
                                        $this->paramBuilder->paramLocalBuild($var);   
            }
            
        }

        /*
        *  Process  External Objects if exist
        */
        protected function makeRemoteParams($realVars) 
        {
            require_once 'paramBuild.php';

            foreach ($realVars as $var) 
            {
                foreach ($this->dataBases as $dataBase) 
                {  
                        $id = $dataBase->getAttribute('xml:id');
                        if ($dataBase->getAttribute('group') != 'IMPEX') { 
                                $pos = strpos($var, $id);
                                if ($pos === false) 
                                                    continue;
                                $arrComp = explode('(', $var);
                                $var_ = $arrComp[0];
                                paramBuild_($var_);
                                $this->searchChain = strtr($this->searchChain, $var, param2dd($var));
                                $var = param2dd($var);
                                break;
                         }
                }
            }
        }

    /*
    *         process  WS parameters
    */      
    protected function makeWsParams($realVars) {

      if (!$this->derivedParamMgr) $this->derivedParamMgr = new DerivedParamMgr('derivedParam');

      foreach ($realVars as $var){
                // Derived Params
           if (strncmp(strtolower($var), 'ws_', 3) == 0) {
               // check if resource file exists
               $varName = substr($var,3);

           if (!$this->derivedParamMgr->objectExistsByName($varName)) {
               return false; //TODO error processing
           }
           $varId = $this->derivedParamMgr->getObjectIdByName($varName);
           $resFile = $varId.'.res';

           if(!file_exists(USERWORKINGDIR.$resFile)) {
	      return false; //TODO process error
	    }
	    copy(USERWORKINGDIR.$resFile, $var.'.res'); 

            //TODO if needed to replace alias???
	    $realObject = $this->parser->replaceAll($var);

            $realWsVars = $this->parser->getVars($realObject); 
            foreach ($realWsVars as $wsVar) { 
                 if ((strncmp($wsVar, "impex___", 8) === 0) || (strncmp($wsVar, "spase___", 8) === 0)){
                      $tmpArr = explode('_',$wsVar);
                      $n = count($tmpArr); 
                      if ((strncmp($wsVar, "impex___LATMOS_", 15) === 0) || (strncmp($wsVar, "spase___IMPEX_NumericalOutput_LATMOS_", 37)=== 0)) {
                           $newVar = str_replace ("_".$tmpArr[$n-2]."_".$tmpArr[$n-1], "", $wsVar);
                      }
                      else {
                           $newVar = str_replace ("_".$tmpArr[$n-1], "", $wsVar);
                      }
                      $texte = file_get_contents($var.'.res'); 
                      $newText = str_replace($wsVar, $newVar,$texte);
                      file_put_contents($var.'.res' , $newText); 
                 }
             }
            //functions for plotting parameter
            $this->paramBuilder->makeWsParamPlot($var);
 
            // $realWsVars = $this->parser->getVars($realObject); 
 
            /*Check if WS_ is from Local Params without codes*/   
            if ($this->paramsWithoutCode) {            
               $this->makeLocalParams($realWsVars); 
            }

            /*Check if WS_ is from Spectra Params  */   
            if ($this->spectraParams) {          
               $this->makeSpectraParams($realWsVars); 
            }

            /*Check if WS_ is from Remote  Params  */   
            if ($this->dataBases) {          
                $this->makeRemoteParams($realWsVars);
            }

            /*Check if WS_ is from WS_ or WSD_ Params ONE LEVEL OF NESTING ONLY!!! */   
            foreach ($realWsVars as $wsVar) { 
                  if (strncmp(strtolower($wsVar), 'ws_', 3) === 0) {         
                     $this->paramBuilder->makeWsParamPlot($wsVar); 
                     $wsVarName = substr($wsVar,3);
                     $varId = $this->derivedParamMgr->getObjectIdByName($wsVarName);
                     $tempObj = $this->derivedParamMgr->getObject($varId);
                     $expression = $this->parser->replaceAll($tempObj["buildchain"]);
                     $resFile = $varId.'.res';
                     if(!file_exists(USERWORKINGDIR.$resFile)) 
                          return false; //TODO process error
                     
                     copy(USERWORKINGDIR.$resFile, $wsVar.'.res');
                     $lines = file($wsVar.'.res');
                     $wsWsVarsStr = explode(' ',$lines[0]);

                     $wsWsVars = array();
                     foreach ($wsWsVarsStr as $wsWsVar) if (trim($wsWsVar) != '') $wsWsVars[] = $wsWsVar;
                            $this->paramBuilder->makeParamCalcul($this->parser->convert2Idl($expression), $wsWsVars, $wsVar);
                            /*Check if WS_ is from Local Params without codes*/   
                            if ($this->paramsWithoutCode) {                                                      
                               $this->makeLocalParams($wsWsVars); 
                            }
                  }
                  elseif (strncmp(strtolower($wsVar), 'wsd_', 4) === 0)                                          
                      $this->makeWsdParams(array($wsVar));
                  elseif ((strncmp($wsVar, "impex___", 8) === 0) || (strncmp($wsVar, "spase___", 8) === 0)){
		    $imp = array('realVars'=> array($wsVar), 'fromWS' => true);
                    $this->makeWsrParams($imp);
                  }
            } 
            //function to calculate parameter
            $this->paramBuilder->makeParamCalcul($this->parser->convert2Idl($realObject),$realWsVars,$var);  
          }
      }

    } 

    /* Process WSD objects */
    protected function makeWsdParams($realVars) 
    {
        $paramMgr = new DerivedParamMgr('myDataParam');                
        $xpd = new domxpath($paramMgr->contentDom); 

        foreach ($realVars as $var)
            if (strncmp($var, "wsd_", 4) == 0) {
                        $name = explode('wsd_',$var);
                        $params = $xpd->query("//mydata[@name='".$name[1]."']");  
                        if ($params->length >  0)
                            $this->paramBuilder->makeWsdParam($params->item(0)->getAttribute('xml:id'));
            }
            elseif ((strncmp($var, "impex___", 8) === 0) || (strncmp($var, "spase___", 8) === 0))  {
    
            }
    }
    

     /* Process SIMU et INSITU request param*/
     protected function makeWsrParams($imp){
        $realVars = $imp['realVars'];
        $fromWS = $imp['fromWS'];
       
        $isMulti =  $this->obj->timeTables ? true: false; 
        if (!$isMulti){
           $startDate = $this->obj->startDate;
           $stopDate = $this->getStopDate();
        }
        else {
           $interval = $this->getFirstTimeTableInterval();
           $startDate = $interval['start'];
           $stopDate = $interval['stop'];
        }

        $paramMgr = new DerivedParamMgr('mySimuParam');
    
        $simuParamMgr = new SimuParamMgr();        
        $myWsrParamMgr = new WsrParamMgr();
             
        $myBaseManager = new BaseManager();
        $xpd = new domxpath($myBaseManager->baseDom);
        $data =  array();

        $paramDom = new DomDocument("1.0");
        $paramDom->load(getenv('USER_WS_PATH').'WsParams.xml');
        $xpdSimu = new domxpath($paramDom);  

        foreach ($realVars as $var) {

            $isVector = false;
            $isSimu = strncmp($var, "impex___", 8) === 0 || strncmp($var, "spase___", 8) === 0;
            $isClWeb = strncmp($var, "CLWEB_", 6) === 0;

        // All simulation parameters and arguments has prefix "spase___" or "impex___" (old version) 
            if ($isSimu || $isClWeb) { 
                if ($fromWS && !$isClWeb) {
                   $this->argumentsByParam = $this->cutSimuArgs($var);
                   foreach ($this->argumentsByParam as $key => $value) 
                        $var = $key;
                } 
                else $var = $this->param2dd($var);
 
                $fileExists = false;
               // ClWeb
                if ($isClWeb ){ 
                   $data['paramID'] = $var; 
                   $data['startDate'] = $startDate;
                   $data['stopDate'] = $stopDate;
                   // Creation request parameters
                   $myWsrParamMgr->setParams($data);
                   // definitions mask for wsr parameter
                   $mask = $myWsrParamMgr->getMask();
                   // definitions download file name for wsr parameter
                   $fileName = $myWsrParamMgr->getFileName(); 
//                   $dataType = "mywsrdata";
                }
                // Simulation
                else {
		    // Creation request simu parameters
                    $simuParamMgr->setParams($var, $startDate, $stopDate, $this->argumentsByParam);
                    // definitions mask for simu parameter
                    $mask = $simuParamMgr->getMask();
                     // definitions download file name for simu parameter
                    $fileName = $simuParamMgr->getFileName();
                    // test if request parameter is Vector
                    $isVector = $simuParamMgr->isVector();
//                    $dataType = "mysimudata";
                }

		// definition query for seaching mask in base.xml 
                $params = $xpd->query("//vi[mask='".$mask."']");

if (file_exists('/home/budnik/public_html/AMDA-NG4/trunk/data/budnik/LOG'))
                     unlink('/home/budnik/public_html/AMDA-NG4/trunk/data/budnik/LOG');
                // VI exists!!!   
                if ($params->length >  0) {  
                    // IMPORTANT !!! It is the public method of baseManager class to define ALL needed intervals to request!!!
                    // Used in moreData.php also 
                    //TODO move from moreData.php to PlotMgr.php for new kernel
                    $intervalsToGet = $myBaseManager->intervalsToGet(strtotime($startDate), strtotime($stopDate),$mask);
                    $paramsWsr = $xpdSimu->query("//*[@mask='".$mask."']");
                    $wsrID = $paramsWsr->item(0)->getAttribute('xml:id');         
                    // data recuperation
                   $start = $intervalsToGet['start'];
                   $stop = $intervalsToGet['stop'];

                    if (count($start) > 0) {
                      for ($i = 0; $i < count($start); $i++) {
                        if ($isClWeb) {
                            $myWsrParamMgr->updateInterval($start[$i],$stop[$i]);
                            $fileNames = $myWsrParamMgr->getData();
                          }
                        else {  
                            $simuParamMgr->updateInterval($start[$i],$stop[$i]);
                            $fileNames = $simuParamMgr->getSimuData();
                         }                           
                        }  
                     //   if (!$isClWeb) $isVector  = $simuParamMgr->isVector();
                    }
                    $this->paramBuilder->makeWsdParam($wsrID);
                }
               // No such VI - It is the very first request
                else {		 
                    if ($isClWeb){
                        $fileNames = $myWsrParamMgr->getData();
                        $var = $this->param2dd($var);
                    }
                    else {  
                        $fileNames = $simuParamMgr->getSimuData();
                        $isVector = $simuParamMgr->isVector();
                    }

                    $input = (object)array(
                        "varName" => $var,
                        "isVector"=> $isVector,
                        "fileName"=> $fileName,
                        "mask"=> $mask,
                        "startDate"=> strtotime($startDate),
                        "stopDate"=> strtotime($stopDate)
                    );     

                    if ($fileNames[$var]){
                        if ($isClWeb){
                            $tmpObj = $this->makeWsrParamsObj($input, $isClWeb);
                            $res = $paramMgr->createWsrParameter($tmpObj,'myWsrParam');
                        }
                        else {
                            $tmpObj = $this->makeWsrParamsObj($input);
                            $res = $paramMgr->createWsrParameter($tmpObj,'mySimuParam');
                        }
                    }
                    if ($res['id'])                                
                            $this->paramBuilder->makeWsdParam($res['id']);                    
                }
            }
        }
    }       
    /* Protected request param*/
    protected function makeWsrParamsObj($input, $isClWeb) {

      $params =  explode( '_', $input->varName);

      if (!$isClWeb){
        // Recuperation names of argument(s)
         if (!$input->isVector)
            $varName = $params[count($params) -1];
         else
            $varName = $params[count($params) -3].'_'.$params[count($params) -2].'_'.$params[count($params) -1];
      }
      else {
 
         $dom = new DomDocument();
         $dom->load(baseDirCLWEB);
         $param = $dom->getElementById($input->varName);
         // Name of parameter in CLWeb
         $varName = $param->getAttribute('var');
         // To know if it's Spectra data in ws param we need to indicate "plottype" => $spectra, but in RemoteParams.xml the atribute 'display_type' == 'spectrogram'
         // so we need create variable = 'Spectra'
         if ($param->hasAttribute('display_type') && ($param->getAttribute('display_type') == 'spectrogram')) $spectra = 'Spectra';
      }
      
      $myFileManager = new FilesMgr();
      $object = (object)array(
        "file"=> $input->fileName,
        "format"=> "nc",
        "varName"    => $this->param2dd($varName),
        "mask"=> $input->mask
      );

      if (!$isClWeb){  
      //delete different possible IMPEx prefix
         $tmpTmp =    str_replace ("impex___", "", $this->parameterID);
         $tmpT =    str_replace ("spase___IMPEX_NumericalOutput_", "", $tmpTmp);
         $tmp =    str_replace ("spase___IMPEX_SimulationRun_", "", $tmpT); 
         $tmpArr = explode('_',$tmp);
         $format = "vot";
         $parentTag = "mysimudataList";
         $contentRootId = "mySimuParam-treeRootNode";
         if ($this->getVotFileMgr($input->fileName)) {
	      $infos = $this->fileMgr->getFieldInfoByID($varName);
	      $samplings = $this->fileMgr->getSamplings();
	      $id = $infos['id'];
	      $type = $infos['type'];
	      $units = $infos['unit'];
          }
       }
       else {
         $tmpArr[0] = $this->parameterID;
         $format = "nc";
         $parentTag = "mywsrdataList";
         $varName = $input->varName;
         $contentRootId = "myWsrParam-treeRootNode";
         $infos = $myFileManager->getParamInfo($object);
         $samplings['minSampling'] = $infos["info"]["minsampling"];
         $samplings['maxSampling'] = $infos["info"]["maxsampling"];
         $id =  $infos["info"]["name"];
         $type = $infos["info"]["type"];
         $units = $infos["info"]["unit"];
      }

      //size definition
      if (($infos['size'] == '') && ($infos["info"]["size"] == ''))
         $size = 1;
      else {
        if ($infos["info"]["size"] != '') $size = $infos["info"]["size"];
        else
         $size = $infos['size'];
      }

      $obj = (object)array(
          "name"    => $input->varName,
          "format"=> $format,
          "size"=> $size,
          "realvar"=> $id,
          "file"=> $input->fileName,
          "minsampling" => $samplings['minSampling'],
          "maxsampling" => $samplings['maxSampling'],
          "type" => $type,
          "units" => $units,
//       "ytitle" => str_replace ("impex___", "", $input->varName),
          "ytitle" => $tmpArr[0].' '.$varName,
          "mask"=> $input->mask,
          "args"=> $this->argumentsByParam,
          "start"=> $input->startDate,
          "stop"=> $input->stopDate,
          "parentTag"=> $parentTag,
          "contentRootId" => $contentRootId,
          "plottype" => $spectra
        );


        return $obj;
    }
/* 
 *	Recuperation arguments from request variable name for download parameter
 */
    protected function cutSimuArgs ($var){
        $args = array();
        $tmpTmp =    str_replace ("impex___", "", $var);
        $tmpT =    str_replace ("spase___IMPEX_NumericalOutput_", "", $tmpTmp);
        $tmp =    str_replace ("spase___IMPEX_SimulationRun_", "", $tmpT); 
        $tmpArr = explode('_',$tmp);
        $n = count($tmpArr);
        //LATMOS parameters have 2 arguments: Satellite & ClockAngle others simulations have only Satellite arguments. 
        //Exceptions: LESIA simulations and SINP simulations with target different to Earth.
        if ($tmpArr[0] == 'LATMOS') {
           $args['Satellite'] = $tmpArr[$n-2];
           $args['ClockAngle'] = $tmpArr[$n-1];
           $var = str_replace ("_".$tmpArr[$n-2]."_".$tmpArr[$n-1], "", $var);
        }
        else {
          $args['Satellite'] = $tmpArr[$n-1];
          $var = str_replace ("_".$tmpArr[$n-1], "", $var);
        }
        return array($var => $args);
    }
    /*
    * VOTable format processing done by an instance of VOTableMgr
    */
    protected function getVotFileMgr($fileName) {
        if (!$this->fileMgr)
	    $this->fileMgr = new VOTableMgr();
      
        $this->fileMgr->load(USERDATADIR.$fileName);
        if ($this->fileMgr) 
           return true;
        else 
           return false;
   }

        /* Interval from StartTime and Stop Time */ 
   protected function calculateTimeInt($interval){
       $int = strtotime($interval['stop']) - strtotime($interval['start']);
  
       $days = floor($int / 86400);
       $int -= $days * 86400;

       $hours = floor($int / 3600);
       $int -= $hours * 3600;

       $minutes = floor($int / 60);
       $secs = $int - $minutes*60;
 
       $DdInterval  = sprintf("%04d:%02d:%02d:%02d", $days, $hours, $minutes, $secs); 

       return  $DdInterval; 

  }
/*********************************************************************
*  END : generic functions
**********************************************************************/

/*********************************************************************
*   BEGIN : Search specific functions
**********************************************************************/

        /*
        * Check if local_param  is Special Spectra Cases - to Create special PRO
        */
        protected function makeSpectraParams($realVars) 
        {
            if ($this->spectraParams)
            {
                $spectraParams = new DomDocument('1.0');
                $spectraParams->load(spectraXml);
                $allSpectraParams = $spectraParams->getElementsByTagName("PARAM");

                foreach ($realVars as $theVar) {
                    for ($j = 0; $j < $allSpectraParams->length; $j++) {
                        $paramNode = $allSpectraParams->item($j);
                        if (strpos($theVar, $paramNode->nodeValue) !== false) {
                                    $PRODIR = PRO;                                    
                                    $temp = explode("_",$theVar);
                                    $EMIN = $temp[3];
                                    $EMAX = $temp[4]; 
                                    // generate all necessary codes for models
                                    if ($paramNode->hasAttribute('model')) {
                                        if ($temp[0] == "bcain" || $temp[0] == "bmorsch") {
                                            // satellite
                                            $EMIN =  strtolower($temp[2]);
                                            // sampling
                                            $EMAX =  strtolower($temp[3]);
                                        }
                                        else {
                                            // satellite
                                            $EMIN = strtolower($temp[1]);
                                            //monitor sw  
                                            $EMAX = strtolower($temp[2]);
                                            $this->makeSWMonitorParam($EMAX); 
                                        }
                                        $this->makeOrbitParam($EMIN);                                         
                                    }
                                    else {
                                        $EMIN = $temp[3];
                                        $EMAX = $temp[4]; 
                                    }

                                    if ($EMAX) {
                                        $newFunctionName = $paramNode->nodeValue.$EMIN."_".$EMAX."_.pro";
                                        $SED = "sed 's/EMIN/".$EMIN."/g' ".$PRODIR.$paramNode->nodeValue.".pro | sed  's/EMAX/".$EMAX."/g' > ".$newFunctionName; 
                                    }
                                    else {
                                        $newFunctionName = $paramNode->nodeValue.$EMIN."_.pro";
                                        $SED = "sed 's/EMIN/".$EMIN."/g' ".$PRODIR.$paramNode->nodeValue.".pro > ".$newFunctionName;
                                     }                                    
                                    system($SED);
                                }
                        }
                }
            }
        }
 

        /*
        *   Create search.list with timing for DD_Search
        */        
        protected function createSearchRequestTime() 
        {
            $this->request = fopen(USERWORKINGDIR.'search.list','w');

            fwrite($this->request,$this->getDdTime($this->obj->startDate).PHP_EOL);
            fwrite($this->request,$this->getDdInterval().PHP_EOL);

            fclose($this->request);
        }

        /*
        * Create resource file for DD_Search
        */
        protected function createSearchRequest($realVars, $New_Table) 
        {
              $file = fopen(USERWORKINGDIR.'search.res','w');
              fwrite($file, $New_Table.PHP_EOL);
              fwrite($file,$this -> searchChain.PHP_EOL);
              foreach ($realVars as $var) fwrite($file, $var." ");
              fwrite($file,PHP_EOL.$this->obj->sampling.PHP_EOL);
              fwrite($file,$this->obj->gap.PHP_EOL);
              fclose($file);
            //TODO
            /*
            *   Create file WITH VARS DESCRIPTION
            */
            //              $file = fopen(USERWORKINGDIR.'search_info.res','w');
            //              fwrite($file,count($realVars).PHP_EOL);
            //             foreach ($realVars as $var) fwrite($file, $this->getFullName($searchChain, $var).PHP_EOL);
            //             fclose($file);
        }

        /*
        *  Generate codes for parameters wich are not 'hard-coded'
        */
        protected function makeSearchParams() 
        {
            $searchChain = $this->parser->replaceAll($this->obj->expression);
            $realVars = $this->parser->getVars($searchChain);
            $this -> searchChain = $searchChain;

            // Check if local_param  is Special Spectra Cases - to Create special PRO
            $this->makeSpectraParams($realVars);

            // Process   Local Params without codes if exist
            $this->makeLocalParams($realVars);

            // Make external params
            $this->makeRemoteParams($realVars);

            // Make WS params
            $this->makeWsParams($realVars);

            // Make WSD params
            $this->makeWsdParams($realVars);

            $this->paramBuilder->makeParamCalcul($this->parser->convert2Idl($this->searchChain), $realVars, "");

           return  $realVars;
        }

/*****************************************************************************
*  END:  Search specific functions
******************************************************************************/

/******************************************************************************
*   BEGIN : Plot specific functions
******************************************************************************/
        /*
        * Generic Plot Settings: Orientation: portrait, landscape;
        *                        ColorTable;
        *                        Font;
        *                        PPP
        */
        protected function setPlot()
        {
            $resFile = file(USERWORKINGDIR.'DD_Request.res');
            $res = fopen('./DD_Request.res','w');
            fwrite($res, strtolower($this->obj->orientation).PHP_EOL); 
            for ($k = 1; $k < 3; $k++) fwrite($res,$resFile[$k]);
            fwrite($res, $this->obj->ppp.PHP_EOL); 
            fclose($res);
                                    
            $res = fopen('./DD_PlotSettings.res','w');  
            fwrite($res,$this->obj->charSize.PHP_EOL);
            fwrite($res,$this->obj->thickness.PHP_EOL);
            fclose($res);
            //TODO forced layout with charsize
            $this->scatterOffsetX = $this->obj->format == 'PNG' && !$this->obj->forcedLayout ? $this->obj->charSize * 0.23 : 0.03;
            $this->scatterOffset = $this->obj->format == 'PNG' && !$this->obj->forcedLayout ? $this->obj->charSize * 0.05 : 0.06;
            $this->firstOffset =  $this->obj->format == 'PNG' && !$this->obj->forcedLayout ? $this->obj->charSize * 0.07 : 0.08;
        }

         protected function cmp($a, $b){ 
              return  $a->height < $b->height;
           }

/*
        *
        */
        protected function makeForcedLayout($panels) 
        { 
            $TotalHeight = 0.0;      
    $PWidth = 0.9;
            $ScatterCoef = 1;
            
            $PN = 1;
    
            $ScatterPanels = array();
            $TimePanels = array();

            foreach ($panels as $panel) { 
                    if ($panel->plotType == 'SCATTER') {
                        if (strtolower($this->obj->orientation) == 'portrait') $panel->width *= 1.625;
                            $ScatterPanels[] = $panel;
                    }
                    else $TimePanels[] = $panel;
            }

            usort($ScatterPanels, '$this->cmp');

            $widthArr = array();
            foreach ($ScatterPanels as $panel) $widthArr[] = $panel->width;

            $PlotPanels = array(array());

            for ($i = 0; $i < count($ScatterPanels); $i++) 
            { 
                $panel = $ScatterPanels[$i];
                if (!$panel->pN) { 
                    $width = $panel->width;  
                    $panel->pN = $PN;  
                    $PlotPanels[$PN][] = $panel;
                        for ($j = 1; $j < count($widthArr); $j++) { 
                        $panel1 = $ScatterPanels[$j];
                        if (!$panel1->pN) {
                            $widthNew = $width + $widthArr[$j];
                            if ($widthNew < $PWidth*$ScatterCoef) {
                            $width = $widthNew;
                            $panel1->pN = $PN;
                            $PlotPanels[$PN][] = $panel1;
                            if ($j == count($widthArr) -1) {
                                $PN++; break 1;
                            }
                            }
                            else if ($j < count($widthArr) -1) {
                                continue;
                            }
                            else {
                                $PN++; break 1;   
                            }
                        }
                    }
                }
            }
 
            foreach ($TimePanels as $panel) {
                            $TotalHeight += $panel->height;
            } 

            $ScatterMaxHeight = array();
            $ScatterTotalWidth = array();
            for ($i = 1; $i <= $PN; $i++) {
                $height = 0.0;
                $width = 0.0;
                foreach($PlotPanels[$i] as $panel){
                    $height = $panel->height > $height ? $panel->height : $height;
                    $width += $panel->width;
                } 
                $ScatterMaxHeight[$i] = $height;
                $ScatterTotalWidth[$i] = $width;
                $TotalHeight +=$height; 
            }
               
                
            $PHeight = 1.0 - $PN*$this->scatterOffset - $this->firstOffset;
            $heightCoeff = $TotalHeight > $PHeight ? $PHeight/$TotalHeight : 1.0;

            $first = true;  
      
            foreach (array_reverse($TimePanels) as $panel)
            {
                if ($panel->children[0]->paramArgs == "OrbitPresentation=CYL") {
                    $width = $panel->width*$heightCoeff;                    
                    $panel->XPmin = ($PWidth - $width)/2.0;
                    $panel->XPmax = $panel->XPmin + $width;
                }
                else { 
                    $panel->XPmin = 0.0;
                    $panel->XPmax = $PWidth;
                }
                //Start from lower panel
                $panel->YPmin =  $first  ? $this->firstOffset : $YPmax;
                $YPmax =  $panel->YPmin + $panel->height*$heightCoeff;
                $panel->YPmax = $YPmax;
                $first = false; 
            }

            if ($first) $YPmax = $this->firstOffset + 0.03;
            else  $YPmax += $this->scatterOffset;

            // Y coordinates     
            for ($i = 1; $i <= $PN; $i++) { 
                $newWidth = ($PN-1)*$this->scatterOffsetX;
                $realHeight = $ScatterMaxHeight[$i]*$heightCoeff;
                foreach ($PlotPanels[$i] as $panel) {                   
                    $panelCoeff = $panel->height > $realHeight ? $realHeight/$panel->height : 1;    
                    $panel->YPmin = $YPmax;
                    $panel->YPmax =  $panel->YPmin + $panel->height*$panelCoeff;     
                    $width = $panel->width*$panelCoeff;
                    $panel->panelCoeff = $panelCoeff;
                    $newWidth += $width;
                }          
                // X coordinates
                $xOffset = 0.0;
                foreach ($PlotPanels[$i] as $panel) {                          
                    $width = $panel->width*$panel->panelCoeff;
                    $panel->XPmin = ($PWidth - $newWidth)/2.0 + $xOffset;
                    $panel->XPmax = $panel->XPmin + $width;
                    $xOffset +=  $this->scatterOffsetX+$width;
                }

                $YPmax += $this->scatterOffset + $realHeight;
            }
            $arrayToReturn = array_reverse($TimePanels);

            for ($i = 1; $i <= $PN; $i++)
                    foreach ($PlotPanels[$i] as $panel)
                            $arrayToReturn[] =  $panel;

            return  $arrayToReturn;
        }

        /*
        *
        */
        protected function makeLayout($panels) 
        {
            $TotalHeight = 0.0;      
            $PWidth = 0.9;

            $ScatterN = 0;
            $TimeN = 0; 
            foreach ($panels as $panel){ 
                        if ($panel->plotType == 'SCATTER') {
                            $ScatterN++;
                            if (strtolower($this->obj->orientation) == 'portrait') 
                                            $panel->width *= 1.625; //0.65/0.4; coeff for portrait
                        }
                        else $TimeN++;

                        $TotalHeight += $panel->height;     
            }

            $addOffset = $TimeN > 0 && $panels[count($panels)-1]->plotType == 'SCATTER';
                
            $PHeight = 0.99 - $this->firstOffset - $ScatterN*$this->scatterOffset - $this->firstOffset*$addOffset;
            $heightCoeff = $TotalHeight > $PHeight ? $PHeight/$TotalHeight : 1.0;

            //Loop through all panels
            $first = true;
            $firstTime = true;
            $allPanels = array(); 

            foreach (array_reverse($panels) as $panel)
            {
                $isScatter = $panel->plotType == 'SCATTER';
                // width for scatter
                if ($isScatter) {                         
                    $width = $panel->width*$heightCoeff;                    
                    $panel->XPmin = ($PWidth - $width)/2.0;
                    $panel->XPmax = $panel->XPmin + $width;
                }
                else if ($panel->children[0]->paramArgs == "OrbitPresentation=CYL") {
                     $width = $panel->width*$heightCoeff;                    
                     $panel->XPmin = ($PWidth - $width)/2.0;
                     $panel->XPmax = $panel->XPmin + $width;
                }
                else {
                    $panel->XPmin = 0.0;
                    $panel->XPmax = $PWidth;
                }

                //Start from lower panel
                if ($first) 
                            $panel->YPmin = $this->firstOffset;
                else if ($isScatter) 
                            $panel->YPmin = $YPmax+$this->scatterOffset;
                else if ($addOffset &&  $firstTime && !$isScatter) {
                    $firstTime = false;
                    $panel->YPmin = $YPmax+$this->firstOffset;
                }
                else  
                    $panel->YPmin = $YPmax;

                $YPmax =  $panel->YPmin + $panel->height*$heightCoeff;
                $panel->YPmax = $YPmax;
                $first = false;
             }
            return array_reverse($panels);
        }

 protected function setPlotObjects($panels,$tmpname,$isMulti){

    require_once 'paramBuild.php';

    $this->request = fopen($tmpname,'w');
 
            if ($this->obj->forcedLayout) 
                    $panels = $this->makeForcedLayout($panels);  
            else 
                    $panels = $this->makeLayout($panels);
                     
    foreach ($panels as $panel){
                // if it is  SCATTER PLOT
                $isScatter = $panel->plotType == 'SCATTER';                
                $Xmin = $panel->xRangeMin;
                $Xmax = $panel->xRangeMax;
                $Ymin = $panel->y1RangeMin;
                $Ymax = $panel->y1RangeMax; 

                $Ytitle = $panel->y1Title;
                $Xtitle = $panel->xTitle;
   
                $objectsToDraw = $panel->children;
                    
                $multiOnePanel = false;
                // check if SPECIAL MULTI could be done
                if (count($objectsToDraw) > 1 && $this->obj->forcedMulti && !$isScatter) {
                        $multiOnePanel = true;
                        foreach ($objectsToDraw as $objectToDraw) {
                            // only scalars
                            if (!$objectToDraw->isScatter) {
                                            $multiOnePanel = false;
                            }
                        }
                    }

                if ($multiOnePanel) {
                        $aPanel = new stdClass(); 
                        $aPanel->XPmin = $panel->XPmin;
                        $aPanel->XPmax = $panel->XPmax;
                        $aPanel->YPmin = $panel->YPmin;
                        $aPanel->YPmax = $panel->YPmax;
                        $aPanel->Xmin = $Xmin;
                        $aPanel->Xmax = $Xmax;
                        $aPanel->Ymin = $Ymin;
                        $aPanel->Ymax = $Ymax; 

                        $aPanel->object = 'multi';
                        $aPanel->args = ''; 
                        foreach ($objectsToDraw as $objectToDraw) {
                            $aPanel->args .= '"'.$objectToDraw->name.'",';
                            $paramMulti =  explode('(',$objectToDraw->name);
                            $paramList[] = $paramMulti[0];
                        }
                        $aPanel->args = substr($aPanel->args, 0, -1);
                        $allPanels[] = $aPanel;  
                    }
                // loop though parameters : each parameter == panel if not MULTI
              else {
                  foreach ($objectsToDraw as $objectToDraw) {
                      $aPanel = new stdClass(); 
                      $aPanel->XPmin = $panel->XPmin;
                      $aPanel->XPmax = $panel->XPmax;
                      $aPanel->YPmin = $panel->YPmin;
                      $aPanel->YPmax = $panel->YPmax;
                      $aPanel->Xmin = $Xmin;
                      $aPanel->Xmax = $Xmax;
                      $aPanel->Ymin = $Ymin;
                      $aPanel->Ymax = $Ymax;

                      if ($isScatter) { 
                         $paramIDx = explode('(', $objectToDraw->name); 
                         $paramList[] = $paramIDx[0];
                         $paramIDy = explode('(',$panel->scatterParam->data->name);
 
                         $oneVI = $paramIDy[0] === $paramIDx[0] ? true : false;
 
                         if ($this->isRemoteParam($paramIDx[0])) {

                                if (!$infoMgr) $infoMgr = new ParamsInfoMgr();
                                $info = $infoMgr->getRemoteParamInfo($paramIDx[0]);
                                                                
                                paramBuild_($paramIDx[0]); 
                                $paramIDx[0] = param2dd($paramIDx[0]);
                                $objectToDraw->name = implode('(', $paramIDx);
                                                              
                                $YTitle_default = $info['title'] ? $info['title'].'('.$paramIDx[1] : $objectToDraw->name;

                                if ($oneVI) {   
                                        $paramIDy[0] = $paramIDx[0];
                                        $panel->scatterParam->data->name = implode('(', $paramIDy);
                                $XTitle_default = $info['title'] ? $info['title'].'('.$paramIDy[1] : $panel->scatterParam->data->name;
                                }
                            }
      
                            if (!$oneVI) {
 
                                if ($this->isRemoteParam($paramIDy[0])) {

                                    if (!$infoMgr) $infoMgr = new ParamsInfoMgr();
                                    $info = $infoMgr->getRemoteParamInfo($paramIDy[0]); 

                                    paramBuild_($paramIDy[0]); 
                                    $paramIDy[0] = param2dd($paramIDy[0]);
                                    $panel->scatterParam->data->name = implode('(', $paramIDy);
                                    $XTitle_default = $info['title'] ? $info['title'].'('.$paramIDy[1] : $panel->scatterParam->data->name;             
                                }        
                                        $paramList[] = $paramIDy[0];
                            }
                            $aPanel->object = 'scatter';
                            $aPanel->args = '"'.$objectToDraw->name.'",'.'"'.$panel->scatterParam->data->name.'"';
                            $aPanel->args .= $this->parseScatterArguments($objectToDraw->paramArgs);   
                                                 
                         if ($Xtitle || $XTitle_default) {
                            $Xtitle_ = $Xtitle ? $Xtitle : $XTitle_default; 
                            $aPanel->args .= ', XTITLE="'.$Xtitle_.'"';
                         }
                          
                        if ($Ytitle || $YTitle_default) {
                            $Ytitle_ = $Ytitle ? $Ytitle : $YTitle_default; 
                            $aPanel->args .= ', Y1TITLE="'.$Ytitle_.'"';
                        }
                     }
                     else {
                        // to make proper AMDA objects
                        $comp = -1;
                        $addArgs = $this->parseSimuArguments($objectToDraw->paramArgs);
                        $this->argumentsByParam[$objectToDraw->name] = (array)$addArgs;    
                        if (strpos($objectToDraw->name, '(') !== false) {
                            //first check params without codes 
                            $paramID = explode('(', $objectToDraw->name);
                            $comp =  substr($paramID[1], 0, -1);
                            //TODO double ??? of MakeLocalParam A CORRIGER
                            $paramWithoutCode = $this->isParamWithoutCode($paramID[0]);      
                            $currPairs = array("(" => "_", ")" => "",":" => "_");
                            $objectToDraw->name = strtr($objectToDraw->name, $currPairs);  
                        }    
                        else {
                            $paramWithoutCode = $this->isParamWithoutCode($objectToDraw->name);
                        }
                        if ($this->isRemoteParam($objectToDraw->name)) { 

                                if ($comp != -1) $this->makePlotRemoteParams($paramID[0], $tmpname, $comp);
                                else  $this->makePlotRemoteParams($objectToDraw->name, $tmpname, $comp);
                                                                                
                            $currPairs = array(":" => "_", '-' => "_");
                            $objectToDraw->name = strtr($objectToDraw->name, $currPairs); 
                        }
                        //================================================================
                        // TODO THEMIS_IRAP init is created from special templates!!!
                        $objToTest = str_replace("_", " ", $objectToDraw->name);
                        if (sscanf($objToTest, "th%s pe%s %s", $X, $mode, $property) == 3){       
                            $themis = "th".$X;
                            $mode = "pe".$mode;
                            $SED = "sed 's/X/".$X."/g' ".TEMPLATES."thX_YYYY_".$property."_init.pro | sed 's/YYYY/".$mode."/g' > ./".$themis."_".$mode."_".$property."_init.pro";
                            system($SED);
                            if ($property == 'v' || $property == 't' || $property == 'mode' ) {
                                for ($j = 0; $j < 3; $j++) {
                                    $SED = "sed 's/X/".$X."/g' ".TEMPLATES."thX_YYYY_".$property."_Z_init.pro | sed 's/YYYY/".$mode."/g' | sed 's/Z/".$j."/g' > ./".$themis."_".$mode."_".$property."_".$j."_init.pro";
                                    system($SED);  
                                }
                            }                       
                           }
                    
                        if (sscanf($objToTest, "th%s bh", $X) === 1){                                                                                                                                        
                            $themis = "th".$X;                    
                            $SED = "sed 's/X/".$X."/g'  ".TEMPLATES."thX_bh_init.pro  > ./".$themis."_bh_init.pro"; 
                            system($SED);   
                            for ($j = 0; $j < 3; $j++) {
                                    $SED = "sed 's/X/".$X."/g' ".TEMPLATES."thX_bh_Z_init.pro | sed 's/Z/".$j."/g' > ./".$themis."_bh_".$j."_init.pro";
                                    system($SED);               
                                } 
                                    
                        }       
//==================================================

                        $aPanel->object = $objectToDraw->name;
                        if ($objectToDraw->paramArgs != $this->defaults) {
                                    $aPanel->args = $this->parseArguments($objectToDraw->paramArgs);
                        }
                        // new Y Title
                        if ($Ytitle && ($paramWithoutCode || $this->isRemoteParam($objectToDraw->name) || (strncmp(strtolower($objectToDraw->name), 'ws_', 3) == 0))) {
                            if ($objectToDraw->paramArgs != $this->defaults) $aPanel->args .= ',';
                            $aPanel->args .=  'AY1TITLE="'.$Ytitle.'"';
                        }
                        // linear Z scaling for ros_mip
                        if (substr($objectToDraw->name,0,7) == 'ros_mip') {
                                if ($objectToDraw->paramArgs != $this->defaults) $aPanel->args .= ',';
                                $aPanel->args .= '/LLIN';
                        }
                        // linear Y scaling for ros_ies AZ, ELV
                        if (strpos($objectToDraw->name,'ros') === 0 && 
                            (strpos($objectToDraw->name,'elv') > 0 || strpos($objectToDraw->name,'az') > 0)) {
                                if ($objectToDraw->paramArgs != $this->defaults) $aPanel->args .= ',';
                                $aPanel->args .= '/LIN';
                        }
                        $paramList[] = $aPanel->object; 
 
                    } 
                    $allPanels[] = $aPanel;
                   }
                } //else
            } //foreach ($panels as $panel)

            //Now write everything into request file
            fwrite($this->request,count($allPanels).PHP_EOL);
        
            foreach ($allPanels as $aPanel) { 
                fwrite($this->request, 
                        $aPanel->object.' '.$aPanel->XPmin.' '.$aPanel->YPmin.' '.$aPanel->XPmax.' '.$aPanel->YPmax.' '.
                        $aPanel->Xmin.' '.$aPanel->Xmax.' '.$aPanel->Ymin.' '.$aPanel->Ymax.' '.$aPanel->args.PHP_EOL); 
            }

            $this->nObjects = count($allPanels);

            // Finish request file by StartTime and TimeInterval if ONE time only
            if (!$isMulti) {
                $StartTime = $this->getDdTime($this->obj->startDate);
                $TimeInt =  $this->getDdInterval();
                fwrite($this->request,$StartTime.PHP_EOL);
                fwrite($this->request,$TimeInt.PHP_EOL); 
            } 
            fclose($this->request); 
            
            return $paramList;
        }

        /*
        *  Arguments parser
        */ 
        protected function getColor($value) {

            switch ($value) {
               case  'black' :  return 'COLOR = !dnc-1'; 
               case  'red'   :  return 'COLOR = !dnc-2';
               case  'orange'   :  return 'COLOR = !dnc*190/256';
               case  'yellow'   :  return 'COLOR = !dnc*160/256';
               case  'green' :  return 'COLOR = !dnc*80/256';   
               case  'light-green' :  return 'COLOR = !dnc*100/256';
               case  'light-blue'  :  return 'COLOR = !dnc*40/256'; 
               case  'blue'  :  return 'COLOR = !dnc*15/256'; 
               default:         return 'COLOR = !dnc-1';
           }
        }

        /*
        *   Arguments parser
        */ 
  protected function parseArguments($args)
        {
            $argArr = explode('&',$args);
            foreach ($argArr as $key => &$arg) {
                $temp = explode('=', $arg);
                //translate some arguments
                if ($temp[1] == 'no') unset($argArr[$key]);
                if ($temp[1] == 'yes') $arg = '/'.$temp[0];
                if ($temp[0] == 'Scale') $arg = '/'.$temp[1];
                if ($temp[0] == 'RefFrame') $arg = 'FRAME="'.$temp[1].'"';
                if ($temp[0] == 'OrbitPresentation') $arg = '/'.$temp[1];

                if ($temp[0] == 'Spacecraft') {
                                    $arg = '/'.$temp[1];
                                    $this->makeOrbitParam($temp[1]);
                }

                if ($temp[0] == 'SW_Monitor') {
                                    $arg = '/'.$temp[1];
                                    $this->makeSWMonitorParam($temp[1]);
                }

                if ($temp[0] == 'Color') {
                            $arg = $this->getColor($temp[1]);                                    
                }
                if ($temp[0] == 'Channel' && ( $temp[1] == 'spectrogram' || $temp[1] == 'stack_plot')) unset($argArr[$key]); 
                if ( ($temp[0] == 'anode' || $temp[0] == 'channel') && $temp[1] == 'all') unset($argArr[$key]);

                if ($temp[0] == 'Symbol') {
                                if ($temp[1] == '0') $arg = 'PSYM = -3';
                                else $arg = 'PSYM = '.$temp[1];
                } 
// if ($temp[0] == 'energyCB') $arg = '/enaverage';
        //       if ($temp[0] == 'anodeCB') $arg = '/anaverage';
    }

            $argArrModif = array_values($argArr);
            return implode(',',$argArrModif);
}

        /*
        *  Scatter Arguments parser
        */ 
        protected function parseScatterArguments($args)
        {
            $argArr = explode('&',$args);
    
            foreach ($argArr as $key => &$arg) {
                
                $temp = explode('=', $arg);
                if ($temp[0] != 'Color' && $temp[0] != 'Symbol') unset($argArr[$key]);

                if ($temp[0] == 'Color') {
                                            $arg = $this->getColor($temp[1]);   
                }
                if ($temp[0] == 'Symbol') {
                                            if ($temp[1] == '0') $arg = 'PSYM = -3';
                    else $arg = 'PSYM = '.$temp[1];
                }    
            }        
            $argArrModif = array_values($argArr);
            if (count($argArrModif) > 0) return ','.implode(',',$argArrModif); 
                    else return ''; 
        }

        /*
        *  Simu Arguments parser
        */ 
        protected function parseSimuArguments($args)
        {
            $argArr = explode('&',$args);
            
            $arguments = array();
            foreach ($argArr as $key => &$arg) {
                
                $temp = explode('=', $arg);
                
                $arguments[$temp[0]] = $temp[1];
            }        
            return $arguments; 
        }


    /*
    *  put down list of time tables (tt) and current table (current)
    */   
        protected function prepareTimeTablePlot($dir) 
        {
            //set time from first time table first interval
            $ttMgr = new TimeTableMgr();      
            $timeTables = $this->obj->timeTables;
        //    $firstTable = $timeTables[0];
        //    $tt = $ttMgr->loadIntervalsFromTT($firstTable->id);
        //    $intervals = $tt['intervals']; 
            $offset = 0;  
            $file = fopen($dir.'/tt', 'w');
            foreach ($timeTables as $timeTable) {
                $ttType = (substr($timeTable->id,0,6) == 'shared') ?  'sharedtimeTable' : 'timeTable'; 
                $tt = $ttMgr->loadIntervalsFromTT($timeTable->id, $ttType);
                $intervals = $tt['intervals'];
                $totalCount = $tt['totalCount'];
                fwrite($file,json_encode($timeTable).PHP_EOL); 
                if ($offset == 0) {                        
                    $this->totalInt = $totalCount;
                    $this->ttName = $timeTable->timeTableName;
                    $this->currentInt = 1;
                    $ff = fopen($dir.'/current', 'w');
                    fwrite($ff, $this->totalInt.PHP_EOL);   
                    fwrite($ff, json_encode($timeTable));     
                    fclose($ff);
                    $firstInterval = $intervals[0];
                }
                $offset += $totalCount;
            }
            fclose($file);
            //save current tt object (first one)  and current time interval (first one)            
            return $firstInterval;
        }

        
        /*
        *  put down list of time tables (tt) and current table (current)
        */   
        protected function getFirstTimeTableInterval() 
        {
            //set time from first time table first interval
            $ttMgr = new TimeTableMgr();      
            $timeTables = $this->obj->timeTables;
            
            $firstTable = $timeTables[0];
            //    $tt = $ttMgr->loadIntervalsFromTT($firstTable->id);
            $ttType = (substr($firstTable->id,0,6) == 'shared') ?  'sharedtimeTable' : 'timeTable'; 
            $tt = $ttMgr->loadIntervalsFromTT($firstTable->id, $ttType); 
            $intervals = $tt['intervals']; 
            //save current tt object (first one)  and current time interval (first one)
            $firstInterval = $intervals[0];
            return $firstInterval;
        }
/******************************************************************************
 *  END:  Plot specific functions
*******************************************************************************/

        /*
        * Change NAME in JSON resource
        */
        protected function renameInResource($name, $id) {

                    $obj = json_decode(file_get_contents(USERREQDIR.$id));
                    $obj->name = $name;

                    $file = fopen(USERREQDIR.$id, 'w');
                    fwrite($file, json_encode($obj));
                    fclose($file);                
            }

 
        /*
        *    Make new request/condition resource file (JSON!!) and add it to content file
        *    ATTENTION : it is not DD parameter!!!
        */ 
protected function createParameter($p)
        {
            if ($this -> objectExistsByName($p->name)) {
                        $p->id  = $this->getObjectIdByName($p->name);
                        $this -> deleteObject($p);
                    }
                    $this->id = $this->setId(); 
                    if (!$this->id) return array('error' => ID_CREATION_ERROR);

            //if alias exists, replace alias name by parameter name        
            if (file_exists(USERWSDIR.'Alias.xml')) {
                            if ($this->type == 'condition') {
                                    $p->expression =  $this->resetAlias($p->expression); 
                                    $info = $p->expression;
                            }
                            else if ($this->type == 'request') {
                                    $info = '';
                                    for ($i=0; $i < count($p->children); $i++) {
                                            for ($j=0; $j < count($p->children[$i]->children); $j++) {
                                                    $p->children[$i]->children[$j]->name =  $this->resetAlias($p->children[$i]->children[$j]->name);
                                                    $info = $info.' '.$p->children[$i]->children[$j]->name;
                                            }
                                    }
                            }    
                }
    
                $this->descFileName = USERREQDIR.$this->id;
                $p->id = $this->id;
                // save request as json  
                $file = fopen($this->descFileName, 'w');
                fwrite($file, json_encode($p));
                fclose($file);

                $this -> addToContent($p, $folder);

            return array('id' => $this->id, 'info' => $info);
}
        
        /*
        *        Delete request/condition JSON file
        */
        protected function deleteParameter($id){

                if (file_exists(USERREQDIR.$id)) 
                                unlink(USERREQDIR.$id);
        }

        /*
        * TODO       Check file JSON objects differ in names only
        */
protected function renameOnly($p) {

return false;
        }

        /*
        * Add header in download result
        */  
public function addHeaderInDownloadResult($opts)
{
    //remove previous header if it exist
    if (file_exists('header.txt'))
    unlink('header.txt');
   
//use ParamsInfoMgr to get infor about parameters
$infoMgr = new ParamsInfoMgr();
   
$params = explode(' ',$opts['params']);

if (strcmp($opts['header'],'0') === 0)
{
                            //header in data files
  if (strcmp($opts['structure'],'2') === 0)  
  {
    //one file by param
    foreach ($params as $param)
   {
    $filenames = glob(strtoupper($param)."*.txt");
    if (count($filenames) === 0) 
                            continue;
     foreach ($filenames as $filename) {
        //parameter info
        $infos = $infoMgr->GetParamInfo($param);
        $str = $infoMgr->ParamInfosToString($infos);
        system("sed -i '1i\\#".$str."' '".$filename."'");
        //time info
        $str = "Time Format : ".$opts['timeformat'];
        
        if (strcmp($opts['milli'],'1') === 0)
        {
        if (strcmp($opts['timeformat'],'YYYY-MM-DDThh:mm:ss') === 0)
            $str .= ".mls";
        else
            $str .= " mls";  
        }
        
        system("sed -i '1i\\#".$str."' '".$filename."'");
                                       }
   }
  }
  else
  {
    //all in one file
    $str = "";
   
    foreach ($params as $param)
        {
        $infos = $infoMgr->GetParamInfo($param);
                                    $str .= ('#'.$infoMgr->ParamInfosToString($infos).PHP_EOL);
        }
                            
    $filenames = glob("ALL*.txt");
    if (count($filenames) > 0) 
     foreach ($filenames as $filename)
    {
      $ex_str = explode(PHP_EOL,$str);
       
      //parameters info
      for ($i = sizeof($ex_str)-1; $i >= 0 ; $i--)
       system("sed -i '1i\\".$ex_str[$i]."' '".$filename."'");
     //time info
     $str = "Time Format : ".$opts['timeformat'];
     
     if (strcmp($opts['milli'],'1') === 0)
    {
      if (strcmp($opts['timeformat'],'YYYY-MM-DDThh:mm:ss') === 0)
        $str .= ".mls";
      else
        $str .= " mls";  
    }
     
     system("sed -i '1i\\#".$str."' '".$filename."'");
                                   if (strpos($filename, "NONSTANDARD_SAMPLING") === false) {
                                    //sampling info
                                    $str = "Sampling Time : ".$opts['sampling'];
                                    system("sed -i '1i\\#".$str."' '".$filename."'");
                                    }
   }
  }
}
else
{
 //header in separate file
 $header = fopen("header.txt","w");
 if ($opts['structure'] != '2')
 {
 
  //all in one file- add sampling info
   fprintf($header,"#Sampling Time : ".$opts['sampling'].PHP_EOL);
 }
 //time info
  $str = "#Time Format : ".$opts['timeformat'];
  
  if (strcmp($opts['milli'],'1') === 0)
  {
    if (strcmp($opts['timeformat'],'YYYY-MM-DDThh:mm:ss') === 0)
     $str .= ".mls";
   else
                                $str .= " mls";  
                        }
  
                        //fprintf($header,$str.PHP_EOL);
                        foreach ($params as $param)
                        {
                            //param info
                            $infos = $infoMgr->GetParamInfo($param);
                            $str = $infoMgr->ParamInfosToString($infos);
                            fprintf($header,"#".$str.PHP_EOL);
                        }
                    }
        }
  

        public function compressDownloadResult($id,$newName,$opts)
        {
                if ($opts['downloadSrc'] == '2')
                    $ext = "fits";
                else
                {
                    if (strcmp($opts['fileformat'],'vot') === 0)
                        $ext = "xml";
                    else
                        $ext = "txt";
                }
                    
                if (strncmp($opts['compression'],'zip',3) === 0)
                {
                    system("zip -Dj ".$id." *.".$ext." 1> /dev/null 2> /dev/null");
                    if (strcmp($newname,$id) != 0)
                        rename($id.'.zip',$newName.'.zip');
                }
                else 
                {
                    exec("tar -czf ".$id.".tar.gz  *.".$ext);
                 //   exec("gzip ".$id.".tar");

                    if (strcmp($newname,$id) != 0)
                        rename($id.'.tar.gz', $newName.'.tar.gz');
                }
                                
                foreach (glob("*.".$ext) as $file) 
                    unlink($file);
        }

 
        public function generateVOTableFromDownloadResult($id,$newName,$inputCompressed = true,$canBeAlreadyVOTable = false)
        {
                $resultDir = USERWORKINGDIR.$id.'_/';
                $opts = $this->getPrintOptions($resultDir);
                
                $votMgr = new BuildVOTable();
                        return $votMgr->downloadResultToVOTable($id,$newName,$opts,$inputCompressed,$canBeAlreadyVOTable);
        }
 
        /*
         *      Renaming and zipping of PDF/PS
        */
        public function postProcessing($id, $newName) {

                switch ($this->type) {
                    case 'download' :
                            $resultDir = USERWORKINGDIR.$id.'_/';
                            
                            $opts = $this->getPrintOptions($resultDir);
                            
                            if ((strcmp($opts['downloadSrc'],'2') !== 0) && (strcmp($opts['fileformat'],'vot') !== 0))
                            {
                                //add header
                                $this->addHeaderInDownloadResult($opts);
                            }
                            
                            //generate votable if necessary
                            if ((strcmp($opts['fileformat'],'vot') === 0))
                                $this->generateVOTableFromDownloadResult($id,$newName,false,false);
                                
                            //compression
                            $this->compressDownloadResult($id,$newName,$opts);
                                
                    break;
                case 'condition' :   
                                break;
                case 'request' :
                            $resultDir = USERWORKINGDIR.$id.'_/';
                            switch ($this->obj->format) {                            
                                case 'PS' :
                                    if (file_exists($resultDir.'idl.ps')) {
                                        rename($resultDir.'idl.ps',$resultDir.$newName.'.ps');
                                        exec('gzip '.$resultDir.$newName.'.ps'); 
                                    }
                                break;
                                case 'PDF' : 
                                    if (file_exists($resultDir.'idl.ps'))  
                                                exec('ps2pdf -sPAPERSIZE=a4 '.$resultDir.'idl.ps');
                                    unlink($resultDir.'idl.ps');
                                    rename($resultDir.'idl.pdf',$resultDir.$newName.'.pdf');
                                break;
                                default : 
                            } 
                        break;

                default :
                }
            }

        /*
        * Get real IDL plot settings : !x.window & !y.window for each panel
        */
        protected function getPlotSettings(){

            if (!file_exists('plotSettings')) return null;
            $settings = file('plotSettings');
            $plotSetting = new stdClass();
                    
            for ($i = 0; $i < count($settings)/2; $i++) {
                $xArr = explode(' ',trim($settings[$i*2]));
                $yArr = explode(' ',trim($settings[$i*2+1]));
                $plotSetting->xmin =  $xArr[0];
                $plotSetting->xmax =  $xArr[count($xArr)-1];
                $plotSetting->ymin =  $yArr[0];
                $plotSetting->ymax =  $yArr[count($yArr)-1];
                $plotSettings[$i] = $plotSetting;
            }
            return $plotSettings;
        }

/******************************************************************************
*  Start:  Print specific functions
******************************************************************************/ 

        protected function makePrintRequest($dir) 
        {
            $ff = fopen($dir.'print.res','w');
            foreach ($this->obj->list as $param) {
                if ((strncmp($param, "impex___", 8) === 0) || (strncmp($param, "spase___", 8) === 0)){
                    $argumentsByParam = $this->cutSimuArgs($param);
                            foreach ($argumentsByParam as $key => $value) $param = $key;
                }
                fwrite($ff, $this->param2dd_print($param).' ');
            }
            if ($this->obj->structure == '2') {
                fwrite($ff, PHP_EOL.'multi -1'.PHP_EOL);
            }
            else {
                fwrite($ff, PHP_EOL.'uniq '.$this->obj->sampling.PHP_EOL);
            }
            fwrite($ff,'none'.PHP_EOL);
            $timeFormatNumber = $this->obj->milli ? 
                                $this->timeFormat[$this->obj->timeformat] + 4 : $this->timeFormat[$this->obj->timeformat];
            fwrite($ff, $timeFormatNumber.PHP_EOL);
            fclose($ff);
        }
      
        protected function createPrintRequestTime($dir) 
        {
            $ff = fopen($dir.'print.list','w');
            fwrite($ff,$this->getDdTime($this->obj->startDate).PHP_EOL);
            fwrite($ff,$this->getDdInterval().PHP_EOL);

            fclose($ff);
        }

        protected function createPrintPostOptions($dir) 
        {
            $ff = fopen($dir.'print.opt','w');
            fwrite($ff,$this->obj->compression.PHP_EOL);
            fwrite($ff,$this->obj->fileformat.PHP_EOL);
            fwrite($ff,$this->obj->header.PHP_EOL);
            fwrite($ff,$this->obj->structure.PHP_EOL);
            foreach ($this->obj->list as $param)
            {
 
              if ((strncmp($param, "impex___", 8) === 0) || (strncmp($param, "spase___", 8) === 0)){
		$argumentsByParam = $this->cutSimuArgs($param);
		foreach ($argumentsByParam as $key => $value) $param = $key;
	    }
 
                    if ($this->obj->downloadSrc == '2')
                        fwrite($ff, $param->url.' ');
                    else
                        fwrite($ff, $param.' '); //data
            }
            fwrite($ff,PHP_EOL.$this->obj->sampling.PHP_EOL);
            fwrite($ff,$this->obj->timeformat.PHP_EOL);
            if ($this->obj->milli)
                fwrite($ff,'1'.PHP_EOL);
            else
                fwrite($ff,'0'.PHP_EOL);
            fwrite($ff,$this->obj->downloadSrc.PHP_EOL);
            fclose($ff);
        }

        protected function getDateArray()
        {
            if ($this->obj->startDate){
                $matches=array();
                $tmpArr = explode("T", $this->obj->startDate);
                $dateArr = explode("-", $tmpArr[0]);
                $timeArr = explode(":", $tmpArr[1]);
                preg_match("/([0-9]+)/", $timeArr[2], $matches);

                return array("success" => true, 
                            "year"  => ($dateArr[0]),
                            "month" => ($dateArr[1]),
                            "day"   => ($dateArr[2]),
                            "hour"  => ($timeArr[0]),
                            "min"   => ($timeArr[1]),
                            "sec"   => ($matches[1])
                );
            }
            else
                return array('success' => false);    
        }

        protected function getStopDate()
        {
            $date = $this->getDateArray ();
            return gmdate("Y-m-d\TH:i:s\Z", mktime($date['hour'] + intval($this->obj->durationHour), $date['min']+ intval($this->obj->durationMin), $date['sec']+ intval($this->obj->durationSec), $date['month'], $date['day']+ intval($this->obj->durationDay), $date['year'])); 
        }


/******************************************************************************
 *  End:  Print specific functions
*******************************************************************************/  

/*****************************************************************
*                           PUBLIC FUNCTIONS
*****************************************************************/

        public function getPrintOptions($dir) 
        {
                $postOptions = file($dir.'print.opt');
                return array('compression'  => trim($postOptions[0]),
                            'fileformat'   => trim($postOptions[1]),
                            'header'       => trim($postOptions[2]),
                            'structure'    => trim($postOptions[3]),
                            'params'       => trim($postOptions[4]),
                            'sampling'     => trim($postOptions[5]),
                            'timeformat'   => trim($postOptions[6]),
                            'milli'        => trim($postOptions[7]),
                            'downloadSrc' => trim($postOptions[8]));
        }

        public function updatePrintRequestTime($dir, $interval) 
        {
                $ff = fopen($dir.'print.list','w');
                fwrite($ff,$this->getDdTime($interval['start']).PHP_EOL);
                fwrite($ff,$this->calculateTimeInt($interval).PHP_EOL);

                fclose($ff);
        }
        
        public function updatePrintSampling($dir, $sampling) 
        {
                $ff = file($dir.'print.res');
                $newSampling = explode(' ',$ff[1]);
                $ff[1] = $newSampling[0].' '.$sampling.PHP_EOL;

                file_put_contents($dir.'print.res',$ff);
        }

        public function setZoomStart($interval)
        {
            if (!$interval) {
                $time = strtotime($this->obj->startDate);
                $dt = $this->obj->durationDay*86400 +
                                    $this->obj->durationHour*3600 +
                                    $this->obj->durationMin*60 + $this->obj->durationSec;
        
                $stopTime = gmdate("Y-m-d\TH:i:s",$time+$dt); 
                $interval['start'] = $this->obj->startDate;
                $interval['stop'] = $stopTime;
            }
            $ff = fopen('zoom_intervals','w');
            fwrite($ff, json_encode($interval).PHP_EOL);
            fclose($ff);  
        }

        /*
        * Launch process in bkgr; return PID
        */
        public function background($Command)
        {
            $PID = exec("$Command 1> /dev/null 2> /dev/null & echo $!");
            if ($PID != null)  return $PID;

            return false;
        }

        /* Stop Time from StartTime and Interval*/ 
        public function convertTime($obj)
        {

                $time = strtotime($obj->startDate);

                $interval = $obj->durationDay*86400 +
                            $obj->durationHour*3600 +
                            $obj->durationMin*60 + $obj->durationSec;
                $stopTime = gmdate("Y-m-d\TH:i:s", $time+$interval);
                $obj->stopDate = $stopTime;

                return $obj;
        }

        /*
        * Update Time in request 
        */
        public function updateTime($requestName, $interval) 
        {
            $requestFile = file($requestName); 
            $request = fopen($requestName,'w');

            $nObjects = 1 + $requestFile[0]; 

            for ($k = 0; $k < $nObjects; $k++) fwrite($request,$requestFile[$k]);
            
            fwrite($request,$this->getDdTime($interval['start']).PHP_EOL);
            fwrite($request,$this->calculateTimeInt($interval).PHP_EOL);

            fclose($request);          
        }

        //From Start Time and Stop Time 
        public function createSearchRequestTime2($interval) 
        {
                $this->request = fopen(USERWORKINGDIR.'search.list','w');

                fwrite($this->request,$this->getDdTime($interval['start']).PHP_EOL);
                fwrite($this->request,$this->calculateTimeInt($interval).PHP_EOL);

                fclose($this->request);
        }

        //TODO 
        public function markAsUndefined($paramId)
        {
            $n_requests = 0;

            return $n_requests;
        }

        /*
        *   Get Object JSON!!! (request or condition) into Edit
        */ 
        public function getObject($id) 
        {
                    if (!file_exists(USERREQDIR.$id)) return array('error' => NO_OBJECT_FILE);
                
                    if (!($objToGet = $this->contentDom->getElementById($id))) return array('error' => NO_SUCH_ID);
                    $obj = json_decode(file_get_contents(USERREQDIR.$id));
                    //if alias exists, replace parameter name by alias name        
                    if (file_exists(USERWSDIR.'Alias.xml')) {
                        if ($this->type == 'condition') {
                                $obj->expression =  $this->setAlias($obj->expression); 
                        }
                        else if ($this->type == 'request') {
                                for ($i=0; $i < count($obj->children); $i++) {
                                        for ($j=0; $j < count($obj->children[$i]->children); $j++) {
                                                $obj->children[$i]->children[$j]->name =  $this->setAlias($obj->children[$i]->children[$j]->name);
                                        }
                                }
                                //TODO Ajout des SCATTER
                                // if $obj->children[$i]->plotType == "SCATTER" 
                                //$obj->children[$i]->scatterParam->data->name pour 1 panel (bug si 2 panels devient $obj->children[$i]->scatterParam->data->data->name)
                            }    
                    }      
                    //if Start Time - Stop Time
                    if (!$obj->timeTables) $obj =  $this->convertTime($obj);    
                return  $obj;
        }

        public function cachekiller()
        {
                $date = getdate();
                $cachekiller = $date['mday'].$date['hours'].$date['minutes'].$date['seconds'];

            return $cachekiller;
        }

        public function killPlot($tabId)
        {
                $tmpname =  'Plot'.$tabId;
                
                $resDirPng = USERWORKINGDIR.$tmpname.'_';
                
                if (!is_dir($resDirPng))
                    return array('success' => false, 'message' => 'Cannot find workind directory');
                    
                touch($resDirPng."/cancelplot");
        
            return array('success' => true);
        }
      
        public function killMultiPlot($tabIds)
        {              
            if (!is_dir(USERWORKINGDIR."CANCELPLOT")) 
                                mkdir(USERWORKINGDIR."CANCELPLOT");

            foreach ($tabIds as $tabId)    
                    touch(USERWORKINGDIR."CANCELPLOT/".$tabId);
        
            return array('success' => true);
        }


        public function initResDir($tmpname)
        {
                $resDir = USERWORKINGDIR.$tmpname.'_/';

                if (!is_dir($resDir)) {
                        if (!mkdir($resDir, 0775)) 
                            return array('success' => false, 'error' => 'can\'t create directory'); 
                    }

                if (!chdir($resDir)){
                        return array('success' => false, 'error' => 'can\'t change directory');
                    }

            return array('success' => true, 'resdir' => $resDir);
        }

        /*
        *  Try to estimate File Size from param size, time int and sampling
        */
        public function estimateOutputFileSize($obj, $ttInterval)
        {
            $timeConvert = array("s" => 1, "m" => 60, "h" => 3600);

            if (!$obj) $obj = $this->obj;

            $interval = $ttInterval ? $ttInterval : $obj->durationDay*86400 +
                                                    $obj->durationHour*3600 +
                                                    $obj->durationMin*60 + $obj->durationSec;

            $infoMgr = new ParamsInfoMgr();

            $multi = $obj->structure == 2;

            $timeLength = $obj->milli ? 25 : 20;

            $fileSize = $multi ? 0 : $timeLength;
            $sampling_virtual = 60.0;
            $size_virtual = 1;

            foreach ($obj->list as $param) {

                    $info = $infoMgr->GetParamInfo($param);

                    if (!$info['success']) {
                        $size = $size_virtual;
                        $sampling = $sampling_virtual;
                    } 
                    else {
                        // derived parameter
                        if (strncmp(strtolower($param), 'ws_', 3) == 0) { 
                            $size = 1;                                  
                            if ($multi) {                                                                                
                                    $sampling = $info['infos']['timestep']; 
                                }                 
                        }  
                        // my data parameter
                        else if (strncmp(strtolower($param), 'wsd_', 4) == 0) {                                 
                            $size = $info['infos']['size'];
                            $sampling = $info['infos']['minsampling'];                          
                        }
                        else {                                                                                     
                            $codeinfo = $info['codeinfos'];
                            $ddinfo = $info['ddinfos'];
                            if ($codeinfo) {
                                $size = $codeinfo['size'];
                                $sampling = $codeinfo['minsampling']; 
                            }
                            else {
                                $size = $ddinfo['parameter']['size'];
                                $sampling = substr($ddinfo['dataset']['minsampling'], 0, -1);  
                                $sampling *= $timeConvert[substr($ddinfo['dataset']['minsampling'], -1)];
                            }
                            // if component
                            if (sscanf($param,"%[^'('](%d)",$par,$cstart) == 2) $size = 1;
                        }
                    }

                    if ($multi) {
                        $fileSize += $interval / $sampling * ($size * 13 + $timeLength); 
                    }
                    else {
                        $fileSize += $size * 13;
                    }                                   
            }

            if (!$multi) {
                $fileSize *= $interval / $obj->sampling;
            }
        
            if ($obj->fileformat === 'vot')
                $fileSize *= 1.8;

            return  $fileSize;
        }
      
        /*
        *   THE MAIN EXECUTE PART
        */
        public function execute($obj) 
        {
            $this -> setObject($obj);
        
            //TODO define user in AmdaAction constructor : unique SESSION_ID   
            if (isset($obj->username) && isset($obj->password) && isset($obj->sessionID))
            {
                $dd = new WSUserMgr();
                $dd->init($obj->username,$obj->password,$obj->sessionID);
            }
            else
                $dd = new UserMgr();

            //TODO error handling 
            if (($res = $dd -> ddCheckUser()) != 0) {
                if ($res == 156) 
                    return array('error' => 'Your AMDA session is expired.<br/> Login please!');

                return array('error' => 'ddLogin error: '.$res);
                }

            if ($dd->user == 'impex') {
                if ($dd->getWsSize() > DISK_QUOTA * 100)
                    error_log('Natacha! It is time to CLEAN UP IMPEX WS!',1,'nbourrel@irap.omp.eu');
            }
            else {
                // check disk space
                if ($dd->getWsSize() > DISK_QUOTA)
                    return array('error' => 'Please clean up your workspace.<br/>No more space is available');
            }

            $isMulti =  $this->obj->timeTables ? true: false; 

            //  AMDA Statistics
            $amdaStat = new AmdaStats($dd->user);
        
            chdir(USERWORKINGDIR);
                    
            // temp name for output    
            $cachekiller = $this->cachekiller();
            $text = 'result';
            if ($this->obj->name) $text = $this->obj->name;
            if ($this->obj->output) $text = $this->obj->output;
            if ($this->obj->outputName) $text = $this->obj->outputName;
                
            $newName = $text.'_'.$cachekiller;

            // temp name for output
            $tmpname = $this->getRandomName();
            $isPNG = false;

            if ($this->obj->format && $this->obj->format == 'PNG') 
            {
                        $isPNG = true;
                        $tmpname =  'Plot'.$this->obj->tabId;
            }
            
            //TODO organize better : functions, classes? 
            switch ($this->type) 
            {
                    // DD Search
                    case 'condition':
                        $tmpname =  $newName;
                        //if alias exists, replace alias name by parameter name
                        if (file_exists(USERWSDIR.'Alias.xml')) 
                        {
                            $obj->expression = $this->resetAlias($obj->expression);
                        }
                        $realVars = $this -> makeSearchParams(); 
                    
                        if ($amdaStat->success) 
                            $amdaStat->addTask('mining', $dd->user, $realVars);

                        $this -> createSearchRequest($realVars, $tmpname);
                        
                        if ($this->paramsWithoutCode) 
                            $this->makeLocalParams($realVars);
                
                        if ($isMulti) {      
                            $cmd = 'php '.CLASSPATH.'../MultiRequestMgr.php \''.json_encode($this->obj).'\' \''.$tmpname.'\' \''.$dd->user.'\' \''.$dd->IP.'\''; 
                        }
                        else {
                            $this -> createSearchRequestTime();            
                            $cmd = DDBIN."DD_Search ".$dd->user." ".$dd->IP." ".DDPROJECT." ".DDPROJLIB;
                        }
                        break;
                    // DD Print
                    case 'download':

                        if ($this->obj->downloadSrc == 0) {
                            if (file_exists(USERWSDIR.'Alias.xml')) {
                                    foreach($this->obj->list as &$param)
                                        $param = $this->resetAlias($param);
                            } 
                            $isSimu = false;
                            foreach ($this->impex_prefix as $impex_prefix) if (strpos($param, $impex_prefix) !== false) $isSimu = true;
                            
                            if ((!$isMulti) || (!$isSimu)) {
                                $outputFileSize = $this->estimateOutputFileSize();
                                if ( $outputFileSize > DISK_QUOTA ) { //DISK_QUOTA
                                    $outputFileSize /= 1024*1024;
                                    return array('error' => "Sorry! Requested file is too large: ".$outputFileSize."MB 
                                                        <br/>Decrease requested time interval" );
                                }
                            }
                        }

                        $res = $this->initResDir($tmpname);
                        if (!$res['success'])
                                return $res;

                        $resDir = $res['resdir'];
                    
                        switch ($this->obj->downloadSrc)
                        {
                            case '2' : //fits images

                                $this->createPrintPostOptions($resDir);
                                $cmd = '';
                                foreach ($this->obj->list as $file) {
                                    if ($cmd != '')
                                        $cmd .= "|";
                                    $cmd .= "wget -O ".$file->name." \"".$file->url."\"";
                                }
                                break;

                            case '0' : //data

                                if ($amdaStat->success)                        
                                    $amdaStat->addTask('print', $dd->user, $obj->list);
                                
                                if ($this->paramsWithoutCode) 
                                        $this->makeLocalParams($obj->list);
                                if ($this->spectraParams) 
                                        $this->makeSpectraParams($obj->list);
                                if ($this->dataBases) 
                                        $this->makeRemoteParams($obj->list); 
                                    
                                $this->makeWsParams($obj->list);

                                $wsdVars = $this->getWsdVars($obj->list);
                                if ($wsdVars) $this->makeWsdParams($wsdVars);
                                
                                 // if 'impex' in $realVars upload simulation data 
                                $impexVars = $this->getImpexVars($obj->list);
                                $imp = array('realVars'=> $impexVars, 'fromWS' => true);
                                if ($impexVars) $this->makeWsrParams($imp);
                                
                                $this->makePrintRequest($resDir);  
                                $this->createPrintPostOptions($resDir);

                                if ($isMulti) {     
                                    $cmd = 'php '.CLASSPATH.'../MultiRequestMgr.php \''.json_encode($this->obj).'\' \''.$tmpname.'\' \''.$dd->user.'\' \''.$dd->IP.'\' \''.$newName.'\''; 
                                }
                                else
                                {                            
                                    $this->createPrintRequestTime($resDir); 
                                    $cmd = DDBIN.'DD_Print '.$dd->user.' '.$dd->IP.' '.DDPROJECT.' '.DDPROJLIB; 
                                }
                               break;
                        }
                        break;
                    // DD Plot / DD PS
                    case 'request':
                    //if alias exists, replace alias name by parameter name
                        if (file_exists(USERWSDIR.'Alias.xml')) 
                        {
                            for ($i=0; $i < count($obj->children); $i++) 
                            {
                                    for ($j=0; $j < count($obj->children[$i]->children); $j++) 
                                    {
                                        $obj->children[$i]->children[$j]->name =  $this->resetAlias($obj->children[$i]->children[$j]->name);
                                    }
                            }
                        }

                        // For PNG, PS, PDF  
                        $res = $this->initResDir($tmpname);
                        if (!$res['success'])
                                    return $res;
                        $resDirPng = $res['resdir'];

                        if (file_exists($resDirPng."/cancelplot"))
                                    unlink($resDirPng."/cancelplot");

                        // general Plot settings : DD_request.res
                        $this -> setPlot();
 
                        // amda statistics 
                        if ($amdaStat->success) {
                            $varArray = array();
                            foreach ($this->obj->children as $panel)  
                                foreach ($panel->children as $plotVar)                           
                                                $varArray[] = $plotVar->text;                     
                            $amdaStat->addTask('plot', $dd->user, $varArray);   
                        }

                        // panels definitions  with/without time
                        $realVars = $this->setPlotObjects($this->obj->children, $tmpname);                               
                        if ($this->paramsWithoutCode) $this->makeLocalParams($realVars);
                       
                        $this->makeWsParams($realVars);

                        $wsdVars = $this->getWsdVars($realVars);
                        if ($wsdVars) $this->makeWsdParams($wsdVars);

                 //       if 'impex' in $realVars upload simulation data
                        $impexVars = $this->getImpexVars($realVars); 
                        $imp = array('realVars'=> $impexVars, 'fromWS' => false);
                        if ($impexVars) $this->makeWsrParams($imp);
                   
                        //Interactive session : create special folder
                        if ($this->obj->format == 'PNG') 
                        {                       
                            // Time table plot
                            if ($isMulti) 
                            {
                                $interval = $this->prepareTimeTablePlot($resDirPng);
                                $this->updateTime('./'.$tmpname, $interval); //update/add time
                                $this->setZoomStart($interval);  
                            }
                            else 
                                $this->setZoomStart();
                                $cmd = DDBIN."DD_Plot ".$tmpname." ".$dd->user." ".$dd->IP." ".DDPROJECT." ".DDPROJLIB;
                        }
                        else 
                        { 
                            if ($isMulti) {   
                                $cmd = 'php '.CLASSPATH.'../MultiRequestMgr.php \''.json_encode($this->obj).'\' \''.$tmpname.'\' \''.$dd->user.'\' \''.$dd->IP.'\' \''.$newName.'\'';  
                            }
                            else {                         
                                $cmd = DDBIN."DD_PS ".$tmpname." ".$dd->user." ".$dd->IP." ".DDPROJECT." ".DDPROJLIB;  
                            } 
                        }  
                        break;

                    default :
                        return array('error' => $this->type." NOT IMPLEMENTED YET");
                }
                $pid =  $this->background($cmd);    
                //TODO KILL method for real TIMEOUT
                $jobMgr = new JobsMgr();
                $cyclesNumber = $isPNG  ? PLOT_CYCLES_NUMBER: JOB_CYCLES_NUMBER;  
            
                $reqStart = time();
            
                //TODO return ERRORS also
                for ($cycle = 0; $cycle < $cyclesNumber; $cycle++) 
                {
                        sleep(JOBTIMEOUT); 
                        
                        $reqTime = time() - $reqStart;
                        
                        if ($reqTime > $cyclesNumber*JOBTIMEOUT)
                        break;

                        if ( $isPNG && is_dir(USERWORKINGDIR."CANCELPLOT") ) {
                        if (file_exists(USERWORKINGDIR."CANCELPLOT/".$this->obj->tabId))
                                        rename(USERWORKINGDIR."CANCELPLOT/".$this->obj->tabId,$resDirPng."/cancelplot");

                        if (count(scandir(USERWORKINGDIR."CANCELPLOT")) == 2) rmdir(USERWORKINGDIR."CANCELPLOT");

                        }
                        if ( $isPNG && file_exists($resDirPng."/cancelplot"))
                        {
                            if (!$jobMgr->isFinished($pid))
                            { 
                                $cmd = 'kill -9 '.$pid;
                                exec($cmd);
                            }

                            foreach (glob($resDirPng.'/*') as $filename)
                                unlink($filename);

                            rmdir($resDirPng);
                        
                        return array('error' => "Plot canceled!");
                        }

                        if ($jobMgr->isFinished($pid))
                        {
                            if (!$isPNG) $newId = $jobMgr->addJob($this->obj, 0, $tmpname, $newName);

                            if ($tmpname !== $newName) $this->postProcessing($tmpname, $newName);
                            $arrayMain = array('pid' => '0', 'name' => $newName, 'id' => $tmpname);
                                        
                            if ($isPNG) 
                            { 
                                $outputName = $this->obj->outputName? $this->obj->outputName : $tmpname;  
                                rename($tmpname.'.png', $outputName.'_'.$cachekiller.'.png');
                                $arrayMain['name'] = $outputName.'_'.$cachekiller;
                            
                                $plotSettings = $this->getPlotSettings();
                                $arrayMain['children'] = $plotSettings; 
                                $arrayMain['tabId']  =  $this->obj->tabId; 
                                // PNG plotting of time tables
                                if ($isMulti) 
                                {
                                    $arrayTable = array('startDate' => $interval['start'],
                                                        'stopDate' => $interval['stop'], 
                                                        'intervalN' =>  $this->currentInt, 
                                                        'totalN' => $this->totalInt,
                                                        'tableName' => $this->ttName);      
                                    return array_merge($arrayMain, $arrayTable);
                                }
                            }
                        return $arrayMain;  
                        }
                } 

                // Add Job in BATCH MODE        
                $newId = $jobMgr->addJob($this->obj, $pid, $tmpname);

                return array('pid' => $pid, 'id' => $newId, 'rawname' => $tmpname);                
        }
}
?>