/************************************************************************
*                                                                       *
* PROGRAM:   erbe_tsi_erbs_read.c                                       *
*                                                                       *
* VERSION:   4.0                       DATE:  May 24, 1999              *
*                                                                       *
* PURPOSE:   The purpose of this code is to output the ERBE Total       *
*            Solar Irradiance data set as requested by the user.        *
*                                                                       *
* ALGORITHM: None.                                                      *
*                                                                       *
* INVOCATION:    erbe_tsi_erbs_read                                     *
*      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: There are four parameters of in this data set; date (yy/mm/dd),*
*        time (hr:mm:sc), solar irradiance (watts per meter squared),   *
*        standard deviation (watts per meter squared).                  *
*        The parameter(s) information is as follows:                    *
*        Parameter        Minimum        Maximum         Format         *
*        ---------        -------        -------         ------         *
*        Date:  YY          84             99            Integer        *
*               MM          01             12            Integer        *
*          for 1984         10             12            Integer        *
*               DD          01             31            Integer        *
*          for 84/10        25             29            Integer        *
*        Time:  HR          00             23            Integer        *
*      for 84/10/25  (HR)   15             15            Integer        *
*               MM          00             59            Integer        *
*      for 84/10/25  (MM)   24             24            Integer        *
*               SC          00             59            Integer        *
*      for 84/10/25  (SC)   45             45            Integer        *
*      Assumptions made for the following parameters:                   *
*        IRR              1363.0         1368.0          Real           *
*        STD                 0.0            2.0          Real           *
*                                                                       *
* Contact Information:  If you have any questions, please contact:      *
*       Langley DAAC User and Data Services                             *
*       Mail Stop 157D                                                  *
*       NASA Langley Research Center                                    *
*       Hampton, VA 23681-2199                                          *
*                                                                       *
*       Telephone:  (757)864-8656            FAX: (757)864-8807         *
*       E-mail:     larc-asdc-uds@lists.nasa.gov                        *
*                                                                       *
************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define  SIZE   81        /* Number of characters on a line */


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

  void Greetings();            /* Declare function */

  unsigned char   buf[SIZE];  /* String used to read in data */

  char     filename[30],      /* Input file name */
           temp_name[30],     /* Input temporary name */
           letter;            /* Delimeter */

  FILE     *fptr;             /* Input file pointer */

  int      amount,            /* Number of parameters selected */
           count,             /* Counter */
           day,               /* Data collection day */
           flag,              /* Loop flag */
           hour,              /* Data collection hour */
           minute,            /* Data collection minute */
           month,             /* Data collection month */
           param[4],          /* Stores selection order */
           rec_count,         /* Record counter */
           second,            /* Data collection second */
           year;              /* Data collection year */ 

  double   irr,               /* Solar Irradiance */
           std;               /* Standard Deviation */


          /*************************************/
          /*                                   */
          /*       Begin Read Program          */
          /*                                   */
          /*************************************/

  (void) Greetings();

  /* Initialize array */
  for (count = 0; count < 4; count++)
     param[count] = -1;

  /* Prompt for file name */
  if (argc == 1)
  {
     printf("Please enter the input data file name. Include\n");
     printf("the entire path. --> ");
     scanf("%s",temp_name);
     strcpy(filename,temp_name);
  }
  else if (argc != 0)  /* File name included at command line */
  {
    strcpy(filename,argv[1]);
  }

  /* Open input file name */
  fptr = fopen(filename,"r");

  if (fptr == NULL)
  {
     printf("*****************************************\n");
     printf("* ERROR - Unable to open the data file. *\n");
     printf("*         Program has terminated.       *\n");
     printf("*****************************************\n");
     printf("\n");
     exit ( -1 );
  }

  printf("\n\nThe filename for this input data file is %s.\n",filename);

  /* Reads first line of granule.  This is just text. */
  fgets((char *) &buf[0], SIZE, fptr);

  /* Loop flag set to off */
  flag = 0;
  
  while (flag == 0)
  {
  printf("\n\n");
  printf("There are four parameters of information that were\n");
  printf("collected at each event.  These parameters are:\n");
  printf("\n");
  printf("    No.      Parameter       Units\n");
  printf("    ---      ---------       -----\n");
  printf("     1.      Date            YY/MO/DD\n");
  printf("     2.      Time            HR:MM:SC\n");
  printf("     3.      Irradiance      Watts per meter squared\n");
  printf("     4.      Standard Dev.   Watts per meter squared\n");
  printf("\n\n");

  printf("Enter 'a' or 'A' to output all parameters in the above format.\n");
  printf("Enter 'x' or 'X' to eXit this program.\n");
  printf("Enter the total number of parameters to output (1-4). --> ");
  scanf("%s",temp_name);

  if ((strcmp("a",temp_name)) == 0)
  {
     for (count = 0; count < 4; count++)
        param[count] = count+1;
     amount = 4;
     flag = 1;
  }
  else if ((strcmp("A",temp_name)) == 0)
  {
     for (count = 0; count < 4; count++)
        param[count] = count+1;
     amount = 4;
     flag = 1;
  }
  else if ((strcmp("x",temp_name)) == 0)
  {
     printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
     printf("~ You have requested to exit this program.  ~\n");
     printf("~ This read program has ended.              ~\n");
     printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
     exit(0);
  }
  else if ((strcmp("X",temp_name)) == 0)
  {
     printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
     printf("~ You have requested to exit this program.  ~\n");
     printf("~ This read program has ended.              ~\n");
     printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
     exit(0);
  }
  else 
  {
     amount = atoi(temp_name);
     if ((amount < 1) || (amount > 4))
     {
        printf("The amount should be between 1-4.\n");
        printf("Please try again.\n\n");
        flag = 0;
     }
     else
        flag = 2;
  }
} /* End of while */

  printf("\n\n");

  if (flag ==2)
  {
     for (count = 0; count < amount; count++)
     {
        flag = 2;
        while (flag == 2)
        {
         printf("Enter parameter number %1d --> ",count+1);
         scanf("%s",temp_name);
         if ((strcmp("x",temp_name)) == 0)
         {
            printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
            printf("~ You have requested to exit this program.  ~\n");
            printf("~ This read program has ended.              ~\n");
            printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
            exit(0);
         }
         else if ((strcmp("X",temp_name)) == 0)
         {
            printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
            printf("~ You have requested to exit this program.  ~\n");
            printf("~ This read program has ended.              ~\n");
            printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
            exit(0);
         }
         else
         {
            param[count] = atoi(temp_name);
            if ((param[count] >= 1) && (param[count] <= 4))
               flag = 0;
            else
            { 
               printf("The amount should be between 1-4.\n");
               printf("Please try again.\n\n");
               flag = 2;
            }
          }
         } /* End of while */
     } /* End of for */
  } /* End of if */

  printf("\n");
  for (count = 0; count < amount; count++)
  {
     if (param[count] == 1)
        printf(" YY/MO/DD ");
     else if (param[count] == 2)
        printf(" HR:MM:SC ");
     else if (param[count] == 3)
        printf("   IRR   ");
     else if (param[count] == 4)
        printf(" STD ");
  }
  printf("\n");


  rec_count = 0;

  while ((feof(fptr)) == 0)
  {
     year      = 0;
     month     = 0;
     day       = 0;
     hour      = -1;
     minute    = -1;
     second    = -1;
     irr       = 0.0;
     std       = 0.0;

     fscanf(fptr,"  %2d%c%2d%c%2d%2d%c%2d%c%2d%lf%lf",&year,&letter,&month,
            &letter,&day,&hour,&letter,&minute,&letter,&second,&irr,
            &std);

     if (feof(fptr) != 0)
     {
        printf("The total number of records in this file is %d.\n",rec_count);
        printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
        printf("~ End of file has been reached.       ~\n");
        printf("~ Program has completed successfully! ~\n");
        printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
        exit ( 0 );
     }

     rec_count++;

     for (count = 0; count < amount; count++)
     {
        switch (param[count])
        {
           case 1:
             if (year < 10)
               printf(" 0%1d/",year);
             else
               printf(" %2d/",year);

             if (month < 10)
               printf("0%1d/",month);
             else
               printf("%2d/",month);

             if (day < 10)
               printf("0%1d ",day);
             else
               printf("%2d ",day);
             break;
           case 2:
             if (hour < 10)
               printf(" 0%1d:",hour);
             else
               printf(" %2d:",hour);

             if (minute < 10)
               printf("0%1d:",minute);
             else
               printf("%2d:",minute);

             if (second < 10)
               printf("0%1d ",second);
             else
               printf("%2d ",second);
             break;
           case 3:
             printf(" %-7.2f ",irr);
             break;
           case 4:
             printf(" %-3.2f ",std);
             break;
           default:
             break;
        }
    }
    printf("\n");
  }   /* End of while */
}   /* End of main function */
/****************************************************************************
*                                                                           *
* FUNCTION:  Greetings                                                      *
*                                                                           *
* PURPOSE:   To print a banner at the beginning of the execution to inform  *
*            the users what version of this read program he or she is       *
*            working with.                                                  *
*                                                                           *
****************************************************************************/
void Greetings()
{   /* Begin Greetings function */
   printf("\n\n\n");
   printf("   *****************************************************\n");
   printf("   *                                                   *\n");
   printf("   *            ERBE Total Solar Irradiance            *\n");
   printf("   *             Read Program Version 4.0              *\n");
   printf("   *                                                   *\n");
   printf("   *                                                   *\n");
   printf("   *                   May 24, 1999                    *\n");
   printf("   *****************************************************\n");
   printf("\n\n\n");
}   /* End of Greetings function */

