/****************************************************************************/ /* */ /* WDChour_to_TEXT.c */ /* */ /****************************************************************************/ /****************************************************************************/ /* This is a filter program that reads a WDChour format data file and writes*/ /* out the same data in text columns. */ /* */ /* Usage: */ /* WDChour_to_TEXT [-s] [-e | -h] [-o] [-c] [-a] [-g] [-v] [-y] */ /* [<] WDChourFile > TEXTFile */ /* [-s YYMMDDHH] Time of the first record included. If missing */ /* then start of file is assumed. */ /* [-e YYMMDDHH] 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 delimited by aposthropes */ /* If missing then all stations included. */ /* Stations are identified by three-letter code */ /* and separated by exactly one space. */ /* [-c 'CompList'] Components to be written. Possible values are */ /* tXYZHDZ (=time,X,Y,Z,H,D,F). Default value */ /* is tXYZ. Time is written in the format */ /* YYYY MM DD HH MM SS. */ /* [-a AverPeriod] Don't write all data values but averages over */ /* AverPeriod. */ /* [-g Century] Force the century to this value (e.g. 19) */ /* neglecting the value in the data file. */ /* [-v] Write headers (= Station name + Component) */ /* If missing then no headers are written */ /* [-y] Write the year string with two digits. Default */ /* value is 4 digits (century included). */ /* WDChourFile Name of WDChour-format file. */ /* TEXTFile Name of TEXT 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 14.02.2020 */ /****************************************************************************/ /****************************************************************************/ /* Version history: */ /* */ /* 1.02 14.02.2020 Date strings must be YYYYMMDD (four digits for year) */ /* Replaced StrToSecs -> StrToSecsC and */ /* SecsToStr -> SecsToStrC. */ /* 1.01 13.11.2014 Added -g option. */ /* 1.0 03.11.2014 First official release */ /****************************************************************************/ #include #include #include #include "Usage.h" #include "MagnData.h" #include "WDChour.h" char *version = "1.02"; char *date = "14.02.2020"; #define HeaderLineCount 4 #define UsageLineCount 27 char *HeaderText[HeaderLineCount] = { "***************************************************************************", " This program reads an WDChour format magnetometer data file and writes out", " the data for given components and stations in columnar format (TEXT). ", "***************************************************************************", }; char *UsageText[UsageLineCount] = { " [-s] [-e | -h] [-o] [-c] [-a] [-g] [-v] [-y] [<] WDChourFile > TEXTFile", " [-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 delimited by aposthropes ", " If missing then all stations included. ", " Stations are identified by three-letter code ", " and separated by exactly one space. ", " [-c 'CompList'] List of components to be printed. Possible ", " values for the components are t,X,Y,Z,H,D,F. ", " Here 't' is time. The format of time is ", " YYYY MM DD HH MM SS. Default value for CompList ", " is 't' + components in datafile. ", " Unit of D is 0.1 arcmin. For other magnetic ", " components the unit is 'nT'. ", " [-a AverPeriod] Write data values as averages over AverPeriod. ", " [-g Century] Force the century (e.g. 19) to this value ", " neglecting the value in the data file. ", " [-v] Write headers (= Station names + components). ", " If missing then no headers are written. ", " [-y] Write year string with two digits. Default value", " is four (century included). ", " WDChourFile Name of WDChour-format file. ", " TEXTFile Name of ASCII file. ", }; /*--------------------------------------------------------------------------*/ /* Write headers (= Station name + component). */ /*--------------------------------------------------------------------------*/ void WriteHeaders(NetworkPtr NETWORK, char *CompStr, long Year2option) { long i,CompCount; StationPtr s; CompCount = strlen(CompStr); /* Write time */ if (CompStr[0] == 't') { if (Year2option == 1) printf("YY MM DD HH MM SS "); else printf("YYYY MM DD HH MM SS "); CompCount--; } /* Write station names and components */ s = NETWORK->StationList; while (s != NULL) { for (i = strlen(CompStr)-CompCount; i < strlen(CompStr); i++) printf(" %s %c ",s->StationID,CompStr[i]); s = s->Next; } printf("\n"); } /*--------------------------------------------------------------------------*/ /* Write horizontal line. */ /*--------------------------------------------------------------------------*/ void WriteHorLine(NetworkPtr NETWORK, char *CompStr, long Year2option) { long i,CompCount; CompCount = strlen(CompStr); /* Write a dashed line */ if (CompStr[0] == 't') { if (Year2option == 1) printf("--------------------"); else printf("----------------------"); CompCount--; } for (i = 0; i < CompCount*(NETWORK->StationCount);i++) printf("--------"); printf("\n"); } /*--------------------------------------------------------------------------*/ /* The main procedure */ /*--------------------------------------------------------------------------*/ int main(int argc, char *argv[]) { long params; long status = 0; long FileCount = 0; long HourCount = 0; long AvePeriod = 0; long i,Ave; char FileName[100] = ""; char StartTimeStr[20] = ""; char EndTimeStr[20] = ""; char StationStr[200] = ""; char ComponentStr[20] = ""; StationPtr s; Time_sec t,EndTime,CurrTime; long FirstComp; long HeaderFlag = 0; long Year2option = 0; /* Print year with two digits, default = 4 */ char TimeStr[20]; char c; long Century; long CenturyOption = 9999; Network_struct NETWORK; /*==========================*/ /* 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' : strcpy(StartTimeStr,argv[++params]); break; case 'e' : strcpy(EndTimeStr,argv[++params]); break; case 'h' : HourCount = atol(argv[++params]); break; case 'o' : strcpy(StationStr,argv[++params]); break; case 'c' : strcpy(ComponentStr,argv[++params]); break; case 'a' : AvePeriod = atol(argv[++params]); break; case 'v' : HeaderFlag = 1; break; case 'y' : Year2option = 1; break; case 'g' : CenturyOption = atol(argv[++params]); break; case 'p' : /* Do nothing */ 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) && (strlen(EndTimeStr) > 0)) { PrintUsage(argv[0],1,HeaderLineCount,UsageLineCount); return FAIL; } if ((HourCount != 0) && (strlen(StartTimeStr) == 0)) { PrintUsage(argv[0],1,HeaderLineCount,UsageLineCount); return FAIL; } if (HourCount != 0) { SecsToStrC(StrToSecsC(StartTimeStr,0)+3600*HourCount,EndTimeStr); } /*====================================================*/ /* Read the data from an WDChour format file into memory */ /*====================================================*/ status = ReadWDChour(FileName,&NETWORK,StartTimeStr,EndTimeStr,StationStr, &Century); if (CenturyOption != 9999) Century = CenturyOption; if (status != 0) { if (status == FileError) fprintf(stderr,"Error in reading file %s\n",FileName); if (status == OutOfMemory) fprintf(stderr,"Out of memory while reading file %s\n",FileName); if (status == FileFormatError) fprintf(stderr,"%s is not an WDChour format file\n",FileName); if (status == IllegalComponent) fprintf(stderr,"Illegal component used in WDC file. Must be XYZ or HDZ\n"); return FAIL; } /*===============================*/ /* Check the magnetic components */ /*===============================*/ if (strlen(ComponentStr) == 0) { *ComponentStr = 't'; strcpy(ComponentStr+1,NETWORK.StationList->Components); } else { for (i=0;iTimeStep; if (strlen(StartTimeStr) == 0) t = NETWORK.StationList->StartTime; else t = StrToSecsC(StartTimeStr,0); if (strlen(EndTimeStr) == 0) EndTime = NETWORK.StationList->EndTime; else EndTime = StrToSecsC(EndTimeStr,0); while (t < EndTime) { FirstComp = 0; if (ComponentStr[0] == 't') { /* Write time */ CurrTime = t; SecsToStrC(CurrTime,TimeStr); if (Year2option == 0) { /* Write century */ printf("%2d", Century); } printf("%.2s %.2s %.2s %.2s %.2s %.2s ",TimeStr+2,TimeStr+4, TimeStr+6,TimeStr+8,TimeStr+10,TimeStr+12); FirstComp++; } s = NETWORK.StationList; /* go through the stations */ while (s != NULL) { for (i=FirstComp;iNext; } printf("\n"); t += AvePeriod; } FreeNetwork(&NETWORK); return OK; }