/****************************************************************************/
/*                                                                          */
/*                          IAGA_make_missing.c                             */
/*                                                                          */
/****************************************************************************/
/****************************************************************************/
/* This program generates an IAGA-format data file and fills all data       */
/* values with missing data marker 999999.                                  */
/*                                                                          */
/* Usage:                                                                   */
/*    IAGA_make_missing -s [-e | -h] -t -o   > IAGAFile                     */
/*          -s YYMMDDHHMM       Time of first record to be written.         */
/*          -e YYMMDDHHMM       Time of the block not anymore written.      */
/*          -h HH               Number of hours included. Either -e or -h   */
/*                              must be specified, not both.                */
/*          -d SampleRate       Sample rate = time difference between       */
/*                              successive data points.                     */
/*          -o 'Station list'   List of stations delimited by aposthropes   */
/*                              Stations are identified by three-letter     */
/*                              code and separated by exactly one space.    */
/*                              Format : 'SOR MAS KIL KEV'                  */
/*          IAGAFile            Name of IAGA-format file.                   */
/*                                                                          */
/****************************************************************************/
/****************************************************************************/
/*                            Lasse Hakkinen                                */
/*                    Finnish Meteorological Institute                      */
/*                        Geophysical Research Division                     */
/*                              P.O.Box 503                                 */
/*                      FIN-00101, Helsinki, Finland                        */
/*                      e-mail: Lasse.Hakkinen@fmi.fi                       */
/*                      phone : (+358)-9-19294634                           */
/*                      fax   : (+358)-9-19294603                           */
/*                                                                          */
/*                      version 1.02        9.9.1999                        */
/****************************************************************************/
/****************************************************************************/
/*  Version history:                                                        */
/*                                                                          */
/*  1.02 09.09.1999 Fixed a Y2K bug in NewTime.h file which resulted in     */
/*                  incorrect year in date strings if year >= 2000.         */
/*  1.01 28.10.1997 Moved NewStation-routine to MagnData.h file. No change  */
/*                  in program behavior.                                    */
/*  1.0  21.11.1996 First official release                                  */
/****************************************************************************/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "Usage.h"
#include "MagnData.h"
#include "IAGA.h"

char *version = "1.02";
char *date = "9.9.1999";

#define HeaderLineCount 4
#define UsageLineCount 13

char *HeaderText[HeaderLineCount] = {
"**************************************************************************",
" This program generates an IAGA format data file with missing data markers",
" for all field values of specified stations.",
"**************************************************************************",
};

char *UsageText[UsageLineCount] = {
" -s [-e | -h] -d -o > IAGA_file",
"       -s YYMMDDHH         Time of the first record to be written",
"      [-e YYMMDDHH]        Time of the last record not written anymore.",
"      [-h HH]              Number of hours included. Either -e or -h must",
"                           be specified, not both.",
"       -d SampleRate       Sample rate = time difference between successive",
"                           data points.",
"       -o 'Station list'   List of stations enclosed in quotes. Stations",
"                           are identified by three-letter code and",
"                           separated by a single space. e.g. 'SOR KEV KIL'.",
"                           The station parameters must be defined in the",
"                           StatInfo.h file.",
"      IAGA_file            Name of generated IAGA-format file.",
};



/*--------------------------------------------------------------------------*/
/*                          The main procedure                              */
/*--------------------------------------------------------------------------*/

int main(int argc, char *argv[])
{
    long j,params;                  /* Dummy index variables                */
    long status     = 0;            /* Status variable for file operations  */
    long HourCount  = 0;            /* Number of hours to be processed      */
    long SampleRate = 0;            /* Sample rate in seconds               */
    char StationID[4]  = "???";     /* Three letter station ID code         */
    Time_sec StartTime = MIN_TIME;  /* Time of first data block (seconds)   */
    Time_sec EndTime   = MAX_TIME;  /* No blocks included after this time   */
    Time_sec Time;                  /* Current time                         */
    char StationStr[400]  = "";     /* List of stations to be processed     */
    StationInfoPtr p;               /* Pointer to StationInfoStruct defined */
                                    /* StatInfo.h file.                     */
    Network_struct IMAGE;           /* Structure containing the data        */
                                    /* for all stations.                    */

    
    /*==========================*/
    /* Analyse the command line */
    /*==========================*/
    if (argc == 1) {        /* No arguments, show the usage text */
        PrintUsage(argv[0],0,HeaderLineCount,UsageLineCount);
        return OK;
    }

    for (params = 1; params < argc; params++) {
        if (*argv[params] != '-') {
            fprintf(stderr,"\n### %s: \"%s\" Illegal parameter.\n\n",
                    argv[0], argv[params]);
            PrintUsage(argv[0],1,HeaderLineCount,UsageLineCount);
            return FAIL;
        } else {
            switch (*(argv[params]+1)) {
                case 's' : StartTime = StrToSecs(argv[++params],0); break;
                case 'e' : EndTime   = StrToSecs(argv[++params],0); break;
                case 'h' : HourCount = atol(argv[++params]);    break;
                case 'd' : SampleRate = atol(argv[++params]);   break;
                case 'o' : strcpy(StationStr,argv[++params]);   break;
                default  :
                    fprintf(stderr,"\n### %s: \"%s\" is not an option.\n\n",
                            argv[0], argv[params]);
                    PrintUsage(argv[0],1,HeaderLineCount,UsageLineCount);
                    return FAIL;
                    break;
            }
        }
    }
    
    /*====================================*/
    /* Check the values of the parameters */
    /*====================================*/
    
    if (SampleRate == 0) {
        PrintUsage(argv[0],1,HeaderLineCount,UsageLineCount);
        return FAIL;
    }
    if ((HourCount != 0) && (EndTime != MAX_TIME)) {
        PrintUsage(argv[0],1,HeaderLineCount,UsageLineCount);
        return FAIL;
    }
    if (strlen(StationStr) == 0) {
        PrintUsage(argv[0],1,HeaderLineCount,UsageLineCount);
        return FAIL;
    }
    if ((HourCount != 0) && (StartTime == MIN_TIME)) {
        PrintUsage(argv[0],1,HeaderLineCount,UsageLineCount);
        return FAIL;
    }
    if (HourCount != 0) {
        EndTime = StartTime+3600*HourCount;
    }


    /*======================================================*/
    /* Create the network structure containing the stations */
    /* and fill the data fields with missing values         */
    /*======================================================*/

    InitNetwork(&IMAGE);
    for (j=0; j<strlen(StationStr); j += 4) {
        strncpy(StationID,StationStr+j,3);
        p = FindStationInfo(StationID);
        if (strncmp(p->StationID,"   ",3) == 0) {
            fprintf(stderr,"### Station %s not found in StatInfo.h file.\n",
                    StationID);
            fprintf(stderr,"### Add station to StatInfo.h file and ");
            fprintf(stderr,"recompile the program\n\n");
            return FAIL;
        }
        else {
            if (NewStation(&IMAGE,p,StartTime,EndTime,
                                SampleRate,60*SampleRate) != 0) {
                fprintf(stderr,"### Out of memory !\n");
                return FAIL;
            }
        }
    }


    /*==============================*/
    /* Write the data into stdout   */
    /*==============================*/
    
    WriteIAGA(NULL,&IMAGE,NULL,NULL,NULL);

    FreeNetwork(&IMAGE);
    return OK;
}

