/****************************************************************************/ /* */ /* IAGA_reorder.c */ /* */ /****************************************************************************/ /****************************************************************************/ /* This program reorders the stations in a single IAGA file. */ /* */ /* Usage: */ /* IAGA_reorder [-o | -m] [-p] [<] IAGAFile > NewIAGAFile */ /* -o StationList Defines how the stations are ordered in the */ /* final IAGA file. e.g. 'SOR MAS KEV'. */ /* Stations which are not mentioned in StationList */ /* will NOT be written into final data file. */ /* -m Orders the stations according to geomagnetic */ /* (CGM) latitudes. Either -o or -m may be used, */ /* not both. */ /* -g Orders the stations according to geographic */ /* latitudes. Only one of -o , -m and -g may be */ /* used. */ /* [-p] Prevents usage-text from appearing. Useful when */ /* reading data from standard input. */ /* IAGAFile Name of IAGA-format file. */ /* NewIAGAFile Name of new 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.15 27.04.2018 */ /****************************************************************************/ /****************************************************************************/ /* Version history: */ /* */ /* 1.15 27.04.2018 Station lists (All and CGM) are read using command */ /* IMAGE_stations. */ /* 1.14 27.11.2017 Added HLP, PPN, NGK and WNG stations. */ /* 1.13 13.11.2015 Added SUW and BRZ stations */ /* 1.12 18.09.2014 Added RAN station */ /* 1.11 01.04.2011 Added geographic sorting (-g option) */ /* 1.10 13.01.2010 Added JCK station to the default list. */ /* 1.09 02.07.2008 Added DON station to the default list. */ /* 1.08 27.10.2007 Added NOR and SOL stations to the default list. */ /* 1.07 12.11.2004 Added MEK station to the default list. */ /* 1.06 30.03.2004 Added BER and KAR stations to the default list. */ /* 1.05 13.11.2002 Added -m option. */ /* 1.04 22.11.2001 Added JAN station to the default list */ /* 1.03 18.09.2001 Added TAR station to the default list */ /* 1.02 24.05.2001 Added IVA station to the default list */ /* 1.01 09.09.1999 Fixed a Y2K bug in NewTime.h file which resulted in */ /* incorrect year in date strings if year >= 2000. */ /* 1.0 02.06.1998 First official release */ /****************************************************************************/ #include #include #include #include "Usage.h" #include "MagnData.h" #include "IAGA.h" #define MaxStationCount 100 /* Maximum number of stations in input file */ #define BuffSize 1440 /* Size of one data block in an IAGA file */ // char *IMAGEStationList = "NAL LYR HOR HOP BJN JAN TRO AND LEK ABK KIR LOZ NOR SOR ALT MAS KAU KEV KIL IVA MUO PEL SOD JCK DON RAN RVK LYC OUJ MEK HAN DOB SOL BER KAR NUR UPS TAR BRZ SUW WNG NGK PPN HLP"; // char *IMAGECGMLat = "NAL LYR HOR HOP BJN NOR JAN SOR TRO ALT AND KEV MAS KIL LEK ABK KAU IVA MUO KIR LOZ SOD PEL JCK DON RAN RVK LYC OUJ DOB SOL MEK HAN BER KAR NUR UPS TAR BRZ HLP WNG SUW NGK PPN"; char *version = "1.15"; char *date = "27.04.2018"; #define HeaderLineCount 3 #define UsageLineCount 17 char *HeaderText[HeaderLineCount] = { "**************************************************************************", "This program reorders stations within a single IAGA file. ", "**************************************************************************", }; char *UsageText[UsageLineCount] = { " [-o | -m] [-p] [<] IAGAFile > NewIAGAFile ", " -o StationList Defines how the stations are ordered in the ", " final data file. e.g. 'SOR MAS KEV'. ", " Stations which are not mentioned are NOT ", " included in the final data file. ", " -m Reorders the stations according to geomagnetic", " (CGM) latitudes. This option will work only ", " for IMAGE stations. Only one of options -o, -m", " and -g may be used. If none of them is defined", " then a default ordering will be used. ", " -g Reorders the stations according to geographic ", " latitudes. This option works for all stations.", " Only one of options -o, -m and -g may be used.", " -p Prevents usage text from appearing. Useful ", " when reading from standard input. ", " IAGAFile1 Name of IAGA-format file. ", " NewIAGAFile Name of new IAGA-format file. ", }; /*------------------------------------------------------*/ /* Sort the x table from highest value to lowest and */ /* sort the station list string (s) simultaneously. */ /*------------------------------------------------------*/ void sort(long *x, char *s, long count) { long i,j; long max,ind,temp; char c[4]; for (i=0;i 1) { PrintUsage(argv[0],1,HeaderLineCount,UsageLineCount); return FAIL; } /*============================================*/ /* Read the station order lists (ALL and CGM) */ /*============================================*/ fp = popen("/proj/image/bin/IMAGE_stations ANY","r"); fgets(IMAGEStationList, sizeof(IMAGEStationList) - 1, fp); fp = popen("/proj/image/bin/IMAGE_stations ANY CGM","r"); fgets(IMAGECGMLat, sizeof(IMAGECGMLat) - 1, fp); /*===========================*/ /* Try to open the IAGA file */ /*===========================*/ if (FileCount == 0) IAGAFile = stdin; else { if ((IAGAFile = fopen(FileName, "r")) == NULL) { fprintf(stderr,"\n### %s - Unable to open file: %s\n",argv[0],FileName); return FAIL; } } /*==================================================*/ /* Determine which ordering is used. */ /*==================================================*/ if (strlen(StationStr) == 0) { if (GeoMagOrdering == 1) strcpy(StationStr,IMAGECGMLat); else strcpy(StationStr,IMAGEStationList); } for (i=0;i<4*MaxStationCount;i++) ID[i] = ' '; /*==================================================*/ /* Read data blocks and write them in proper order. */ /* This must be done in a bit awkward manner since */ /* we may be reading from stdin. */ /*==================================================*/ status = ReadIAGABlock(IAGAFile,Block[0]); while (status != 0) { BlockNbr = 0; do { strncpy(ID+4*BlockNbr, Block[BlockNbr]+12,4); strncpy(Lat_str,Block[BlockNbr]+15,5); Latitude[BlockNbr] = atol(Lat_str); status = ReadIAGABlock(IAGAFile,Block[++BlockNbr]); } while ((status != 0) && (strncmp(Block[BlockNbr]+12,Block[0]+12,3) != 0)); /*** Order stations geographically ***/ if (GeoGraphOrdering == 1) { sort(Latitude, ID, BlockNbr); strncpy(StationStr,ID,4*BlockNbr-1); StationStr[4*BlockNbr-1] = '\0'; GeoGraphOrdering = 0; // This needs to be done only once } /*** Write data blocks in proper order ***/ for (i=0;i