/*
   File:        dstime.c
   Author(s):   Andrew Moore
   Created:     Thu Oct 30 13:15:22 EST 2003
   Description: Utilities for extracting timecodes from datsets (see utils/timestring.h for more info)
   Copyright (c) Carnegie Mellon University
*/

#include "dstime.h"

ivec *mk_row_to_daycode_from_date_attnum(datset *ds,int date_attnum,
					 char **r_errmess)
{
  int num_rows = ds_num_rows(ds);
  ivec *row_to_daycode = mk_ivec(num_rows);
  int row;

  *r_errmess = NULL;

  if ( !ds_symbol_ref_ok(ds,date_attnum) )
    *r_errmess = mk_printf("Column %d (attribute %s) of the dataset should\n"
			   "not be a bunch of numbers. It should be a\n"
			   "categorical attribute full of dates.\n",
			   date_attnum,ds_attnum_to_name(ds,date_attnum));

  for ( row = 0 ; *r_errmess == NULL && row < num_rows ; row++ )
  {
    char *date_string = ds_row_attnum_to_name(ds,row,date_attnum);
    int daycode = string_to_daycode(date_string,r_errmess);

    if ( *r_errmess == NULL )
    {
      char *s = mk_string_from_daycode(daycode);
      ivec_set(row_to_daycode,row,daycode);
#ifdef VERBOSE
      printf("%s --> daycode %d --> %s\n",date_string,daycode,s);
#endif
      free_string(s);
    }
    
  }

  if ( *r_errmess != NULL )
  {
    free_ivec(row_to_daycode);
    row_to_daycode = NULL;
  }

  return row_to_daycode;
}

ivec *mk_row_to_hmcode_from_time_attnum(datset *ds,int time_attnum,
					 char **r_errmess)
{
  int num_rows = ds_num_rows(ds);
  ivec *row_to_hmcode = mk_ivec(num_rows);
  int row;

  *r_errmess = NULL;

  if ( !ds_symbol_ref_ok(ds,time_attnum) )
    *r_errmess = mk_printf("Column %d (attribute %s) of the dataset should\n"
			   "not be a bunch of numbers. It should be a\n"
			   "categorical attribute full of times.\n",
			   time_attnum,ds_attnum_to_name(ds,time_attnum));

  for ( row = 0 ; *r_errmess == NULL && row < num_rows ; row++ )
  {
    char *time_string = ds_row_attnum_to_name(ds,row,time_attnum);
    int hmcode = string_to_hmcode(time_string,r_errmess);

    if ( *r_errmess == NULL )
    {
      char *s = mk_string_from_hmcode(hmcode);
      ivec_set(row_to_hmcode,row,hmcode);
      printf("%s --> hmcode %d --> %s\n",time_string,hmcode,s);
      free_string(s);
    }
  }

  if ( *r_errmess != NULL )
  {
    free_ivec(row_to_hmcode);
    row_to_hmcode = NULL;
  }

  return row_to_hmcode;
}

ivec *mk_row_to_mincode_from_time_and_date_attnums(datset *ds,
						   int date_attnum,
						   int time_attnum,
						   char **r_errmess)
{
  char *errmess;
  ivec *row_to_daycode = 
    mk_row_to_daycode_from_date_attnum(ds,date_attnum,&errmess);
  ivec *row_to_hmcode = ( row_to_daycode == NULL ) ? NULL : 
    mk_row_to_hmcode_from_time_attnum(ds,time_attnum,&errmess);
  ivec *row_to_mincode = NULL;

  my_assert( (row_to_hmcode==NULL) == (*r_errmess != NULL) );

  if ( row_to_hmcode != NULL )
    row_to_mincode = mk_mincodes_from_daycodes_and_hmcodes(row_to_daycode,
							   row_to_hmcode);

  if ( row_to_daycode != NULL ) free_ivec(row_to_daycode);
  if ( row_to_hmcode != NULL ) free_ivec(row_to_hmcode);

  return row_to_mincode;
}

