void pglpfw_loadLPF ( int *iret ) /************************************************************************ * pglpfw_loadLPF * * * * This function opens a LP file and load the VG files into different * * layers by the specified color, fill modes, and group types. If the * * the VG file does not exist, it will be created. * * * * void pglpfw_loadLPF ( iret ) * * * * Input parameters: * * Output parameters: * * *iret int 0 = normal * * -1 = no file name * * -2 = error on read * * Return parameters: * * NONE * * * ** * * Log: * * T. Lee/SAIC 04/02 * * T. Lee/SAIC 05/02 added layer display mode * * E. Safford/SAIC 06/02 param change to ctb_lygetname * * J. Wu/SAIC 12/02 fix the crashing from multi-exposures * * H. Zeng/XTRIA 03/03 removed one call to pglayer_setDefGrp() * * E. Safford/SAIC 04/04 ChngMade flag TRUE if outfile specified * ***********************************************************************/ { Widget curwid; char fname[FILE_FULLSZ], vfile[FILE_FULLSZ]; char outfile[FILE_FULLSZ]; char path[LLPATH]; char gtype[10], ugtype[10], laynam[10], mode[4], umode[4]; char cmode[5], ucmode[5], *cptr; int ipos, ier, ier1, ntop, icolr, layer_num; int ii, grpid; Boolean can_open, outfile_ok; /*---------------------------------------------------------------------*/ *iret = 0; can_open = FALSE; outfile_ok = FALSE; if ( strlen(_fileName) <= (size_t)0 ) { *iret = -1; return; } else { strcpy (fname, _dirPath); strcat (fname, _fileName); cst_srch ( 0, strlen(fname), ".lpf", fname, &ipos, &ier ); if ( ier == -4 ) { strcat(fname, ".lpf"); } } /* * Open the layer product file. */ ctb_lyrd ( fname, &ntop, &ier ); if ( ntop < 0 || ier < 0 ) { er_wmsg ( "CTB", &ier, fname, &ier1, 3, strlen(fname) ); NxmErr_update(); pglayer_setChngMade ( 0, FALSE ); *iret = -2; return; } /* * Prompt error message if default is used. */ else if ( ier > 0 ) { er_wmsg ( "CTB", &ier, " ", &ier1, 3, 1 ); NxmErr_update(); } /* * Delete all records and clear drawing area */ pgpalw_deleteAll(); /* * Store the LPF into layer structure. */ ntop = G_MIN ( ntop, MAX_LAYERS ); curwid = pgpalw_getOperWid ( FUNC_LAYER ); pgpalw_setCurOper ( curwid ); pgpalw_actvLayer ( False ); for ( ii = 0; ii < ntop; ii++ ) { /* * Set color mode. */ ctb_lygetcmode ( ii, cmode, &ier ); cst_lcuc ( cmode, ucmode, &ier1 ); if ( strcmp ( ucmode, "MONO" ) == 0 ) { pglayer_setDsplClr ( ii, FALSE ); } else { pglayer_setDsplClr ( ii, TRUE ); } /* * Set color ID. */ ctb_lygetcolor ( ii, &icolr, &ier ); if ( ier == 0 ) { pglayer_setMonoClr ( ii, icolr ); } else { icolr = 19; pglayer_setMonoClr ( ii, icolr ); } /* * Get the output VG file name */ outfile_ok = FALSE; ctb_lygetoutfile( ii, outfile, &ier ); if ( strlen( outfile ) > (size_t)0 ) { /* * Check if the outfile is valid VG file. If the file does not exist, * create it. If the file is invalid, an error message is generated. * * The outfile_ok flag signals a good outfile condition. Otherwise * we have no outfile available. */ cvg_valid ( outfile, &ier ); if ( ier >= 0 ) { outfile_ok = TRUE; if ( ier > 0 ) { cvg_crvgf( outfile, &ier ); ier = 2; er_wmsg( "CVG", &ier, outfile, &ier1, 3, strlen(outfile) ); NxmErr_update(); } } else { er_wmsg ( "CVG", &ier, outfile, &ier1, 3, strlen(vfile)); NxmErr_update(); } } /* * Set input VG file name. */ ctb_lygetfile ( ii, vfile, &ier ); /* * Check if this is a valid VG file. * * If the file does not exist, and we did not find a file and there, * is no specified output file, then create one. If the file is * invalid, an error message is written out. */ cvg_valid ( vfile, &ier ); if ( ier >= 0 ) { can_open = TRUE; if ( ier > 0 && !outfile_ok ) { cvg_crvgf ( vfile, &ier ); ier = 2; er_wmsg ( "CVG", &ier, vfile, &ier1, 3, strlen(vfile)); NxmErr_update(); } } else { can_open = FALSE; er_wmsg ( "CVG", &ier, vfile, &ier1, 3, strlen(vfile)); NxmErr_update(); } if ( (cptr = strrchr (vfile, '/') ) == (char *) NULL) { pglayer_setFileName ( ii, vfile ); strcpy (path, "./"); } else { cptr++; pglayer_setFileName ( ii, cptr ); cptr[0] = '\0'; strcpy ( path, vfile ); } pglayer_setFilePath ( ii, path ); /* * Set group type. */ ctb_lygetgrptyp ( ii, gtype, &ier ); cst_lcuc ( gtype, ugtype, &ier1 ); if ( ier == 0 ) { ces_gtgid ( ugtype, &grpid, &ier1 ); if ( ier1 == 0 ) { pglayer_setDefGrp ( ii, grpid ); } } /* * Set fill mode. */ ctb_lygetfmode ( ii, mode, &ier ); cst_lcuc ( mode, umode, &ier1 ); if ( strcmp (umode, "OFF") == 0 ) { pglayer_setFill ( ii, FALSE ); } else { pglayer_setFill ( ii, TRUE ); } /* * Set display mode. */ ctb_lygetdsply ( ii, mode, &ier ); cst_lcuc ( mode, umode, &ier1 ); if ( strcmp (umode, "OFF") == 0 ) { pglayer_setDsplOn ( ii, FALSE ); } else { pglayer_setDsplOn ( ii, TRUE ); } /* * Set layer name. */ ctb_lygetname ( ii, sizeof(laynam), laynam, &ier ); if ( ier == 0 ) { pglayer_setName ( ii, laynam ); } /* * Prepare to draw in a new layer. */ pglpfw_prepLPF ( ii ); /* * Reset flags for change mode. */ pglayer_setChngMade ( ii, FALSE ); /* * Load VG files. */ if ( can_open ) { pgfilw_openVGF (FALSE, &ier); } /* * If the outfile_ok is true, then reset the file name for the layer. * * Also, since there is a specified output file in the lpf, set the * save flag so any save to it is done without the initial Save As. */ if ( outfile_ok ) { if ( (cptr = strrchr (outfile, '/') ) == (char *) NULL) { pglayer_setFileName ( ii, outfile ); strcpy (path, "./"); } else { cptr++; pglayer_setFileName ( ii, cptr ); cptr[0] = '\0'; strcpy ( path, outfile ); } pglayer_setFilePath ( ii, path ); pglayer_setChngMade ( ii, TRUE ); pglayer_setFileSaved( ii, True ); } } /* * If reaching MAX_LAYERS, set "ADD LAYER" button insensitive. */ layer_num = pglayer_getNumLayers(); if ( layer_num == MAX_LAYERS ) { pgpalw_setBtnSntv ( FUNC_LAYER, FALSE ); } /* * Bring up the layering window. */ pglayrw_manageLayers (); }
void cvg_open ( char *filnam, int wrtflg, FILE **fptr, int *iret ) /************************************************************************ * cvg_open * * * * This function opens a VG file. * * * * cvg_open ( filnam, wrtflg, fptr, iret ) * * * * Input parameters: * * *filnam char VG filename * * wrtflg int Write flag * * * * Output parameters: * * **fptr FILE File pointer * * *iret int Return code * * 2 = created vg file * * 0 = normal * * -1 = error opening VG file * * -13 = error verifying vg file * * -25 = no filehead elm in file * * -47 = no file name specified * ** * * Log: * * D. Keiser/GSC 1/97 Copied from UTF_OPEN * * E. Wehner/EAi 6/97 Create VGF if does not exist * * E. Wehner/EAi 7/97 Produce error msg on bad hdr * * E. Wehner/EAi 9/97 Handle null file name * * E. Safford/GSC 10/98 clean up & rework error mesgs * * A. Hardy/GSC 1/01 changed fptr from int to FILE * * E. Safford/SAIC 04/02 avoid cvg_rdhdr if bad filehead * * T. Lee/SAIC 11/03 used cvgcmn.h * * T. Piper/SAIC 09/06 Added wrtflg check after first cfl_inqr * ***********************************************************************/ { int ier, ier1, flag, curpos; long maxbytes; int err_log, dtl_log, dbg_log; VG_DBStruct el; char grp[4], newfil[256]; /*---------------------------------------------------------------------*/ *fptr = NULL; strcpy(grp, "CVG"); err_log = 0; dtl_log = 2; dbg_log = 4; *iret = 0; /* Check to see if there is a file of the specified name, * open it if it is there. */ if ( !filnam) { *iret = ier1 = -47; er_lmsg ( &dbg_log, grp, &ier1, filnam, &ier, strlen(grp), strlen(filnam) ); return; } cfl_inqr(filnam, NULL, &maxbytes, newfil, &ier); if ( ier < 0 && wrtflg == G_TRUE ) { /* * Attempt to create a vg file if specified file doesn't exist */ cvg_crvgf(filnam, &ier); if (ier == 0) { /* * Verify file was created */ cfl_inqr(filnam, NULL, &maxbytes, newfil, &ier); } if (ier != 0) { *iret = ier1 = -1; er_lmsg ( &err_log, grp, &ier1, filnam, &ier, strlen(grp), strlen(filnam) ); return; } else { /* * File was created successfully */ *iret = ier1 = 2; er_lmsg ( &dtl_log, grp, &ier1, filnam, &ier, strlen(grp), strlen(filnam) ); } } /* * Open file with either read, or read-write permissions */ if (wrtflg == G_TRUE) { *fptr = cfl_uopn(filnam, &ier); } else { *fptr = cfl_ropn(filnam, " ", &ier); } if (( ier != 0 ) || ( *fptr == NULL )) { /* open failed */ *iret = ier1 = -1; er_lmsg ( &err_log, grp, &ier1, filnam, &ier, strlen(grp), strlen(filnam) ); } else { if ( (size_t)maxbytes < (sizeof(FileHeadType) + sizeof(VG_HdrStruct)) ) { *iret = -25; } else { /* * Check to see that the file is a valid VGF file... */ curpos = 0; cvg_rdhdr(newfil, *fptr, curpos, (int)maxbytes, &el, &flag, &ier); if (ier != 0) { *iret = ier1 = -13; er_lmsg ( &err_log, grp, &ier1, filnam, &ier, strlen(grp), strlen(filnam) ); } if (el.hdr.vg_type != FILEHEAD_ELM) { *iret = ier1 = -25; er_lmsg ( &err_log, grp, &ier1, filnam, &ier, strlen(grp), strlen(filnam) ); } } } }
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 ) /************************************************************************ * TAGTOVGF * * * * This program converts a file of pgen elements in <tag>value format * * into a standard VGF file. * * * ** * * Log: * * D.W.Plummer/NCEP 06/03 * * S. Jacobs/NCEP 5/05 Increased size of the buffer * * S. Danz/AWC 07/06 Switch to new cvg_writefD() function * * Q. Zhou/Chu 06/12 Added converting head only file * ***********************************************************************/ { int loc, ier; VG_DBStruct el; char fn[32], vgfn[128], buffer[10000], *str; int nw; FILE *fp; /*---------------------------------------------------------------------*/ printf ( "Enter the ASCII file name to read element from :\n" ); scanf ( " %s", fn ); printf ( "Enter the VGF file name to write element to :\n" ); scanf ( " %s", vgfn ); fp = cfl_ropn ( fn, "", &ier ); nw = 0; while ( ier == 0 ) { str = fgets ( buffer, sizeof(buffer), fp ); if ( str != (char *)NULL ) { if ( buffer[0] != '!' ) { cvg_t2v ( buffer, &el, &ier ); if ( ier == 0 ) { if ( el.hdr.vg_type != FILEHEAD_ELM ) { cvg_writefD ( &el, -1, el.hdr.recsz, vgfn, &loc, &ier ); if ( ier != 0 ) { printf("ERROR writing element to file %s\n", vgfn ); } else { nw++; } } } else { printf("ERROR processing buffer=\"%s\"\n", buffer ); } } } else { ier = -1; } } if ( nw==0) { cvg_crvgf(vgfn, &ier); } printf("Number of elements decoded and written successfully = %d\n", nw ); return ( 0 ); }