/* ***********************************************************************
 * *                                                                     *
 * *  Method to convert a Universal ASCII Lighting Format file into      *
 * *  tokens                                                             *
 * *                                                                     *
 * ***********************************************************************
 * File: utils/ualf/UalfTokenize.C
 *
 *                      COPYRIGHT (c) 2006  BY
 *          SIGMET INCORPORATED, WESTFORD MASSACHUSETTS, U.S.A.
 * 
 * THIS SOFTWARE IS FURNISHED UNDER A LICENSE AND MAY BE USED AND  COPIED
 * ONLY  IN  ACCORDANCE WITH  THE  TERMS  OF  SUCH  LICENSE  AND WITH THE
 * INCLUSION OF THE ABOVE COPYRIGHT NOTICE.  THIS SOFTWARE  OR  ANY OTHER
 * COPIES  THEREOF MAY NOT BE PROVIDED OR OTHERWISE MADE AVAILABLE TO ANY
 * OTHER PERSON.  NO TITLE TO AND OWNERSHIP OF  THE  SOFTWARE  IS  HEREBY
 * TRANSFERED.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>


#include "sigtypes.h"
#include "user_lib.h"
#include "UalfTokenize.h"

#define LF_VERSION             0
#define LF_YEAR                1
#define LF_MON                 2
#define LF_DAY                 3
#define LF_HOUR                4
#define LF_MIN                 5
#define LF_SEC                 6
#define LF_NSEC                7
#define LF_LAT                 8
#define LF_LON                 9
#define LF_PEAK_CURNT         10
#define LF_STROKE_DATA        11
#define LF_NUM_SENSORS        12
#define LF_DEG_FREEDOM        13
#define LF_ELLIPSE_ANG        14
#define LF_ELLIPSE_MAJOR      15
#define LF_ELLIPSE_MINOR      16
#define LF_CHI_SQUARE         17
#define LF_RISE_TIME          18
#define LF_PEAK_TO_ZERO       19
#define LF_MAX_RATE_OF_RISE   20 
#define LF_CLOUD_INDICATOR    21 
#define LF_ANGLE_INDICATOR    22 
#define LF_SIGNAL_INDICATOR   23 
#define LF_TIMING_INDICATOR   24 
#define MAX_LF_PARAMS         25
#define MAX_LF_PARAMS_HEADER   7


/*******************************************************
**
** Public Methods
**
*******************************************************/
/* ===============================================
 * Process on line of ualf lightning data 
 */
UINT2 UalfTokenize::tokenize (const char *sline_a)
{
  UINT2 lcheck_strtol;
  char  *stok;
  char  *sptr;
  char  stemp[300];

  strcpy(stemp, sline_a);
  fLat = 0.0;
  fLon = 0.0;
  iTokCnt = 0;
  iMills = 0;
  stok = strtok(stemp, "	");
  while(stok != NULL)
  {
    lcheck_strtol = TRUE;
    switch(iTokCnt)
    {
      case LF_VERSION             :
        iVersion = strtol(stok, &sptr, 10);
        break;
      case LF_YEAR                :
        iYear = strtol(stok, &sptr, 10);
        break;
      case LF_MON                 :
        iMon = strtol(stok, &sptr, 10);
        break;
      case LF_DAY                 :
        iDay = strtol(stok, &sptr, 10);
        break;
      case LF_HOUR                :
        iHour = strtol(stok, &sptr, 10);
        break;
      case LF_MIN                 :
        iMin = strtol(stok, &sptr, 10);
        break;
      case LF_SEC                 :
        iSec = strtol(stok, &sptr, 10);
        break;
      case LF_NSEC                :
        //Convert nano sec to mills sec
        iMills = strtol(stok, &sptr, 10)/1000000;
        break;
      case LF_LAT                 :
        fLat = strtod( stok, &sptr);
        break;
      case LF_LON                 :
        fLon = strtod( stok, &sptr);
        break;
      case LF_PEAK_CURNT          :
        iPeakCurnt = strtol(stok, &sptr, 10);
        break;
      case LF_ELLIPSE_ANG         :
        fEllipseAng = strtod( stok, &sptr);
        break;
      case LF_ELLIPSE_MAJOR  :
        iEllipseMajor = strtol(stok, &sptr, 10);
        break;
      case LF_ELLIPSE_MINOR  :
        iEllipseMinor = strtol(stok, &sptr, 10);
        break;
      case LF_STROKE_DATA         :
      case LF_CLOUD_INDICATOR     :
        //Soon to be implemented
        lcheck_strtol = FALSE;
        break;
      case LF_NUM_SENSORS         :
      case LF_DEG_FREEDOM         :
      case LF_CHI_SQUARE          :
      case LF_RISE_TIME           :
      case LF_PEAK_TO_ZERO        :
      case LF_MAX_RATE_OF_RISE    :
      case LF_ANGLE_INDICATOR     :
      case LF_SIGNAL_INDICATOR    :
      case LF_TIMING_INDICATOR    :
        lcheck_strtol = FALSE;
        break;
    }

    if (lcheck_strtol)
    {
      if (sptr == stok)
        return FALSE;
    }

    stok = strtok(NULL, "	");
    iTokCnt++;
  }

  memset( &ymdsTime, 0, sizeof(ymdsTime) );
  if (iYear<1950) return FALSE;
  if (iYear>3000) return FALSE;
  if (iMon<1) return FALSE;
  if (iMon>12) return FALSE;
  if (iDay<1) return FALSE;
  if (iDay>31) return FALSE;
  if (iHour<0) return FALSE;
  if (iHour>23) return FALSE;
  if (iMin<0) return FALSE;
  if (iMin>59) return FALSE;
  if (iSec<0) return FALSE;
  if (iSec>59) return FALSE;
  if (iMills<0) return FALSE;
  if (iMills>1000) return FALSE;

  ymdsTime.iyear = iYear;
  ymdsTime.imon = iMon;
  ymdsTime.iday = iDay;
  ymdsTime.isec = iPackTime( iHour, iMin, iSec );
  ymdsTime.imills = iMills;

  return TRUE;
}

SINT4 UalfTokenize::getMaxParams()
{
  return MAX_LF_PARAMS;
}

SINT4 UalfTokenize::getMaxHdrParams()
{
  return MAX_LF_PARAMS_HEADER;
}

SINT4 UalfTokenize::getTokCnt()
{
  return iTokCnt;
}

//if iVersion == 9 then we have a header line
//that we created 
SINT4 UalfTokenize::getVersion()
{
  return iVersion;
}

SINT4 UalfTokenize::getPeakCurnt()
{
  return iPeakCurnt;
}

SINT4 UalfTokenize::getEllipseMinor()
{
  return iEllipseMinor;
}

SINT4 UalfTokenize::getEllipseMajor()
{
  return iEllipseMajor;
}

FLT8 UalfTokenize::getEllipseAng()
{
  return fEllipseAng;
}

FLT8 UalfTokenize::getLat()
{
  return fLat;
}

FLT8 UalfTokenize::getLon()
{
  return fLon;
}
SINT4 UalfTokenize::getHour()
{
  return iHour;
}

SINT4 UalfTokenize::getMinute()
{
  return iMin;
}

SINT4 UalfTokenize::getSeconds()
{
  return iSec;
}

struct ymds_time UalfTokenize::getYmdsTime()
{
  return ymdsTime;
}
