/* qsort function - sort largest to smallest */ static int box_areasort(const void *p1, const void *p2) { const BoxPack *b1 = p1, *b2 = p2; const float a1 = box_area(b1); const float a2 = box_area(b2); if (a1 < a2) return 1; else if (a1 > a2) return -1; return 0; }
void create_grid_from_text_file( char *file, int *nlon, int *nlat, double *x, double *y, double *dx, double *dy, double *area, double *angle_dx ) { double *xb, *yb; int nx, ny, nxp, nyp, i, j, n; FILE *pFile; char mesg[256], txt[128]; nx = *nlon; ny = *nlat; nxp = nx + 1; nyp = ny + 1; pFile = fopen (file,"r"); if(!pFile) { strcpy(mesg, "create_grid_from_file: 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_text_file */
Features one_track_features(enum one_track_feature_type type, Track tr, int lookahead, int raw, void *medoids, char **objects, int ol, int discard_bad_features, Flow **flows_movie, int fl_tt, Real scale, FeatureMedoid **fms, int mn){ Real magnitude, orientation; Features f = NULL; Real tmp_x, tmp_y; int i, t, tt; assert(tr); tt = tr->tt; assert(medoids == NULL); if (raw == 0) assert(fms); switch(type){ case POSITION_X: /* Position x shouldn't be used directly as feature Distance computation is in two-track-features */ assert(raw == 1); f = allocFeatures(tt, 1); for (t = 0; t < tt; t ++) f->v[t][0] = box_center_x(tr->ds[t]); break; case POSITION_Y: /* Position y shouldn't be used directly as feature Distance computation is in two-track-features */ assert(raw == 1); f = allocFeatures(tt, 1); for (t = 0; t < tt; t ++) f->v[t][0] = box_center_y(tr->ds[t]); break; case UPPER_Y: /* Upper y shouldn't be used directly as feature */ assert(raw == 1); f = allocFeatures(tt, 1); for (t = 0; t < tt; t ++) f->v[t][0] = tr->ds[t]->y1; break; case AREA: /* This feature should be raw, will be used in box_ratio */ assert(raw == 1); f = allocFeatures(tt, 1); for (t = 0; t < tt; t ++) f->v[t][0] = box_area(tr->ds[t]); break; case MODEL_COLOR: f = allocFeatures(tt, 1); for (t = 0; t < tt; t ++){ f->v[t][0] = unpack_box_color_h(tr->ds[t]); if (raw == 0) f->v[t][0] = quantize(f->v[t][0], "color", fms, mn); } break; case MODEL_NAME: /* This feature must be provided with objects */ assert(objects); f = allocFeatures(tt, 1); for (t = 0; t < tt; t ++){ for (i = 0; i < ol; i ++) if (strcmp(tr->ds[t]->model, objects[i]) == 0) break; if (i == ol) panic("one_track_features: model %s not in the objects", tr->ds[t]->model); f->v[t][0] = i; } break; case FLOW_MAGNITUDE_IN_BOX: case FLOW_ORIENTATION_IN_BOX: assert(flows_movie); /* Notice: the flows_movie should have been padded with the last one before being passed in. The actual length relationship should be (tt + lookahead - 1) == (tr->tt) */ assert(tt == fl_tt); f = allocFeatures(tt, 1); for (t = 0; t < tt; t ++){ tmp_x = average_flow_x_in_box(tr->ds[t], flows_movie[t], scale); tmp_y = average_flow_y_in_box(tr->ds[t], flows_movie[t], scale); magnitude = DISTANCE_2D(0, 0, tmp_x, tmp_y); orientation = atan2(tmp_y, tmp_x); if (raw == 0){ magnitude = quantize(magnitude, "velocity", fms, mn); orientation = quantize(orientation, "orientation", fms, mn); } if (type == FLOW_MAGNITUDE_IN_BOX) f->v[t][0] = magnitude; else f->v[t][0] = orientation; } break; default: panic("one_track_features: type %d unrecognized", type); } /* Discard bad (padded) features if we wish */ if (discard_bad_features == 1){ int new_tt = tt - (lookahead - 1); for (t = new_tt; t < tt; t ++) free(f->v[t]); f->tt = new_tt; } return f; }
/*********************************************************************** 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 */