/****************************************************************************/ /* */ /* GADF_extract.c */ /* */ /****************************************************************************/ /****************************************************************************/ /* This program extracts data from an GADF-format data file. The user may */ /* give start and end times and optionally the station list. The data may */ /* may be read either from a file or from stdin. Results are written to */ /* standard output. */ /* */ /* Usage: */ /* GADF_extract [-s] [-e | -h] [-o] [<] GADFFile > NewGADFFile */ /* [-s YYYYMMDDHHMM] Time of the first record included */ /* [-e YYYYMMDDHHMM] Time of the record not included anymore */ /* [-h HH] Number of hours included */ /* [-o 'Station list'] List of stations delimited by quotes. */ /* Stations are identified by three-letter */ /* code and separated by exactly one space. */ /* e.g. 'SOR MAS KEV KIL'. */ /* If missing then all stations included. */ /* GADFFile Name of GADF-format file. */ /* NewGADFFile Name of generated GADF-format file. */ /* */ /****************************************************************************/ /****************************************************************************/ /* Lasse Hakkinen */ /* Finnish Meteorological Institute */ /* Department of Geophysics */ /* 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 13.02.2020 */ /****************************************************************************/ /****************************************************************************/ /* Version history: */ /* */ /* 1.02 13.02.2020 Date strings must be YYYYMMDD (four digits for year) */ /* Replaced StrToSecs -> StrToSecsC and */ /* SecsToStr -> SecsToStrC. */ /* 1.01 02.07.1999 Fixed a Y2K bug in NewTime.h file which resulted in */ /* incorrect year in date strings if year >= 2000. */ /* 1.0 26.02.1996 First official release */ /****************************************************************************/ #include #include #include #include "Usage.h" #include "MagnData.h" #include "GADF.h" #define BuffSize 432 /* Size of one block in an GADF file */ char *version = "1.02"; char *date = "13.02.2020"; #define HeaderLineCount 4 #define UsageLineCount 14 char *HeaderText[HeaderLineCount] = { "**************************************************************************", "This program extracts data for given time interval or for given stations", "from an GADF format magnetometer data file.", "**************************************************************************", }; char *UsageText[UsageLineCount] = { " [-s] [-e | -h] [-o] [<] GADFFile > NewGADFFile", " [-s YYYYMMDDHH] Time of the first record included. If missing", " then start of file is assumed.", " [-e YYYYMMDDHH] Time of the record not included anymore.", " If missing then end of file is assumed.", " [-h HH] Number of hours included. Either -e or -h can", " be specified, not both.", " [-o 'Station list'] List of stations enclosed in quotes", " If missing then all stations included.", " Stations are identified by three-letter code", " and separated by exactly one space.", " e.g. 'SOR KEV KIL'.", " GADFFile Name of GADF-format file.", " NewGADFFile Name of extracted GADF-format file.", }; /*--------------------------------------------------------------------------*/ /* The main procedure */ /*--------------------------------------------------------------------------*/ int main(int argc, char *argv[]) { long params; long status = 0; long FileCount = 0; char FileName[100]; /* Name of the original data file */ Time_sec Time; /* Time of the current data block (seconds) */ Time_sec StartTime = MIN_TIME; /* Time of first data block (seconds) */ Time_sec EndTime = MAX_TIME; /* Time of last block (seconds) */ long HourCount = 0; /* Number of hours included */ char Buffer[BuffSize]; /* Buffer for one data block */ char StationList[200] = ""; /* List of stations from command line */ FILE *GADFFile; /* GADF format data file to be processed */ /*==========================*/ /* 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] != '-') { strcpy(FileName,argv[params]); FileCount++; } else { switch (*(argv[params]+1)) { case 's' : StartTime = StrToSecsC(argv[++params],0); break; case 'e' : EndTime = StrToSecsC(argv[++params],0); break; case 'h' : HourCount = atol(argv[++params]); break; case 'o' : strcpy(StationList,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 (FileCount > 1) { PrintUsage(argv[0],1,HeaderLineCount,UsageLineCount); return FAIL; } if ((HourCount != 0) && (EndTime != MAX_TIME)) { 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; } /*===========================*/ /* Try to open the GADF file */ /*===========================*/ if (FileCount == 0) GADFFile = stdin; else { if ((GADFFile = fopen(FileName, "rb")) == NULL) { fprintf(stderr,"\n### %s - Unable to open file: %s\n",argv[0],FileName); return FAIL; } } /*===================================*/ /* Extract proper data from the file */ /*===================================*/ while (ReadGADFBlock(GADFFile,Buffer)) { /* Check if we are already more than 24 hours past EndTime */ Time = GetGADFTime(Buffer); if ((EndTime != MAX_TIME) && (Time >= EndTime)) break; if ((Time >= StartTime) && (Time < EndTime) && StationInList(Buffer+GADF_StationCode,StationList)) { WriteGADFblock(stdout,Buffer); } } if (FileCount == 1) fclose(GADFFile); return OK; }