#include #include #include #include int main(int argc, char **argv) { long lineCtr=0, polyCtr=0; long polyId, sid, editedPingCnt, pingCnt; char line[1024], cmd[1024], cmdResults[1024]; char polygonXyFilePath[1024], metaDataFilePath[1024], uneditedCmFileDir[1024], cmFileName[1024], editedCmFileDir[1024]; FILE *ofp, *ifp; if(argc != 3){ fprintf(stderr,"%s sid_filelist_metadata.txt dirOfEditedCmFiles < all_edits.gmt > editResult.txt \n", *argv); exit (-1); } strcpy(metaDataFilePath, *++argv); strcpy(editedCmFileDir, *++argv); /* create the temp dirs if needed */ sprintf(cmd, "mkdir -p %s", editedCmFileDir); system(cmd); sprintf(cmd, "mkdir -p /tmp/polyDebug"); system(cmd); /* read from polygon.gmt file piped into stdin, skip first two comment lines */ while (fgets(line, 1024, stdin) != NULL) { if (++lineCtr > 2) break; } /* read first polygon id and sid */ sscanf(line,"> -L\"%ld\" -D\"%ld\"", &polyId, &sid); ++polyCtr; /* create a tmp file with first bad point xy value */ sprintf(polygonXyFilePath, "/tmp/polyDebug/poly%ld.sid%ld.xy", polyId, sid); ofp = fopen(polygonXyFilePath,"w"); /* read all bad point xy in this polygon */ while (fgets(line, 1024, stdin) != NULL) { while (*line != '>') { lineCtr++; fprintf(ofp, "%s", line); if (fgets(line, 1024, stdin) == NULL) { fclose(ofp); // exit (0); break; } } fclose(ofp); /* we got all the xy for this polygon. Use these to edit the cm file */ /* grep for sid in file with metadata and file paths */ sprintf(cmd, "grep %ld %s", sid, metaDataFilePath); if (ifp = popen(cmd, "r")) { fscanf(ifp,"%ld %s %s", &sid, cmFileName, uneditedCmFileDir); if ( -1 == pclose(ifp)) { printf("pclose(0x%lx) failed to execute properly; bailing...", (unsigned long)ifp); exit (-1); } } else { printf("popen(%s) failed to execute properly; bailing...", cmd); exit (-1); } /* run edit_poly_cm program and put edited cm file in $editedCmFileDir */ // edit_poly_cm 117.xy < /seasat2/data/public/SIO_multi/WEST06MV.cm > WEST06MV_e.cm sprintf(cmd, "edit_poly_cm %s < %s/%s > %s/edited_%s", polygonXyFilePath, uneditedCmFileDir, cmFileName, editedCmFileDir, cmFileName); printf("Editing cmFile %s/%s\n", uneditedCmFileDir, cmFileName); if (ifp = popen(cmd, "r")) { /* echo editing results to stout */ fscanf(ifp,"%s", cmdResults); if ( -1 == pclose(ifp)) { printf("pclose(0x%lx) failed to execute properly; bailing...", (unsigned long)ifp); exit (-1); } } else { printf("popen(%s) failed to execute properly; bailing...", cmd); exit (-1); } printf("The edited cmFile is %s/%s\n", editedCmFileDir, cmFileName); //FIXME: should check if number of points == 0 // need to redirect the stderr of the edit_poly_cm to someplace NOT stdin which is redirected to file by command, then read file if we want ping count // sscanf(cmdResults,"number of points %ld, number of points edited %ld", &pingCnt, &editedPingCnt); // printf("edited %ld of %ld points in file \n", editedPingCnt, pingCnt); /* read next poly id and sid */ sscanf(line,"> -L\"%ld\" -D\"%ld\"", &polyId, &sid); sprintf(polygonXyFilePath, "/tmp/polyDebug/poly%ld.sid%ld.xy", polyId, sid); ofp = fopen(polygonXyFilePath,"w"); ++polyCtr; } /* we have read all the polygons and edited all the cmFiles; clean up after ourselves... */ printf("read %ld polygons containing %ld nodes\n", polyCtr, lineCtr-3); // sprintf(cmd, "rm -rf /tmp/polyDebug"); system(cmd); }