#include #include #include #include /*---------------------------------------------------------*/ /* NOTE - Let height and grav be the same length (VAR_LEN) */ /* since remove_commas() works on both. */ /*---------------------------------------------------------*/ #define L_LENGTH 255 #define MAX_CHAR 80 #define VAR_LEN 11 char response[MAX_CHAR]; char version[11]; void help(); int demand_or_file(); int demand_comp(FILE *ofp); int file_comp(FILE *ifp, char *ifile, FILE *ofp); FILE *sfopen(char file[MAX_CHAR], char *mode); void print_cannot_delete(char file[MAX_CHAR]); void print_invalid_option(); void prompt_user(char text[MAX_CHAR]); void show_ofile(char ofile[MAX_CHAR]); void show_notice(); void show_disclaimer(); void wait_for_input(); void error_grav(FILE *ofp, char *pid1, char *pid2, char *grav1, char *grav2); void error_height(FILE *ofp, char *pid1, char *pid2, char *height1, char *height2); void error_pid(FILE *ofp, char *pid1, char *pid2, int *found_pid1, int *found_pid2); void error_dsdata(char *ifile); void compute_results (FILE *ofp, char *pid1, char *pid2, char *height1, char *height2, char *grav1, char *grav2, int *found_pid1, int *found_pid2, int *run_num, int run_type); void remove_commas (char *data); void error_decimal(FILE *ofp, char *pid1, char *pid2, char *height1, char *height2, char *grav1, char *grav2); void main() { FILE *ifp; FILE *ofp; char ifile[MAX_CHAR]; char ofile[MAX_CHAR]; int io_error=0; int keep_going=1; int run_type=1; strncpy(version,"\0",11); strcpy(version,"2.0"); show_disclaimer(); show_notice(); run_type = demand_or_file(); /*-----------------------------------*/ /* If by File, Get Filename. */ /*-----------------------------------*/ if (run_type == 2) { printf("\n\n\n"); strncpy(response,"\0",MAX_CHAR); printf (" Enter DSDATA Input Filename\n"); prompt_user(" "); strcpy(ifile,response); if ((ifp=sfopen(ifile,"r")) == NULL) exit(1); } /*-----------------------------------*/ /* Use a standard output filename. */ /* User will be able to delete or */ /* append if it already exists. */ /*-----------------------------------*/ strncpy(ofile,"\0",MAX_CHAR); strcpy(ofile,"lvl_dh.out"); if ((ofp=sfopen(ofile,"w")) == NULL) exit(2); /*-------------------------------------------------------*/ /* Keep prompting for two PIDs until user wants to quit. */ /*-------------------------------------------------------*/ keep_going=1; do { io_error=0; if (run_type == 2) io_error=file_comp(ifp, ifile, ofp); else io_error=demand_comp(ofp); strncpy(response,"\0",MAX_CHAR); if (io_error == 0) { printf("\ Do you want to do another computation (Y/N)?\n\ (Default is Y)\n"); prompt_user (" "); } else strcpy(response,"n"); if ((response[0] == 'n') || (response[0] == 'N')) keep_going=0; else { keep_going=1; if (run_type == 2) rewind(ifp); } } while (keep_going == 1); printf ("\n\ ===============================================================================\n"); fprintf (ofp,"\n\ ===============================================================================\n"); fclose(ofp); show_ofile(ofile); } int demand_or_file() { int type=1; int okay=0; do { printf ("\n\n\ ------------- RUN TYPE -------------\n\ Interactive Prompt...........1\n\ Dsdata File..................2\n\ Help.........................H\n\ Quit.........................Q\n\ ------------------------------------\n"); strncpy(response,"\0",MAX_CHAR); printf (" Enter Option\n"); prompt_user (" "); switch (response[0]) { case '1': type = 1; okay=1; break; case '2': type = 2; okay=1; break; case 'H': help(); break; case 'h': help(); break; case 'Q': exit(0); case 'q': exit(0); default: { printf ("\n\n INVALID OPTION - Enter 1 or 2...\n\n"); } } /*end-switch*/ } while (okay == 0); return(type); } int demand_comp(FILE *ofp) { char pid1[7]; char pid2[7]; char height1[VAR_LEN]; char height2[VAR_LEN]; char grav1[VAR_LEN]; char grav2[VAR_LEN]; int found_pid1=1; int found_pid2=1; char temp[16]; static int run_num; int run_type=1; int io_error=0; strncpy(pid1,"\0",7); strncpy(pid2,"\0",7); strncpy(height1,"\0",VAR_LEN); strncpy(height2,"\0",VAR_LEN); strncpy(grav1,"\0",VAR_LEN); strncpy(grav2,"\0",VAR_LEN); strncpy(temp,"\0",16); ++run_num; if (run_num == 1) { printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"); printf("\n\ Input height is in units of Meter.\n\ EXAMPLE - 425.342\n\ \n\ Input gravity is in units of Milligal.\n\ EXAMPLE - 979304.5\n\n"); } printf ("\n\n"); /*--------------------------------------*/ /* Get First Station height */ /*--------------------------------------*/ strncpy(response,"\0",MAX_CHAR); printf (" What is Station #1 height\n\n"); prompt_user(" "); strncpy(height1,response,strlen(response)); /*--------------------------------------*/ /* Get Second Station gravity */ /*--------------------------------------*/ strncpy(response,"\0",MAX_CHAR); printf (" What is Station #1 gravity\n\n"); prompt_user (" "); strncpy(grav1,response,strlen(response)); /*--------------------------------------*/ /* Get Second Station height */ /*--------------------------------------*/ strncpy(response,"\0",MAX_CHAR); printf ("\n\n"); printf (" What is Station #2 height\n\n"); prompt_user (" "); strncpy(height2,response,strlen(response)); /*--------------------------------------*/ /* Get First Station gravity */ /*--------------------------------------*/ strncpy(response,"\0",MAX_CHAR); printf (" What is Station #2 gravity\n\n"); prompt_user (" "); strncpy(grav2,response,strlen(response)); remove_commas(height1); remove_commas(height2); remove_commas(grav1); remove_commas(grav2); compute_results(ofp, pid1, pid2, height1, height2, grav1, grav2, &found_pid1, &found_pid2, &run_num, run_type); return(io_error); } int file_comp(FILE *ifp, char *ifile, FILE *ofp) { char line[L_LENGTH]; char pid1[7]; char pid2[7]; char height1[VAR_LEN]; char height2[VAR_LEN]; char grav1[VAR_LEN]; char grav2[VAR_LEN]; int found_pid1=0; int found_pid2=0; int results_written=0; int io_error=0; int dsdata_confirmed=0; int static run_num; int run_type=2; int i=0; ++run_num; if (run_num == 1) { printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"); printf("\n\ Input PID is 6 character alphanumeric.\n\ EXAMPLE - MN2526\n\n"); } /*-----------------------------------*/ /* Get the first PID */ /*-----------------------------------*/ strncpy(pid1,"\0",7); strncpy(response,"\0",MAX_CHAR); printf (" Enter Station #1 PID\n"); prompt_user(" "); strncpy(pid1,response,6); for(i=0;i<7;i++) if (islower(pid1[i])) pid1[i] = toupper(pid1[i]); /*-----------------------------------*/ /* Get the second PID */ /*-----------------------------------*/ strncpy(pid2,"\0",7); strncpy(response,"\0",MAX_CHAR); printf (" Enter Station #2 PID\n"); prompt_user(" "); strncpy(pid2,response,6); for(i=0;i<7;i++) if (islower(pid2[i])) pid2[i] = toupper(pid2[i]); /*------------------------------------------------------------*/ /* Read the input file and extract the data for the two PIDs */ /* Discontinue search after both gravity values are found. */ /* After both grav values are found you should already have */ /* both heights. */ /* You should never find a '1 National Geodetic Survey'*/ /* record w/both heights since the program should have found */ /* both grav values and exited before then. */ /*------------------------------------------------------------*/ strncpy(height1,"\0",VAR_LEN); strncpy(height2,"\0",VAR_LEN); strncpy(grav1,"\0",VAR_LEN); strncpy(grav2,"\0",VAR_LEN); strncpy(line,"\0",L_LENGTH); while((int)fgets(line,L_LENGTH,ifp) != 0) { if ((strncmp(line+9,"HEIGHT - ",14) == 0) || (strncmp(line+7,"* NAVD",6) == 0)) dsdata_confirmed=1; if ((strcmp(grav1,"") != 0) && (strcmp(grav2,"") != 0)) { remove_commas(height1); remove_commas(height2); remove_commas(grav1); remove_commas(grav2); compute_results(ofp, pid1, pid2, height1, height2, grav1, grav2, &found_pid1, &found_pid2, &run_num, run_type); results_written=1; break; } else if (strncmp(line+1,pid1,6) == 0) { found_pid1=1; if ((strncmp(line+9,"HEIGHT - ",14) == 0) || (strncmp(line+7,"* NAVD",6) == 0)) strncpy(height1,line+26,10); else if (strncmp(line+9,"MODELED GRAV- ",15) == 0) strncpy(grav1,line+26,10); } else if (strncmp(line+1,pid2,6) == 0) { found_pid2=1; if ((strncmp(line+9,"HEIGHT - ",14) == 0) || (strncmp(line+7,"* NAVD",6) == 0)) strncpy(height2,line+26,10); else if (strncmp(line+9,"MODELED GRAV- ",15) == 0) strncpy(grav2,line+26,10); } strncpy(line,"\0",L_LENGTH); } /*end-while*/ if (dsdata_confirmed == 0) { error_dsdata(ifile); io_error=1; } else if (results_written == 0) compute_results(ofp, pid1, pid2, height1, height2, grav1, grav2, &found_pid1, &found_pid2, &run_num, run_type); return(io_error); } void compute_results (FILE *ofp, char *pid1, char *pid2, char *height1, char *height2, char *grav1, char *grav2, int *found_pid1, int *found_pid2, int *run_num, int run_type) { double dheight1=0L; double dheight2=0L; double dgrav1=0L; double dgrav2=0L; double potdif=0L; double result=0L; double half=0.5L; double k_gal=1.0e-6L; double gconst=0.0424e-6L; int error=0; if (*run_num == 1) { printf("\ Level Height Difference Computation\n\ LVL_DH Program \n\ Version %s\n\ \n\ ===============================================================================\n\n",version); fprintf(ofp,"\ Level Height Difference Computation\n\ LVL_DH Program \n\ Version %s\n\ \n\ ===============================================================================\n\n",version); } if (run_type == 2) { printf ("\ Computation #: %d\n\ PID 1 = %-6.6s PID 2 = %-6.6s\n", *run_num,pid1,pid2); fprintf (ofp,"\ Computation #: %d\n\ PID 1 = %-6.6s PID 2 = %-6.6s\n", *run_num,pid1,pid2); } else { printf ("\ Computation #: %d\n",*run_num); fprintf (ofp,"\ Computation #: %d\n",*run_num); } /*--------------------------------------------------------*/ /* NOTE - Variable 'const' changed to 'gconst' because */ /* the word 'const' is reserved in C++ */ /*--------------------------------------------------------*/ if ((run_type == 2) && ((*found_pid1 == 0) || (*found_pid2 == 0))) { error_pid (ofp, pid1, pid2, found_pid1, found_pid2); return; } if ((strcmp(height1,"") == 0) || (strcmp(height2,"") == 0)) { error_height(ofp, pid1, pid2, height1, height2); error=1; } if ((strcmp(grav1,"") == 0) || (strcmp(grav2,"") == 0)) { error_grav(ofp, pid1, pid2, grav1, grav2); error=1; } if (error == 1) return; if ((strchr(height1,'.') == NULL) || (strchr(height2,'.') == NULL) ||(strchr(grav1,'.') == NULL) || (strchr(grav2,'.') == NULL)) { error_decimal(ofp, pid1, pid2, height1, height2, grav1, grav2); return; } remove_commas(height1); remove_commas(height2); remove_commas(grav1); remove_commas(grav2); dheight1 = atof(height1); dheight2 = atof(height2); dgrav1 = atof(grav1); dgrav2 = atof(grav2); /*---------------------*/ /* Compute output */ /*---------------------*/ dgrav1 = k_gal * dgrav1; dgrav2 = k_gal * dgrav2; potdif = ( dgrav2 + gconst * dheight2 ) * dheight2 - ( dgrav1 + gconst * dheight1 ) * dheight1 ; result = potdif / ( ( dgrav1 + dgrav2 ) * half ) ; /*------------------------------*/ /* Print results to screen */ /*------------------------------*/ printf ("\ %-11.11s %-11.11s %-11.11s %-11.11s %s\n\ %-11.11s %-11.11s %-11.11s %-11.11s %s\n\n\ %-11.11s %-11.11s %-11.11s %-11.11s %-11.3f\n\n", "HEIGHT 1", "GRAV 1", "HEIGHT 2", "GRAV 2", "LEVEL DIFF.", "(meters)", "(mgal)", "(meters)", "(mgal)", "(meters)", height1, grav1, height2, grav2, result); /*------------------------------*/ /* Print results to out file */ /*------------------------------*/ fprintf (ofp,"\ %-11.11s %-11.11s %-11.11s %-11.11s %s\n\ %-11.11s %-11.11s %-11.11s %-11.11s %s\n\n\ %-11.11s %-11.11s %-11.11s %-11.11s %-11.3f\n\n", "HEIGHT 1", "GRAV 1", "HEIGHT 2", "GRAV 2", "LEVEL DIFF.", "(meters)", "(mgal)", "(meters)", "(mgal)", "(meters)", height1, grav1, height2, grav2, result); } int available(char file[MAX_CHAR]) { FILE *ifp; int status=0; /*---------------------------------------------------------------*/ /* This fuction is used instead of the library function access() */ /* because access() is not available on unix systems. */ /*---------------------------------------------------------------*/ if ((ifp=fopen(file,"r")) != NULL) { status = 1; fclose(ifp); } else status = 0; return(status); } FILE *sfopen(char file[MAX_CHAR], char *mode) { FILE *fp; char fmode[3]; int status=0; strncpy(fmode,"\0",3); strcpy(fmode,mode); /*---------------------------------------------------------*/ /* Try to open a file as NEW. */ /*---------------------------------------------------------*/ if (strncmp(mode,"w",1) == 0) { if (available(file) == 1) { if (strstr(file,".out") != NULL) status = append_or_new(file); else status = request_remove(file); if (status == 0) return(NULL); else if (status == 1) strcpy(fmode,"w"); else if (status == 2) strcpy(fmode,"a"); } fp = fopen(file,fmode); } /*---------------------------------------------------------*/ /* Try to open a file as OLD. */ /*---------------------------------------------------------*/ else if (strncmp(mode,"r",1) == 0) { if ((fp = fopen(file, fmode)) == NULL) { fprintf (stdout,"\n \ ERROR - File %s not found.\n",file); } } return (fp); } int append_or_new(char file[MAX_CHAR]) { int status=0; fprintf (stdout,"\n WARNING - Program generated output file %s Already Exists\n",file); fprintf(stdout,"\n\ -------------- OPTIONS ------------\n\ D = Delete the file and open as new.\n\ A = Append data to end of file.\n\ Q = Quit and leave file as is.\n\ -----------------------------------\n"); strncpy(response,"\0",MAX_CHAR); prompt_user(" Enter D, A, or Q :"); switch(response[0]) { case 'D': status = remove(file); if (status < 0) print_cannot_delete(file); return(1); case 'd': status = remove(file); if (status < 0) print_cannot_delete(file); return(1); case 'A': return(2); case 'a': return(2); case 'Q': fprintf(stdout,"\n %s left as is.\n",file); return(0); case 'q': fprintf(stdout,"\n %s left as is.\n",file); return(0); default: print_invalid_option(); append_or_new(file); } return(0); } void print_cannot_delete(char file[MAX_CHAR]) { fprintf (stdout,"\n\ --------------------------------------------------------------------------\n\ ERROR - Program was unable to delete file %s\n\ You will have exit and delete via DOS command.",file); } int request_remove(char file[MAX_CHAR]) { int status=0; fprintf (stdout,"\n WARNING - %s Already Exists",file); strncpy(response,"\0",MAX_CHAR); prompt_user(" Okay to delete it (y/n) ? :"); if ((strncmp(response,"Y",1)==0) || (strncmp(response,"y",1)==0)) status = remove(file); else { fprintf(stdout,"\n %s left as is.\n",file); return(0); } if(status < 0) { fprintf (stdout,"\n\n Sorry - Program was unable to delete \ file %s",file); fprintf(stdout,"\n Delete %s by DOS command and \ try again.",file); return(0); } return(1); } void print_invalid_option() { fprintf (stdout,"\n \ INVALID OPTION -- INVALID OPTION -- INVALID OPTION -- INVALID OPTION \n \ Please Try Again...\n \ \n"); fflush(stdin); fflush(stdout); wait_for_input(); } void prompt_user(char text[MAX_CHAR]) { int c; fflush(stdin); fflush(stdout); printf ("%s",text); scanf("%[^\r\n]",response); c=getc(stdin); /* Incase code converted to K+R C */ } void show_ofile(char ofile[MAX_CHAR]) { fprintf(stdout,"\n\ ------------------------------------------------------\n\ Output File = %s\n\ ------------------------------------------------------\n",ofile); } void show_notice() { int i; for (i=0;i<25;i++) fprintf(stdout,"\n"); fprintf (stdout,"\ LVL_DH \n\ National Geodetic Survey\n\ Leveled Difference in Height Computation Program\n\ Version %s 02/10/97\n\n\ The LVL_DH program computes the expected leveled difference in height between\n\ two published NAVD 88 bench marks. The expected leveled difference in height\n\ includes the corrections documented in \"NOAA Technical Memorandum NOS NGS-34\"\n\ except for the orthometric correction, and should be very close to the\n\ actual leveled (observed) height difference.\n",version); wait_for_input(); } void show_disclaimer() { int i; for (i=0;i<25;i++) fprintf(stdout,"\n"); fprintf (stdout,"\ DISCLAIMER \n \ \n \ This program and supporting information is furnished by the government of\n \ the United States of America, and is accepted/used by the recipient with\n \ the understanding that the U. S. government makes no warranties, express or\n \ implied, concerning the accuracy, completeness, reliability, or suitability\n \ of this program, of its constituent parts, or of any supporting data.\n \ \n \ The government of the United States of America shall be under no liability \n \ whatsoever resulting from any use of this program. This program should\n \ not be relied upon as the sole basis for solving a problem whose incorrect\n \ solution could result in injury to person or property.\n \ \n \ This program is the property of the government of the United States of\n \ America. Therefore, the recipient further agrees not to assert proprietary\n \ rights therein and not to represent this program to anyone as being other\n \ than a government program.\n \ \n"); wait_for_input(); } void wait_for_input() { fprintf(stdout,"\n \ --------------------------------------------------------------------------\n"); fflush(stdin); fflush(stdout); strncpy(response,"\0",MAX_CHAR); prompt_user(" Press carriage return to Continue..."); } void help() { printf ("\n\n\ ----------------------------------- HELP ---------------------------------\n\ \n\ In INTERACTIVE mode the program will prompt for :\n\ 1. First station orthometric height.\n\ 2. First station gravity value.\n\ 3. Second station orthometric height.\n\ 4. Second station gravity value.\n\n\ Output goes to a file called lvl_dh.out.\n\ \n\ In DSDATA FILE mode the program will prompt for :\n\ 1. Digital Data Sheet (DSDATA) file name.\n\ 2. First station PID.\n\ 3. Second station PID.\n\n\ where PID is the 6 character alphanumeric identifier found on the\n\ left hand side of the National Geodetic Survey Digital Data Sheet.\n\n\ Output goes to a file called lvl_dh.out.\n"); wait_for_input(); } void error_grav(FILE *ofp, char *pid1, char *pid2, char *grav1, char *grav2) { /*----------------------------------------*/ /* Write error message to screen */ /*----------------------------------------*/ if (strcmp(grav1,"") == 0) printf ("ERROR - No modeled gravity exists for %s\n",pid1); if (strcmp(grav2,"") == 0) printf ("ERROR - No modeled gravity exists for %s\n",pid2); /*----------------------------------------*/ /* Write error message to output file. */ /*----------------------------------------*/ if (strcmp(grav1,"") == 0) fprintf (ofp,"ERROR - No modeled gravity exists for %s\n",pid1); if (strcmp(grav2,"") == 0) fprintf (ofp,"ERROR - No modeled gravity exists for %s\n",pid2); } void error_height(FILE *ofp, char *pid1, char *pid2, char *height1, char *height2) { /*----------------------------------------*/ /* Write error message to screen */ /*----------------------------------------*/ if (strcmp(height1,"") == 0) printf ("ERROR - No height exists for %s\n",pid1); if (strcmp(height2,"") == 0) printf ("ERROR - No height exists for %s\n",pid2); /*----------------------------------------*/ /* Write error message to output file. */ /*----------------------------------------*/ if (strcmp(height1,"") == 0) fprintf (ofp,"ERROR - No height exists for %s\n",pid1); if (strcmp(height2,"") == 0) fprintf (ofp,"ERROR - No height exists for %s\n",pid2); } void error_pid(FILE *ofp, char *pid1, char *pid2, int *found_pid1, int *found_pid2) { /*----------------------------------------*/ /* Write error message to screen */ /*----------------------------------------*/ if (*found_pid1 != 1) printf ("ERROR - PID %s not found.\n",pid1); if (*found_pid2 != 1) printf ("ERROR - PID %s not found.\n",pid2); /*----------------------------------------*/ /* Write error message to output file. */ /*----------------------------------------*/ if (*found_pid1 != 1) fprintf (ofp,"ERROR - PID %s not found.\n",pid1); if (*found_pid2 != 1) fprintf (ofp,"ERROR - PID %s not found.\n",pid2); } void error_decimal(FILE *ofp, char *pid1, char *pid2, char *height1, char *height2, char *grav1, char *grav2) { /*----------------------------------------*/ /* Write error message to screen */ /*----------------------------------------*/ if (strchr(height1,'.') == NULL) printf ("ERROR - Station %s is missing decimal place for height %s\n",pid1,height1); if (strchr(height2,'.') == NULL) printf ("ERROR - Station %s is missing decimal place for height %s\n",pid2,height2); if (strchr(grav1,'.') == NULL) printf ("ERROR - Station %s is missing decimal place for grav %s\n",pid1,grav1); if (strchr(grav2,'.') == NULL) printf ("ERROR - Station %s is missing decimal place for grav %s\n",pid2,grav2); /*----------------------------------------*/ /* Write error message to output file. */ /*----------------------------------------*/ if (strchr(height1,'.') == NULL) fprintf (ofp,"ERROR - Station %s is missing decimal place for height %s\n",pid1,height1); if (strchr(height2,'.') == NULL) fprintf (ofp,"ERROR - Station %s is missing decimal place for height %s\n",pid2,height2); if (strchr(grav1,'.') == NULL) fprintf (ofp,"ERROR - Station %s is missing decimal place for grav %s\n",pid1,grav1); if (strchr(grav2,'.') == NULL) fprintf (ofp,"ERROR - Station %s is missing decimal place for grav %s\n",pid2,grav2); } void error_dsdata(char *ifile) { printf ("\n\ NOTICE - No height records of any kind were found in file %s\n\ The program suspects that your input file\n\ is not an NGS Digital Data Sheet (DSDATA) file.\n",ifile); } void remove_commas (char *data) { char temp[VAR_LEN]; int i=0; int j=0; if (strchr(data,',') == NULL) return; strncpy(temp,"\0",VAR_LEN); for(i=0;i