//ldis2019 - Linux equivalent of Dis2002.exe for RPi - CJC - 9/30/2019
//Developed with gcc on ubuntu 6.06
//choose subfolder, load support files, choose 2 db entries, gen html report, extensions
// g++ ldis2019.c -lm on RPi, new parm procIDstartend
//sqrt of smallest square - use: gcc ldis2019.c -lm -> still getting 0.0 use g++
//                            test2018041500000020180415999999
//                    ./a.out test2018052109193520180521092219
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
char vstr1[35] = "microMETER ldis2019.c v2.8 ";
//#define mxc = 260
char CMA[2] = ","; //allow extra byte for null terminator
/*....0....+....0....+....0....+....0....+....0....+....0....+....0....+*/
char QS[2];
char qt = '"'; //QS[0]=qt;<--put in main()//(char) 34;
char CRLF[4] = "\n\r";
char ATPRS[8]; //char *ATPRS="0.085/kwh";
char FE1[100]; //FF1 seems to be a keyword!
char FF2[100], FF3[100], FF4[100], FF5[100]; //font face
char TD1[100], TD2[100], TD3[100], TD4[100], TD5[100], TD6[100]; //table data
char TR1[100], TR2[100], TR3[100]; //table row color
char CK[800][800];
char DB[800][800];
char str1[800], str2[800], temp2[40], temp3[40], hd1[5], hd2[9], hd3[7], he1[5], he2[9], he3[7];
char buf1[2];
char rec3[80];
char FIL1[40], FIL2[40], FIL3[40], FIL4[40], FIL5[40];
char zero[2] = "0";
char TXT1S[78];
int i, j, k, SW = 1, count = 0, pcount = 0, ccount = 0, tcount = 0, I17 = 17, I41 = 41, debug = 0;
int COL, C00, C01, C02, C03, C04, C05, C06, C07, C08, C09, C10, C11;
float PRICE, SCAL, SCA2, SCA3, KWH, TA[6], TB[260];
double x, y, z;
float A1[260], B1[260], C1[260], D1[260], E1[260], F1[260], G1[260], H1[260];//latest values
float A2[260], B2[260], C2[260], D2[260], E2[260], F2[260], G2[260], H2[260];//previous values
float A3[260], B3[260], C3[260], D3[260], E3[260], F3[260], G3[260], H3[260];//numeric differences
float A4[260], B4[260], C4[260], D4[260], E4[260], F4[260], G4[260], H4[260];//detail line values
//    chan,    kwdem,   lcum,    hcum,    amp,     lcum,    hcum,    ser

float BK[260], PF[260], LV[260], AL[260], AH[260];
FILE * fil1, * fil2, * fil3, * fil4, * fil5;
struct {
  char h1[4];
  char h2[8];
  char h3[6];
  char s01[41];
  char s02[41];
  char s03[41];
  char s04[41];
  char s05[41];
  char s06[41];
  char s07[41];
  char s08[41];
  char s09[41];
  char s10[41];
  char s11[41];
  char s12[41];
  char s13[41];
  char s14[41];
  char s15[41];
  char s16[41];
}
dbln;

struct {
  char c1[1];
  char n1[2];
  char c2[1];
  char n2[3];
  char c3[1];
  char n3[5];
  char c4[1];
  char n4[5];
  char c5[1];
  char n5[3];
  char c6[1];
  char n6[5];
  char c7[1];
  char n7[5];
  char c8[1];
  char n8[5];
}
segm;
// As if C was too bloated, itoa() was omitted from ANSI C.
//itoa(Year,temp2,10);
const char * itoa(int n, char * string, int m) { //itoa(Hour,temp3,10);
  sprintf(string, "%d", n);
  return string;
}
const char * ftoa(float n, char * string, int m) { //ftoa(PRICE,ATPRS,10);
  sprintf(string, "%.2f", n);
  return string;
}
//const char *frmt(char *string0, char *string, int m){//frmt(temp3,temp3,2);
//strcpy(string,string0);strcat(string,"x");
//return string;}

//double pow(double, double);

#include <stdio.h>
double squareRoot(double n) {
   double i, precision = 0.00001;
   for(i = 1; i*i <=n; ++i);           //Integer part
   for(--i; i*i < n; i += precision);  //Fractional part
   return i;
}
/*
int main() {
   int n = 24;
   printf("Square root of %d = %lf", n, squareRoot(n));
   return 0;
}
*/




/* Begin main program */
char parm1[50], start1[20], end1[20], dtime1[20], dbrow1[800];
int sw1 = 0, sw2 = 0;
//strncpy(substr, buff+10, 4);
int main(int argc, char * argv[]) {
  if (debug == 1) printf("debug = 1\n");

  printf("%s  run with procIDstartend \n\r",vstr1);
  //segmentation fault if run procID not same as data rows

  //    while(--argc>0) printf((argc>1)?"%s ":"%s\n",++argv);
  QS[0] = qt;

  strcpy(parm1, argv[1]);
  strncpy(hd1, parm1 + 0, 4);
  strncpy(start1, parm1 + 4, 14);
  strncpy(end1, parm1 + 18, 14);

  //strcpy(hd1,argv[1]);
  printf(" procID = %s\n", hd1); //causes seg fault if arg not present
  //printf("  start = %s\n", start1);
  //printf("    end = %s\n", end1);
  if (debug == 1) printf("strlen hd1 = %d\n", strlen(hd1));
  if (strlen(hd1) != 4) {
    strcpy(hd1, "mm01");
    printf("Fixed hd1 = %s\n", hd1);
  };
  if (strlen(hd1) != 4) {
    printf("%s\n Invalid subfolder - s/b like mm01. ");
    exit(EXIT_FAILURE);
  };

  //microdat global data
  if (debug == 1) printf("microdat.txt\n");
  strcpy(FIL1, hd1);
  strcat(FIL1, "/microdat.txt"); //printf("\n\rFIL1 %s ",FIL1);
  if (debug == 1) printf("microdat.txt2\n");
  fil1 = fopen(FIL1, "r"); //printf("\n fopen fil1= %d",fil1);
  if (debug == 1) printf("microdat.txt3\n");
  k = fscanf(fil1, "%f", & SCAL); //str1);SCAL=atof(str1);printf("\n\r%s ",str1);
  if (debug == 1) printf("microdat.txt3a\n");

  k = fscanf(fil1, "%f", & SCA2);
  if (debug == 1) printf("microdat.txt3b\n");

  k = fscanf(fil1, "%f", & PRICE);  
  if (debug == 1) printf("microdat.txt3c\n");
  //segmentation fault if run procID not same as data rows
  k = fscanf(fil1, "%d", & COL);
  if (debug == 1) printf("microdat.txt4\n");
  k = fscanf(fil1, "%d %d %d %d", & C00, & C01, & C02, & C03);
  k = fscanf(fil1, "%d %d %d %d", & C04, & C05, & C06, & C07);
  k = fscanf(fil1, "%d %d %d %d", & C08, & C09, & C10, & C11);
  if (debug == 1) printf("microdat.txt\n");
  if (debug == 1) {printf("\n%f ",SCAL);printf("\n%f ",SCA2);printf("\n%f ",PRICE);printf("\n%d ",COL);}
  if (debug == 1) printf("\n%d %d %d %d ",C00,C01,C02,C03);
  if (debug == 1) printf("\n%d %d %d %d ",C04,C05,C06,C07);
  if (debug == 1) printf("\n%d %d %d %d ",C08,C09,C10,C11);
  fclose(fil1);

  //text line
  if (debug == 1) printf("micro.txt\n");
  strcpy(FIL4, hd1);
  strcat(FIL4, "/micro.txt"); //printf("\n\rFIL4 %s ",FIL4);
  fil4 = fopen(FIL4, "r");
  fgets(TXT1S, 70, fil4); //printf("\n\r%s ",TXT1S);
  fclose(fil4);
  //if (debug == 1) printf("\n\r micro dat,txt ok ");
  

//ctdat
  if (debug == 1) printf("ctdat.txt\n");
  strcpy(FIL2, hd1);
  strcat(FIL2, "/ctdat.txt"); //printf("\n\rFIL2 %s ",FIL2);
  fil2 = fopen(FIL2, "r");
  i = 0;
  while (i < 16) {
    fgets(str1, 26, fil2);
    strcpy(CK[i], str1); //printf("\n\r%s ",CK[i]);
    fgets(str1, 02, fil2); //strcpy(QS,str1);//printf("\n\rstr1=%s ",str1);//eat"
    fgets(str1, 05, fil2);
    BK[i] = atof(str1); //printf("\n\rstr1=%s ",str1);
    fgets(str1, 02, fil2); //printf("\n\rstr1=%s ",str1);//eat,
    fgets(str1, 05, fil2);
    PF[i] = atof(str1); //printf("\n\rstr1=%s ",str1);
    fgets(str1, 02, fil2); //printf("\n\rstr1=%s ",str1);//eat,
    fgets(str1, 04, fil2);
    LV[i] = atof(str1); //printf("\n\rstr1=%s ",str1);
    fgets(str1, 02, fil2); //printf("\n\rstr1=%s ",str1);//eat,
    fgets(str1, 04, fil2);
    AL[i] = atof(str1); //printf("\n\rstr1=%s ",str1);
    fgets(str1, 02, fil2); //printf("\n\rstr1=%s ",str1);//eat,
    fgets(str1, 04, fil2);
    AH[i] = atof(str1); //printf("\n\rstr1=%s ",str1);
    fgets(str1, 25, fil2); //printf("\n\rstr1=%s ",str1);//end at .
    fgets(str1, 25, fil2); //printf("\n\rstr1=%s ",str1);//end of line (appears as blank)
    if (debug == 1) printf("\n\r%f ", BK[i]);
    i++;
  }
  if (debug == 1) printf("\n\r"); /*flush*/
  fclose(fil2);
  if (debug == 1) printf("\n\r ct dat ok "); //exit(EXIT_SUCCESS); 


  //
  //mmdb1 - 674 bytes, hdr 18 bytes
  //mm0120020909131542,01,010,38979,00028,010,38979,00028,10000, 15 more groups mMII
  //....+....1....+....2....+....3....+....4....+....5....+....6....+....7
  strcpy(FIL5, hd1);
  strcat(FIL5, "/mmdb1.txt");
  if (debug == 1) printf("\n\rFIL5 %s ", FIL5);
  fil5 = fopen(FIL5, "r"); //exit(EXIT_SUCCESS); 
  //                      676->679 for Vista compatibility    filling DB[j] with mmdb1 rows j=0,j++
  //sw1 and sw2 were never set in ldis2007.c.  we can set here to stop file reads
  //read goes directly into DB array
  printf("  start = %s\n", start1);
  while (fgets(DB[count], 679, fil5) != NULL && sw1 == 0 && sw2 == 0) {
    strcpy(dbrow1, DB[count]);
    strncpy(dtime1, dbrow1 + 4, 14);
    tcount++;
    if (strcmp(dtime1, start1) >= 0 && strcmp(dtime1, end1) <= 0) //admit rows in window
    {
      count++; //read to eof, count and save all in DB[]
      if (pcount == 0) printf("   prev = %s\n", dtime1);//prev rec
      //if (pcount == 0) printf(" value of count: %d\n", count);
      // snag previous and current entries
      if (pcount == 0) pcount = count;// snag first one (which will already be in DB[0]
                       ccount = count;// snag each one after within this window
    }
  } //end while loop

  printf("   curr = %s\n", dtime1);//curr rec
  printf("    end = %s\n", end1);
  printf(" eligible rows = %d", count);
  printf(" total rows = %d\n", tcount);

  if (debug == 1) printf("\n\r count  %d ", count);
  if (debug == 1) printf("\n\r pcount %d ", pcount);
  if (debug == 1) printf("\n\r ccount %d ", ccount);

  printf("\n\r");
  fclose(fil5);
  //i = count - 1;
  i = ccount - 1;// should be same as count

  if (debug == 1) printf("\n\r database loaded to DB\n\r");

  //exit(EXIT_SUCCESS); 

  //extract calc data: latest DB->dbln->16x{->segm->A1-H1[i]          later records -> DB[i}
  strcpy(dbln.h1, DB[i]); //copies latest saved record into struct
  if (debug == 1) printf("\n\r%s", dbln.h1); //print all up to null
  strncpy(temp3, dbln.h1, 4);
  if (strcmp(hd1, temp3) != 0) {
    printf("\n\r%s %s", hd1, dbln.h1);
    printf("\n\r mmdb1 file folder mismatch");
    exit(EXIT_FAILURE);
  };

  strncpy(hd1, dbln.h1, 4);
  strncpy(hd2, dbln.h2, 8);
  strncpy(hd3, dbln.h3, 6); //header data

  j = 1;
  strncpy(segm.c1, dbln.s01, I41); //Copy first n characters of first segment 
  if (debug == 1) printf("\n\r%s", segm.c1); //print all up to null
  strncpy(temp3, "     ", 5); //we need to clear temp3 before shorter length use!
  strncpy(temp3, segm.n1, 2);
  A1[j] = atof(temp3);
  strncpy(temp3, segm.n2, 3);
  B1[j] = atof(temp3);
  strncpy(temp3, segm.n3, 5);
  C1[j] = atof(temp3);
  strncpy(temp3, segm.n4, 5);
  D1[j] = atof(temp3);
  strncpy(temp3, "     ", 5); //we need to clear temp3 before shorter length use!
  strncpy(temp3, segm.n5, 3);
  E1[j] = atof(temp3);
  strncpy(temp3, segm.n6, 5);
  F1[j] = atof(temp3);
  strncpy(temp3, segm.n7, 5);
  G1[j] = atof(temp3);
  strncpy(temp3, segm.n8, 5);
  H1[j] = atof(temp3);
  if (debug == 1) printf("\n\r%f", H1[j]);
  if (debug == 1) printf("\n\r%s", temp3);

  j = 2;
  strncpy(segm.c1, dbln.s02, I41); //Copy first n characters of second segment 
  //printf("\n\r%s",segm.c1);//print all up to null
  strncpy(temp3, "     ", 5); //we need to clear temp3 before shorter length use!
  strncpy(temp3, segm.n1, 2);
  A1[j] = atof(temp3);
  strncpy(temp3, segm.n2, 3);
  B1[j] = atof(temp3);
  strncpy(temp3, segm.n3, 5);
  C1[j] = atof(temp3);
  strncpy(temp3, segm.n4, 5);
  D1[j] = atof(temp3);
  strncpy(temp3, "     ", 5); //we need to clear temp3 before shorter length use!
  strncpy(temp3, segm.n5, 3);
  E1[j] = atof(temp3);
  strncpy(temp3, segm.n6, 5);
  F1[j] = atof(temp3);
  strncpy(temp3, segm.n7, 5);
  G1[j] = atof(temp3);
  strncpy(temp3, segm.n8, 5);
  H1[j] = atof(temp3);
  if (debug == 1) printf("\n\r%f", H1[j]);
  if (debug == 1) printf("\n\r%s", temp3);

  j = 3;
  strncpy(segm.c1, dbln.s03, I41); //Copy first n characters of segment 
  //printf("\n\r%s",segm.c1);//print all up to null
  strncpy(temp3, "     ", 5); //we need to clear temp3 before shorter length use!
  strncpy(temp3, segm.n1, 2);
  A1[j] = atof(temp3);
  strncpy(temp3, segm.n2, 3);
  B1[j] = atof(temp3);
  strncpy(temp3, segm.n3, 5);
  C1[j] = atof(temp3);
  strncpy(temp3, segm.n4, 5);
  D1[j] = atof(temp3);
  strncpy(temp3, "     ", 5); //we need to clear temp3 before shorter length use!
  strncpy(temp3, segm.n5, 3);
  E1[j] = atof(temp3);
  strncpy(temp3, segm.n6, 5);
  F1[j] = atof(temp3);
  strncpy(temp3, segm.n7, 5);
  G1[j] = atof(temp3);
  strncpy(temp3, segm.n8, 5);
  H1[j] = atof(temp3);
  if (debug == 1) printf("\n\r%f", H1[j]);
  if (debug == 1) printf("\n\r%s", temp3);

  j = 4;
  strncpy(segm.c1, dbln.s04, I41); //Copy first n characters of segment 
  //printf("\n\r%s",segm.c1);//print all up to null
  strncpy(temp3, "     ", 5); //we need to clear temp3 before shorter length use!
  strncpy(temp3, segm.n1, 2);
  A1[j] = atof(temp3);
  strncpy(temp3, segm.n2, 3);
  B1[j] = atof(temp3);
  strncpy(temp3, segm.n3, 5);
  C1[j] = atof(temp3);
  strncpy(temp3, segm.n4, 5);
  D1[j] = atof(temp3);
  strncpy(temp3, "     ", 5); //we need to clear temp3 before shorter length use!
  strncpy(temp3, segm.n5, 3);
  if (debug == 1) printf("\n\r .n5 %s", temp3);
  E1[j] = atof(temp3);
  if (debug == 1) printf("\n\r atof %f", E1[j]);
  strncpy(temp3, segm.n6, 5);
  F1[j] = atof(temp3);
  strncpy(temp3, segm.n7, 5);
  G1[j] = atof(temp3);
  strncpy(temp3, segm.n8, 5);
  H1[j] = atof(temp3);
  if (debug == 1) printf("\n\r%f", H1[j]);
  if (debug == 1) printf("\n\r%s", temp3);
  if (debug == 1) printf("\n\r%  f", A1[j]);
  if (debug == 1) printf("\n\r%f", B1[j]);
  if (debug == 1) printf("\n\r%f", C1[j]);
  if (debug == 1) printf("\n\r%f", D1[j]);
  if (debug == 1) printf("\n\r%f", E1[j]);

  j = 5;
  strncpy(segm.c1, dbln.s05, I41); //Copy first n characters of segment 
  //printf("\n\r%s",segm.c1);//print all up to null
  strncpy(temp3, "     ", 5); //we need to clear temp3 before shorter length use!
  strncpy(temp3, segm.n1, 2);
  A1[j] = atof(temp3);
  strncpy(temp3, segm.n2, 3);
  B1[j] = atof(temp3);
  strncpy(temp3, segm.n3, 5);
  C1[j] = atof(temp3);
  strncpy(temp3, segm.n4, 5);
  D1[j] = atof(temp3);
  strncpy(temp3, "     ", 5); //we need to clear temp3 before shorter length use!
  strncpy(temp3, segm.n5, 3);
  E1[j] = atof(temp3);
  strncpy(temp3, segm.n6, 5);
  F1[j] = atof(temp3);
  strncpy(temp3, segm.n7, 5);
  G1[j] = atof(temp3);
  strncpy(temp3, segm.n8, 5);
  H1[j] = atof(temp3);
  if (debug == 1) printf("\n\r%f", H1[j]);
  if (debug == 1) printf("\n\r%s", temp3);

  j = 6;
  strncpy(segm.c1, dbln.s06, I41); //Copy first n characters of segment 
  //printf("\n\r%s",segm.c1);//print all up to null
  strncpy(temp3, "     ", 5); //we need to clear temp3 before shorter length use!
  strncpy(temp3, segm.n1, 2);
  A1[j] = atof(temp3);
  strncpy(temp3, segm.n2, 3);
  B1[j] = atof(temp3);
  strncpy(temp3, segm.n3, 5);
  C1[j] = atof(temp3);
  strncpy(temp3, segm.n4, 5);
  D1[j] = atof(temp3);
  strncpy(temp3, "     ", 5); //we need to clear temp3 before shorter length use!
  strncpy(temp3, segm.n5, 3);
  E1[j] = atof(temp3);
  strncpy(temp3, segm.n6, 5);
  F1[j] = atof(temp3);
  strncpy(temp3, segm.n7, 5);
  G1[j] = atof(temp3);
  strncpy(temp3, segm.n8, 5);
  H1[j] = atof(temp3);
  if (debug == 1) printf("\n\r%f", H1[j]);
  if (debug == 1) printf("\n\r%s", temp3);

  j = 7;
  strncpy(segm.c1, dbln.s07, I41); //Copy first n characters of segment 
  //printf("\n\r%s",segm.c1);//print all up to null
  strncpy(temp3, "     ", 5); //we need to clear temp3 before shorter length use!
  strncpy(temp3, segm.n1, 2);
  A1[j] = atof(temp3);
  strncpy(temp3, segm.n2, 3);
  B1[j] = atof(temp3);
  strncpy(temp3, segm.n3, 5);
  C1[j] = atof(temp3);
  strncpy(temp3, segm.n4, 5);
  D1[j] = atof(temp3);
  strncpy(temp3, "     ", 5); //we need to clear temp3 before shorter length use!
  strncpy(temp3, segm.n5, 3);
  E1[j] = atof(temp3);
  strncpy(temp3, segm.n6, 5);
  F1[j] = atof(temp3);
  strncpy(temp3, segm.n7, 5);
  G1[j] = atof(temp3);
  strncpy(temp3, segm.n8, 5);
  H1[j] = atof(temp3);
  if (debug == 1) printf("\n\r%f", H1[j]);
  if (debug == 1) printf("\n\r%s", temp3);

  j = 8;
  strncpy(segm.c1, dbln.s08, I41); //Copy first n characters of segment 
  //printf("\n\r%s",segm.c1);//print all up to null
  strncpy(temp3, "     ", 5); //we need to clear temp3 before shorter length use!
  strncpy(temp3, segm.n1, 2);
  A1[j] = atof(temp3);
  strncpy(temp3, segm.n2, 3);
  B1[j] = atof(temp3);
  strncpy(temp3, segm.n3, 5);
  C1[j] = atof(temp3);
  strncpy(temp3, segm.n4, 5);
  D1[j] = atof(temp3);
  strncpy(temp3, "     ", 5); //we need to clear temp3 before shorter length use!
  strncpy(temp3, segm.n5, 3);
  E1[j] = atof(temp3);
  strncpy(temp3, segm.n6, 5);
  F1[j] = atof(temp3);
  strncpy(temp3, segm.n7, 5);
  G1[j] = atof(temp3);
  strncpy(temp3, segm.n8, 5);
  H1[j] = atof(temp3);
  if (debug == 1) printf("\n\r%f", H1[j]);
  if (debug == 1) printf("\n\r%s", temp3);

  j = 9;
  strncpy(segm.c1, dbln.s09, I41); //Copy first n characters of segment 
  //printf("\n\r%s",segm.c1);//print all up to null
  strncpy(temp3, "     ", 5); //we need to clear temp3 before shorter length use!
  strncpy(temp3, segm.n1, 2);
  A1[j] = atof(temp3);
  strncpy(temp3, segm.n2, 3);
  B1[j] = atof(temp3);
  strncpy(temp3, segm.n3, 5);
  C1[j] = atof(temp3);
  strncpy(temp3, segm.n4, 5);
  D1[j] = atof(temp3);
  strncpy(temp3, "     ", 5); //we need to clear temp3 before shorter length use!
  strncpy(temp3, segm.n5, 3);
  E1[j] = atof(temp3);
  strncpy(temp3, segm.n6, 5);
  F1[j] = atof(temp3);
  strncpy(temp3, segm.n7, 5);
  G1[j] = atof(temp3);
  strncpy(temp3, segm.n8, 5);
  H1[j] = atof(temp3);
  if (debug == 1) printf("\n\r%f", H1[j]);
  if (debug == 1) printf("\n\r%s", temp3);

  j = 10;
  strncpy(segm.c1, dbln.s10, I41); //Copy first n characters of segment 
  //printf("\n\r%s",segm.c1);//print all up to null
  strncpy(temp3, "     ", 5); //we need to clear temp3 before shorter length use!
  strncpy(temp3, segm.n1, 2);
  A1[j] = atof(temp3);
  strncpy(temp3, segm.n2, 3);
  B1[j] = atof(temp3);
  strncpy(temp3, segm.n3, 5);
  C1[j] = atof(temp3);
  strncpy(temp3, segm.n4, 5);
  D1[j] = atof(temp3);
  strncpy(temp3, "     ", 5); //we need to clear temp3 before shorter length use!
  strncpy(temp3, segm.n5, 3);
  E1[j] = atof(temp3);
  strncpy(temp3, segm.n6, 5);
  F1[j] = atof(temp3);
  strncpy(temp3, segm.n7, 5);
  G1[j] = atof(temp3);
  strncpy(temp3, segm.n8, 5);
  H1[j] = atof(temp3);
  if (debug == 1) printf("\n\r%f", H1[j]);
  if (debug == 1) printf("\n\r%s", temp3);

  j = 11;
  strncpy(segm.c1, dbln.s11, I41); //Copy first n characters of segment 
  //printf("\n\r%s",segm.c1);//print all up to null
  strncpy(temp3, "     ", 5); //we need to clear temp3 before shorter length use!
  strncpy(temp3, segm.n1, 2);
  A1[j] = atof(temp3);
  strncpy(temp3, segm.n2, 3);
  B1[j] = atof(temp3);
  strncpy(temp3, segm.n3, 5);
  C1[j] = atof(temp3);
  strncpy(temp3, segm.n4, 5);
  D1[j] = atof(temp3);
  strncpy(temp3, "     ", 5); //we need to clear temp3 before shorter length use!
  strncpy(temp3, segm.n5, 3);
  E1[j] = atof(temp3);
  strncpy(temp3, segm.n6, 5);
  F1[j] = atof(temp3);
  strncpy(temp3, segm.n7, 5);
  G1[j] = atof(temp3);
  strncpy(temp3, segm.n8, 5);
  H1[j] = atof(temp3);
  if (debug == 1) printf("\n\r%f", H1[j]);
  if (debug == 1) printf("\n\r%s", temp3);

  j = 12;
  strncpy(segm.c1, dbln.s12, I41); //Copy first n characters of segment 
  //printf("\n\r%s",segm.c1);//print all up to null
  strncpy(temp3, "     ", 5); //we need to clear temp3 before shorter length use!
  strncpy(temp3, segm.n1, 2);
  A1[j] = atof(temp3);
  strncpy(temp3, segm.n2, 3);
  B1[j] = atof(temp3);
  strncpy(temp3, segm.n3, 5);
  C1[j] = atof(temp3);
  strncpy(temp3, segm.n4, 5);
  D1[j] = atof(temp3);
  strncpy(temp3, "     ", 5); //we need to clear temp3 before shorter length use!
  strncpy(temp3, segm.n5, 3);
  E1[j] = atof(temp3);
  strncpy(temp3, segm.n6, 5);
  F1[j] = atof(temp3);
  strncpy(temp3, segm.n7, 5);
  G1[j] = atof(temp3);
  strncpy(temp3, segm.n8, 5);
  H1[j] = atof(temp3);
  if (debug == 1) printf("\n\r%f", H1[j]);
  if (debug == 1) printf("\n\r%s", temp3);

  j = 13;
  strncpy(segm.c1, dbln.s13, I41); //Copy first n characters of segment 
  //printf("\n\r%s",segm.c1);//print all up to null
  strncpy(temp3, "     ", 5); //we need to clear temp3 before shorter length use!
  strncpy(temp3, segm.n1, 2);
  A1[j] = atof(temp3);
  strncpy(temp3, segm.n2, 3);
  B1[j] = atof(temp3);
  strncpy(temp3, segm.n3, 5);
  C1[j] = atof(temp3);
  strncpy(temp3, segm.n4, 5);
  D1[j] = atof(temp3);
  strncpy(temp3, "     ", 5); //we need to clear temp3 before shorter length use!
  strncpy(temp3, segm.n5, 3);
  E1[j] = atof(temp3);
  strncpy(temp3, segm.n6, 5);
  F1[j] = atof(temp3);
  strncpy(temp3, segm.n7, 5);
  G1[j] = atof(temp3);
  strncpy(temp3, segm.n8, 5);
  H1[j] = atof(temp3);
  if (debug == 1) printf("\n\r%f", H1[j]);
  if (debug == 1) printf("\n\r%s", temp3);

  j = 14;
  strncpy(segm.c1, dbln.s14, I41); //Copy first n characters of segment 
  //printf("\n\r%s",segm.c1);//print all up to null
  strncpy(temp3, "     ", 5); //we need to clear temp3 before shorter length use!
  strncpy(temp3, segm.n1, 2);
  A1[j] = atof(temp3);
  strncpy(temp3, segm.n2, 3);
  B1[j] = atof(temp3);
  strncpy(temp3, segm.n3, 5);
  C1[j] = atof(temp3);
  strncpy(temp3, segm.n4, 5);
  D1[j] = atof(temp3);
  strncpy(temp3, "     ", 5); //we need to clear temp3 before shorter length use!
  strncpy(temp3, segm.n5, 3);
  E1[j] = atof(temp3);
  strncpy(temp3, segm.n6, 5);
  F1[j] = atof(temp3);
  strncpy(temp3, segm.n7, 5);
  G1[j] = atof(temp3);
  strncpy(temp3, segm.n8, 5);
  H1[j] = atof(temp3);
  if (debug == 1) printf("\n\r%f", H1[j]);
  if (debug == 1) printf("\n\r%s", temp3);

  j = 15;
  strncpy(segm.c1, dbln.s15, I41); //Copy first n characters of segment 
  //printf("\n\r%s",segm.c1);//print all up to null
  strncpy(temp3, "     ", 5); //we need to clear temp3 before shorter length use!
  strncpy(temp3, segm.n1, 2);
  A1[j] = atof(temp3);
  strncpy(temp3, segm.n2, 3);
  B1[j] = atof(temp3);
  strncpy(temp3, segm.n3, 5);
  C1[j] = atof(temp3);
  strncpy(temp3, segm.n4, 5);
  D1[j] = atof(temp3);
  strncpy(temp3, "     ", 5); //we need to clear temp3 before shorter length use!
  strncpy(temp3, segm.n5, 3);
  E1[j] = atof(temp3);
  strncpy(temp3, segm.n6, 5);
  F1[j] = atof(temp3);
  strncpy(temp3, segm.n7, 5);
  G1[j] = atof(temp3);
  strncpy(temp3, segm.n8, 5);
  H1[j] = atof(temp3);
  if (debug == 1) printf("\n\r%f", H1[j]);
  if (debug == 1) printf("\n\r%s", temp3);

  j = 16;
  strncpy(segm.c1, dbln.s16, I41); //Copy first n characters of segment 
  //printf("\n\r%s",segm.c1);//print all up to null
  strncpy(temp3, "     ", 5); //we need to clear temp3 before shorter length use!
  strncpy(temp3, segm.n1, 2);
  A1[j] = atof(temp3);
  strncpy(temp3, segm.n2, 3);
  B1[j] = atof(temp3);
  strncpy(temp3, segm.n3, 5);
  C1[j] = atof(temp3);
  strncpy(temp3, segm.n4, 5);
  D1[j] = atof(temp3);
  strncpy(temp3, "     ", 5); //we need to clear temp3 before shorter length use!
  strncpy(temp3, segm.n5, 3);
  E1[j] = atof(temp3);
  strncpy(temp3, segm.n6, 5);
  F1[j] = atof(temp3);
  strncpy(temp3, segm.n7, 5);
  G1[j] = atof(temp3);
  strncpy(temp3, segm.n8, 5);
  H1[j] = atof(temp3);
  if (debug == 1) printf("\n\r%f", H1[j]);
  if (debug == 1) printf("\n\r%s", temp3);
  if (debug == 1) printf("\n\r latest line extracted");

  //extract calc data: prev   DB->dbln->16x{->segm->A2-H2[i]                 previous data -> DB[0]
  strcpy(dbln.h1, DB[0]); //copies latest saved record into struct   i-1
  if (debug == 1) printf("\n\r%s", dbln.h1); //print all up to null

  j = 1;
  strncpy(segm.c1, dbln.s01, I41); //Copy first n characters of first segment 
  strncpy(he1, dbln.h1, 4);
  strncpy(he2, dbln.h2, 8);
  strncpy(he3, dbln.h3, 6); //header data

  if (debug == 1) printf("\n\r%s", segm.c1); //print all up to null
  strncpy(temp3, "     ", 5); //we need to clear temp3 before shorter length use!
  strncpy(temp3, segm.n1, 2);
  A2[j] = atof(temp3);
  strncpy(temp3, segm.n2, 3);
  B2[j] = atof(temp3);
  strncpy(temp3, segm.n3, 5);
  C2[j] = atof(temp3);
  strncpy(temp3, segm.n4, 5);
  D2[j] = atof(temp3);
  strncpy(temp3, "     ", 5); //we need to clear temp3 before shorter length use!
  strncpy(temp3, segm.n5, 3);
  E2[j] = atof(temp3);
  strncpy(temp3, segm.n6, 5);
  F2[j] = atof(temp3);
  strncpy(temp3, segm.n7, 5);
  G2[j] = atof(temp3);
  strncpy(temp3, segm.n8, 5);
  H2[j] = atof(temp3);

  j = 2;
  strncpy(segm.c1, dbln.s02, I41); //Copy first n characters of second segment 
  if (debug == 1) printf("\n\r%s", segm.c1); //print all up to null
  strncpy(temp3, "     ", 5); //we need to clear temp3 before shorter length use!
  strncpy(temp3, segm.n1, 2);
  A2[j] = atof(temp3);
  strncpy(temp3, segm.n2, 3);
  B2[j] = atof(temp3);
  strncpy(temp3, segm.n3, 5);
  C2[j] = atof(temp3);
  strncpy(temp3, segm.n4, 5);
  D2[j] = atof(temp3);
  strncpy(temp3, "     ", 5); //we need to clear temp3 before shorter length use!
  strncpy(temp3, segm.n5, 3);
  E2[j] = atof(temp3);
  strncpy(temp3, segm.n6, 5);
  F2[j] = atof(temp3);
  strncpy(temp3, segm.n7, 5);
  G2[j] = atof(temp3);
  strncpy(temp3, segm.n8, 5);
  H2[j] = atof(temp3);

  j = 3;
  strncpy(segm.c1, dbln.s03, I41); //Copy first n characters of segment 
  if (debug == 1) printf("\n\r%s", segm.c1); //print all up to null
  strncpy(temp3, "     ", 5); //we need to clear temp3 before shorter length use!
  strncpy(temp3, segm.n1, 2);
  A2[j] = atof(temp3);
  strncpy(temp3, segm.n2, 3);
  B2[j] = atof(temp3);
  strncpy(temp3, segm.n3, 5);
  C2[j] = atof(temp3);
  strncpy(temp3, segm.n4, 5);
  D2[j] = atof(temp3);
  strncpy(temp3, "     ", 5); //we need to clear temp3 before shorter length use!
  strncpy(temp3, segm.n5, 3);
  E2[j] = atof(temp3);
  strncpy(temp3, segm.n6, 5);
  F2[j] = atof(temp3);
  strncpy(temp3, segm.n7, 5);
  G2[j] = atof(temp3);
  strncpy(temp3, segm.n8, 5);
  H2[j] = atof(temp3);

  j = 4;
  strncpy(segm.c1, dbln.s04, I41); //Copy first n characters of segment 
  if (debug == 1) printf("\n\r%s", segm.c1); //print all up to null
  strncpy(temp3, "     ", 5); //we need to clear temp3 before shorter length use!
  strncpy(temp3, segm.n1, 2);
  A2[j] = atof(temp3);
  strncpy(temp3, segm.n2, 3);
  B2[j] = atof(temp3);
  strncpy(temp3, segm.n3, 5);
  C2[j] = atof(temp3);
  strncpy(temp3, segm.n4, 5);
  D2[j] = atof(temp3);
  strncpy(temp3, "     ", 5); //we need to clear temp3 before shorter length use!
  strncpy(temp3, segm.n5, 3);
  E2[j] = atof(temp3);
  strncpy(temp3, segm.n6, 5);
  F2[j] = atof(temp3);
  strncpy(temp3, segm.n7, 5);
  G2[j] = atof(temp3);
  strncpy(temp3, segm.n8, 5);
  H2[j] = atof(temp3);

  j = 5;
  strncpy(segm.c1, dbln.s05, I41); //Copy first n characters of segment 
  if (debug == 1) printf("\n\r%s", segm.c1); //print all up to null
  strncpy(temp3, "     ", 5); //we need to clear temp3 before shorter length use!
  strncpy(temp3, segm.n1, 2);
  A2[j] = atof(temp3);
  strncpy(temp3, segm.n2, 3);
  B2[j] = atof(temp3);
  strncpy(temp3, segm.n3, 5);
  C2[j] = atof(temp3);
  strncpy(temp3, segm.n4, 5);
  D2[j] = atof(temp3);
  strncpy(temp3, "     ", 5); //we need to clear temp3 before shorter length use!
  strncpy(temp3, segm.n5, 3);
  E2[j] = atof(temp3);
  strncpy(temp3, segm.n6, 5);
  F2[j] = atof(temp3);
  strncpy(temp3, segm.n7, 5);
  G2[j] = atof(temp3);
  strncpy(temp3, segm.n8, 5);
  H2[j] = atof(temp3);

  j = 6;
  strncpy(segm.c1, dbln.s06, I41); //Copy first n characters of segment 
  if (debug == 1) printf("\n\r%s", segm.c1); //print all up to null
  strncpy(temp3, "     ", 5); //we need to clear temp3 before shorter length use!
  strncpy(temp3, segm.n1, 2);
  A2[j] = atof(temp3);
  strncpy(temp3, segm.n2, 3);
  B2[j] = atof(temp3);
  strncpy(temp3, segm.n3, 5);
  C2[j] = atof(temp3);
  strncpy(temp3, segm.n4, 5);
  D2[j] = atof(temp3);
  strncpy(temp3, "     ", 5); //we need to clear temp3 before shorter length use!
  strncpy(temp3, segm.n5, 3);
  E2[j] = atof(temp3);
  strncpy(temp3, segm.n6, 5);
  F2[j] = atof(temp3);
  strncpy(temp3, segm.n7, 5);
  G2[j] = atof(temp3);
  strncpy(temp3, segm.n8, 5);
  H2[j] = atof(temp3);

  j = 7;
  strncpy(segm.c1, dbln.s07, I41); //Copy first n characters of segment 
  if (debug == 1) printf("\n\r%s", segm.c1); //print all up to null
  strncpy(temp3, "     ", 5); //we need to clear temp3 before shorter length use!
  strncpy(temp3, segm.n1, 2);
  A2[j] = atof(temp3);
  strncpy(temp3, segm.n2, 3);
  B2[j] = atof(temp3);
  strncpy(temp3, segm.n3, 5);
  C2[j] = atof(temp3);
  strncpy(temp3, segm.n4, 5);
  D2[j] = atof(temp3);
  strncpy(temp3, "     ", 5); //we need to clear temp3 before shorter length use!
  strncpy(temp3, segm.n5, 3);
  E2[j] = atof(temp3);
  strncpy(temp3, segm.n6, 5);
  F2[j] = atof(temp3);
  strncpy(temp3, segm.n7, 5);
  G2[j] = atof(temp3);
  strncpy(temp3, segm.n8, 5);
  H2[j] = atof(temp3);

  j = 8;
  strncpy(segm.c1, dbln.s08, I41); //Copy first n characters of segment 
  if (debug == 1) printf("\n\r%s", segm.c1); //print all up to null
  strncpy(temp3, "     ", 5); //we need to clear temp3 before shorter length use!
  strncpy(temp3, segm.n1, 2);
  A2[j] = atof(temp3);
  strncpy(temp3, segm.n2, 3);
  B2[j] = atof(temp3);
  strncpy(temp3, segm.n3, 5);
  C2[j] = atof(temp3);
  strncpy(temp3, segm.n4, 5);
  D2[j] = atof(temp3);
  strncpy(temp3, "     ", 5); //we need to clear temp3 before shorter length use!
  strncpy(temp3, segm.n5, 3);
  E2[j] = atof(temp3);
  strncpy(temp3, segm.n6, 5);
  F2[j] = atof(temp3);
  strncpy(temp3, segm.n7, 5);
  G2[j] = atof(temp3);
  strncpy(temp3, segm.n8, 5);
  H2[j] = atof(temp3);

  j = 9;
  strncpy(segm.c1, dbln.s09, I41); //Copy first n characters of segment 
  if (debug == 1) printf("\n\r%s", segm.c1); //print all up to null
  strncpy(temp3, "     ", 5); //we need to clear temp3 before shorter length use!
  strncpy(temp3, segm.n1, 2);
  A2[j] = atof(temp3);
  strncpy(temp3, segm.n2, 3);
  B2[j] = atof(temp3);
  strncpy(temp3, segm.n3, 5);
  C2[j] = atof(temp3);
  strncpy(temp3, segm.n4, 5);
  D2[j] = atof(temp3);
  strncpy(temp3, "     ", 5); //we need to clear temp3 before shorter length use!
  strncpy(temp3, segm.n5, 3);
  E2[j] = atof(temp3);
  strncpy(temp3, segm.n6, 5);
  F2[j] = atof(temp3);
  strncpy(temp3, segm.n7, 5);
  G2[j] = atof(temp3);
  strncpy(temp3, segm.n8, 5);
  H2[j] = atof(temp3);

  j = 10;
  strncpy(segm.c1, dbln.s10, I41); //Copy first n characters of segment 
  if (debug == 1) printf("\n\r%s", segm.c1); //print all up to null
  strncpy(temp3, "     ", 5); //we need to clear temp3 before shorter length use!
  strncpy(temp3, segm.n1, 2);
  A2[j] = atof(temp3);
  strncpy(temp3, segm.n2, 3);
  B2[j] = atof(temp3);
  strncpy(temp3, segm.n3, 5);
  C2[j] = atof(temp3);
  strncpy(temp3, segm.n4, 5);
  D2[j] = atof(temp3);
  strncpy(temp3, "     ", 5); //we need to clear temp3 before shorter length use!
  strncpy(temp3, segm.n5, 3);
  E2[j] = atof(temp3);
  strncpy(temp3, segm.n6, 5);
  F2[j] = atof(temp3);
  strncpy(temp3, segm.n7, 5);
  G2[j] = atof(temp3);
  strncpy(temp3, segm.n8, 5);
  H2[j] = atof(temp3);

  j = 11;
  strncpy(segm.c1, dbln.s11, I41); //Copy first n characters of segment 
  if (debug == 1) printf("\n\r%s", segm.c1); //print all up to null
  strncpy(temp3, "     ", 5); //we need to clear temp3 before shorter length use!
  strncpy(temp3, segm.n1, 2);
  A2[j] = atof(temp3);
  strncpy(temp3, segm.n2, 3);
  B2[j] = atof(temp3);
  strncpy(temp3, segm.n3, 5);
  C2[j] = atof(temp3);
  strncpy(temp3, segm.n4, 5);
  D2[j] = atof(temp3);
  strncpy(temp3, "     ", 5); //we need to clear temp3 before shorter length use!
  strncpy(temp3, segm.n5, 3);
  E2[j] = atof(temp3);
  strncpy(temp3, segm.n6, 5);
  F2[j] = atof(temp3);
  strncpy(temp3, segm.n7, 5);
  G2[j] = atof(temp3);
  strncpy(temp3, segm.n8, 5);
  H2[j] = atof(temp3);

  j = 12;
  strncpy(segm.c1, dbln.s12, I41); //Copy first n characters of segment 
  if (debug == 1) printf("\n\r%s", segm.c1); //print all up to null
  strncpy(temp3, "     ", 5); //we need to clear temp3 before shorter length use!
  strncpy(temp3, segm.n1, 2);
  A2[j] = atof(temp3);
  strncpy(temp3, segm.n2, 3);
  B2[j] = atof(temp3);
  strncpy(temp3, segm.n3, 5);
  C2[j] = atof(temp3);
  strncpy(temp3, segm.n4, 5);
  D2[j] = atof(temp3);
  strncpy(temp3, "     ", 5); //we need to clear temp3 before shorter length use!
  strncpy(temp3, segm.n5, 3);
  E2[j] = atof(temp3);
  strncpy(temp3, segm.n6, 5);
  F2[j] = atof(temp3);
  strncpy(temp3, segm.n7, 5);
  G2[j] = atof(temp3);
  strncpy(temp3, segm.n8, 5);
  H2[j] = atof(temp3);

  j = 13;
  strncpy(segm.c1, dbln.s13, I41); //Copy first n characters of segment 
  if (debug == 1) printf("\n\r%s", segm.c1); //print all up to null
  strncpy(temp3, "     ", 5); //we need to clear temp3 before shorter length use!
  strncpy(temp3, segm.n1, 2);
  A2[j] = atof(temp3);
  strncpy(temp3, segm.n2, 3);
  B2[j] = atof(temp3);
  strncpy(temp3, segm.n3, 5);
  C2[j] = atof(temp3);
  strncpy(temp3, segm.n4, 5);
  D2[j] = atof(temp3);
  strncpy(temp3, "     ", 5); //we need to clear temp3 before shorter length use!
  strncpy(temp3, segm.n5, 3);
  E2[j] = atof(temp3);
  strncpy(temp3, segm.n6, 5);
  F2[j] = atof(temp3);
  strncpy(temp3, segm.n7, 5);
  G2[j] = atof(temp3);
  strncpy(temp3, segm.n8, 5);
  H2[j] = atof(temp3);

  j = 14;
  strncpy(segm.c1, dbln.s14, I41); //Copy first n characters of segment 
  if (debug == 1) printf("\n\r%s", segm.c1); //print all up to null
  strncpy(temp3, "     ", 5); //we need to clear temp3 before shorter length use!
  strncpy(temp3, segm.n1, 2);
  A2[j] = atof(temp3);
  strncpy(temp3, segm.n2, 3);
  B2[j] = atof(temp3);
  strncpy(temp3, segm.n3, 5);
  C2[j] = atof(temp3);
  strncpy(temp3, segm.n4, 5);
  D2[j] = atof(temp3);
  strncpy(temp3, "     ", 5); //we need to clear temp3 before shorter length use!
  strncpy(temp3, segm.n5, 3);
  E2[j] = atof(temp3);
  strncpy(temp3, segm.n6, 5);
  F2[j] = atof(temp3);
  strncpy(temp3, segm.n7, 5);
  G2[j] = atof(temp3);
  strncpy(temp3, segm.n8, 5);
  H2[j] = atof(temp3);

  j = 15;
  strncpy(segm.c1, dbln.s15, I41); //Copy first n characters of segment  
  if (debug == 1) printf("\n\r%s", segm.c1); //print all up to null
  strncpy(temp3, "     ", 5); //we need to clear temp3 before shorter length use!
  strncpy(temp3, segm.n1, 2);
  A2[j] = atof(temp3);
  strncpy(temp3, segm.n2, 3);
  B2[j] = atof(temp3);
  strncpy(temp3, segm.n3, 5);
  C2[j] = atof(temp3);
  strncpy(temp3, segm.n4, 5);
  D2[j] = atof(temp3);
  strncpy(temp3, "     ", 5); //we need to clear temp3 before shorter length use!
  strncpy(temp3, segm.n5, 3);
  E2[j] = atof(temp3);
  strncpy(temp3, segm.n6, 5);
  F2[j] = atof(temp3);
  strncpy(temp3, segm.n7, 5);
  G2[j] = atof(temp3);
  strncpy(temp3, segm.n8, 5);
  H2[j] = atof(temp3);

  j = 16;
  strncpy(segm.c1, dbln.s16, I41); //Copy first n characters of segment  
  if (debug == 1) printf("\n\r%s", segm.c1); //print all up to null
  strncpy(temp3, "     ", 5); //we need to clear temp3 before shorter length use!
  strncpy(temp3, segm.n1, 2);
  A2[j] = atof(temp3);
  strncpy(temp3, segm.n2, 3);
  B2[j] = atof(temp3);
  strncpy(temp3, segm.n3, 5);
  C2[j] = atof(temp3);
  strncpy(temp3, segm.n4, 5);
  D2[j] = atof(temp3);
  strncpy(temp3, "     ", 5); //we need to clear temp3 before shorter length use!
  strncpy(temp3, segm.n5, 3);
  E2[j] = atof(temp3);
  strncpy(temp3, segm.n6, 5);
  F2[j] = atof(temp3);
  strncpy(temp3, segm.n7, 5);
  G2[j] = atof(temp3);
  strncpy(temp3, segm.n8, 5);
  H2[j] = atof(temp3);
  if (debug == 1) printf("\n\r older line extracted");
  if (debug == 1) printf("\n\r pre calc delta");
  //pre calc delta
  j = 1;
  while (j < I17) {
    //chan,           kwdem,            lcum,             hcum,
    A3[j] = A1[j] - A2[j];
    B3[j] = B1[j];
    C3[j] = C1[j] - C2[j];
    D3[j] = D1[j] - D2[j];
    //amp,            lcum,             hcum,             ser
    E3[j] = E1[j];
    F3[j] = F1[j] - F2[j];
    G3[j] = G1[j] - G2[j];
    H3[j] = H1[j] - H2[j];
    //calc cumulative usage data
    D4[j] = D3[j] * 65536;
    C4[j] = C3[j] + D4[j]; //tp cum C4
    G4[j] = G3[j] * 65536;
    F4[j] = F3[j] + G4[j]; //ap cum F4

    if (debug == 1) printf("\n\r D2D1 calc %f %f ", D2[j], D1[j]);
    if (debug == 1) printf("\n\r D4D3 calc %f %f ", D4[j], D3[j]);
    if (debug == 1) printf("\n\r C2C1 calc %f %f ", C2[j], C1[j]);
    if (debug == 1) printf("\n\r C4C3 calc %f %f ", C4[j], C3[j]);

    E4[j] = (E3[j] / SCAL) * (BK[j - 1] / 20);
    if (debug == 1) printf("\n\r E4 calc %f %f ", SCAL, E3[j]);
    B4[j] = (B3[j] / SCAL) * (BK[j - 1] / 20) * (LV[j - 1] / 1000);
    C4[j] = (C4[j] / SCA2) * (BK[j - 1] / 20) * PF[j - 1] * (LV[j - 1] / 120); //'Scale to kwh 
    F4[j] = (F4[j] / SCA2) * (BK[j - 1] / 20) * PF[j - 1] * (LV[j - 1] / 120); //'Scale to kvah 
    if (debug == 1) printf("\n\r C4 calc %f %f %f %f %f ", SCA2, C3[j], BK[j - 1], PF[j - 1], LV[j - 1]);
    if (debug == 1) printf("\n\r C3 calc %f %f ", C1[j], C2[j]);
    
    //t(total) calc F4[0]
    E4[0] = E4[0] + E4[j]; //t amps
    B4[0] = B4[0] + B4[j]; //t kw
    C4[0] = C4[0] + C4[j]; //t kwh
    F4[0] = F4[0] + F4[j]; //t kvah

    //CK BK[260],PF[260],LV[260],AL[260],AH[260];detail line follows:
    //ckt name amperes kva	kwh kvah kvarh	$ cost	%   alarm demand X10(tm)  pf
    //A1  CK   E4      B4       C4  F4   calc    calc tcalc   F1   html   rsv     calc         
    j++;
  }



  if (debug == 1) printf("\n\r html report");
  //html report
  strcpy(FIL3, hd1);
  strcat(FIL3, "/mm.htm");
  if (debug == 1) printf("\n\rFIL3 %s ", FIL3);
  fil3 = fopen(FIL3, "w");
  if (debug == 1) printf("\n\r header");
  //html header components
  strcpy(FE1, "<font face =");
  strcat(FE1, QS);
  strcat(FE1, "verdana, arial, helvetica");
  strcat(FE1, QS);
  strcat(FE1, " color=");
  strcat(FE1, QS);
  strcpy(FF2, FE1);
  strcpy(FF3, FE1);
  strcpy(FF4, FE1);
  strcpy(FF5, FE1);
  strcat(FE1, "#000000");
  strcpy(temp2, " ");
  strcat(temp2, QS);
  strcat(temp2, "size=1>");
  strcat(FE1, temp2);
  strcat(FF2, "#ff0000");
  strcat(FF2, temp2);
  strcat(FF3, "#0000ff");
  strcat(FF3, temp2);
  strcat(FF4, "#00dd00");
  strcat(FF4, temp2);
  strcat(FF5, "#ffffff");
  strcat(FF5, temp2);
  strcpy(TD1, "<TD NOWRAP ALIGN=");
  strcat(TD1, QS);
  strcpy(TD2, TD1);
  strcpy(TD3, TD1);
  strcpy(TD4, TD1);
  strcpy(TD5, TD1);
  strcpy(TD6, TD1);
  strcat(TD1, "center");
  strcat(TD1, QS);
  strcat(TD1, ">");
  strcat(TD2, "left");
  strcat(TD2, QS);
  strcat(TD2, " Colspan = 7>");
  strcat(TD3, "right");
  strcat(TD3, QS);
  strcat(TD3, " Colspan = 4>");
  strcat(TD4, "right");
  strcat(TD4, QS);
  strcat(TD4, " Colspan = 1>");
  strcat(TD5, "center");
  strcat(TD5, QS);
  strcat(TD5, " Colspan = 1>");
  strcat(TD6, "left");
  strcat(TD6, QS);
  strcat(TD6, " Colspan = 1>");

  fputs("<HTML><HEAD><TITLE>microMETER</TITLE>", fil3);
  fputs(CRLF, fil3);
  //'<meta http-equiv="refresh" content="n">
  //Print #1, "<meta http-equiv=" & Q$ & "refresh" & Q$ & " content=" & Q$ & "180" & Q$ & ">"
  fputs("<meta http-equiv=", fil3);
  fputs(QS, fil3);
  fputs("refresh", fil3);
  fputs(QS, fil3);
  fputs(" content=", fil3);
  fputs(QS, fil3);
  fputs("180", fil3);
  fputs(QS, fil3);
  fputs(">", fil3);
  fputs(CRLF, fil3);
  fputs("<body>", fil3);
  fputs("<TABLE>", fil3);
  fputs(CRLF, fil3);
  //1st
  fputs("<TR bgcolor=", fil3);
  fputs(QS, fil3);
  fputs("#aaaaaa", fil3);
  fputs(QS, fil3);
  fputs(">", fil3);
  fputs(TD1, fil3);
  fputs(FE1, fil3);
  fputs(TXT1S, fil3);
  fputs("</TD>", fil3); //TD2 Colspan = 7 
  fputs(TD2, fil3);
  fputs(FE1, fil3);
  fputs(hd1, fil3);
  fputs(" from ", fil3);
  fputs(he2, fil3);
  fputs(" at ", fil3);
  fputs(he3, fil3);
  fputs(" to ", fil3);
  fputs(hd2, fil3);
  fputs(" at ", fil3);
  fputs(hd3, fil3);
  fputs("</TD>", fil3);
  fputs(TD3, fil3);
  fputs(FE1, fil3);
  fputs(vstr1, fil3);
  fputs("</TD>", fil3);
  fputs(CRLF, fil3);
  //2nd
  fputs("<TR bgcolor=", fil3);
  fputs(QS, fil3);
  fputs("#aaaaaa", fil3);
  fputs(QS, fil3);
  fputs(">", fil3);
  fputs(TD1, fil3);
  fputs(FE1, fil3);
  fputs("channel and circuit name", fil3);
  fputs("</TD>", fil3);
  fputs(TD1, fil3);
  fputs(FE1, fil3);
  fputs("amperes", fil3);
  fputs("</TD>", fil3);
  fputs(TD1, fil3);
  fputs(FE1, fil3);
  fputs("kva", fil3);
  fputs("</TD>", fil3);
  fputs(TD1, fil3);
  fputs(FF5, fil3);
  fputs("kwh", fil3);
  fputs("</TD>", fil3);
  fputs(TD1, fil3);
  fputs(FF5, fil3);
  fputs("kvah", fil3);
  fputs("</TD>", fil3);
  fputs(TD1, fil3);
  fputs(FF5, fil3);
  fputs("kvarh", fil3);
  fputs("</TD>", fil3);
  fputs(TD1, fil3);
  fputs(FF3, fil3);
  fputs("$ cost", fil3);
  fputs("</TD>", fil3);
  fputs(TD1, fil3);
  fputs(FE1, fil3);
  fputs("%", fil3);
  fputs("</TD>", fil3);
  fputs(TD1, fil3);
  fputs(FF2, fil3);
  fputs("alarm", fil3);
  fputs("</TD>", fil3);
  fputs(TD1, fil3);
  fputs(FE1, fil3);
  fputs("demand", fil3);
  fputs("</TD>", fil3);
  fputs(TD1, fil3);
  fputs(FF4, fil3);
  fputs("X10(tm)", fil3);
  fputs("</TD>", fil3);
  fputs(TD1, fil3);
  fputs(FE1, fil3);
  fputs("power", fil3);
  fputs("</TD></TR>", fil3);
  fputs(CRLF, fil3);
  //3rd
  fputs("<TR bgcolor=", fil3);
  fputs(QS, fil3);
  fputs("#aaaaaa", fil3);
  fputs(QS, fil3);
  fputs(">", fil3);
  fputs(TD1, fil3);
  fputs(FE1, fil3);
  fputs("(true power)", fil3);
  fputs("</TD>", fil3);
  fputs(TD1, fil3);
  fputs(FE1, fil3);
  fputs("watts/volts", fil3);
  fputs("</TD>", fil3);
  fputs(TD1, fil3);
  fputs(FE1, fil3);
  fputs("kw dem", fil3);
  fputs("</TD>", fil3);
  fputs(TD1, fil3);
  fputs(FF5, fil3);
  fputs("used", fil3);
  fputs("</TD>", fil3);
  fputs(TD1, fil3);
  fputs(FF5, fil3);
  fputs("", fil3);
  fputs("</TD>", fil3);
  fputs(TD1, fil3);
  fputs(FF5, fil3);
  fputs("", fil3);
  fputs("</TD>", fil3);
  //ATPRS=ftoa(PRICE);itoa(Hour,temp3,10);
  ftoa(PRICE / 100.0, ATPRS, 10);
  fputs(TD1, fil3);
  fputs(FE1, fil3);
  fputs(ATPRS, fil3);
  fputs("</TD>", fil3);
  fputs(TD1, fil3);
  fputs(FE1, fil3);
  fputs("of total", fil3);
  fputs("</TD>", fil3);
  fputs(TD1, fil3);
  fputs(FF2, fil3);
  fputs("limits", fil3);
  fputs("</TD>", fil3);
  fputs(TD1, fil3);
  fputs(FE1, fil3);
  fputs("graph", fil3);
  fputs("</TD>", fil3);
  fputs(TD1, fil3);
  fputs(FE1, fil3);
  fputs("control", fil3);
  fputs("</TD>", fil3);
  fputs(TD1, fil3);
  fputs(FE1, fil3);
  fputs("factor", fil3);
  fputs("</TD></TR>", fil3);
  fputs(CRLF, fil3);
  //calc detail
  //CK BK[260],PF[260],LV[260],AL[260],AH[260];detail line follows:
  //ckt# name amperes kva  kwh kvah kvarh   $cost %       alarm      demand X10(tm) pf
  //A1   CK   E4      B4   C4  F4   calc    calc  tcalc   F1         html   rsv     calc

  j = 1;
  while (j < I17) {
    fputs("<TR bgcolor=", fil3);
    fputs(QS, fil3);
    k = 2; //  If C00 = 3 Then '3-phase, set k
    if (j == 2 | j == 4 | j == 6 | j == 8 | j == 10 | j == 12 | j == 14 | j == 16) k = 1;
    if (k == 1) fputs("#fofodf", fil3);
    if (k == 2) fputs("#ffffee", fil3);
    if (k == 3) fputs("#fff7d7", fil3);
    fputs(QS, fil3);
    fputs(">", fil3);
    //fputs(TD4,fil3);ftoa(A1[j],temp3,10);fputs(FE1,fil3);fputs(temp3,fil3);fputs("</TD>",fil3);
    fputs(TD6, fil3);
    fputs(FE1, fil3);
    fputs(CK[j - 1], fil3);
    fputs("</TD>", fil3);
    fputs(TD4, fil3);
    ftoa(E4[j], temp3, 5);
    fputs(FE1, fil3);
    fputs(temp3, fil3);
    fputs("</TD>", fil3); //a
    fputs(TD4, fil3);
    ftoa(B4[j], temp3, 5);
    fputs(FE1, fil3);
    fputs(temp3, fil3);
    fputs("</TD>", fil3); //kw
    fputs(TD4, fil3);
    ftoa(C4[j], temp3, 5);
    fputs(FE1, fil3);
    fputs(temp3, fil3);
    fputs("</TD>", fil3); //kwh
    if (F4[j] < C4[j]) C4[j] = F4[j]; //impossible, make same
    fputs(TD4, fil3);
    ftoa(F4[j], temp3, 5);
    fputs(FE1, fil3);
    fputs(temp3, fil3);
    fputs("</TD>", fil3); //kvah
    x =  (F4[j] * F4[j]) - (C4[j] * C4[j]);
    if (x < 0) x = 0; //impossible but make sure
    //y = pow(x, .5); //sqrt of smallest square    gcc test.c -o test -lm
    //y=sqrt(x);//sqrt of smallest square - use: gcc ldis2019.c -lm -> still getting 0.0 use g++
    int n = x;
    y = squareRoot(n);
    if(debug == 1) printf("Square root of %d = %lf  ", n, squareRoot(n));
    if(debug == 1) printf("Squar2 root of %d = %lf \n", x, y);

    fputs(TD4, fil3);
    ftoa(y, temp3, 5);
    fputs(FE1, fil3);
    fputs(temp3, fil3);
    fputs("</TD>", fil3); //
    y = PRICE * C4[j] / 100.0;
    fputs(TD4, fil3);
    ftoa(y, temp3, 5);
    fputs(FE1, fil3);
    fputs(temp3, fil3);
    fputs("</TD>", fil3); //
    y = 0;
    if (C4[0] > 0) y = C4[j] / C4[0] * 100.0;
    fputs(TD4, fil3);
    ftoa(y, temp3, 5);
    fputs(FE1, fil3);
    fputs(temp3, fil3);
    fputs("</TD>", fil3); //
    strcpy(temp3, " ");
    if (AH[j - 1] < 255 && E1[j] > AH[j - 1]) strcpy(temp3, "H");
    if (AL[j - 1] > 000 && E1[j] < AL[j - 1]) strcpy(temp3, "L");
    fputs(TD5, fil3);
    temp3;
    fputs(FE1, fil3);
    fputs(temp3, fil3);
    fputs("</TD>", fil3); //alarm
    strcpy(temp3, " ");
    fputs(TD5, fil3);
    temp3;
    fputs(FE1, fil3);
    fputs(temp3, fil3);
    fputs("</TD>", fil3); //dem gr
    strcpy(temp3, " ");
    fputs(TD5, fil3);
    temp3;
    fputs(FE1, fil3);
    fputs(temp3, fil3);
    fputs("</TD>", fil3); //x10
    y = 0;
    if (F4[j] > 0) y = C4[j] / F4[j];
    fputs(TD4, fil3);
    ftoa(y, temp3, 5);
    fputs(FE1, fil3);
    fputs(temp3, fil3);
    fputs("</TD>", fil3); //pf

    fputs("</TR>", fil3);
    fputs(CRLF, fil3);
    j++;
  }
  //totals line
  j = 0; //                                      Vista dddddd    header aaaaaa
  fputs("<TR bgcolor=", fil3);
  fputs(QS, fil3);
  fputs("#aaaaaa", fil3);
  fputs(QS, fil3);
  fputs(">", fil3);
  fputs(TD6, fil3);
  fputs(FE1, fil3);
  fputs("totals", fil3);
  fputs("</TD>", fil3);
  fputs(TD4, fil3);
  ftoa(E4[j], temp3, 5);
  fputs(FE1, fil3);
  fputs(temp3, fil3);
  fputs("</TD>", fil3); //a
  fputs(TD4, fil3);
  ftoa(B4[j], temp3, 5);
  fputs(FE1, fil3);
  fputs(temp3, fil3);
  fputs("</TD>", fil3); //kw
  fputs(TD4, fil3);
  ftoa(C4[j], temp3, 5);
  fputs(FE1, fil3);
  fputs(temp3, fil3);
  fputs("</TD>", fil3); //kwh
  if (F4[j] < C4[j]) C4[j] = F4[j]; //impossible, make same
  fputs(TD4, fil3);
  ftoa(F4[j], temp3, 5);
  fputs(FE1, fil3);
  fputs(temp3, fil3);
  fputs("</TD>", fil3); //kvah
  x = (F4[j] * F4[j]) - (C4[j] * C4[j]);
  if (x < 0) x = 0; //impossible but make sure
  //y = pow(x, .5); //sqrt of smallest square    gcc test.c -o test -lm
  //y=sqrt(x);//sqrt of smallest square - use: gcc ldis2019.c -lm -> still getting 0.0
  int n = x;
  y = squareRoot(n);

  fputs(TD4, fil3);
  ftoa(y, temp3, 5);
  fputs(FE1, fil3);
  fputs(temp3, fil3);
  fputs("</TD>", fil3); //
  y = PRICE * C4[j] / 100.0;
  fputs(TD4, fil3);
  ftoa(y, temp3, 5);
  fputs(FE1, fil3);
  fputs(temp3, fil3);
  fputs("</TD>", fil3); //
  y = 0;
  if (C4[0] > 0) y = C4[j] / C4[0] * 100.0;
  fputs(TD4, fil3);
  ftoa(y, temp3, 5);
  fputs(FE1, fil3);
  fputs(temp3, fil3);
  fputs("</TD>", fil3); //
  strcpy(temp3, " ");
  // if(AH[j-1]<255 && E1[j]>AH[j-1])strcpy(temp3,"H");
  // if(AL[j-1]>000 && E1[j]<AL[j-1])strcpy(temp3,"L");
  fputs(TD5, fil3);
  temp3;
  fputs(FE1, fil3);
  fputs(temp3, fil3);
  fputs("</TD>", fil3); //alarm
  strcpy(temp3, " ");
  fputs(TD5, fil3);
  temp3;
  fputs(FE1, fil3);
  fputs(temp3, fil3);
  fputs("</TD>", fil3); //dem gr
  strcpy(temp3, " ");
  fputs(TD5, fil3);
  temp3;
  fputs(FE1, fil3);
  fputs(temp3, fil3);
  fputs("</TD>", fil3); //x10
  y = 0;
  if (F4[j] > 0) y = C4[j] / F4[j];
  fputs(TD4, fil3);
  ftoa(y, temp3, 5);
  fputs(FE1, fil3);
  fputs(temp3, fil3);
  fputs("</TD>", fil3); //pf

  fputs("</TR>", fil3);
  fputs(CRLF, fil3);

  if (debug == 1) printf("\n\r end report\n\r");
  //end report
  fputs("</TD>", fil3);
  fputs("</TR>", fil3);
  fputs("</TABLE>", fil3);
  fputs("</BODY>", fil3);
  fputs("</HTML>", fil3);
  fclose(fil3);
  exit(EXIT_SUCCESS);
}