/************************************************************************
*                                                                       *
* PROGRAM:   bio_burn_res_fuel_read.c                                   *
*                                                                       *
* VERSION:   1.1                       DATE:  December 31, 1996         *
* VERSION:   2.0                       DATE:  January 31, 1999          *
*                                                                       *
* PURPOSE:   The purpose of this code is to read the Biomass            *
*            Burning Tropical data set by Dr. Wei Min Hao.              *
*            This sample read software concentrates on the residues and *
*            fuel data set granules.                                    *
*                                                                       *
* ALGORITHM: None.                                                      *
*                                                                       *
* INVOCATION:    read_5x5_hao_res_fuel datafile                         *
*      WHERE:                                                           *
*          <datafile> - optional                                        *
*                       input ASCII data file                           *
*                                                                       *
* FILE/RECORD REFERENCES:                                               *
*            Currently, this program will produce no output files.  ALL *
*            output will be printed to the screen.                      *
*                                                                       *
* EXTERNAL ROUTINES:  None.                                             *
*                                                                       *
* INTERNAL ROUTINES:                                                    *
*            #include <stdio.h>                                         *
*            #include <stdlib.h>                                        *
*            #include <string.h>                                        *
*                                                                       *
* NOTES:    This program has been set up to read all files which        *
*           pertain to the Tropical data set.  Each file has a          *
*           different layout.  All files are in ASCII.                  *
*           The read program is able to identify which location the     *
*           user would like to read.  The locations are Africa,         *
*           America, and Asia.                                          *
*           This read software reads the residues and fuel granules.    *
*           These files consist of the first four lines of text         *
*           information.  This information pretains to a particular     *
*           file. The rest of the file consists of actual data.         *
*           There are three parameters in these granules:               *
*             1.  Coordinate (latitude and longitude value)             *
*             2.  Agricultural residues burned annually                 *
*                               OR                                      *
*                 Fuelwood burned annually                              *
*             3.  Agricultural residues monthly                         *
*                               OR                                      *
*                 Fuelwood burned monthly                               *
*                                                                       *
* CONTACT INFORMATION: If you have any questions with the data or       *
*            software, please contact:                                  *
*            EOSDIS Langley DAAC                                        *
*            Science, Users and Data Services                           *
*            NASA Langley Research Center                               *
*            Mail Stop 157D                                             *
*            2 South Wright Street                                      *
*            Hampton, Virginia 23681-2199                               *
*            U.S.A.                                                     *
*                                                                       *
*            Telephone: (757) 864-8656                                  *
*            FAX:       (757) 864-8807                                  *
*            E-mail:    larc@eos.nasa.gov                               *
*                                                                       *
* REVISION INFORMATION:                                                 *
* 12-03-1996 Sample read software files were renamed.  This only        *
*            the comment section of the software.  The makefile for     *
*            this code was also changed.                                *
*                                                                       *
* 01-31-1999 Updated code with new address change wording of some code. *
*                                                                       *
************************************************************************/

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

#define LINESIZE 81
#define NUM_PARAMS 3

main(argc,argv)
int argc;
char *argv[];
{
  char buf[LINESIZE],           /* buffer to hold text line               */ 
       coord[12],               /* coordinate value                       */
       fname[LINESIZE],         /* data file name given in command line   */
       in_file[LINESIZE],       /* data file to be read                   */
       list[3],                 /* stores user's input from keyboard      */
       tmp[LINESIZE];           /* place holding for the param selection  */

  int  c,                       /* counter                                */
       count,                   /* counter                                */
       counter,                 /* counter                                */
       i,                       /* counter                                */
       j,                       /* counter                                */
       params[NUM_PARAMS];      /* integer format for params selected     */

  float  annual,                /* annual data value                      */
         month;                 /* monthly data value                     */

  FILE *fptr;                   /* file pointer to input data file        */

  void Display_greeting();

   /* Initialize the params array to 0 */

   for (count=0; count < NUM_PARAMS; count++)
      params[count] = -99;

   (void) Display_greeting();

  /* Get name of data file to be read */

  if (argc == 1) 
  {
    printf("Please enter the name of the data file to be read:  ");
    scanf("%s",fname);
    strcpy(in_file,fname);
  }
  else
  {
    strcpy(in_file,argv[1]);
  }

  printf("\n\n");
  printf("The data file to be read is %s \n", in_file);

  /* Open data file to read */

  fptr = fopen(in_file,"r");

  /* Check to see if eof encountered  */

  if(fptr == NULL)
  {
    printf("**********************************************************\n");
    printf("*                                                        *\n");
    printf("*           ERROR -- UNABLE TO OPEN THE DATA SET         *\n");
    printf("*                   PROGRAM TERMINATED                   *\n");
    printf("*                                                        *\n");
    printf("**********************************************************\n");
    exit(-1);
  }

  /* Print the parameters available on this data file  */

  printf("\n\n");
  printf("The following are the parameters on this data file: \n");
  printf("\n\n");
  printf("No.   Parameter               Units \n");
  printf("---   ---------               ----- \n");
  printf("\n");
  printf(" 1.   Coordinate              Degrees\n");
  printf(" 2.   Annual Value            x 10^3 Tons/Year\n");
  printf(" 3.   Monthly Value           x 10^3 Tons/Month\n");
  printf("\n\n");
  
  /*  Selection of parameters whether all or some  */

  list[0] = 'Q';
  while ((list[0]!='a') && (list[0]!='A') && (list[0]!='s') && (list[0]!='S') && (list[0]!='x') && (list[0]!='X'))
  {
    printf(" Do you want all parameters to be printed \n");
    printf(" Please select: a or A for All; S for Some; X to eXit: ");
    scanf("%s",tmp); 
    strcpy(list,tmp); 
    printf("\n\n");
  }  /* end of while statement */

  if ((list[0] == 'a') || (list[0] == 'A'))
  {
    for(count=0; count < NUM_PARAMS; count++)
      params[count] = count;
  }
  else if ((list[0] == 's') || (list[0] == 'S'))
  { 
    printf("****  Selection of parameters  ***\n");
    tmp[0] = 'S';
    count = 0;
    while(((tmp[0] != 'x') && (tmp[0] != 'X') || (tmp[0] != 'q') && (tmp[0] != 'Q')) && (count < NUM_PARAMS))
    {
       printf("Please enter parameter# you wish to view \n");
       printf("Enter q to end selection process or X to exit the program--> ");
       scanf("%s",tmp);
       strcpy(list,tmp);

       if ((list[0] == 'q') || (list[0] == 'Q'))
       {
         if(params[0] == -99)
         {
           strcpy(tmp,"    ");             
           printf("\n");
           printf(" Selection of parameters have not yet been made \n");
           printf(" Do you want to exit the program -- enter x or X \n");
           printf(" OR Enter parameters numbers \n");
           scanf("%s",tmp);
           strcpy(list,tmp);
         }
         else break;
       }

       if ((list[0] == 'x') || (list[0] == 'X'))
       {
         printf("\n\n");
         printf(" **** EXITING THE PROGRAM PER YOUR REQUEST ****\n");
         printf("\n\n");
         exit(0);
       }

       if((atoi(list) > NUM_PARAMS) || (atoi(list) <= 0))
       {
         printf("\n");
         printf("A NUMBER NOT AVAILABLE FOR PARAMETER SELECTION HAS BEEN ENTERED\n");
         strcpy(tmp,"    ");
       }
       else
       {
         params[count] = atoi(list) - 1;
         count++;
       }
    }                                      /* end of the for statement */
  }                                        /* end of the if-else statement */
  else if ((list[0] = 'x') || (list[0] = 'X'))
  {
   printf("\n\n");
   printf(" **** EXITING THE PROGRAM PER YOUR REQUEST *****\n");
   exit(0);
  }


  /* Read the first four lines of the data file since this is header info */
  /* Currently, this information is not being printed.                    */

  for(i=1; i<= 4; i++)
     fgets(&buf[0],LINESIZE,fptr);

  /* Print the headers for the requested parameters */

  for (i = 1; i <= count; i++)
  {
     switch (params[i-1])
     {
	case 0:
          printf(" Coordinate  ");
          break;
        case 1:
          printf("  Annual    ");
          break;
        case 2:
          printf("   Monthly  ");
          break;
        default:
          break;
     }         /* end of case statement */
  } /* end of the for loop */
  printf("\n");

  counter = 0;         /* counter is for # of records read */

  /* This while loop read all data records until the EOF. */

  while ((feof(fptr) == 0))
  {
    annual     = 0.0;
    month      = 0.0;
    
    strcpy(buf," ");

    /* Reads each line from input file */

    fscanf(fptr,"%[^\n]%c",buf,tmp);

    /*  End of file encountered */

    if (feof(fptr) != 0)
    {
      printf("\n\n");
      printf("****************************************************\n");
      printf("*                                                  *\n");
      printf("*              END OF FILE ENCOUNTERED             *\n");
      printf("*                                                  *\n");
      printf("*          %d records of data were read           *\n",counter);
      printf("*                                                  *\n");
      printf("*  EXECUTION OF PROGRAM COMPLETED AND SUCCESSFUL   *\n");
      printf("*                                                  *\n");
      printf("****************************************************\n");
      exit(0);
    }

    for (i=0; i<11; i++)
     coord[i] = buf[i]; 

    for (i=0; i<10; i++)
     tmp[i] = buf[11+i];
    annual = atof(tmp);

    for (i=0; i<10; i++)
     tmp[i] = buf[21+i];
    month = atof(tmp);


    /*  Print the parameters selected to the screen */

    for (i = 0; i < count; i++)
    {
       switch (params[i])
       {
           case 0:
             for (j=0; j<11; j++)
                printf("%c",coord[j]);
             break;
           case 1:
             printf("%10.2f  ",annual);
             break;
           case 2:
             printf("%10.2f  ",month);
             break;
           default:
             break;
       }         /* end of case statement */
    }    /* end of the for-statement  */

    if (i == count)
      printf("\n");

    counter++;

  }  /* end of the while statement */

} /* end of the main program */


void Display_greeting()
  {
   printf("***************************************************************\n");
   printf("*                                                             *\n");
   printf("*               BIOMASS BURNING TROPICAL DATA                 *\n");
   printf("*      Agricultural Residues Burned and Fuelwood Burned       *\n");
   printf("*                                                             *\n");
   printf("*                        Read Program                         *\n");
   printf("*                                                             *\n");
   printf("* Version 2.0                               January 31, 1999  *\n");
   printf("***************************************************************\n");
   printf("\n\n");
 }  /* end of the display greeting */
