<functions>
<engine>JScript</engine>
<function name="FormatDate(date, bTime)"><![CDATA[// format date to "DD-MMM-YY HH:MM" string, e.g.: 19-JAN-2004 20:40
function FormatDate(date, bTime) 
{
  //echo(bTime);
 // var month_arr = new Array("I","II","III","IV","V","VI","VII","VIII","IX","X","XI","XII");
  var month_arr = new Array("JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC");
  var month = month_arr[date.getMonth()];
  var year = new String(date.getYear());
  if(year >= 2000)
    year = year.substr(2,2);
  var day = date.getDate();
  temp = (day<10?"0":"")+day+"-"+month+"-"+year;
  if(bTime==1) {
    var hour = date.getHours()
    var minute = date.getMinutes()
    temp += " "+(hour<10?"0":"")+hour+":";
    temp += (minute<10?"0":"")+minute;
  }
  return temp;
}]]>
</function>
<function name="MainScript"><![CDATA[// ----------- BAND definition ----------------------------------------
var BAND_CNT = 7;

//------------ BAND no. --1-------2-----3-----4-----5------6-------7---
var BAND_MIN = new Array(0.3,    0.3,  0.8,  1.2,  5.0,   25.0,  50.0);
var BAND_MAX = new Array(100.0,  0.8,  1.2,  5.0,  25.0,  50.0,  100.0);

// ------------ Measurement date range definition ---------------------- 
// format: Date(Year, Month, Day, Hour, Minute, Second)          
       
var date_from = new Date(1999, 00, 04, 00, 00, 00);
var date_to   = new Date(2004, 09, 20, 00, 00, 00);

// ---------------------------------------------------------------------

var spectrum,CTime,CSpectrum,CPlace,CData,CMach;
var i,j,val,m, bn, ln, pl, mch, lines, meas_num;
var file;
// ---------------------------------------------------------------------


DO=Data; //DDS data object

// get actual timestamp
var tm=new Date();
// some help strings
_pomstr = new String();
_strmach = new String();
_str = new String();

echo("Script started:  "+actdate());
// open file for output
// file = fopen("C:\\Program Files\\result.txt");
// store output file in the same directory, from which script was loaded
file = fopen("result.txt");

// and write basic informations
fwrite(file, "Measurement Point History\n");
fwrite(file, "*************************\n");
fwrite(file, "Database: \n");
fwrite(file, "Station: \n");
fwrite(file, "Report date: "+FormatDate(tm, 1));
fwrite(file, "\nPeriod reported: "+FormatDate(date_from, 0)+" To "+FormatDate(date_to, 0)+"\n");


// build string with units
for(i=0; i<BAND_CNT; i++)
  _pomstr += " In/Sec";

// walk through all machines

for(mch=0; mch<DO.count; mch++)
{
CMach=DO.M(mch);   // Get machine
//echo(CMach.name);

// write to out. file machine info
fwrite(file, "\nMachine "+(mch+1)+":  "+CMach.name);
fwrite(file, "\n-----------------------------------");
//echo("Machine "+(mch+1)+": "+CMach.name);
//echo("-----------------------------------");


// Now walk through all measurement places
for(pl=0; pl<CMach.count; pl++)
{

CPlace = CMach.P(pl);  // get place
//echo(CPlace.name);

//CPlaceRPM = CPlace.Rpm;
//echo("Place RPM= " + CPlaceRPM);

// walk through all data cells
for(i=0; i< CPlace.count ; i++)
{
 
 CData = CPlace.C(i);  // get data cell
 place_rpm = CPlace.Rpm;
 
 //echo("Cell "+i+":"+CData.PathToRoot);
 //echo("----------------");

 // is this data cell spectrum?
 if(CData.type=="SPEC")
 {
    // build string in 'Machine-XYY' e.g.: EastID Fan-H01
    //_strmach = " ";
    _strmach = " "+CMach.name;
    
    var path_str = new String(CData.PathToRoot);
    
    //echo(path_str);

    if(path_str.indexOf("Horizontal") != -1)
      _strmach += "-H"+(pl<10?"0":"")+(pl+1);
    else if(path_str.indexOf("Vertical") != -1)
      _strmach += "-V"+(pl<10?"0":"")+(pl+1);  
    else if(path_str.indexOf("Axial") != -1)
      _strmach += "-A"+(pl<10?"0":"")+(pl+1);
    
    // check right format...
    flen = _strmach.length;
    if(flen<18)
       for(ii=0; ii<18-flen; ii++)
         _strmach+=" ";  
      
    _strmach +="(RPM) APS  0 ";  
    
    // walk through all measurement in the data cell
    for(m=0; m<CData.GetNumberOfDynamic; m++)
    {
       // get measurement time   
       tm = new Date(CData.meas_date(m));
      
       // if we are in the range, compute RMS for all bands
       if(date_from <= tm && tm <= date_to) 
       {
      //  echo(_strmach+_pomstr);
        fwrite(file, "\n"+_strmach+_pomstr);

       _str = "";
       // get spectrum data into the data array 'spectrum'
       spectrum = CData.getspectrum(m);

       var cell_rpm = spectrum.RPM;
       if(cell_rpm == 0)
         cell_rpm = place_rpm;
       
       // for all bands compute RMS 
       for(bn=0; bn<BAND_CNT; bn++)
       {
         // get start and end lines for band
         
         var line_from = Math.ceil(BAND_MIN[bn]*cell_rpm/60/spectrum.step);
         var line_to = Math.floor(BAND_MAX[bn]*cell_rpm/60/spectrum.step);
         
         val = 0;
         for(ln=line_from; ln<line_to; ln++) {
           //rms computation 
           val += Math.pow(spectrum.ampl(ln)/1.41421,2);
         }
         val = Math.sqrt(val);
         // some formating stuff
         _str += "  "+val.toFixed(3);
         vlen = _str.length;
         if(vlen < (bn+1)*6)
           for(ii=0; ii<(bn+1)*6-vlen; ii++)
             _str+=" "; 
       } 
       // formating is important for us
       fdate = FormatDate(tm, 1);
       frpm = cell_rpm.toFixed(0);
       fstr = " "+fdate+"  "+frpm; 
       flen = fstr.length;
       if(flen<23)
         for(ii=0; ii<23-flen; ii++)
           fstr+=" ";
       fstr += " 100.0";     
       flen = fstr.length;
       if(flen<31)
         for(ii=0; ii<31-flen; ii++)
           fstr+=" ";
     //  echo (fstr+_str); 
       fwrite (file, "\n"+fstr+_str); 
       }  
    } 
  //  echo ("------------------------------");
    fwrite (file, "\n"); 
 }
} //for i
} //for pl
} // for mch

echo("Script finished:  "+actdate());
]]>
</function>
</functions>
