int main(void) { GDBM_FILE dbf; int result; datum data; datum info; datum data2; datum info2; float lat1; float lon1; float lat2; float lon2; float distance; /* For parsing post */ char *method; char *content; int content_length = 0; char *nvpair1; char *nvpair2; char *zip_1; char *zip_2; method = (char *)getenv("REQUEST_METHOD"); if(strcmp(method,"POST") != 0) { do_error_page("Only method POST is supported."); } content_length = atoi(getenv("CONTENT_LENGTH")); if(content_length > 100) { do_error_page("Content Length expectation exceeded."); } content = (char *)malloc(content_length+1); if(content == NULL) { do_error_page("Server was unable to malloc memory. Server out of memory."); } fread(content,1,content_length,stdin); nvpair1 = strtok(content,"&"); nvpair2 = strtok('\0',"&"); zip_1 = strtok(nvpair1,"="); zip_1 = strtok('\0',"="); zip_2 = strtok(nvpair2,"="); zip_2 = strtok('\0',"="); if(strlen(zip_1) != 5) { do_error_page("Zip code #1 does not appear to be a valid US zip code."); } if(strlen(zip_2) != 5) { do_error_page("Zip code #2 does not appear to be a valid US zip code."); } if((dbf = gdbm_open("zips_gdbm",1024,GDBM_READER, 0755, 0)) ==NULL) { fprintf(stderr, "Unable to open gdbm data file.\n"); exit(1); } data.dptr = zip_1; data.dsize = 5; info = gdbm_fetch(dbf,data); if(info.dptr == NULL) { do_error_page("Zip code #1 was not found in the data base.\n"); } sscanf(info.dptr,"%f%f",&lon1, &lat1); free(info.dptr); data2.dptr = zip_2; data2.dsize = 5; info2 = gdbm_fetch(dbf,data2); if(info2.dptr == NULL) { do_error_page("Zip code #2 was not found in the data base.\n"); } sscanf(info2.dptr,"%f%f",&lon2,&lat2); free(info2.dptr); gdbm_close(dbf); distance = great_circle_distance(lat1,lon1,lat2,lon2); printf("Content-Type: text/html\n\n"); printf("<HTML><HEAD><TITLE>Zipdy Results</TITLE>\n"); printf("<BODY BGCOLOR=#FFFFFF>\n"); printf("The distance between %s and %s is: %f.\n", zip_1, zip_2, distance); printf("</BODY></HTML>\n"); free(content); return 0; }
/*********************************************************************** void create_grid_from_file( char *file, int *nlon, int *nlat, double *x, double *y, double *dx, double *dy, double *area, double *angle_dx ) the grid location is defined through ascii file. calculate cell length, cell area and rotation angle ************************************************************************/ void create_grid_from_file( char *file, int *nlon, int *nlat, double *x, double *y, double *dx, double *dy, double *area, double *angle_dx ) { double *xt, *yt, *xc, *yc; double p1[4], p2[4]; int nx, ny, nxp, nyp, ni, nj, i, j, n; int is_uniform; char mesg[256], txt[128]; /************************************************************ identify the grid_file is ascii file or netcdf file, if the file name contains ".nc", it is a netcdf file, otherwise it is ascii file. *********************************************************/ nx = *nlon; ny = *nlat; nxp = nx + 1; nyp = ny + 1; if(strstr(file, ".nc") ) { int fid, vid; ni = *nlon/2; nj = *nlat/2; xc = (double *)malloc((ni+1)*(nj+1)*sizeof(double)); yc = (double *)malloc((ni+1)*(nj+1)*sizeof(double)); xt = (double *)malloc( ni * nj *sizeof(double)); yt = (double *)malloc( ni * nj *sizeof(double)); fid = mpp_open(file, MPP_READ); if(mpp_dim_exist(fid, "grid_xt") ) get_grid_v1(fid, xt, yt, xc, yc); else if(mpp_dim_exist(fid, "rlon") || mpp_dim_exist(fid, "i") ) get_grid_v2(fid, ni, nj, xt, yt, xc, yc); else if(mpp_dim_exist(fid, "lon") ) get_grid_v3(fid, ni, nj, xt, yt, xc, yc); mpp_close(fid); for(j=0; j<nj+1; j++) for(i=0; i<ni+1; i++) { x[j*2*nxp+i*2] = xc[j*(ni+1)+i]; y[j*2*nxp+i*2] = yc[j*(ni+1)+i]; } for(j=0; j<nj; j++) for(i=0; i<ni; i++) { x[(j*2+1)*nxp+i*2+1] = xt[j*ni+i]; y[(j*2+1)*nxp+i*2+1] = yt[j*ni+i]; } /* check if the grid is uniform of not */ is_uniform = 1; for(j=0; j<nj; j++) { for(i=1; i<ni; i++) { if(yt[j*ni+i] != yt[j*ni]) is_uniform = 0; } } for(i=0; i<ni; i++) { for(j=1; j<nj; j++) { if(xt[j*ni+i] != xt[i]) is_uniform = 0; } } if(is_uniform) { for(j=0; j<nj+1; j++) for(i=0; i<ni; i++) { x[j*2*nxp+i*2+1] = xt[i]; y[j*2*nxp+i*2+1] = yc[j*(ni+1)+i]; } for(j=0; j<nj; j++) for(i=0; i<ni+1; i++) { x[(j*2+1)*nxp+i*2] = xc[j*(ni+1)+i]; y[(j*2+1)*nxp+i*2] = yt[j*ni]; } } else { for(j=0; j<nj+1; j++) for(i=0; i<ni; i++) { x[j*2*nxp+i*2+1] = (xc[j*(ni+1)+i]+xc[j*(ni+1)+i+1])*0.5; y[j*2*nxp+i*2+1] = (yc[j*(ni+1)+i]+yc[j*(ni+1)+i+1])*0.5; } for(j=0; j<nj; j++) for(i=0; i<ni+1; i++) { x[(j*2+1)*nxp+i*2] = (xc[j*(ni+1)+i]+xc[(j+1)*(ni+1)+i])*0.5; y[(j*2+1)*nxp+i*2] = (yc[j*(ni+1)+i]+yc[(j+1)*(ni+1)+i])*0.5; } } for(j=0; j<nyp; j++) for(i=0; i<nx; i++) { p1[0] = x[j*nxp+i]*D2R; p2[0] = x[j*nxp+i+1]*D2R; p1[1] = y[j*nxp+i]*D2R; p2[1] = y[j*nxp+i+1]*D2R; dx[j*nx+i] = great_circle_distance(p1, p2); } for(j=0; j<ny; j++) for(i=0; i<nxp; i++) { p1[0] = x[j*nxp+i]*D2R; p2[0] = x[(j+1)*nxp+i]*D2R; p1[1] = y[j*nxp+i]*D2R; p2[1] = y[(j+1)*nxp+i]*D2R; dy[j*nxp+i] = great_circle_distance(p1, p2); } for(j=0; j<ny; j++) for(i=0; i<nx; i++) { p1[0] = x[j*nxp+i]*D2R; p1[1] = x[j*nxp+i+1]*D2R; p1[2] = x[(j+1)*nxp+i+1]*D2R; p1[3] = x[(j+1)*nxp+i]*D2R; p2[0] = y[j*nxp+i]*D2R; p2[1] = y[j*nxp+i+1]*D2R; p2[2] = y[(j+1)*nxp+i+1]*D2R; p2[3] = y[(j+1)*nxp+i]*D2R; area[j*nx+i] = poly_area(p1, p2, 4); } /* currently set angle to 0 */ for(j=0; j<nyp; j++) for(i=0; i<nxp; i++) angle_dx[j*nxp+i] = 0; free(xt); free(yt); free(xc); free(yc); } else { create_grid_from_text_file(file, nlon, nlat, x, y, dx, dy, area, angle_dx ); } }; /* create_grid_from_file */
int main(void) { GDBM_FILE dbf; GDBM_FILE dbf2; int result; FILE *five; FILE *ten; FILE *fifteen; FILE *twenty; FILE *twenty_five; FILE *fifty; datum data; datum info; datum data2; datum data3; datum info3; datum data4; float lat1; float lon1; float lat2; float lon2; float distance; char buff[10]; if((dbf = gdbm_open("zips_gdbm",1024,GDBM_READER, 0755, 0)) ==NULL) { fprintf(stderr, "Unable to open gdbm data file.\n"); exit(1); } if((dbf2 = gdbm_open("zips_gdbm_2",1024,GDBM_READER,0755,0)) ==NULL) { fprintf(stderr, "Unable to open second gdbm data file.\n"); exit(1); } if((five = fopen("five.txt","w")) == NULL) { fprintf(stderr, "Unable to open 5 mi. out file.\n"); exit(1); } if((ten = fopen("ten.txt","w")) == NULL) { fprintf(stderr, "Unable to open 10 mi. out file.\n"); exit(1); } if((fifteen = fopen("fifteen.txt","w")) == NULL) { fprintf(stderr, "Unable to open 15 mi. out file.\n"); exit(1); } if((twenty = fopen("twenty.txt","w")) == NULL) { fprintf(stderr, "Unable to open 20 mi. out file.\n"); exit(1); } if((twenty_five = fopen("twenty_five.txt","w")) == NULL) { fprintf(stderr, "Unable to open 25 mi. out file.\n"); exit(1); } if((fifty = fopen("fifty.txt","w")) == NULL) { fprintf(stderr, "Unable to open 50 mi. out file.\n"); exit(1); } printf("Processing...\n"); data = gdbm_firstkey(dbf); while(data.dptr) { data2 = gdbm_nextkey(dbf,data); info = gdbm_fetch(dbf,data); sscanf(info.dptr,"%f%f",&lon1, &lat1); sprintf(buff,"%s",data.dptr); buff[5] = '\0'; printf("Testing Zip Code: %s\n", buff); fprintf(five,"%s:",buff); fprintf(ten,"%s:", buff); fprintf(fifteen,"%s:",buff); fprintf(twenty,"%s:", buff); fprintf(twenty_five,"%s:",buff); fprintf(fifty, "%s:",buff); free(data.dptr); free(info.dptr); data = data2; data3 = gdbm_firstkey(dbf2); while(data3.dptr) { data4 = gdbm_nextkey(dbf2,data3); info3 = gdbm_fetch(dbf2,data3); sscanf(info3.dptr,"%f%f",&lon2,&lat2); distance = great_circle_distance(lat1,lon1,lat2,lon2); data3.dptr[5] = '\0'; if(distance <= 5) { fprintf(five, " %s", data3.dptr); } if(distance <= 10) { fprintf(ten, " %s", data3.dptr); } if(distance <= 15) { fprintf(fifteen, " %s",data3.dptr); } if(distance <= 20) { fprintf(twenty, " %s",data3.dptr); } if(distance <= 25) { fprintf(twenty_five," %s",data3.dptr); } if(distance <= 50) { fprintf(fifty," %s",data3.dptr); } free(data3.dptr); free(info3.dptr); data3 = data4; } fprintf(five,"\n",buff); fprintf(ten,"\n",buff); fprintf(fifteen,"\n",buff); fprintf(twenty,"\n",buff); fprintf(twenty_five,"\n",buff); fprintf(fifty,"\n",buff); } fclose(five); fclose(ten); fclose(fifteen); fclose(twenty); fclose(twenty_five); fclose(fifty); gdbm_close(dbf); gdbm_close(dbf2); printf("Finished.\n"); return 0; }
double coordinate_calculation::great_circle_distance(const FixedPointCoordinate &coordinate_1, const FixedPointCoordinate &coordinate_2) { return great_circle_distance(coordinate_1.lat, coordinate_1.lon, coordinate_2.lat, coordinate_2.lon); }
/*********************************************************************** void create_grid_from_file( char *file, int *nlon, int *nlat, double *x, double *y, double *dx, double *dy, double *area, double *angle_dx ) the grid location is defined through ascii file. calculate cell length, cell area and rotation angle ************************************************************************/ void create_grid_from_file( char *file, int *nlon, int *nlat, double *x, double *y, double *dx, double *dy, double *area, double *angle_dx ) { double *xb, *yb, *xt, *yt, *xc, *yc; double p1[4], p2[4]; int nx, ny, nxp, nyp, ni, nj, i, j, n; FILE *pFile; char mesg[256], txt[128]; /************************************************************ identify the grid_file is ascii file or netcdf file, if the file name contains ".nc", it is a netcdf file, otherwise it is ascii file. *********************************************************/ nx = *nlon; ny = *nlat; nxp = nx + 1; nyp = ny + 1; if(strstr(file, ".nc") ) { int fid, vid; ni = *nlon/2; nj = *nlat/2; xc = (double *)malloc((ni+1)*(nj+1)*sizeof(double)); yc = (double *)malloc((ni+1)*(nj+1)*sizeof(double)); xt = (double *)malloc( ni * nj *sizeof(double)); yt = (double *)malloc( ni * nj *sizeof(double)); fid = mpp_open(file, MPP_READ); vid = mpp_get_varid(fid, "grid_lon"); mpp_get_var_value(fid, vid, xc); vid = mpp_get_varid(fid, "grid_lat"); mpp_get_var_value(fid, vid, yc); vid = mpp_get_varid(fid, "grid_lont"); mpp_get_var_value(fid, vid, xt); vid = mpp_get_varid(fid, "grid_latt"); mpp_get_var_value(fid, vid, yt); mpp_close(fid); for(j=0; j<nj+1; j++) for(i=0; i<ni+1; i++) { x[j*2*nxp+i*2] = xc[j*(ni+1)+i]; y[j*2*nxp+i*2] = yc[j*(ni+1)+i]; } for(j=0; j<nj; j++) for(i=0; i<ni; i++) { x[(j*2+1)*nxp+i*2+1] = xt[j*ni+i]; y[(j*2+1)*nxp+i*2+1] = yt[j*ni+i]; } for(j=0; j<nj+1; j++) for(i=0; i<ni; i++) { x[j*2*nxp+i*2+1] = (xc[j*(ni+1)+i]+xc[j*(ni+1)+i+1])*0.5; y[j*2*nxp+i*2+1] = (yc[j*(ni+1)+i]+yc[j*(ni+1)+i+1])*0.5; } for(j=0; j<nj; j++) for(i=0; i<ni+1; i++) { x[(j*2+1)*nxp+i*2] = (xc[j*(ni+1)+i]+xc[(j+1)*(ni+1)+i])*0.5; y[(j*2+1)*nxp+i*2] = (yc[j*(ni+1)+i]+yc[(j+1)*(ni+1)+i])*0.5; } for(j=0; j<nyp; j++) for(i=0; i<nx; i++) { p1[0] = x[j*nxp+i]; p2[0] = x[j*nxp+i+1]; p1[1] = y[j*nxp+i]; p2[1] = y[j*nxp+i+1]; dx[j*nx+i] = great_circle_distance(p1, p2); } for(j=0; j<ny; j++) for(i=0; i<nxp; i++) { p1[0] = x[j*nxp+i]; p2[0] = x[(j+1)*nxp+i]; p1[1] = y[j*nxp+i]; p2[1] = y[(j+1)*nxp+i]; dy[j*nxp+i] = great_circle_distance(p1, p2); } for(j=0; j<ny; j++) for(i=0; i<nx; i++) { p1[0] = x[j*nxp+i]; p1[1] = x[j*nxp+i+1]; p1[2] = x[(j+1)*nxp+i+1]; p1[3] = x[(j+1)*nxp+i]; p2[0] = y[j*nxp+i]; p2[1] = y[j*nxp+i+1]; p2[2] = y[(j+1)*nxp+i+1]; p2[3] = y[(j+1)*nxp+i]; area[j*nx+i] = poly_area(p1, p2, 4); } /* currently set angle to 0 */ for(j=0; j<nyp; j++) for(i=0; i<nxp; i++) angle_dx[j*nxp+i] = 0; free(xt); free(yt); free(xc); free(yc); } else { pFile = fopen (file,"r"); if(!pFile) { strcpy(mesg, "RegularSphericalGrid: Can not open ascii file "); strcat(mesg,file); mpp_error(mesg); } fscanf(pFile, "%s%*[^\n]",txt); /* header line (ignored) */ xb = (double *) malloc(nxp*sizeof(double)); yb = (double *) malloc(nyp*sizeof(double)); for(i=0;i<nxp;i++) fscanf(pFile, "%lg", xb+i); /* longitude */ fscanf(pFile, "%s%*[^\n]",txt); /* header line (ignored) */ for(j=0;j<nyp;j++) fscanf(pFile, "%lg", yb+j); /* latitude */ fclose(pFile); n=0; for(j=0; j<nyp; j++) { for(i=0; i<nxp; i++) { x[n] = xb[i]; y[n] = yb[j]; angle_dx[n++] = 0; } } /* zonal length */ n = 0; for(j=0; j<nyp; j++) { for(i=0; i<nx; i++ ) { dx[n++] = spherical_dist(x[j*nxp+i], y[j*nxp+i], x[j*nxp+i+1], y[j*nxp+i+1] ); } } /* meridinal length */ n = 0; for(j=0; j<ny; j++) { for(i=0; i<nxp; i++ ) { dy[n++] = spherical_dist(x[j*nxp+i], y[j*nxp+i], x[(j+1)*nxp+i], y[(j+1)*nxp+i] ); } } /* cell area */ n = 0; for(j=0; j<ny; j++) { for(i=0; i<nx; i++ ) { area[n++] = box_area(x[j*nxp+i]*D2R, y[j*nxp+i]*D2R, x[(j+1)*nxp+i+1]*D2R, y[(j+1)*nxp+i+1]*D2R ); } } free(xb); free(yb); } }; /* create_grid_from_file */