/************************************************************/
/*                                                          */
/*  This program reads the TARFOX uwc131a_bag data file.    */
/*                                                          */
/************************************************************/


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


   FILE *fp;
   FILE *tp;
 
   char *store[14];
   char line[512];
   char buf5[512];
   char buf6[512];
   char buf7[512];
   char buf8[512];

   char buf9[512];
   char buf10[512];
   char buf11[512];
   char buf12[512];
   char buf13[512];
   char buf14[512];

   char buf15[512];
   char buf16[512];
   char buf17[512];
   char buf18[512];
   char buf19[512];
   char buf20[512];
   char buf21[512]; 
   char buf22[512];
   
 
   int nlhead;
   int ffi;
   char buf1[80];
   char buf2[512];
   char buf3[512];
   char buf4[512];

   int ivol;
   int nvol;

   int dx;
   int npv;
   int nauxv;
   int nscoml;
   int ncoml;
   int y1;
   int y2;
   int m1;
   int m2;
   int d1;
   int d2;

   int iv[20000];
   int auxv[20000][6];
   int pv[20000][6];
   int ans;

   int store1[20000];
   int store2[20000][6];
   int store3[20000][6];
   int x,y;
   int select[6];
   int num;
   int num_recs;
   int num_sel;
   int i,j,k,n;
   char data_file[30];
   char out_file[30];
   char instring[80];
   char *currentpos, *lastpos;
   int ans;
   float scale[6];

   float pv_scales[6];
   float auxv_scales[6];

   char pv_parms[6][180];
   char auxv_parms[6][180];

   void get_file();
   void read_file();
   void print_file();
   void custom_menu();
   void set_pv_parms();
   void set_auxv_parms();
   void print_pv();
   void print_auxv();


void get_file()
{ 
   printf("Please enter name of data file: ");
   scanf("%s",data_file);
   printf("%s\n",data_file);
   fp=fopen(data_file,"r");
   strcat(data_file,".rpt");
   strcpy(out_file,data_file);
   printf("%s %s\n","Name of report file: ",out_file);
   tp=fopen(out_file,"w");
}


void set_pv_parms()
{
  strcpy(pv_parms[0],"PRIMARY VARIABLES:");
  strcpy(pv_parms[1],"1. LIGHT SCATTERING AT 450 NM ");
  strcpy(pv_parms[2],"2. LIGHT SCATTERING AT 550 NM ");
  strcpy(pv_parms[3],"3. LIGHT SCATTERING AT 750 NM ");
  strcpy(pv_parms[4],"4. BACKSCATTERING AT 450 NM ");
  strcpy(pv_parms[5],"5. BACKSCATTERING AT 550 NM ");
  strcpy(pv_parms[6],"6. BACKSCATTERING AT 750 NM ");
}


void print_pv()
{
  int m;
  m=0;
  set_pv_parms();
  for (m=0;m<7;m++)
  {
   printf("%s\n",pv_parms[m]);
  }
}

void set_auxv_parms()
{ 
  strcpy(auxv_parms[0],"AUXILIARY VARIABLES:");
  strcpy(auxv_parms[1],"1. TIME IN HHMMSS FORMAT [UTC]");
  strcpy(auxv_parms[2],"2. GPS LATITUDE (TRIMBLE TNL-3000) [decimal degrees]");
  strcpy(auxv_parms[3],"3. GPS LONGITUDE (TRIMPLE TNL-3000) [decimal degrees]");
  strcpy(auxv_parms[4],"4. STATIC PRESSURE (ROSEMOUNT MODEL 830BA VARIABLE CAPACITANCE) [millibars]");
  strcpy(auxv_parms[5],"5. RELATIVE HUMIDITY OF SAMPLE STREAM FOR MS ELECTRON INTEGRATING 3-WAVELENGTH NEPHELOMTER) [percent]");
  strcpy(auxv_parms[6],"6. BAG SWITCH [1 means valve-open/bag-filling; 0 means valve-closed]");
}

void print_auxv()
{
  int m;
  m=0;
  set_auxv_parms();
  for (m=0;m<7;m++)
  {
   printf("%s\n",auxv_parms[m]);
  }
}
  


void read_file()
{
   int i;

   printf("Reading file...\n");
   fscanf(fp,"%d",&nlhead);
   fscanf(fp,"%d\n",&ffi);  
   fgets(buf1,80,fp);
   fgets(buf2,80,fp);
   fgets(buf3,80,fp);
   fgets(buf4,80,fp);
   fscanf(fp,"%d",&ivol);
   fscanf(fp,"%d\n",&nvol);
   fscanf(fp,"%d",&y1);
   fscanf(fp,"%d",&m1);
   fscanf(fp,"%d",&d1);
   fscanf(fp,"%d",&y2);   
   fscanf(fp,"%d",&m2);   
   fscanf(fp,"%d\n",&d2);
   fscanf(fp,"%d\n",&dx); 
   fgets(buf5,180,fp);
   fscanf(fp,"%d\n",&npv);
  
   for (i=0;i<6;i++)
    fscanf(fp,"%e",&pv_scales[i]);
   fscanf(fp,"\n");

   /*   fgets(buf6,180,fp);  */
   fgets(buf7,180,fp);

   fgets(buf8,180,fp);
   fgets(buf9,180,fp);
   fgets(buf10,180,fp);
   fgets(buf11,180,fp);
   fgets(buf12,180,fp);
   fgets(buf13,180,fp);
   fscanf(fp,"%d\n",&nauxv);

   fgets(buf14,180,fp);
   fgets(buf15,180,fp);
   fgets(buf16,180,fp);
   fgets(buf17,180,fp);
   fgets(buf18,180,fp);
   fgets(buf19,180,fp);
   fgets(buf20,180,fp);
   fgets(buf21,180,fp);
   fscanf(fp,"%d\n",&nscoml);
   fscanf(fp,"%d\n",&ncoml);

   for (i=0;i<14;i++)
   {
    fgets(line, sizeof(line), fp);
    store[i] = (char *)malloc(sizeof(line));
    strcpy(store[i], line);
   }

   /* reading data*/
   while (!feof(fp))
   {   
     fscanf(fp,"%d",&iv[num_recs]);
       store1[num_recs]=iv[num_recs];
     for (i=0;i<6;i++)
     {
        fscanf(fp,"%d",&auxv[num_recs][i]);
        store2[num_recs][i]=auxv[num_recs][i];
     }
     for (i=0;i<6;i++)
     { 
        fscanf(fp,"%d",&pv[num_recs][i]);
        store3[num_recs][i]=pv[num_recs][i];
     }
      num_recs++;  
   }
   printf("Finished reading\n");
   printf("%s %d\n\n","Number of records: ",num_recs);
   
}



void custom_menu()
{
 
   int i,j,k;
   int sel_num;
   ans=0;
   sel_num=0;
   printf("If you want all the data written to output file, enter 1\n");
   printf("To select primary variables enter 2\n");
   printf("To select auxiliary variables enter 3\n");

   scanf("%d",&ans);
   /*printf("%d\n",ans);*/
   if (ans == 1)
   {
      print_file();
   }
   

    if (ans == 2)
   { 
     
     i=0;
     read_file();
     print_auxv();

     printf("Output of each selection is written to output file in the same order.\n");
     printf("Enter the number(s) of the auxiliary variable(s) to view: \n");
     printf("Enter -1 when selection is done: \n");
    
     while (sel_num != -1)
     { scanf("%d",&sel_num);
     /*printf("%d",sel_num);*/
       select[i]=sel_num;
       i++;
     }
     num_sel=i-1;
     
     /*  for (j=0;j<num_sel;j++)
       printf("%d ",select[j]);
     printf("\n");*/


    for (k=0;k<num_recs-1;k++)
    {
       for (j=0;j<num_sel;j++)
       {
           if (select[j]==1)
           {
             fprintf(tp,"%s   %s\n","INDEPENDENT VARIABLE  ",auxv_parms[1]);                     
             fprintf(tp,"      %d                        %d\n",iv[k],store2[k][0]);
	   }
          

           if (select[j]==2)
           {
             fprintf(tp,"%s   %s\n","INDEPENDENT VARIABLE  ",auxv_parms[2]);
             fprintf(tp,"      %d                        %d\n",iv[k],store2[k][1]);
	   }

           if (select[j]==3)
           {
             fprintf(tp,"%s   %s\n","INDEPENDENT VARIABLE  ",auxv_parms[3]);
            
             
               fprintf(tp,"      %d                        %d\n",iv[k],store2[k][2]);
            
             
	   }
          
           if (select[j]==4)
           {
             fprintf(tp,"%s   %s\n","INDEPENDENT VARIABLE  ",auxv_parms[4]);
           
             
               fprintf(tp,"      %d                        %d\n",iv[k],store2[k][3]);
            
             
	   }

           if (select[j]==5)
           {
             fprintf(tp,"%s   %s\n","INDEPENDENT VARIABLE  ",auxv_parms[5]);
            
             
               fprintf(tp,"      %d                        %d\n",iv[k],store2[k][4]);
            
             
	   }

           if (select[j]==6)
           {
             fprintf(tp,"%s   %s\n","INDEPENDENT VARIABLE  ",auxv_parms[6]);
            
             
               fprintf(tp,"      %d                        %d\n",iv[k],store2[k][5]);
            
             
	   }

        fprintf(tp,"\n");
        fprintf(tp,"************************************************************************\n");
        fprintf(tp,"\n");
       }
    }

  }



    if (ans == 3)
    {
    

     i=0;
     read_file();
     print_pv();
     printf("Output of each selection is written to output file in the same order.\n\n");
     printf("Enter the number(s) of the auxiliary variable(s) to view: \n");
     printf("Enter -1 when selection is done: \n");
    
     while (sel_num != -1)
     { scanf("%d",&sel_num);
     /*printf("%d",sel_num);*/
       select[i]=sel_num;
       i++;
     }
     num_sel=i-1;
     /*printf("%d\n",num_sel);*/
     
     /*  for (j=0;j<num_sel;j++)
       printf("%d ",select[j]);
     printf("\n");*/


  for (k=0;k<num_recs-1;k++)
  {
      for (j=0;j<num_sel;j++)
       {
           if (select[j]==1)
           {
             fprintf(tp,"%s   %s\n","INDEPENDENT VARIABLE  ",pv_parms[1]);
             
            
               fprintf(tp,"      %d                        %d\n",iv[k],store3[k][0]);
            
            
	   }
          

           if (select[j]==2)
           {
             fprintf(tp,"%s   %s\n","INDEPENDENT VARIABLE  ",pv_parms[2]);
          
            
               fprintf(tp,"      %d                        %d\n",iv[k],store3[k][1]);
            
            
	   }

           if (select[j]==3)
           {
             fprintf(tp,"%s   %s\n","INDEPENDENT VARIABLE  ",pv_parms[3]);
        
             
               fprintf(tp,"      %d                        %d\n",iv[k],store3[k][2]);
            
             
	   }
          
           if (select[j]==4)
           {
             fprintf(tp,"%s   %s\n","INDEPENDENT VARIABLE  ",pv_parms[4]);
           
             
               fprintf(tp,"      %d                        %d\n",iv[k],store3[k][3]);
            
             
	   }

           if (select[j]==5)
           {
             fprintf(tp,"%s   %s\n","INDEPENDENT VARIABLE  ",pv_parms[5]);
          
             
               fprintf(tp,"      %d                        %d\n",iv[k],store3[k][4]);
            
             
	   }

           if (select[j]==6)
           {
             fprintf(tp,"%s   %s\n","INDEPENDENT VARIABLE  ",pv_parms[6]);
           
             
               fprintf(tp,"      %d                        %d\n",iv[k],store3[k][5]);
            
             
	   }
	fprintf(tp,"\n");
        fprintf(tp,"*************************************************************************8\n");
        fprintf(tp,"\n");
       }
     }
   }


}




void print_file()
{
   int i,x;
  
   read_file();
 
   fprintf(tp,"%s %d\n","Number of lines in file header:",nlhead);
   fprintf(tp,"%s %d\n","File format index: ",ffi);
   fprintf(tp,"%s %s","Originator of data file: ",buf1);
   fprintf(tp,"%s %s","Organization of the data file originator: ",buf2);
   fprintf(tp,"%s %s","Source of the measurements (instrument name): ",buf3);
   fprintf(tp,"%s %s","Mission the data is supporting: ",buf4);

   fprintf(tp,"%s %d\n","Volume number out of total number of volumes: ",ivol);
   fprintf(tp,"%s %d\n","Total number of volumes required to store the complete dataset: ",nvol);

   fprintf(tp,"%s %d %d %d\n","UT date at which the data file begins: ",y1,m1,d1);
   fprintf(tp,"%s %d %d %d\n","Date of data reduction or revision: ",y2,m2,d2);

   fprintf(tp,"%s %d\n","Interval between values of the independent variable: ",dx);
   fprintf(tp,"%s",buf5);
   fprintf(tp,"%s %d\n","Number of primary variables in the data file: ",npv);
   fprintf(tp,"%s\n","Array of scale factors, one for each primary variable: ");

   for (i=0;i<6;i++)
    fprintf(tp,"%1.1E ",pv_scales[i]);
   fprintf(tp,"\n");

   /*   fprintf(tp,"%s\n",buf6);*/
   fprintf(tp,"%s\n","Array of values used to indicate missing or erroneous data:");
   fprintf(tp,"%s\n",buf7);
   fprintf(tp,"%s\n",buf8);

   fprintf(tp,"%s",buf9);
   fprintf(tp,"%s",buf10);
   fprintf(tp,"%s",buf11);
   fprintf(tp,"%s",buf12);
   fprintf(tp,"%s",buf13);
   fprintf(tp,"%d\n",nauxv);

   fprintf(tp,"%s",buf14);
   fprintf(tp,"%s",buf15);
   fprintf(tp,"%s",buf16);
   fprintf(tp,"%s",buf17);
   fprintf(tp,"%s",buf18);
   fprintf(tp,"%s",buf19);
   fprintf(tp,"%s",buf20);
   fprintf(tp,"%s",buf21);

   fprintf(tp,"%d\n",nscoml);
   fprintf(tp,"%d\n",ncoml);
    
   for (i=0;i<14;i++)
     fputs(store[i], tp);
   
  
   for (x=0;x<num_recs-1;x++)
   {
      fprintf(tp,"%d ",store1[x]);
      for (i=0;i<6;i++)
      {
         fprintf(tp,"%d ",store2[x][i]);
      }
      fprintf(tp,"\n");
      for (i=0;i<6;i++)
      {  
        fprintf(tp,"%d ",store3[x][i]);
      }
      
      fprintf(tp,"\n");
      
   }  
 
}

void main()
{
  get_file();
  custom_menu();
  close(fp);
  close(tp);
}






      
