/* Quick routine to try and make a map in the GrADS format byte 0 = 1 (write the data .. don't skip) byte 1 = 1 (political boundary) byte 2 = number of lon,lat pairs byte 3 - n = encoded lon/lat pairs in 3-bytes. See GrADS Documentation */ #include //#include // Define maximum number of connected line segment possible // GrADS maximum is only 256 segments #define MAXLINESEGMENTS 250 #define MAXBUFFERSIZE 255 /*unique id changed by user to manipulate map background file with mpt command*/ #define MPTID 24 void printUsage( char *progName ); main (int argc, char* argv[]) { FILE *infile, *outfile; char buffer[MAXBUFFERSIZE]; float flat, flon; // latitude and longitude read from file int flag; // LLP flag 0 --> start new line int ncount; // counter for number of lat/lon pairs to write int ilat,ilon; // pointers to memory for lat and lon unsigned char *data, *currec; // output grads data file .. I hope int i,c, INTSIZE = sizeof (int), linenum; int overwrite = 0, recordtype, thindata = 0; extern char *optarg; extern int optind, optopt, opterr; // check the arg count if ( argc < 3 ) { // print usage message printUsage( argv[0] ); return 1; } // parse the arg options while ((c = getopt(argc, argv, "hot:")) != -1) { switch(c) { case 'o': overwrite = 1; break; case 't': if ( (sscanf( optarg,"%d", &thindata)) != 1 ) { fprintf( stderr, "-t without valid numeric argument\n", optopt ); printUsage( argv[0] ); return 1; } break; case ':': fprintf( stderr, "-%c without arg\n", optopt ); printUsage( argv[0] ); return 1; break; case '?': fprintf( stderr, "unknown arg %c\n", optopt ); case 'h': printUsage( argv[0] ); return 1; break; } } if ( (argc - optind) != 2 ) { printUsage( argv[0] ); return 1; } // if overwrite flag not set, check if the outfile exist if (!overwrite) { outfile = fopen( argv[optind+1], "r" ); // if file exists do not overwrite, exit. if ( outfile != NULL ) { close( outfile ); fprintf( stderr, "Outfile already exists!\n" ); fprintf( stderr, "Use -o option to force overwrite.\n" ); return 1; } } outfile = fopen( argv[optind+1], "w" ); if ( outfile == NULL ) { fprintf( stderr, "Cannot open outfile!\n" ); return 1; } // Open the input file infile = fopen( argv[optind], "r" ); if ( infile == NULL ) { fprintf( stderr, "Cannot open infile!\n" ); close(outfile); return 1; } // malloc space for the GrADS map formated data data = (unsigned char *) malloc (MAXLINESEGMENTS * 6 * sizeof(char) + 3); if ( !data) { fprintf( stderr, "Cannot malloc!\n" ); return 1; } currec = data + 3; ncount = 0; i = 0; // read the E00 file header and discard fgets( buffer, MAXBUFFERSIZE-1, infile ); fgets( buffer, MAXBUFFERSIZE-1, infile ); while ( (fgets( buffer, MAXBUFFERSIZE-1, infile ) != NULL) && (sscanf(buffer,"%d%d%*d%*d%*d%*d%d", &linenum, &recordtype, &ncount) == 3) && (linenum != -1) ) { recordtype = MPTID; if (recordtype == 0) { recordtype = MPTID; } // setup the new record header data[0] = 1; data[1] = recordtype; currec = data + 3; i=0; for ( c = 0; c < ncount; c++ ) { if ( (fscanf( infile, "%f %f", &flon, &flat)) == EOF) { // cleanup and exit close( infile ); close( outfile ); free(data); return 1; } ilon = (int) ( ( ( flon < 0 ) ? flon + 360 : flon ) * 10000 ); ilat = (int) ((flat + 90.0) * 10000); currec[0] = ( (unsigned char *)(&ilon) )[INTSIZE-2]; currec[1] = ( (unsigned char *)(&ilon) )[INTSIZE-3]; currec[2] = ( (unsigned char *)(&ilon) )[INTSIZE-4]; currec[3] = ( (unsigned char *)(&ilat) )[INTSIZE-2]; currec[4] = ( (unsigned char *)(&ilat) )[INTSIZE-3]; currec[5] = ( (unsigned char *)(&ilat) )[INTSIZE-4]; currec += 6; i++; if (i == MAXLINESEGMENTS) { data[2] = i; fwrite(data, sizeof(char), (3 + i * 6), outfile); data[0] = 1; data[1] = recordtype; currec = data + 3; i = 0; } } // if there is still data in the buffer write it out if (i > 0) { data[2] = i; fwrite(data, sizeof(char), (3 + i * 6), outfile); i = 0; } //fscanf( infile, "%*s"); fgets( buffer, MAXBUFFERSIZE-1, infile ); } // if there is still data in the buffer write it out if (i > 0) { data[2] = i; fwrite(data, sizeof(char), (3 + i * 6), outfile); } close( infile ); close( outfile ); free(data); return 0; } void printUsage( char *progName ) { fprintf( stderr, "Usage: %s [OPTION]... infile outfile\n", progName ); fprintf( stderr, "Options:\n" ); fprintf( stderr, " -o force overwrite of outfile\n" ); fprintf( stderr, " -t:num thin data; drop every num data record\n" ); return; }