#! /usr/bin/python3

import WXP
import sys

WXP.Prog.readResrcFile()

print( "Version: %s" % WXP.Wxp.getVersion())

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

print( "***************** Wxp ******************************" )
print( "%s" % WXP.Wxp.getComputerName())
print( "%f" % WXP.Const.MISS)

print( "***************** Date *****************************" )
test_Date = 1;
if( test_Date ):
   print( "Testing Date" )
   date = WXP.Date(2022,6,15,12,45,0)
   date._print()
   date.set( "20220604210000" )
   date._print()
   date.setYear(2024)
   print( "%d %d" % (date.getYear(), date.getDay()))
   date.addMonths(2)
   date._print()
   date1 = WXP.Date()
   date1._print()
   WXP.DateTool.decode( "12Z  4 JUN 23", date )
   date._print()
   date.set()
   date.changeTimezone( -18000, "CDT" );
   date._print()

print( "***************** Resrc ****************************" )
test_Resrc = 1;
if( test_Resrc ):
   resrc = WXP.Resrc()
   resrc.set("file_path", "/home/wxp/etc")
   resrc.printAll()

print( "***************** ResrcFile ************************" )
test_resrcfile = 1;
if( test_resrcfile ):
   print( "Resource file" )
   WXP.Prog.readResrcFile( "testwxp", "/home/wxp/etc" );

   WXP.Wxp.setResrc( "current", "la" )
   WXP.Wxp.setResrc( "device", "d" )
   WXP.Wxp.printAllResrc()

print( "***************** WmoParse ****************************" )
test_MosData = 1;
if( test_MosData ):
   wfile = WXP.Filename( "mos_dat", "la-12" )
   wfile._print()
   string = WXP.String()
   parse = WXP.WmoParse()
   mosdata = WXP.MosData()

   parse.open( wfile )
   print( parse.isOpen())
   parse.useHdrFile( True )
   parse.setHeader( "FOUS2[12345]" )
   mosdata.setModel( 2 )
   j = 0;
   while( parse.getLine( string ) and j < 2 ):
      #if( $parse->isNewProd ){
      #   print "=======================================\n";
      #   print $parse->getHeader . "\n" . $parse->getAwip . "\n";
      #}
      if( "NAM MOS"  in string.get() ):
         mosdata.decode( string.get() )
         for i in range(20):
            parse.getLine( string )
            mosdata.decode( string.get() )
            if( string.contains( "OBV" )): break

         mosdata._print()
 
      j += 1

print( "***************** MosFile **************************" )
test_MosFile = 1;
if( test_MosFile ):
   mosfile = WXP.MosFile()
   mosdata = WXP.MosData()

   mosfile.setModel( WXP.MosFile.NAM )
   file = WXP.Filename( "mos_dat", "la-12" )
   file._print()
   mosfile.open( file, WXP.MosFile.WMO )
   j = 0;
   while( mosfile.read( mosdata ) and j < 2 ):
      mosdata._print()
      j+=1

print( "***************** Level ****************************" )
level = WXP.Level()
level.set( "850mb" )
level._print()

print( "***************** ForeTime *************************" )
fore = WXP.ForeTime()
fore.set( "3day" )
fore._print()

print( "***************** CityData *************************" )
test_CityData = 1
if( test_CityData ):
   filename = WXP.String()
   WXP.FileTool.addPath( WXP.Wxp.getDatabasePath(), "sao.cty", filename )
   print( "File: %s" % filename.get() )

   cityfile = WXP.CityFile()
   cityfile.open( filename.get(), WXP.FileAttr.READ )
   citydata = WXP.CityData()

   i = 0
   while( cityfile.read( citydata ) and i < 10 ):
      print( "%5s %8.2f %8.2f" % (citydata.getId(), citydata.getLat(), citydata.getLon()))
      i += 1

print( "***************** Domain ***************************" )
ept = WXP.EarthPoint()
ept.set( 42, -92 )
ept._print()
pt = WXP.Point()
gpt = WXP.GridPoint()
domain = 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 *****************************" )
unit = WXP.Unit()

print( "54F = %.2fC" % unit.convert( 54, "F", "C" ))
print( "75mi/hr = %.2fkm/hr" % unit.convert( 75, "mi/hr", "km/hr" ))


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

   test_NidsPlot = 1
   if( test_NidsPlot ):
      nidsplot = WXP.NidsPlot( "ct=rad.clr,cof=nids.cfl,if=nids:t=N0B:i=DIX" )

   plot.open( "de=d" )
   domain = WXP.Domain()
   WXP.DomainTool.decode( domain, "ne" )
   plot.setDomain( domain )
   if( test_SatPlot ): satplot.draw( plot )
   if( test_NidsPlot ): nidsplot.draw( plot )
   mapplot.draw( plot )

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

   epts = 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 ************************" )
test_MetarFile = 1
local = 0
if( test_MetarFile ):
   metar = WXP.MetarFile()

   if( local ):
      date = WXP.Date(2003,9,5,12,0,0)
      file = WXP.Filename( "sfc_dat", date )
   else :
      file = WXP.Filename( "sfc_dat", "la" )
   print( "File: %s" % file.getName())

   metar.open( file );
   string = WXP.String()

   i = 0
   while( metar.getLine( string ) and i < 10 ):
      print( ">> %s" % string.get().encode("ascii","ignore") )
      i+=1

print( "***************** Filename *************************" )
test_Filename = 1
if( test_Filename ):
   print( "Testing Filename" )
   sfile = WXP.Filename()
   sfile.set( "sfc_cvt", "la" )
   sfile._print()
   # sfile.set( "sfc_dat", "la" )
   sfile.setNumFiles( "-3" )
   print( sfile.getNumFiles())
   sfile._print()

   i = 0
   while( sfile.setFile( i )):
      print( "%d = %s" % (i,  sfile.getName()))
      i+=1
   sfile.setNumFiles( "-10,2" )
   print( sfile.getNumFiles())
   sfile._print()

   i = 0
   while( sfile.setFile( i )):
      print( "%d = %s" % (i, sfile.getName()))
      i+=1

print( "***************** NameConv *************************" )
test_NameConv = 1
if( test_NameConv ):
   print( "Testing NameConv" )
   nameconv = WXP.NameConv( "nids:t=N0Q:i=DIX" )
   date = WXP.Date()
   date.set()
   date._print()
   string = WXP.String()
   nameconv.encode( date, WXP.NameConv.FILENAME, string )
   string._print()
   nameconv._print()

   print( "Decode /mnt/noaaport/nwstg/nds/DTX/0310011206_N0Q.nid" )
   nameconv.decode( "/mnt/noaaport/nwstg/nds/DTX/0310011206_N0Q.nid" )
   nameconv._print()
   nameconv.getDate( date )
   date._print()

   print( "Encode" )
   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" )
   nameconv.decode( "/mnt/noaaport/nwstg/data/03100112_sao.wmo" )
   nameconv._print()

   print( "NameConvFile" )
   nameconvfile = WXP.NameConvFile()
   nameconv.set( "grib_nam" )
   nameconv.setPath( "" )
   nameconv.setLatest( "la", nameconvfile )
   nameconv._print()
   nameconvfile._print()

print( "***************** WmoParse ****************************" )
test_Parse = 1
if( test_Parse ):
   string = WXP.String()
   file = WXP.Filename( "sev_txt", "la-6" )
   #file.set( "sev_txt", "la-6" )
   file.setNumFiles( "6" );
   file._print()

   parse = WXP.WmoParse()
   parse.open( file );
   parse.setHeader( "W[AS]" )
   j = 0
   while( parse.getLine( string ) and j < 5 ):
      if( parse.isNewProd() ):
         print( "=======================================" )
         print( parse.getHeader())
         print( parse.getAwip())
         print( parse.getLoc())
         print( "---------------------------------------" )
         j+=1

print( "***************** SfcFile **************************" )
file = WXP.Filename( "sfc_cvt", "la" )
sfcfile = WXP.SfcFile()
sfcfile.open( file.getName(), WXP.FileAttr.READ, WXP.SfcFile.UNK )
sfcdata = WXP.SfcData()

i = 0
while( sfcfile.read( sfcdata ) and i < 10 ):
   print( "%5s %3d %3d %s" % ( sfcdata.getId(), sfcdata.getT(), sfcdata.getTd(), sfcdata.getWeather() ))
   i += 1

print( "***************** SfcWmoFile **************************" )
file = WXP.Filename( "sfc_dat", "la" )
sfile = WXP.SfcWmoFile()
sdata = WXP.SfcWmoData()
data = WXP.SfcData()
sfile.open( file );
i = 0;
while( sfile.read( sdata ) and i < 5 ):
   sdata._print()
   if( sdata.isMETAR() ):
      WXP.MetarTool.decode( sdata, data )
      print( "Decoded:" )
      data._print()

   i+=1

