
	/*
	** reads ~yujied/IphoneUsefulSegment/ folder to get raw data
	** reads yujie_seg_info.txt to get subject ID and start times of segs
	** reads yujie_eat_time.txt to get meal times
	**
	** creates new raw data file to be read by PhoneView
	** creates -events file to go along with raw data file
	*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char *argv[])

{
int	total_segs,subject[50],start[50];
int	segid[100],total_meals;
int	si[100],ei[100];	/* start and end index */
int	st[100],et[100];	/* start and end time */
int	i,j,s,g,m;
int	hour,minute,second,deltat;
char	text[320],filename[320],outfile[320],text2[320];
FILE	*fpt,*fpto,*fpta,*fptg;
double	ax,ay,az,yaw,pitch,roll;
int	index;


	/* read info on usable segments */
fpt=fopen("yujie_seg_info.txt","r");
total_segs=0;
while (1)
  {
  i=fscanf(fpt,"%d %d %d",&j,&subject[total_segs],&start[total_segs]);
  if (i != 3)
    break;
  total_segs++;
  }
fclose(fpt);

	/* read info on meals (events) */
fpt=fopen("yujie_eat_time.txt","r");
total_meals=0;
while (1)
  {
  i=fscanf(fpt,"%d %d %d %d %d",&segid[total_meals],
	&si[total_meals],&ei[total_meals],
	&st[total_meals],&et[total_meals]);
  if (i != 5)
    break;
  total_meals++;
  }
fclose(fpt);

for (s=0; s<=30; s++)
  {
	/* see if there are any segments for this subject */
  for (g=0; g<total_segs; g++)
    if (subject[g] == s)
      break;
  if (g == total_segs)
    continue;	/* nope, no good data */

	/* build events file */
  sprintf(outfile,"iphone%03d-events.txt",s);
  fpto=fopen(outfile,"w");
  if (fpto == NULL)
    {
    printf("Unable to open %s for writing\n",outfile);
    exit(0);
    }
  for (m=0; m<total_meals; m++)
    {
    if (subject[segid[m]-1] == s)
      {
	/* calculate meal start time */
      deltat=st[m];
      hour=9+(deltat/(60*60*60));
      deltat-=((hour-9)*(60*60*60));
      minute=0+(deltat/(60*60));
      deltat-=((minute)*(60*60));
      second=(deltat/60);
      sprintf(text,"meal  %02d:%02d:%02d",hour,minute,second);
	/* calculate meal end time */
      deltat=et[m];
      hour=9+(deltat/(60*60*60));
      deltat-=((hour-9)*(60*60*60));
      minute=0+(deltat/(60*60));
      deltat-=((minute)*(60*60));
      second=(deltat/60);
      sprintf(text2,"%s  %02d:%02d:%02d",text,hour,minute,second);
      fprintf(fpto,"%s\n",text2);
      }
    }
  fclose(fpto);
  continue;

	/* build data file */
  sprintf(outfile,"iphone%03d.txt",s);
  fpto=fopen(outfile,"a");
  if (fpto == NULL)
    {
    printf("Unable to open %s for appending\n",outfile);
    exit(0);
    }
  printf("making %s\n",outfile);
  for (g=0; g<total_segs; g++)
    if (subject[g] == s)
      {
	/* calculate START time */
      deltat=start[g];
      hour=9+(deltat/(60*60*60));
      deltat-=((hour-9)*(60*60*60));
      minute=0+(deltat/(60*60));
      deltat-=((minute)*(60*60));
      second=(deltat/60);
      sprintf(text,"START 2011-01-01   %02d:%02d:%02d",hour,minute,second);
      fprintf(fpto,"%s\n",text);

	/* open Accel & Gyro files to read raw data */
      sprintf(filename,"/parl/yujied/IphoneUsefulSegment/%03d_AccelRawVelocity.txt",g+1);
      // printf("\t%s",filename);
      fpta=fopen(filename,"r");
      sprintf(filename,"/parl/yujied/IphoneUsefulSegment/%03d_GyroRawVelocity.txt",g+1);
      // printf("\t%s\n",filename);
      fptg=fopen(filename,"r");
      if (fpta == NULL  ||  fptg == NULL)
        {
        printf("Unable to read raw data segments\n");
        exit(0);
        }
      index=0;
      while (1)
        {
        i=fscanf(fpta,"%lf %lf %lf",&ax,&ay,&az);
        j=fscanf(fptg,"%lf %lf %lf",&yaw,&pitch,&roll);
        if (i != 3  ||  j != 3)
          break;
        index++;
        if (index % 4 != 0)
          continue;	/* only output at 15 Hz; reading at 60 Hz */
        fprintf(fpto,"%.2lf 0 0 0 %.4lf %.4lf %.4lf %.2lf %.2lf %.2lf\n",
		(double)index/60.0,ax,ay,az,yaw,pitch,roll);
        }
      fclose(fpta);
      fclose(fptg);

	/* calculate END time */
      hour+=(index/(60*60*60));
      deltat=(index%(60*60*60));
      minute+=(deltat/(60*60));
      if (minute > 59)
        {
        hour++;
        minute-=60;
        }
      deltat=(index%(60*60));
      second+=(deltat/60);
      if (second > 59)
        {
        minute++;
        if (minute > 59)
          {
          hour++;
          minute-=60;
          }
        second-=60;
        }
      sprintf(text,"END 2011-01-01   %02d:%02d:%02d",hour,minute,second);
      fprintf(fpto,"%s\n",text);
      }

	/* close new data file */
  fclose(fpto);
  }



}


