#! /usr/bin/perl

use WXP;

#WXP::Prog::readResrcFile;
printf "Version: %s\n", WXP::getVersion();

print "***************** Calc *****************************\n";
$test_Calc = 1;
if( $test_Calc ){
   printf "Rel Hum = %.2f\n", WXP::Calc::relHum( 70, 50 );
   printf "Wind Chill = %.2f\n", WXP::Convert::KtoF( WXP::Calc::windChillTemp2( WXP::Convert::FtoK(38), WXP::Convert::KTtoMPS(20)));
}

print "***************** Wxp ******************************\n";
printf "%s\n", WXP::getComputerName();
printf "%f\n", WXP::MISS;

print "***************** Date *****************************\n";
$test_Date = 1;
if( $test_Date ){
   print "Testing Date\n";
   $date = new WXP::Date(2003,6,15,12,45,0);
   $date->print;
   $date->set( "20030604210000" );
   $date->print;
   $date->setYear(2004);
   print $date->getYear, " ", $date->getDay, "\n";
   $date->addMonths(2);
   $date->print;
   $date1 = new WXP::Date;
   $date1->print;
   WXP::DateTool::decode( "12Z  4 JUN 03", $date );
   $date->print;
   $date->set;
   $date->changeTimezone( -18000, "CDT" );
   $date->print;
   
}

print "***************** Resrc ****************************\n";
$test_Resrc = 1;
if( $test_Resrc ){
   $resrc = new WXP::Resrc;
   $resrc->set("file_path", "/home/wxp/etc");
   $resrc->printAll;
}

print "***************** ResrcFile ************************\n";
$test_resrcfile = 1;
if( $test_resrcfile ){
   print "Resource file\n";
   WXP::Prog::readResrcFile( "testwxp", "/home/wxp/etc" );
   
   WXP::setResrc( "current", "la" );
   WXP::setResrc( "device", "d" );
   WXP::printAllResrc;
}

print "***************** WmoParse ****************************\n";
$test_MosData = 1;
if( $test_MosData ){
   $file = new WXP::Filename( "mos_dat", "la-12" );
   $file->print;
   $string = new WXP::String;
   $parse = new WXP::WmoParse;
   $mosdata = new WXP::MosData;
   
   $parse->open( $file );
   print $parse->isOpen ."\n";
   $parse->useHdrFile( 1 );
   $parse->setHeader( "FOUS2[12345]" );
   $mosdata->setModel( 2 );
   $j = 0;
   while( $err = $parse->getLine( $string )){
      #if( $parse->isNewProd ){
      #   print "=======================================\n";
      #   print $parse->getHeader . "\n" . $parse->getAwip . "\n";
      #}
      if( $string->get =~ /NAM MOS/ ){
         $mosdata->decode( $string->get );
         for( $i = 0; $i < 20; $i++ ){
            $parse->getLine( $string );
            $mosdata->decode( $string->get );
            last if $string->contains( "OBV" );
         }
         $mosdata->print;
      }
      $j++;
      last if $j == 1;
   }
}

print "***************** MosFile **************************\n";
$test_MosFile = 1;
if( $test_MosFile ){
   $mosfile = new WXP::MosFile;
   $mosdata = new WXP::MosData;
   
   $mosfile->setModel( WXP::MosFile::NAM );
   undef $file;
   $file = new WXP::Filename( "mos_dat", "la-12" );
   $file->print;
   $mosfile->open( $file, WXP::MosFile::WMO );
   $j = 0;
   while( $err = $mosfile->read( $mosdata )){
      $mosdata->print;
      $j++;
      last if $j == 1;
   }
}

print "***************** Level ****************************\n";
$level = new WXP::Level;
$level->set( "850mb" );
$level->print;

print "***************** ForeTime *************************\n";
$fore = new WXP::ForeTime;
$fore->set( "3day" );
$fore->print;

print "***************** CityData *************************\n";
$test_CityData = 1;
if( $test_CityData ){
   $filename = new WXP::String;
   WXP::FileTool::addPath( WXP::getDatabasePath, "sao.cty", $filename );
   print "File: " . $filename->get . "\n";
   
   $cityfile = new WXP::CityFile;
   $cityfile->open( $filename->get, WXP::File::READ );
   $citydata = new WXP::CityData;
   
   for( $i = 0; $i < 10 && $cityfile->read( $citydata ); $i++ ){
      printf "%5s %8.2f %8.2f\n", $citydata->getId, $citydata->getLat, $citydata->getLon;
   }
}

print "***************** Domain ***************************\n";
$ept = new WXP::EarthPoint;
$ept->set( 42, -92 );
$ept->print;
$pt = new WXP::Point;
$gpt = new WXP::GridPoint;
$domain = new WXP::Domain;
WXP::DomainTool::decode( $domain, "us" );

$domain->print;

$domain->tran( $ept, $pt );
$pt->print;
$domain->tran( $ept, $gpt );
$gpt->print;
$domain->tran( $pt, $ept );
$ept->print;

print "***************** Unit *****************************\n";
$unit = WXP::Unit->new;

printf "54F = %.2fC\n", $unit->convert( 54, "F", "C" );
printf "75mi/hr = %.2fkm/hr\n", $unit->convert( 75, "mi/hr", "km/hr" );

print "***************** Plot *****************************\n";
$test_Figure = 1;
if( $test_Figure ){
   $date = new WXP::Date;
   $plot = new WXP::Plot;
   $mapplot = new WXP::MapPlot( "ct=wxp.clr,co=lred" );
   #$mapplot->setMapFiles( "cnty.mpl" );
   $test_SatPlot = 1;
   if( $test_SatPlot ){
      $satplot = new WXP::SatPlot( "if=sat_ir_g16,cof=sat_ir16.cfl,ct=sat.clr" );
   }

   $test_NidsPlot = 1;
   if( $test_NidsPlot ){
      $nidsplot = new WXP::NidsPlot( "ct=rad.clr,cof=nids.cfl,if=nids:t=N0Q:i=DIX" );
   }
   
   $plot->open( "de=d" );
   $domain = new WXP::Domain;
   WXP::DomainTool::decode( $domain, "ne" );
   $plot->setDomain( $domain );
   $satplot->draw( $plot ) if $test_SatPlot;
   $nidsplot->draw( $plot ) if $test_NidsPlot;
   $mapplot->draw( $plot );

   $ept1 = new WXP::EarthPoint( 40,-75 );
   $ept2 = new WXP::EarthPoint( 35,-80 );
   $plot->setLineColor( "lblue" );;
   $plot->setLineWidth( 2. );;
   $plot->drawLine( $ept1, $ept2 );

   $epts = new WXP::EarthPoints;
   $epts->add( 42, -78 );
   $epts->add( 44, -78 );
   $epts->add( 44, -75 );
   $epts->add( 42, -75 );
   $epts->add( 42, -78 );
   $plot->setFillColor( "yellow" );
   $plot->drawFill( $epts );

   $ept1->set( 38, -77 );
   $plot->setTextColor( "white" );
   $plot->setTextWeight( 2. );
   $plot->setTextHeight( .5 );
   $plot->drawText( $ept1, "TEXT" );

   $plot->saveImage( "./testwxp.gif" );

   $plot->end();
}

print "***************** MetarFile ************************\n";
$test_MetarFile = 1;
if( $test_MetarFile ){
   $metar = new WXP::MetarFile;

   undef $file;
   if( $local ){
      $date = new WXP::Date(2003,9,5,12,0,0);
      $file = new WXP::Filename( "sfc_dat", $date );
   } else {
      $file = new WXP::Filename( "sfc_dat", "la" );
   }
   print "File: " . $file->getName() . "\n";
   
   $metar->open( $file );
   $string = new WXP::String;
   
   for( $i = 0; $i < 10 && $metar->getLine( $string ); $i++ ){
      print ">>" . $string->get() . "|\n";
   }
}

print "***************** Filename *************************\n";
$test_Filename = 1;
if( $test_Filename ){
   print "Testing Filename\n";
   $file = new WXP::Filename;
   $file->set( "sfc_cvt", "la" );
   $file->print;
   # $file->set( "sfc_dat", "la" );
   $file->setNumFiles( "-3" );
   print $file->getNumFiles() . "\n";
   $file->print;
   
   for( $i = 0; $file->setFile( $i ); $i++ ){
      printf "$i = %s\n", $file->getName;
   }
   $file->setNumFiles( "-10,2" );
   print $file->getNumFiles() . "\n";
   $file->print;
   
   for( $i = 0; $file->setFile( $i ); $i++ ){
      printf "$i = %s\n", $file->getName;
   }
}

print "***************** NameConv *************************\n";
$test_NameConv = 1;
if( $test_NameConv ){
   print "Testing NameConv\n";
   $nameconv = new WXP::NameConv( "nids_n0r:i=DIX" );
   $date = new WXP::Date;
   $date->set;
   $date->print;
   $string = new WXP::String;
   $nameconv->encode( $date, WXP::NameConv::FILENAME, $string );
   $string->print;
   $nameconv->print;
   
   print "Decode /mnt/noaaport/nwstg/nds/DTX/0310011206_n0r.nid\n";
   $nameconv->decode( "/mnt/noaaport/nwstg/nds/DTX/0310011206_n0r.nid" );
   $nameconv->print;
   $nameconv->getDate( $date );
   $date->print;
   
   print "Encode\n";
   $nameconv->encode( $date, WXP::NameConv::FILENAME, $string );
   $string->print;
   
   print "Set and encode";
   $nameconv->set( "sfc_dat" );
   $nameconv->encode( $date, WXP::NameConv::FILENAME, $string );
   $string->print;
   $nameconv->print;
   
   print "Decode\n";
   $nameconv->decode( "/mnt/noaaport/nwstg/data/03100112_sao.wmo" );
   $nameconv->print;
   
   print "NameConvFile\n";
   $nameconvfile = new WXP::NameConvFile;
   $nameconv->set( "grib_nam" );
   $nameconv->setPath( "" );
   $nameconv->setLatest( "la", $nameconvfile );
   $nameconv->print;
   $nameconvfile->print;
}

print "***************** WmoParse ****************************\n";
$test_Parse = 1;
if( $test_Parse ){
   $string = new WXP::String;
   $file = new WXP::Filename( "sev_txt", "la-6" );
   #$file->set( "sev_txt", "la-6" );
   $file->setNumFiles( "6" );
   $file->print;

   $parse = new WXP::WmoParse;
   $parse->open( $file );
   $parse->setHeader( "W[AS]" );
   $j = 0;
   while( $err = $parse->getLine( $string )){
      if( $parse->isNewProd ){
         print "=======================================\n";
         print $parse->getHeader . "\n" . $parse->getAwip . "\n" . $parse->getLoc . "\n";
         print "---------------------------------------\n";
         $j++;
      }
      last if $j == 5;
   }
}

print "***************** SfcFile **************************\n";
$file = new WXP::Filename( "sfc_cvt", "la" );
$sfcfile = new WXP::SfcFile;
$sfcfile->open( $file->getName, WXP::File::READ, WXP::SfcFile::UNK );
$sfcdata = new WXP::SfcData;

for( $i = 0; $i < 10 && $sfcfile->read( $sfcdata ); $i++ ){
   printf "%5s %3d %3d %s\n", $sfcdata->getId, $sfcdata->getT, $sfcdata->getTd, $sfcdata->getWeather;
}

print "***************** SfcWmoFile **************************\n";
$file = new WXP::Filename( "sfc_dat", "la" );
$sfile = new WXP::SfcWmoFile;
$sdata = new WXP::SfcWmoData;
$data = new WXP::SfcData;
$sfile->open( $file );
$i = 0;
while( $sfile->read( $sdata ) && $i < 5 ){
   $sdata->print;
   if( $sdata->isMETAR()){
      WXP::MetarTool::decode( $sdata, $data );
      print "Decoded:\n";
      $data->print;
   }
   $i++;
}


