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


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


   FILE *fp;
   FILE *tp;
 
   char buf1[80];
   char buf2[512];
   char buf3[512];
   char buf4[512];
   char buf5[512];
   char buf6[512];
   char buf7[512];
   
   char line1[512];
   char line2[512];
   char line3[512];
   char line4[512];
   char line5[512];
   char line6[512];
   char line7[512];
   char line8[512];
   char line9[512];
   char line10[512];
   char line11[512];
   char line12[512];
   char line13[512];
   char line14[512];
   char line15[512];
   char line16[512];
   
   char buf9[512];
   char buf10[512];

   char l1[512];
   char l2[512];
   char l3[512];
   char l4[512];
   char l5[512];
   char l6[512];
   char l7[512];
   char l8[512];
   char l9[512];
   char l10[512];
   char l11[512];
   char l12[512];
   char l13[512];
   char l14[512];
   char l15[512];
   char l16[512];
   char l17[512];

   char buf12[512];
   char *scom_line[100];

   char buf13[512];
   char *ncom_line[100];
 
   char buf14[512];
   char outfile[30];
   
   int nlhead;
   int ffi;
   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 time[6000];

   int iv[20000];
   int auxv[20000][13];
   int pv[20000][13];
   int store1[20000];
   int store2[20000][14];
   int store3[20000][13];
   int x,y;
   int select[13];
   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 pv_scales[13];
   char pv_parms[14][180];
   char auxv_parms[14][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. CLOUD DROPLET CONCENTRATION (PMS FSSP-100X (DROPLETS 2-47 MICRONS [number per cubic centimeter]");
  strcpy(pv_parms[2],"2. CLOUD DROPLET CONCENTRATION (PMS OAP-200X (DROPLETS 20-320)) [number per liter]"); 
  strcpy(pv_parms[3],"3. LIQUID WATER CONTENT (PMS FSSP-100X (DROPLETS 2-47 MICRONS)) [grams per cubic meter]"); 
  strcpy(pv_parms[4],"4. LIQUID WATER CONTENT (PVM-100A) [grams per cubic meter]");
  strcpy(pv_parms[5],"5. LIQUID WATER CONTENT (KING) [grams per cubic meter]");
  strcpy(pv_parms[6],"6. FFECTIVE RADIUS (FSSP/OAP) [microns]");
  strcpy(pv_parms[7],"7. EFFECTIVE RADIUS (PVM) [microns]");
  strcpy(pv_parms[8],"8. CONDENSATION NUCLEI [GE CNC I (PARTICLES > 0.01 MICRONS)) number per cubic centimeter]");
  strcpy(pv_parms[9],"9. CONDENSATION NUCLEI [TSI MODEL 3760 CNC III (PARTICLES > 0.003 MICRONS)) [number per cubic centimeter]");
  strcpy(pv_parms[10],"10. PARTICLE CONCENTRATION (PCASP-100X (PARTICLES 0.1-3.0 MICRONS)) [number per cubic centimeter]");
  strcpy(pv_parms[11],"11. DRY LIGHT SCATTERING AT 540 NM (MRI MODEL 1567 (IN-HOUSE MODIFIED INTEGRATING NEPHELOMETER) [per meter pathlength]"); 
  strcpy(pv_parms[12],"12. AMBIENT HUMIDITY LIGHT SCATTERING AT 537 NM (IN-HOUSE INTEGRATING 537 NM (IN-HOUSE INTEGRATING))"); 
  strcpy(pv_parms[13],"13. OZONE CONCENTRATION (MONITOR LABS MODEL 8410A ETHYLENE CHEMI-LUMINESCENCE) [ppbv]"); 
}   

void print_pv()
{
  int m;
  m=0;
  set_pv_parms();
  for (m=0;m<14;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. RADAR ALITIUDE (AN/APN22) [meters above terrain]");
  strcpy(auxv_parms[3],"3. GPS LATITUDE (TRIMBLE TNL-3000) [decimal degrees]");
  strcpy(auxv_parms[4],"4. GPS LONGITUDE (TRIMPLE TNL-3000) [decimal degrees]");
  strcpy(auxv_parms[5],"5. STATIC PRESSURE (ROSEMOUNT MODEL 830BA VARIABLE CAPACITANCE) [millibars]");
  strcpy(auxv_parms[6],"6. STATIC TEMPERATURE (ROSEMOUNT MODEL 102CY2CG PLATINUM WIRE RESISTANCE) [degrees C]");
  strcpy(auxv_parms[7],"7. DEWPOINT/FROSTPOINT (CAMBRIDGE SYSTEMS MODEL TH73-244 CHILLED-MIRROR) [degrees C]");
  strcpy(auxv_parms[8],"8. UV RADIATION FROM ABOVE (EPPLEY LAB. INC. MODEL 14042 PHOTO-CELL (295-390 NANOMETERS)) [watts per square meter]");
  strcpy(auxv_parms[9],"9. UV RADIATION FROM BELOW (EPPLEY LAB. INC. MODEL 14042 PHOTO-CELL(295-390 NANOMETERS)) [watts per square meter]))");
  strcpy(auxv_parms[10],"10. VISIBLE RADIATION FROM ABOVE (EPPLEY LAB. INC. MODEL PSP THERMOPILE(0.3-3.0 MICRONS)) [watts per square meter]");
  strcpy(auxv_parms[11],"11. VISIBLE RADIATION FROM BELOW (EPPLEY LAB. INC. MODEL PSP THERMOPILE(0.3-3.0 MICRONS)) [watts per square meter]");
  strcpy(auxv_parms[12],"12. TRUE AIRSPEED (ROSEMOUNT MODEL 831BA VARIABLE CAPACITANCE) [meters per second]");
  strcpy(auxv_parms[13],"13. TRUE HEADING [degrees clockwise from true North]");
}
 

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



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",&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,100,fp);
   fscanf(fp,"%d\n",&npv);

   fgets(buf6,100,fp);

  
   fgets(buf7,100,fp);
 
   fgets(line1,100,fp);
   fgets(line2,100,fp);
   fgets(line3,100,fp);
   fgets(line4,100,fp);
   fgets(line5,100,fp);
   fgets(line6,100,fp);
   fgets(line7,100,fp);
   fgets(line8,100,fp);
   fgets(line9,100,fp);
   fgets(line10,100,fp);
   fgets(line11,100,fp);
   fgets(line12,100,fp);
   fgets(line13,100,fp);
   fgets(line14,100,fp);
   fgets(line15,100,fp);
   fgets(line16,100,fp);

   fscanf(fp,"%d\n",&nauxv);
   fgets(buf9,100,fp);
   fgets(buf10,100,fp);

   fgets(l1,100,fp);
   fgets(l2,100,fp);
   fgets(l3,100,fp);
   fgets(l4,100,fp);
   fgets(l5,100,fp);
   fgets(l6,100,fp);
   fgets(l7,100,fp);
   fgets(l8,100,fp);
   fgets(l9,100,fp);
   fgets(l10,100,fp);
   fgets(l11,100,fp);
   fgets(l12,100,fp);
   fgets(l13,100,fp);
   fgets(l14,100,fp);
   fgets(l15,100,fp);
   fgets(l16,100,fp);
   fgets(l17,100,fp);
  
   fscanf(fp,"%d\n",&nscoml);
   /*  printf("%d\n",nscoml);  */
   if (nscoml > 0)
   {    
   for (i=0;i<nscoml;i++)
     {
       fgets(buf13,sizeof(buf13),fp);
       scom_line[i]=(char *)malloc(sizeof(buf13));
       strcpy(scom_line[i], buf13);
     }
   }
   fscanf(fp,"%d\n",&ncoml);
   /*printf("%d\n",ncoml);*/
   if (ncoml > 0) 
   {     
     for (i=0;i<ncoml;i++)
     {
       fgets(buf12,sizeof(buf12),fp);
       ncom_line[i]=(char *)malloc(sizeof(buf12));
       strcpy(ncom_line[i], buf12);
     }
   }

   /* Reading data */
   /*printf("Reading data\n");*/
 
   i=0;
   j=0;
   x=0;
   num_recs=0;
   while (!feof(fp))
   {   
     fscanf(fp,"%d",&iv[num_recs]);
       store1[num_recs]=iv[num_recs];
     for (i=0;i<13;i++)
     {
        fscanf(fp,"%d",&auxv[num_recs][i]);
        store2[num_recs][i]=auxv[num_recs][i];
     }
     for (i=0;i<13;i++)
     { 
        fscanf(fp,"%d",&pv[num_recs][i]);
        store3[num_recs][i]=pv[num_recs][i];
     }
      num_recs++;  
   }
   
   /*printf("%d\n",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]);
            
             
	   }

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

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

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

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

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

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

          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]);
            
             
	   }

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

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

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

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

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

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

        fprintf(tp,"\n");
        fprintf(tp,"************************************************************************\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\n","(The ffi for these files is 1010 indicating one independent variable)");
   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: ");
   fprintf(tp,"%s\n",buf6);
   fprintf(tp,"%s\n","Array of values used to indicate missing or erroneous data,");
   fprintf(tp,"%s\n","one for each primary variable:");
   fprintf(tp,"%s\n",buf7);
   fprintf(tp,"%s\n\n","Array of names/descriptions, one for each primary variable: ");
 
  fprintf(tp,"%s",line1);
  fprintf(tp,"%s",line2);
  fprintf(tp,"%s",line3);
  fprintf(tp,"%s",line4);
  fprintf(tp,"%s",line5);
  fprintf(tp,"%s",line6);
  fprintf(tp,"%s",line7);
  fprintf(tp,"%s",line8);
  fprintf(tp,"%s",line9);
  fprintf(tp,"%s",line10);
  fprintf(tp,"%s",line11);
  fprintf(tp,"%s",line12);
  fprintf(tp,"%s",line13);
  fprintf(tp,"%s",line14);
  fprintf(tp,"%s",line15);
  fprintf(tp,"%s",line16);
  

   fprintf(tp,"%s %d\n","Number of auxiliary variables: ",nauxv);
   fprintf(tp,"%s\n","Array of scale factors, one for each auxiliary variable:");
   fprintf(tp,"%s",buf9);
   fprintf(tp,"%s","Array of values used to indicate missing or erroneous data,");
   fprintf(tp,"%s\n","one for each auxiliary variable:");
   fprintf(tp,"%s",buf10);
   fprintf(tp,"%s\n\n","Array of names/descriptions, one for each auxiliary variable: ");   
 
   fprintf(tp,"%s",l1);
   fprintf(tp,"%s",l2);
   fprintf(tp,"%s",l3);
   fprintf(tp,"%s",l4);
   fprintf(tp,"%s",l5);
   fprintf(tp,"%s",l6);
   fprintf(tp,"%s",l7);
   fprintf(tp,"%s",l8);
   fprintf(tp,"%s",l9);
   fprintf(tp,"%s",l10);   
   fprintf(tp,"%s",l11);
   fprintf(tp,"%s",l12);
   fprintf(tp,"%s",l13);
   fprintf(tp,"%s",l14);
   fprintf(tp,"%s",l15);
   fprintf(tp,"%s",l16);
   fprintf(tp,"%s",l17);

   fprintf(tp,"%s %d\n","Number of special comment lines: ",nscoml);
   if (nscoml > 0)
   {
     fprintf(tp,"%s\n\n","Array of special comment lines:");
     for (i=0;i<nscoml;i++)
     {
        fputs(scom_line[i],tp);
     }
   }
   fprintf(tp,"%s %d\n","Number of normal comment lines: ",ncoml);
   if (ncoml > 0) 
   {     
     fprintf(tp,"%s\n\n","Array of normal comment lines:");
     for (i=0;i<ncoml;i++)
     {
       fputs(ncom_line[i],tp);
     }
   }

   /* Printing data */
  
   for (x=0;x<num_recs-1;x++)
   {
      fprintf(tp,"%d ",store1[x]);
      for (i=0;i<13;i++)
      {
         fprintf(tp,"%d ",store2[x][i]);
      }
      fprintf(tp,"\n");
      for (i=0;i<13;i++)
      {  
        fprintf(tp,"%d ",store3[x][i]);
      }
      
      fprintf(tp,"\n");
      
   }  
  
}




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

