void shn_clin ( void ) /************************************************************************ * shn_clin( ) * * * * This code logic was copied directly from that of the CLIPVGF utility * * ($GEMPAK/source/programs/util/clipvgf/clipvgf.c). It is needed in * * order to prepare for future calls to subroutine SHN_DFHR. * * * ** * * Log: * * J. Ator/NCEP 04/05 * ***********************************************************************/ { char device[8], dfilnam[128], pro[32]; float xsize, ysize, lllat, lllon, urlat, urlon; float prjang1, prjang2, prjang3; int mode, istat, iunit, itype; int ier; /*---------------------------------------------------------------------*/ mode = 1; ginitp ( &mode, &istat, &ier ); strcpy ( device, "GN" ); iunit = 1; strcpy ( dfilnam, "CLIPVGF" ); itype = 1; xsize = 500.0F; ysize = 500.0F; gsdeva ( device, &iunit, dfilnam, &itype, &xsize, &ysize, &ier, strlen(device), strlen(dfilnam)); lllat = 0.0F; lllon = -135.0F; urlat = 0.0F; urlon = 45.0F; strcpy ( pro, "str" ); prjang1 = 90.0F; prjang2 = -105.0F; prjang3 = 0.0F; gsmprj ( pro, &prjang1, &prjang2, &prjang3, &lllat, &lllon, &urlat, &urlon, &ier, strlen(pro)); }
int main ( int argc, char *argv[] ) /************************************************************************ * main * * * * Main program of shpcv. * * * * int main(argc, argv) * * * * Input parameters: * * argc int number of parameters of command line * * **argv char parameter array of command line * * * * Output parameters: * * Return parameters: * * NONE * * * ** * * Log: * * R. Tian/SAIC 3/04 Initial coding * * R. Tian/SAIC 2/05 Modified shp_mtyp * * T. Piper/SAIC 01/06 Call ip_help if inputs incorrect * * H. Zeng/SAIC 07/07 added calling to shp_rdException() * * S. Jacobs/NCEP 3/11 Added debug_flag and print statements * * S. Jacobs/NCEP 6/14 Moved split func to after dump output * ***********************************************************************/ { dbf_header dbfhdr; shx_record shxrec; shp_record *reclst, *newrec, *currec; shp_part *curprt; char dbfnam[MAXSHP][LLPATH], shxnam[MAXSHP][LLPATH], shpnam[MAXSHP][LLPATH]; FILE *dbffp, *shxfp, *shpfp; long flen; int ifld, nf, opt, rec, prt, file_code, nbin, ier; float ratio, clat, clon; int lfld, dump, rdpt, cbnd, ctbl, pagflg; int numrec, numfil, ii; int mode, istat, iunit, itype; unsigned long shp_fips; char device[8], proj[8], filnam[20], prognm[6]; float xsize, ysize, angle1, angle2, angle3, lllat, lllon, urlat, urlon; Boolean exception_found; /*---------------------------------------------------------------------*/ lfld = G_FALSE; dump = G_FALSE; rdpt = G_FALSE; cbnd = G_FALSE; ctbl = G_FALSE; pagflg = G_FALSE; strcpy ( prognm, "shpcv" ); debug_flag = G_TRUE; /*=================== Parse command line arguments ====================*/ while ( ( opt = getopt ( argc, argv, "r:btldh" ) ) != -1 ) { switch ( opt ) { case 'r': /* reduce points ratio */ ratio = atof ( optarg ); if ( ratio <= 0.0 ) { ratio = 0.01; } rdpt = G_TRUE; break; case 'b': /* create bound and bound info */ cbnd = G_TRUE; break; case 't': /* create station table */ ctbl = G_TRUE; break; case 'l': /* list fields */ lfld = G_TRUE; break; case 'd': /* dump the shape file */ dump = G_TRUE; break; case 'h': /* display help */ default: ip_help ( prognm, &pagflg, &ier, strlen(prognm) ); exit ( 0 ); break; } } if ( optind == argc || argc - optind > MAXSHP ) { ip_help ( prognm, &pagflg, &ier, strlen(prognm) ); exit ( -1 ); } /*======================== Read the shape file ========================*/ /* * Loop over input files. */ currec = NULL; numrec = 0; numfil = argc - optind; for ( nf = 0; nf < numfil; nf++ ) { /* * Get the input names and open them. */ if ( debug_flag ) { printf ( "File %d: %s\n", nf, argv[optind+nf] ); } strcpy ( dbfnam[nf], argv[optind+nf] ); strcat ( dbfnam[nf], ".dbf" ); strcpy ( shxnam[nf], argv[optind+nf] ); strcat ( shxnam[nf], ".shx" ); strcpy ( shpnam[nf], argv[optind+nf] ); strcat ( shpnam[nf], ".shp" ); cfl_inqr ( dbfnam[nf], NULL, &flen, dbfnam[nf], &ier ); dbffp = cfl_ropn ( dbfnam[nf], NULL, &ier ); if ( ier != 0 ) { fprintf ( stderr, "File %s does not exist.\n", dbfnam[nf] ); exit ( -1 ); } cfl_inqr ( shxnam[nf], NULL, &flen, shxnam[nf], &ier ); shxfp = cfl_ropn ( shxnam[nf], NULL, &ier ); if ( ier != 0 ) { fprintf ( stderr, "File %s does not exist.\n", shxnam[nf] ); exit ( -1 ); } cfl_inqr ( shpnam[nf], NULL, &flen, shpnam[nf], &ier ); shpfp = cfl_ropn ( shpnam[nf], NULL, &ier ); if ( ier != 0 ) { fprintf ( stderr, "File %s does not exist.\n", shpnam[nf] ); exit ( -1 ); } /* * Detect platform endian. */ cfl_read ( shpfp, INTEGER_SIZE, (unsigned char *)&file_code, &nbin, &ier ); if ( file_code == 9994 ) { mch_endian = BIG; } else { mch_endian = LITTLE; } /* * Read database header. */ shp_rdbh ( dbffp, &dbfhdr, &ier ); numrec += dbfhdr.nrec; if ( lfld == G_TRUE ) { for ( ifld = 0; ifld < dbfhdr.nfld; ifld++ ) { printf ( "Field: %s\n", dbfhdr.dbflds[ifld].name ); } } else { /* * Read shape record and construct an internal list. */ for ( rec = 0; rec < dbfhdr.nrec; rec++ ) { /* * Read record field data. */ shp_rdbf ( dbffp, rec, &dbfhdr, &ier ); /* * Read record index. */ shp_rshx ( shxfp, rec, &shxrec, &ier ); /* * Read record data. */ shp_rshp ( shpfp, &dbfhdr, &shxrec, &newrec, &ier ); /* * Add the new record on record list. */ newrec->prvrec = currec; if ( currec != NULL ) { currec->nxtrec = newrec; } else { reclst = newrec; } currec = newrec; } } /* * Close up opened files. */ cfl_clos ( dbffp, &ier ); cfl_clos ( shxfp, &ier ); cfl_clos ( shpfp, &ier ); } if ( lfld == G_TRUE ) { shp_mfreeall ( ); exit ( 0 ); } /*======================== Read the shape exception table =============*/ _shpException = NULL; _numShpException = 0; shp_rdException ( &ier ); /*======================= Starting Post-Process =======================*/ /* * Dump shapefile records. */ if ( dump == G_TRUE ) { for ( rec = 0, currec = reclst; rec < numrec; rec++, currec = currec->nxtrec ) { shp_wfld ( stdout, currec, &ier ); shp_wrec ( stdout, currec, &ier ); } shp_mfreeall ( ); exit ( 0 ); } /* * Split record part if it has more than MAXOUT number of points * or it crosses the international dateline. */ shp_splt ( reclst, numrec, &ier ); /* * Detect the map type. */ shp_mtyp ( dbfnam, numfil, &ier ); if ( maptyp == 0 ) { fprintf ( stderr, "Unknown Map Type.\n" ); exit ( -1 ); } /* * Delete not used records. */ shp_drec ( &reclst, &numrec, &ier ); /* * Combine records that have the same key. */ shp_cmbn ( reclst, &numrec, &ier ); /* * Compute record centroid. */ mode = 1; iunit = itype = 1; xsize = ysize = 1.0F; strcpy ( device, "GN" ); strcpy ( filnam, "SHPCV" ); /* * Use North STR projection. */ strcpy ( proj, "STR" ); angle1 = 90.0F; angle2 = -90.0F; angle3 = 0.0F; lllat = -15.0F; lllon = -135.0F; urlat = -15.0F; urlon = -135.0F; ginitp ( &mode, &istat, &ier ); gsdeva ( device, &iunit, filnam, &itype, &xsize, &ysize, &ier, strlen(device), strlen(filnam) ); gsmprj ( proj, &angle1, &angle2, &angle3, &lllat, &lllon, &urlat, &urlon, &ier, strlen(proj) ); for ( currec = reclst, rec = 0; rec < numrec; rec++, currec = currec->nxtrec ) { /* * Get the shape fips code, check if there is a match in * Shape Exception Table. If yes, get manually set * clon&clat value from the table directly. */ if ( sscanf(currec->fields[3].data, "%lu", &shp_fips) == 1 ) { exception_found = FALSE; for ( ii = 0; ii < _numShpException; ii++ ) { if ( _shpException[ii].fips == shp_fips ) { currec->cenlat = _shpException[ii].clat; currec->cenlon = _shpException[ii].clon; exception_found = TRUE; break; } } if ( exception_found ) continue; } /* * Get the shape id, check if there is a match in * Shape Exception Table. If yes, get manually set * clon&clat value from the table directly. */ exception_found = FALSE; for ( ii = 0; ii < _numShpException; ii++ ) { if ( strcasecmp(currec->fields[0].data, _shpException[ii].id) == 0 ) { currec->cenlat = _shpException[ii].clat; currec->cenlon = _shpException[ii].clon; exception_found = TRUE; break; } } if ( exception_found ) continue; /* * Calculate clon&clat mathematically from an algorithm. */ shp_gctr ( currec, &clon, &clat, &ier ); if ( ier == 0 ) { currec->cenlat = clat; currec->cenlon = clon; } else { currec->cenlat = RMISSD; currec->cenlon = RMISSD; } } /* the end of for ( currec ... */ /* * Reduce number of points. */ if ( rdpt == G_TRUE ) { for ( rec = 0, currec = reclst; rec < numrec; rec++, currec = currec->nxtrec ) { for ( prt = 0, curprt = currec->shpart; prt < currec->numprt; prt++, curprt = curprt->nxtprt ) { shp_thin ( curprt, ratio, &ier ); } } } /* * Create station table. */ if ( ctbl == G_TRUE ) { shp_ctbl ( reclst, numrec, &ier ); } /* * Create bound and bound info. */ if ( cbnd == G_TRUE ) { shp_cbnd ( reclst, numrec, &ier ); } /* * Clean up. */ shp_mfreeall ( ); return 0; }
int main ( int argc, char **argv ) /************************************************************************ * clipvgf * * * * This program clips elements in a VGF file based on a bounds * * specification. By default, a simple clipping algorithm is used * * where element points either inside or outside the polygon are kept * * or thrown away based on an input flag. Alternatively, an exact * * algorithm may be requested which clips precisely at the borders. * * * * The bound definition must be in the format: * * bound_name|<area_tag_name>area_tag_value * * and must be enclosed w/ quotes so the shell will ignore directives. * * * * Examples: * * clipvgf input.vgf "STATE_BNDS|<STATE>WY" keep output.vgf rough * * clipvgf input.vgf "STATE_BNDS|<STATE>WY" keep output.vgf exact * * Where "rough" uses the simple clipping algorithm and "exact" yields * * precise clipping at the bounds borders. * * * * The following element classes are not processed: * * CLASS_WATCHES, CLASS_TRACKS, CLASS_SIGMETS * * * * main(argc, argv) * * * * Input parameters: * * argc int number of parameters of command line * * argv char** parameter array of command line * * * * Output parameters: * * Return parameters: * * NONE * * * ** * * Log: * * D.W.Plummer/NCEP 2/02 * * D.W.Plummer/NCEP 5/02 Added exact clipping and label moving * * D.W.Plummer/NCEP 8/02 Account for text grouped with symbol * * D.W.Plummer/NCEP 8/02 Create output VGF, even if it is empty * * H. Zeng/XTRIA 02/03 converted CLASS_CIRCLE to CLASS LINES * * D.W.Plummer/NCEP 8/03 Bug fix - add pt to closed lines * * R. Tian/SAIC 11/04 Added clip jet element * * S. Danz/AWC 07/06 Update to new cvg_writef() parameter * * T. Piper/SAIC 03/07 Added ninout-- for closed line case * * L. Hinson/AWC 07/07 Added clip code for GFA elements * * L. Hinson/AWC 07/07 Add recalcGFAtLblArwLoc function * for GFA elements * * X.Guo/CWS 10/10 Bug fix - Low level graphic don't make * * it all the way north in central US * * L. Hinson/AWC 09/13 Fixed improperly clipped JET_ELM Barbs * * and hashes * ***********************************************************************/ { int ii, jj, ip, ibeg, iend, loc, ne, found, found_txt, joffset, kept, ier; int wrtflg, pagflg; int minpts, maxpts, npts, numpts, npoly; char vg_class, vg_type; char bnd[128], keep[32], bnd_name[64], bnd_tag[64]; char infile[128], ifname[128], outfile[128]; char *cptr; long ifilesize; int more, curpos; float flat, flon, filt, px[LLMXPT], py[LLMXPT]; float plat[LLMXPT], plon[LLMXPT], *ptrlat, *ptrlon; float tlat[LLMXPT], tlon[LLMXPT]; float fltmin, fltmax, flnmin, flnmax; int inout[LLMXPT], tinout[LLMXPT]; char device[8], dfilnam[128], pro[32]; float xsize, ysize, lllat, lllon, urlat, urlon; float prjang1, prjang2, prjang3; int mode, istat, iunit, itype; char errgrp[8]; int ninout; float xinout[LLMXPT], yinout[LLMXPT]; char precision[8]; int tltpts, nbarb, nhash; int tmaxpts, tnpts, tnclip; char hazList[ STD_STRLEN ]; VG_DBStruct el, el_t, el_q, el_lin; FILE *ifptr; int ninxarr, inxarr[100]; /*---------------------------------------------------------------------*/ /* * First check if number of input arguments is correct. */ if ( argc < 5 ) { pagflg = G_FALSE; strcpy ( errgrp, "CLIPVGF" ); ip_help ( errgrp, &pagflg, &ier, strlen(errgrp) ); exit (0); } /* * First input on command line is input vgf file name. */ strcpy ( infile, argv[1] ); wrtflg = 0; cvg_open ( infile, wrtflg, &(ifptr), &ier ); if ( ier != 0 ) { printf("Error opening VGF file %s\n", infile ); exit (0); } cfl_inqr ( infile, NULL, &ifilesize, ifname, &ier ); /* * Second input on command line is bounds name. */ clo_init ( &ier ); strcpy ( bnd, argv[2] ); cptr = cst_split( bnd, '|', sizeof(bnd_name), bnd_name, &ier ); clo_bstype ( bnd_name, &ier ); if ( ier != 0 ) { printf("Error finding bounds type %s\n", bnd_name ); exit (0); } if ( cptr != (char *)NULL ) { strcpy ( bnd_tag, cptr ); clo_bstag ( bnd_tag, &ier ); } fltmin = -90.0F; fltmax = 90.0F; flnmin = -180.0F; flnmax = 180.0F; clo_bsarea ( &fltmin, &flnmin, &fltmax, &flnmax, &ier ); minpts = 3; maxpts = sizeof(px)/sizeof(float); filt = 0.0F; clo_bgnext ( &minpts, &maxpts, &filt, &npoly, px, py, &ier ); if ( ier < 0 ) { printf("Error retrieving bound area %s|%s\n", bnd_name, bnd_tag ); exit (0); } /* * Third input on command line is keep flag. */ strcpy ( keep, argv[3] ); /* * Fourth input on command line is output vgf file name; create it. */ strcpy ( outfile, argv[4] ); cvg_crvgf ( outfile, &ier ); /* * Fifth input on command line is clip precision = "rough" or "exact" */ if ( argv[5] != (char *)NULL ) strcpy ( precision, argv[5] ); else strcpy ( precision, "ROUGH" ); cst_lcuc ( precision, precision, &ier ); /* * All input checks out OK; set up GAREA and PROJ for inpoly. */ mode = 1; ginitp ( &mode, &istat, &ier ); strcpy ( device, "GN" ); iunit = 1; strcpy ( dfilnam, "CLIPVGF" ); itype = 1; xsize = 500.0F; ysize = 500.0F; gsdeva ( device, &iunit, dfilnam, &itype, &xsize, &ysize, &ier, strlen(device), strlen(dfilnam)); /* * Something more sophisticated may be needed here in the future * to set up a proper proj and garea based on the clip area. * For instance, the following definitions probably won't work * on a clipping bound equivalent to Antartica. */ lllat = 0.0F; lllon = -135.0F; urlat = 0.0F; urlon = 45.0F; strcpy ( pro, "str" ); prjang1 = 90.0F; prjang2 = -105.0F; prjang3 = 0.0F; gsmprj ( pro, &prjang1, &prjang2, &prjang3, &lllat, &lllon, &urlat, &urlon, &ier, strlen(pro)); /* * Loop through all the elements to set the range records. */ crg_init ( &ier ); ne = 0; more = G_TRUE; curpos = 0; ifptr = (FILE *) cfl_ropn(ifname, "", &ier); while ( ne < MAX_EDITABLE_ELEMS && more == G_TRUE ) { cvg_rdrecnoc ( ifname, ifptr, curpos, &el, &ier ); if ( ier < 0 ) { more = G_FALSE; } else { crg_set ( &el, curpos, 1, &ier ); curpos += el.hdr.recsz; ne++; } } cfl_clos ( ifptr, &ier ); /* * Loop through all the elements. */ ne = 0; more = G_TRUE; curpos = 0; ifptr = (FILE *) cfl_ropn(ifname, "", &ier); while ( ne < MAX_EDITABLE_ELEMS && more == G_TRUE ) { cvg_rdrecnoc( ifname, ifptr, curpos, &el, &ier ); if ( ier < 0 ) { more = G_FALSE; } else if ( el.hdr.recsz > 0 ) { crg_gginx( el.hdr.grptyp, el.hdr.grpnum, sizeof(inxarr)/sizeof(inxarr[0]), inxarr, &ninxarr, &ier ); /* * Increment file pointer now because element hdrsz may change. */ curpos += el.hdr.recsz; vg_class = el.hdr.vg_class; vg_type = el.hdr.vg_type; switch ( (int)vg_class ) { case CLASS_SYMBOLS: case CLASS_TEXT: case CLASS_WINDS: case CLASS_COMSYM: case CLASS_MARKER: switch ( (int)vg_type ) { case TEXT_ELM: case TEXTC_ELM: flat = el.elem.txt.info.lat; flon = el.elem.txt.info.lon; break; case SPTX_ELM: flat = el.elem.spt.info.lat; flon = el.elem.spt.info.lon; break; case BARB_ELM: case ARROW_ELM: case DARR_ELM: case HASH_ELM: flat = el.elem.wnd.data.latlon[0]; flon = el.elem.wnd.data.latlon[1]; break; case WXSYM_ELM: case CTSYM_ELM: case ICSYM_ELM: case PTSYM_ELM: case PWSYM_ELM: case SKSYM_ELM: case SPSYM_ELM: case TBSYM_ELM: case MARK_ELM: case CMBSY_ELM: flat = el.elem.sym.data.latlon[0]; flon = el.elem.sym.data.latlon[1]; break; } npts = 1; cgr_inpoly ( "M", &npts, &flat, &flon, "M", &npoly, px, py, inout, &ier ); if ( ( inout[0] == 1 && strcmp(keep,"keep") == 0 ) || ( inout[0] == 0 && strcmp(keep,"keep") != 0 ) ) { /* * Check if element is TEXT grouped with a SYMBOL. If this text was going * to be kept but it's symbol was going to be throw away, throw it away also. */ if ( (int)vg_class == CLASS_TEXT && (int)el.hdr.grptyp != 0 && ninxarr > 1 ) { found = G_FALSE; ii = 0; while ( ii < ninxarr && found == G_FALSE ) { crg_goffset ( inxarr[ii], &joffset, &ier ); cvg_rdrecnoc( ifname, ifptr, joffset, &el_q, &ier ); if ( el_q.hdr.vg_class == CLASS_SYMBOLS ) { found = G_TRUE; flat = el_q.elem.sym.data.latlon[0]; flon = el_q.elem.sym.data.latlon[1]; npts = 1; cgr_inpoly ( "M", &npts, &flat, &flon, "M", &npoly, px, py, inout, &ier ); if ((inout[0] == 1 && strcmp(keep,"keep") == 0) || (inout[0] == 0 && strcmp(keep,"keep") != 0)) { cvg_writef( &el, -1, el.hdr.recsz, outfile, FALSE, &loc, &ier ); kept = G_TRUE; } else { kept = G_FALSE; } } ii++; } if ( found == G_FALSE ) { cvg_writef( &el, -1, el.hdr.recsz, outfile, FALSE, &loc, &ier ); kept = G_TRUE; } } else { /* * non-TEXT -- keep it. */ cvg_writef( &el, -1, el.hdr.recsz, outfile, FALSE, &loc, &ier ); kept = G_TRUE; } } else { /* * Element is not to be kept. */ kept = G_FALSE; } /* * Check if element was kept and is a SYMBOL element grouped with TEXT; * make sure any text elements are saved off they were going to be thrown away. */ if ( kept == G_TRUE && (int)vg_class == CLASS_SYMBOLS && (int)el.hdr.grptyp != 0 && ninxarr > 1 ) { ii = 0; while ( ii < ninxarr ) { crg_goffset ( inxarr[ii], &joffset, &ier ); cvg_rdrecnoc( ifname, ifptr, joffset, &el_q, &ier ); if ( el_q.hdr.vg_class == CLASS_TEXT ) { el_t = el_q; switch ( (int)el_t.hdr.vg_type ) { case TEXT_ELM: case TEXTC_ELM: flat = el_t.elem.txt.info.lat; flon = el_t.elem.txt.info.lon; break; case SPTX_ELM: flat = el_t.elem.spt.info.lat; flon = el_t.elem.spt.info.lon; break; } npts = 1; cgr_inpoly ( "M", &npts, &flat, &flon, "M", &npoly, px, py, inout, &ier ); if ( ( kept == G_TRUE ) && ( ( inout[0] == 1 && strcmp(keep,"keep") == 0 ) || ( inout[0] == 0 && strcmp(keep,"keep") != 0 ) ) ) { } else { cvg_writef ( &el_t, -1, el_t.hdr.recsz, outfile, FALSE, &loc, &ier ); } } ii++; } } break; case CLASS_CIRCLE: case CLASS_LINES: case CLASS_FRONTS: /* * convert a circle element to a line element */ if ( vg_class == CLASS_CIRCLE ) { cvg_cir2lin ( &el, 10, &el_lin, &ier ); el = el_lin; vg_class = el.hdr.vg_class; vg_type = el.hdr.vg_type; } switch ( (int)vg_type ) { case LINE_ELM: npts = el.elem.lin.info.numpts; ptrlat = &(el.elem.lin.latlon[ 0]); ptrlon = &(el.elem.lin.latlon[npts]); break; case SPLN_ELM: npts = el.elem.spl.info.numpts; ptrlat = &(el.elem.spl.latlon[ 0]); ptrlon = &(el.elem.spl.latlon[npts]); break; case FRONT_ELM: npts = el.elem.frt.info.numpts; ptrlat = &(el.elem.frt.latlon[ 0]); ptrlon = &(el.elem.frt.latlon[npts]); break; } memcpy ( plat, ptrlat, (size_t)npts*sizeof(float) ); memcpy ( plon, ptrlon, (size_t)npts*sizeof(float) ); if ( el.hdr.closed == 1 ) { plat[npts] = plat[0]; plon[npts] = plon[0]; npts++; } if ( strcmp(precision,"EXACT") == 0 ) { clip_line ( npoly, px, py, npts, plat, plon, (int)el.hdr.closed, sizeof(xinout)/sizeof(float), &ninout, xinout, yinout, inout, &ier ); } else if ( strcmp(precision,"ROUGH") == 0 ) { cgr_inpoly ( "M", &npts, plat, plon, "M", &npoly, px, py, inout, &ier ); ninout = npts; memcpy ( xinout, plat, (size_t)ninout*sizeof(float) ); memcpy ( yinout, plon, (size_t)ninout*sizeof(float) ); } /* * If element is closed, and some points are to be kept and others are not, * then rotate the locations arrays such that a transition point is the first point. */ if ( el.hdr.closed == 1 ) { ip = 0; ninout--; while ( inout[ip] == inout[0] && ip < ninout ) ip++; if ( ip != ninout ) { if (( inout[0] == 1 && strcmp(keep,"keep") == 0 ) || ( inout[0] == 0 && strcmp(keep,"keep") != 0 ) ) { memcpy ( tlat, xinout, (size_t)ninout*sizeof(float) ); memcpy ( tlon, yinout, (size_t)ninout*sizeof(float) ); memcpy ( tinout, inout, (size_t)ninout*sizeof(float) ); for ( ii = 0; ii < ninout; ii++ ) { xinout[ii] = tlat[(ii+ip) % ninout]; yinout[ii] = tlon[(ii+ip) % ninout]; inout[ii] = tinout[(ii+ip) % ninout]; } } } } ip = 0; while ( ip < ninout ) { ibeg = ip; iend = ip; while ( inout[ip] == inout[ibeg] && ip < ninout ) ip++; iend = ip - 1; numpts = iend - ibeg + 1; /* * If element is closed, and some points are to be kept and others are not, * then reset the closed flag. */ if ( el.hdr.closed == 1 && numpts != ninout ) el.hdr.closed = 0; if ( numpts > 1 ) { if (( inout[ibeg] == 1 && strcmp(keep,"keep") == 0 ) || ( inout[ibeg] == 0 && strcmp(keep,"keep") != 0 ) ) { switch ( (int)vg_type ) { case LINE_ELM: el.elem.lin.info.numpts = numpts; ptrlat = &(el.elem.lin.latlon[ 0]); ptrlon = &(el.elem.lin.latlon[numpts]); el.hdr.recsz = ( (int)((sizeof(float) * 2 * (size_t)numpts) + sizeof(VG_HdrStruct) + sizeof(LineInfo) )); break; case SPLN_ELM: el.elem.spl.info.numpts = numpts; ptrlat = &(el.elem.spl.latlon[ 0]); ptrlon = &(el.elem.spl.latlon[numpts]); el.hdr.recsz = ( (int)((sizeof(float) * 2 * (size_t)numpts) + sizeof(VG_HdrStruct) + sizeof(SpLineInfo) )); break; case FRONT_ELM: el.elem.frt.info.numpts = numpts; ptrlat = &(el.elem.frt.latlon[ 0]); ptrlon = &(el.elem.frt.latlon[numpts]); el.hdr.recsz = ( (int)((sizeof(float) * 2 * (size_t)numpts) + sizeof(VG_HdrStruct) + sizeof(FrontInfo) )); break; } memcpy(ptrlat, &(xinout[ibeg]), (size_t)numpts*sizeof(float)); memcpy(ptrlon, &(yinout[ibeg]), (size_t)numpts*sizeof(float)); cvg_writef ( &el, -1, el.hdr.recsz, outfile, FALSE, &loc, &ier ); if ( (int)el.hdr.grptyp != 0 && ninxarr > 1 ) { found = G_FALSE; found_txt = G_FALSE; ii = 0; while ( ii < ninxarr && found == G_FALSE ) { crg_goffset ( inxarr[ii], &joffset, &ier ); cvg_rdrecnoc( ifname, ifptr, joffset, &el_q, &ier ); if ( el_q.hdr.vg_class == CLASS_TEXT ) { found_txt = G_TRUE; el_t = el_q; switch ( (int)el_t.hdr.vg_type ) { case TEXT_ELM: case TEXTC_ELM: flat = el_t.elem.txt.info.lat; flon = el_t.elem.txt.info.lon; break; case SPTX_ELM: flat = el_t.elem.spt.info.lat; flon = el_t.elem.spt.info.lon; break; } npts = 1; cgr_inpoly ( "M", &npts, &flat, &flon, "M", &npoly, px, py, inout, &ier ); if ( ( inout[0] == 1 && strcmp(keep,"keep") == 0 ) || ( inout[0] == 0 && strcmp(keep,"keep") != 0 ) ) { found = G_TRUE; break; } } ii++; } if ( found == G_FALSE && ii == ninxarr && found_txt == G_TRUE ) { switch ( (int)vg_type ) { case LINE_ELM: flat = ( el.elem.lin.latlon[0] + el.elem.lin.latlon[1] ) / 2.0F; flon = ( el.elem.lin.latlon[numpts] + el.elem.lin.latlon[numpts+1] ) / 2.0F; break; case SPLN_ELM: flat = ( el.elem.spl.latlon[0] + el.elem.spl.latlon[1] ) / 2.0F; flon = ( el.elem.spl.latlon[numpts] + el.elem.spl.latlon[numpts+1] ) / 2.0F; break; case FRONT_ELM: flat = ( el.elem.frt.latlon[0] + el.elem.frt.latlon[1] ) / 2.0F; flon = ( el.elem.frt.latlon[numpts] + el.elem.frt.latlon[numpts+1] ) / 2.0F; break; } switch ( (int)el_t.hdr.vg_type ) { case TEXT_ELM: case TEXTC_ELM: el_t.elem.txt.info.lat = flat; el_t.elem.txt.info.lon = flon; break; case SPTX_ELM: el_t.elem.spt.info.lat = flat; el_t.elem.spt.info.lon = flon; break; } cvg_writef ( &el_t, -1, el_t.hdr.recsz, outfile, FALSE, &loc, &ier ); } } } } } break; case CLASS_MET: switch ( (int)vg_type ) { case JET_ELM: npts = tltpts = el.elem.jet.line.spl.info.numpts; ptrlat = &(el.elem.jet.line.spl.latlon[ 0]); ptrlon = &(el.elem.jet.line.spl.latlon[npts]); memcpy ( plat, ptrlat, (size_t)npts*sizeof(float) ); memcpy ( plon, ptrlon, (size_t)npts*sizeof(float) ); if ( strcmp(precision,"EXACT") == 0 ) { clip_line ( npoly, px, py, npts, plat, plon, (int)el.hdr.closed, sizeof(xinout)/sizeof(float), &ninout, xinout, yinout, inout, &ier ); } else if ( strcmp(precision,"ROUGH") == 0 ) { cgr_inpoly ( "M", &npts, plat, plon, "M", &npoly, px, py, inout, &ier ); ninout = npts; memcpy ( xinout, plat, (size_t)ninout*sizeof(float) ); memcpy ( yinout, plon, (size_t)ninout*sizeof(float) ); } ip = 0; while ( ip < ninout ) { ibeg = ip; iend = ip; while ( inout[ip] == inout[ibeg] && ip < ninout ) ip++; iend = ip - 1; numpts = iend - ibeg + 1; if ( numpts > 1 ) { if (( inout[ibeg] == 1 && strcmp(keep,"keep") == 0 ) || ( inout[ibeg] == 0 && strcmp(keep,"keep") != 0 ) ) { memcpy ( &(el_t.hdr), &(el.hdr), sizeof(VG_HdrStruct) ); el_t.elem.jet.line.splcol = el.elem.jet.line.splcol; memcpy ( &(el_t.elem.jet.line.spl.info), &(el.elem.jet.line.spl.info), sizeof(SpLineInfo) ); el_t.elem.jet.line.spl.info.numpts = numpts; ptrlat = &(el_t.elem.jet.line.spl.latlon[ 0]); ptrlon = &(el_t.elem.jet.line.spl.latlon[numpts]); memcpy(ptrlat, &(xinout[ibeg]), (size_t)numpts*sizeof(float)); memcpy(ptrlon, &(yinout[ibeg]), (size_t)numpts*sizeof(float)); nbarb = 0; for ( ii = 0; ii < el.elem.jet.nbarb; ii++ ) { flat = el.elem.jet.barb[ii].wnd.data.latlon[0]; flon = el.elem.jet.barb[ii].wnd.data.latlon[1]; npts = 1; cgr_inpoly ( "M", &npts, &flat, &flon, "M", &npoly, px, py, tinout, &ier ); if (( tinout[0] == 1 && strcmp(keep,"keep") == 0 ) || ( tinout[0] == 0 && strcmp(keep,"keep") != 0 ) ) { memcpy ( &(el_t.elem.jet.barb[nbarb]), &(el.elem.jet.barb[ii]), sizeof(BarbAttr) ); nbarb++; } } el_t.elem.jet.nbarb = nbarb; nhash = 0; for ( ii = 0; ii < el.elem.jet.nhash; ii++ ) { flat = el.elem.jet.hash[ii].wnd.data.latlon[0]; flon = el.elem.jet.hash[ii].wnd.data.latlon[1]; npts = 1; cgr_inpoly ( "M", &npts, &flat, &flon, "M", &npoly, px, py, tinout, &ier ); if (( tinout[0] == 1 && strcmp(keep,"keep") == 0 ) || ( tinout[0] == 0 && strcmp(keep,"keep") != 0 ) ) { memcpy ( &(el_t.elem.jet.hash[nhash]), &(el.elem.jet.hash[ii]), sizeof(HashAttr) ); nhash++; } } el_t.elem.jet.nhash = nhash; cvg_writef ( &el_t, -1, el.hdr.recsz, outfile, FALSE, &loc, &ier ); } } } break; case GFA_ELM: /* Get the Hazard Type... */ cvg_getFld ( &el, TAG_GFA_AREATYPE, hazList, &ier ); npts = el.elem.gfa.info.npts; ptrlat = &(el.elem.gfa.latlon[0]); ptrlon = &(el.elem.gfa.latlon[npts]); memcpy ( plat, ptrlat, (size_t)npts*sizeof(float) ); memcpy ( plon, ptrlon, (size_t)npts*sizeof(float) ); if ( el.hdr.closed == 1 ) { plat[npts] = plat[0]; plon[npts] = plon[0]; npts++; } if(strcmp(hazList,"FZLVL")==0) { /* Is this a Freezing Level? */ if ( strcmp(precision,"EXACT") == 0 ) { clip_line ( npoly, px, py, npts, plat, plon, (int)el.hdr.closed, sizeof(xinout)/sizeof(float), &ninout, xinout, yinout, inout, &ier ); } else if (strcmp(precision,"ROUGH") == 0 ) { cgr_inpoly ( "M", &npts, plat, plon, "M", &npoly, px, py, inout, &ier ); ninout = npts; memcpy ( xinout, plat, (size_t)ninout*sizeof(float) ); memcpy ( yinout, plon, (size_t)ninout*sizeof(float) ); } if ( el.hdr.closed == 1 ) { ip = 0; ninout--; while ( inout[ip] == inout[0] && ip < ninout ) ip++; if ( ip != ninout ) { if (( inout[0] == 1 && strcmp(keep,"keep") == 0 ) || ( inout[0] == 0 && strcmp(keep,"keep") != 0 ) ) { memcpy ( tlat, xinout, (size_t)ninout*sizeof(float) ); memcpy ( tlon, yinout, (size_t)ninout*sizeof(float) ); memcpy ( tinout, inout, (size_t)ninout*sizeof(float) ); for ( ii = 0; ii < ninout; ii++ ) { xinout[ii] = tlat[(ii+ip) % ninout]; yinout[ii] = tlon[(ii+ip) % ninout]; inout[ii] = tinout[(ii+ip) % ninout]; } } } } ip = 0; while ( ip < ninout ) { ibeg = ip; iend = ip; while ( inout[ip] == inout[ibeg] && ip < ninout ) ip++; iend = ip - 1; numpts = iend - ibeg + 1; if (el.hdr.closed == 1 && numpts != ninout ) el.hdr.closed = 0; if ( numpts > 1 ) { if (( inout[ibeg] == 1 && strcmp(keep,"keep") == 0 ) || ( inout[ibeg] == 0 && strcmp(keep,"keep") != 0 )) { el.elem.gfa.info.npts = numpts; ptrlat = &(el.elem.gfa.latlon[ 0]); ptrlon = &(el.elem.gfa.latlon[numpts]); memcpy(ptrlat, &(xinout[ibeg]), (size_t)numpts*sizeof(float)); memcpy(ptrlon, &(yinout[ibeg]), (size_t)numpts*sizeof(float)); /* Recompute Default Text Label & Arrow location */ recalcGFAtLblArwLoc( &el ); el.hdr.recsz = (int) (sizeof(VG_HdrStruct) + sizeof(int)*2 + sizeof(char)* STD_STRLEN * el.elem.gfa.info.nblocks ) + sizeof(float)*numpts*2; cvg_writef ( &el, -1, el.hdr.recsz, outfile, FALSE, &loc, &ier ); } } } } else { /* We have a GFA object (that's not a freezing level) to be clipped */ /* Use clo_clip to clip the GFA Polygon against the specified bounds area. The resulting number of clipped areas (tnclips), and max points (tmaxpts) is returned */ clo_clip(&npts, plat, plon, sys_M, bnd_name, bnd_tag, &tnclip, &tmaxpts, &ier); /* Foreach of the clipped areas, get the clipped area, and write it out to the VGF file */ for (ii = 0; ii < tnclip; ii++) { clo_clipget(&ii, &tnpts, tlat, tlon, &ier); el.elem.gfa.info.npts = tnpts; ptrlat = &(el.elem.gfa.latlon[ 0]); ptrlon = &(el.elem.gfa.latlon[tnpts]); /* Re-Initialize the latlon struct. */ for (jj =0; jj < MAXPTS*2; jj++) { el.elem.gfa.latlon[jj] = 0.00; } memcpy(ptrlat, &(tlat[0]), (size_t)tnpts*sizeof(float)); memcpy(ptrlon, &(tlon[0]), (size_t)tnpts*sizeof(float)); /* Recompute Default Text Label & Arrow location */ recalcGFAtLblArwLoc( &el ); el.hdr.recsz = (int) (sizeof(VG_HdrStruct) + sizeof(int)*2 + sizeof(char)* STD_STRLEN * el.elem.gfa.info.nblocks ) + sizeof(float)*tnpts*2; cvg_writef (&el, -1, el.hdr.recsz, outfile, FALSE, &loc, &ier ); } /* Free up memory left over from the clo routines */ clo_clipdone(&ier); } break; } break; } } ne++; } cfl_clos ( ifptr, &ier ); return(0); }
int main ( int argc, char **argv ) /************************************************************************ * mdp * * * * This program generates the mesoscale discussion latlon pairings. * * Pairings will be written to a file whose filename is based on the * * input filename (filename extension, if exists, is replaced w/ "mdp").* * * * Example: * * mdp input_vgf_file.vgf * * Produces latlon pairings in the ASCII file * * output_vgf_file.mdp * * * * main(argc, argv) * * * * Input parameters: * * argc int number of parameters of command line * * argv char** parameter array of command line * * * * Output parameters: * * Return parameters: * * NONE * * * ** * * Log: * * D.W.Plummer/NCEP 6/02 * * D.W.Plummer/NCEP 8/02 Bug fix for lons > 99.995 and < 100.0 * * G. Grosshans/SPC 11/02 Updated to decode multi-scalloped lines * * G. Grosshans/SPC 12/02 Updated to compute WFO/State * * S. Jacobs/NCEP 5/03 Clean up unused variables and headers * * G. Grosshans/SPC 10/03 Updated for precision * * T. Piper/SAIC 12/05 Updated cst_wrap for CSC * * J. Wu/SAIC 04/06 Added parameter in cst_wrap * * G. Grosshans/SPC 08/08 Updated for SCN 08-45 to add LAT...LON * * and list first point as last point * * to indicate a closed polygon. Add * * word wrapping for ATTN...WFO... * ***********************************************************************/ { int ii, ix, iy, ixfirst, iyfirst, pagflg, ne, ilen, npts, ier, iret, nitems, more, curpos; long ifilesize; float x, y, flat[MAXLISTITEMS], flon[MAXLISTITEMS]; char vg_class, vg_type, buffer[2048], bufferfinal[2048], bufferfinalwfo[2048],str[20], *cptr, errgrp[12], infile[128], ifname[128], outfile[128], info[2048], stpo[4], cstl_list[1000], cstl_liststate[1000], newLineStr[13]=" "; /* 13 spaces */ char blank[2]={' '}, device[13], dfilnam[73], pro[80]; int mode, istat, iunit, itype; float xsize, ysize, lllat, lllon, urlat, urlon, prjang1, prjang2, prjang3; VG_DBStruct el; FILE *ifptr, *ofptr; const int line_len = 66; /*---------------------------------------------------------------------*/ /* * Set defaults for gsdeva and gsmprj */ mode = 1; strcpy ( device, "GN" ); iunit = 1; strcpy ( dfilnam, "MDPSPC" ); itype = 1; xsize = 500.0F; ysize = 500.0F; lllat = 10.0F; lllon = -120.0F; urlat = 50.0F; urlon = -50.0F; strcpy ( pro, "str" ); prjang1 = 90.0F; prjang2 = -105.0F; prjang3 = 0.0F; cstl_list[0] = '\0'; cstl_liststate[0] = '\0'; in_bdta ( &ier ); ginitp ( &mode, &istat, &ier); gsdeva (device, &iunit, dfilnam, &itype, &xsize, &ysize, &iret, strlen(device), strlen(dfilnam)); gsmprj ( pro, &prjang1, &prjang2, &prjang3, &lllat, &lllon, &urlat, &urlon, &iret, strlen(pro)); clo_init ( &ier ); /* * Check if number of input arguments is correct. */ if ( argc < 2 ) { pagflg = G_FALSE; strcpy ( errgrp, "MDPSPC" ); ip_help ( errgrp, &pagflg, &ier, strlen(errgrp) ); gendp (&mode, &ier); exit (0); } /* * First input on command line is input vgf file name. */ strcpy ( infile, argv[1] ); cfl_inqr ( infile, NULL, &ifilesize, ifname, &ier ); ifptr = (FILE *) cfl_ropn(ifname, "", &ier); if ( ier != 0 ) { printf("Error opening VGF file %s\n", infile ); gendp (&mode, &ier); exit (0); } /* * Output filename is input filename w/ "mdp" filename extension. */ strcpy ( outfile, infile ); cptr = strrchr( outfile, '.' ); if ( cptr != (char *)NULL ) { cptr[0] = '\0'; } strcat( outfile, ".mdp" ); /* * Loop through all the elements until a line is found. */ ne = 0; more = G_TRUE; curpos = 0; buffer[0] = '\0'; bufferfinal[0] = '\0'; bufferfinalwfo[0] = '\0'; strcat ( buffer, "LAT...LON " ); while ( ne < MAX_EDITABLE_ELEMS && more == G_TRUE ) { cvg_rdrecnoc( ifname, ifptr, curpos, &el, &ier ); if ( ier < 0 ) { more = G_FALSE; } else { curpos += el.hdr.recsz; vg_class = el.hdr.vg_class; vg_type = el.hdr.vg_type; if ( ( (int)vg_class == CLASS_LINES ) && ( el.hdr.vg_type == SPLN_ELM ) && ( (int)el.elem.spl.info.spltyp == 3 ) ) { /* * Open output file. */ ofptr = (FILE *)cfl_wopn ( outfile, &ier ); if ( ier != 0 ) { printf("Error opening/creating output file %s\n", outfile ); gendp (&mode, &ier); exit (0); } /* * Find FIPS bounded by the closed line */ npts = el.elem.spl.info.numpts; /* FIND WHAT STATES ARE IN MD AREA */ clo_binpoly ( "CNTY_BNDS", npts, el.elem.spl.latlon, &(el.elem.spl.latlon[npts]), &ier ); clo_tgltln ( "CNTY_BNDS", MAXLISTITEMS, &nitems, flat, flon, &ier); for ( ii = 0; ii < nitems; ii++ ) { clo_bginfo( "CNTY_BNDS", ii, info, &ier ); cst_gtag( "STATE", info, "?", stpo, &ier); if (strstr(cstl_liststate, stpo)==NULL) { strcat(cstl_liststate, stpo); strcat(cstl_liststate, " "); } } /* FIND WHAT WFOs ARE IN MD AREA */ clo_binpoly ( "CWA_BNDS", npts, el.elem.spl.latlon, &(el.elem.spl.latlon[el.elem.spl.info.numpts]), &ier ); clo_tgltln ( "CWA_BNDS", MAXLISTITEMS, &nitems, flat, flon, &ier); for ( ii = 0; ii < nitems; ii++ ) { clo_bginfo( "CWA_BNDS", ii, info, &ier ); cst_gtag( "WFO", info, "?", stpo, &ier); strcat(cstl_list, stpo); strcat(cstl_list, "..."); } /* * Format lats and lons into buffer. */ if ( (int)vg_type == LINE_ELM ) { npts = el.elem.lin.info.numpts; } else if ( (int)vg_type == SPLN_ELM ) { npts = el.elem.spl.info.numpts; } for ( ii = 0; ii < npts; ii++ ) { if ( (int)vg_type == LINE_ELM ) { x = el.elem.lin.latlon[ii]; y = -el.elem.lin.latlon[ii+npts]; } else if ( (int)vg_type == SPLN_ELM ) { x = el.elem.spl.latlon[ii]; y = -el.elem.spl.latlon[ii+npts]; } /* * Make sure lats and lons are rounded * to 100ths of deg. */ x = ((int)(x*100.0F)) / 100.0F; y = ((int)(y*100.0F)) / 100.0F; if ( y >= 100.0F ) y -= 100.0F; ix = G_NINT(x*100.0F); iy = G_NINT(y*100.0F); sprintf( str, "%04d%04d ", ix, iy ); strcat ( buffer, str ); if ( ii == 0 ) { ixfirst = ix; iyfirst = iy; } } /* * Repeat first lat/lon point as last lat/lon point * to indicate a closed polygon. */ sprintf( str, "%04d%04d ", ixfirst, iyfirst); strcat ( buffer, str ); sprintf( str, "\n\n" ); strcat ( buffer, str ); /* * Wrap buffer such that only 6 pairs of lat,lons * on one line. * ilen = 55; * cst_wrap( buffer, blank, &ilen, "\n", (char *)NULL, buffer, &ier ); */ ilen = 66; cst_wrap( buffer, blank, &ilen, "\n", newLineStr, buffer, &ier ); } } ne++; } /* * wrap the ATTN...WFO... line */ strcat ( bufferfinalwfo, "ATTN...WFO..." ); strcat ( bufferfinalwfo, cstl_list ); cst_wrap( bufferfinalwfo, "...", &line_len, "\n", (char *)NULL, bufferfinalwfo, &ier ); /* * build the output file string and add the ATTN...WFO... string */ strcat ( bufferfinal,"STATES=" ); strcat ( bufferfinal, cstl_liststate ); strcat ( bufferfinal, "\n\n" ); strcat ( bufferfinal, bufferfinalwfo ); /* strcat ( bufferfinal,"ATTN...WFO..." ); strcat ( bufferfinal, cstl_list ); */ strcat ( bufferfinal, "\n\n" ); strcat ( bufferfinal, buffer ); /* * Write to output file. */ cfl_writ ( ofptr, (int)strlen(bufferfinal), (unsigned char *)bufferfinal, &ier ); /* * close files and exit. */ cfl_clos ( ifptr, &ier ); cfl_clos ( ofptr, &ier ); gendp (&mode, &ier); return(0); }