/****************************************************************************/
/*                                                                          */
/*                             IAGA_find_stations.c                         */
/*                                                                          */
/****************************************************************************/
/****************************************************************************/
/* This program inspects an IAGA format data file and returns a list of     */
/* stations whose data is included in the file.                             */
/*                                                                          */
/* Usage:                                                                   */
/*    IAGA_find_stations IAGAFile                                           */
/*      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.0        11.04.2006                       */
/****************************************************************************/
/****************************************************************************/
/*  Version history:                                                        */
/*                                                                          */
/*  1.0  11.04.2006 First official release                                  */
/****************************************************************************/

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

#define OK   0              /* Return value for successfull completion      */
#define FAIL 1              /* Return value for failed operation            */
#define IAGA_StationID 12
#define IAGABlockSize 1440

char *version = "1.0";
char *date = "11.04.2006";

#define HeaderLineCount 4
#define UsageLineCount  2



char *HeaderText[HeaderLineCount] = {
"**************************************************************************",
" This program inspects an IAGA format data file and returns a list of     ",
" stations whose data is included in the file.                             ",
"**************************************************************************",
};

char *UsageText[UsageLineCount] = {
" IAGAFile                                                                 ",
"      IAGAFile             Name of IAGA format file.                      ",
};



/*--------------------------------------------------------------------------*/
/*  Read one IAGA block from given file.                                    */
/*--------------------------------------------------------------------------*/

long ReadBlock(FILE *FilePtr,char *Buffer)
{
    long count;
    
    count = fread(Buffer,IAGABlockSize,1,FilePtr);
    if (count == 0) return (0); else return (1);
}


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

int main(int argc, char *argv[])
{
    long    params;						/* Index for command line params	*/
    char    Block[1440];                /* One IAGA data block              */
    char    FileName[255] = "";         /* Name of the data file            */
    char	StationName[4];
    char	FirstStationName[4];
    FILE    *IAGAFile;                  /* File pointer to IAGA file        */


    StationName[3] = 0;
    FirstStationName[3] = 0;

    /*==========================*/
    /* Analyse the command line */
    /*==========================*/

    if (argc != 2) {  /* Exactly one argument allowed, show the usage text */
        PrintUsage(argv[0],0,HeaderLineCount,UsageLineCount);
        return OK;
    }

    for (params = 1; params < argc; params++) {
        if (*argv[params] != '-') {
            strcpy(FileName,argv[params]);
        } else {
            switch (*(argv[params]+1)) {
                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;
            }
        }
    }

    
    /*=======================*/
    /* Try to open the file. */
    /*=======================*/
    
	if ((IAGAFile = fopen(FileName, "r")) == NULL) {
		fprintf(stderr,"###  Unable to open file: %s\n",FileName);
		return FAIL;
	}

    /*====================================================*/
    /* Read data blocks until the first station reappears */
    /*====================================================*/
    
    ReadBlock(IAGAFile,Block);
    strncpy(FirstStationName,Block+IAGA_StationID,3);
    printf("%3s",FirstStationName);
    
    ReadBlock(IAGAFile,Block);
    strncpy(StationName,Block+IAGA_StationID,3);
   
    while (strncmp(FirstStationName,StationName,3) != 0) {
   		printf(" %3s",StationName);
    	ReadBlock(IAGAFile,Block);
	    strncpy(StationName,Block+IAGA_StationID,3);
    }
    
    fclose(IAGAFile);
    return OK;
}

