void scale_dump24() const { scale_data( this->scaled_buffer.get(), this->drawable.data(), this->scaled_width, this->drawable.width(), this->scaled_height, this->drawable.height(), this->drawable.rowsize()); ::transport_dump_png24( this->trans, this->scaled_buffer.get(), this->scaled_width, this->scaled_height, this->scaled_width * 3, false); }
void scale_dump24() { uint8_t * scaled_data = static_cast<uint8_t *>(malloc(this->scaled_width * this->scaled_height * 3)); scale_data(scaled_data, this->drawable.data, this->scaled_width, this->drawable.width, this->scaled_height, this->drawable.height, this->drawable.rowsize); ::transport_dump_png24(&this->trans, scaled_data, this->scaled_width, this->scaled_height, this->scaled_width * 3, true); free(scaled_data); }
static void copy_frame(struct conversion_info *ci_ptr) { long start[5]; long count[5]; size_t nitems; message(MSG_INFO, "Inserting frame #%d\n", ci_ptr->frame_index); /* Actually read the data from the image file. */ nitems = fread(ci_ptr->frame_buffer, ci_ptr->frame_nbytes, 1, ci_ptr->img_fp); if (nitems != 1) { message(MSG_FATAL, "Read failed with error %d, return %d\n", errno, nitems); exit(-1); } /* Setup the starts and counts for the data block. */ start[DIM_T] = ci_ptr->frame_index - ci_ptr->frame_zero; start[DIM_X] = 0; start[DIM_Y] = 0; start[DIM_Z] = 0; start[DIM_W] = 0; count[DIM_T] = 1; count[DIM_X] = ci_ptr->dim_lengths[DIM_X]; count[DIM_Y] = ci_ptr->dim_lengths[DIM_Y]; count[DIM_Z] = ci_ptr->dim_lengths[DIM_Z]; count[DIM_W] = ci_ptr->dim_lengths[DIM_W]; /* Perform swapping if necessary. */ if (ci_ptr->swap_size != 0) { swap_data(ci_ptr->swap_size, ci_ptr->frame_nvoxels, ci_ptr->frame_buffer); } /* Scale the raw data into the final range. */ scale_data(ci_ptr->minc_type, ci_ptr->frame_nvoxels, ci_ptr->frame_buffer, COMBINED_SCALE_FACTOR(ci_ptr)); /* For now we perform no conversions on the data as it is stored. * This may be worth modifying in the future, to allow storage of * non-floating-point formats from a typical microPET file. */ ncvarput(ci_ptr->mnc_fd, ncvarid(ci_ptr->mnc_fd, MIimage), start, count, ci_ptr->frame_buffer); }
static ui08 *unthresholded_forecast(storm_file_handle_t *s_handle, track_file_handle_t *t_handle, vol_file_handle_t *v_handle, vol_file_handle_t *map_v_handle, mdv_grid_t *grid, int scan_num, double *precip_lookup) { static int First_call = TRUE; static ui08 *Forecast_grid; static ui08 *Comp_grid; static double *Precip_grid; static si32 Npoints; ui08 *refl; int ix, iy, i_int; int delta_ix, delta_iy; int refl_start_ix, refl_end_ix; int refl_start_iy, refl_end_iy; double lead_time_hr; double time_to_ground_hr; double forecast_delta_x; double forecast_delta_y; double mean_dx_dt; double mean_dy_dt; double contrib_time; double *precip; field_params_t *precip_fparams; /* * allocate grids */ if (First_call) { Npoints = grid->ny * grid->nx; Forecast_grid = (ui08 *) umalloc (Npoints * sizeof(ui08)); Comp_grid = (ui08 *) umalloc (Npoints * sizeof(ui08)); Precip_grid = (double *) umalloc (Npoints * sizeof(double)); First_call = FALSE; } /* * zero out precip grid */ memset((void *) Precip_grid, 0, Npoints * sizeof(double)); /* * create composite grid */ create_composite(Npoints, mdv_comp_base(grid), mdv_comp_top(grid), v_handle->field_plane[Glob->params.dbz_field], Comp_grid); if (get_mean_motion(s_handle, t_handle, scan_num, &mean_dx_dt, &mean_dy_dt)) { return (NULL); } /* * loop through the forecast times */ for (i_int = 0; i_int < N_forecast_intervals; i_int++) { contrib_time = Interval_contrib_time[i_int]; lead_time_hr = Interval_lead_time[i_int]; time_to_ground_hr = Glob->params.time_to_ground / 3600.0; forecast_delta_x = (mean_dx_dt * (lead_time_hr + time_to_ground_hr) / grid->dx); forecast_delta_y = (mean_dy_dt * (lead_time_hr + time_to_ground_hr) / grid->dy); delta_ix = (long) floor(forecast_delta_x + 0.5); delta_iy = (long) floor(forecast_delta_y + 0.5); /* * move the refl grid by the forecast amount in each dirn, * and map it onto the precip grid */ if (delta_ix < 0) { refl_start_ix = -delta_ix; refl_end_ix = grid->nx - 1; } else { refl_start_ix = 0; refl_end_ix = grid->nx - 1 - delta_ix; } if (delta_iy < 0) { refl_start_iy = -delta_iy; refl_end_iy = grid->ny - 1; } else { refl_start_iy = 0; refl_end_iy = grid->ny - 1 - delta_iy; } if (Glob->params.debug) { fprintf(stderr, "forecast_delta_x, forecast_delta_y: %g, %g\n", forecast_delta_x, forecast_delta_y); fprintf(stderr, "delta_ix, delta_iy: %d, %d\n", delta_ix, delta_ix); fprintf(stderr, "refl_start_ix, refl_start_iy: %d, %d\n", refl_start_ix, refl_start_ix); fprintf(stderr, "refl_end_ix, refl_end_iy: %d, %d\n", refl_end_ix, refl_end_ix); } for (iy = refl_start_iy; iy <= refl_end_iy; iy++) { refl = Comp_grid + (iy * grid->nx) + refl_start_ix; precip = (Precip_grid + ((iy + delta_iy) * grid->nx) + (refl_start_ix + delta_ix)); for (ix = refl_start_ix; ix <= refl_end_ix; ix++, refl++, precip++) { *precip += precip_lookup[*refl] * contrib_time; } /* ix */ } /* iy */ } /* i_int */ /* * scale the precip array and copy into the first field * of the volume index */ precip_fparams = map_v_handle->field_params[0]; scale_data(Precip_grid, Forecast_grid, Npoints, precip_fparams->factor, &precip_fparams->scale, &precip_fparams->bias); return (Forecast_grid); }
static ui08 *thresholded_forecast(storm_file_handle_t *s_handle, track_file_handle_t *t_handle, vol_file_handle_t *v_handle, vol_file_handle_t *map_v_handle, mdv_grid_t *grid, int scan_num, double *precip_lookup) { static int First_call = TRUE; static ui08 *Forecast_grid; static ui08 *Comp_grid; static double *Precip_grid; static int Npoints; ui08 *dbz_plane; ui08 *comp, *layer; ui08 comp_val; int comp_base, comp_top; si32 ix, jx, iy, jy; si32 ientry, irun, i_int; si32 n_entries; si32 grid_index; si32 cart_min_ix, cart_min_iy; si32 cart_max_ix, cart_max_iy; double lead_time_hr; double time_to_ground_hr; double dx_dt, dy_dt; double darea_dt; double current_area, forecast_area; double area_ratio, length_ratio; double grid_x, grid_y; double current_x, current_y; double forecast_x, forecast_y; double delta_area; double delta_precip; double time_to_zero_growth; double x, y; double bounding_minx, bounding_miny; double bounding_maxx, bounding_maxy; double xratio, yratio; double forecast_ix, forecast_iy; double contrib_time; mdv_grid_t fcast_cart; cart_params_t *cart; field_params_t *precip_fparams; storm_file_global_props_t *gprops; storm_file_run_t *run; track_file_entry_t *entry; track_file_forecast_props_t *fprops; cart = &v_handle->vol_params->cart; /* * allocate grids */ if (First_call) { Npoints = grid->ny * grid->nx; Forecast_grid = (ui08 *) umalloc (Npoints * sizeof(ui08)); Comp_grid = (ui08 *) umalloc (Npoints * sizeof(ui08)); Precip_grid = (double *) umalloc (Npoints * sizeof(double)); First_call = FALSE; } /* * zero out precip grid */ memset((void *) Precip_grid, 0, Npoints * sizeof(double)); /* * read in track file scan entries */ if (RfReadTrackScanEntries(t_handle, scan_num, "forecast_generate")) return (NULL); n_entries = t_handle->scan_index[scan_num].n_entries; /* * loop through the entries */ entry = t_handle->scan_entries; for (ientry = 0; ientry < n_entries; ientry++, entry++) { gprops = s_handle->gprops + entry->storm_num; fprops = &entry->dval_dt; /* * read in storm props */ if (RfReadStormProps(s_handle, entry->storm_num, "forecast_generate")) { return (NULL); } /* * compute current posn and area */ current_x = gprops->proj_area_centroid_x; current_y = gprops->proj_area_centroid_y; current_area = gprops->proj_area; /* * compute current posn in terms of the cartesian grid */ grid_x = (current_x - grid->minx) / grid->dx; grid_y = (current_y - grid->miny) / grid->dy; /* * compute rates of change of posn and area */ if (entry->forecast_valid) { dx_dt = fprops->proj_area_centroid_x; dy_dt = fprops->proj_area_centroid_y; darea_dt = fprops->proj_area; } else { dx_dt = 0.0; dy_dt = 0.0; darea_dt = 0.0; } /* * loop through the forecast intervals */ for (i_int = 0; i_int < N_forecast_intervals; i_int++) { contrib_time = Interval_contrib_time[i_int]; lead_time_hr = Interval_lead_time[i_int]; time_to_ground_hr = Glob->params.time_to_ground / 3600.0; /* * compute the forecast storm position and area * for the forecast interval time, allowing for the time * the precip takes to get to the ground */ forecast_x = current_x + dx_dt * (lead_time_hr + time_to_ground_hr); forecast_y = current_y + dy_dt * (lead_time_hr + time_to_ground_hr); if (darea_dt < 0.0) { /* * linear trend for decay */ delta_area = darea_dt * lead_time_hr; } else { /* * second order trend for growth */ time_to_zero_growth = 1000.0; delta_area = (darea_dt * lead_time_hr - (darea_dt * lead_time_hr * lead_time_hr) / (time_to_zero_growth * 2.0)); } forecast_area = current_area + delta_area; if (forecast_area <= 0) continue; /* * Compute the cartesian grid parameters at the forecast time. * Here we are assuming that the storm grows or decays in area * but keeps the same reflectivity pattern. So we make the * precip map by moving and growing/shrinking the reflectivty * grid and remapping onto a precip map */ area_ratio = forecast_area / current_area; length_ratio = sqrt(area_ratio); fcast_cart.dx = grid->dx * length_ratio; fcast_cart.dy = grid->dy * length_ratio; fcast_cart.minx = forecast_x - grid_x * fcast_cart.dx; fcast_cart.miny = forecast_y - grid_y * fcast_cart.dy; /* * zero out comp grid */ memset((void *) Comp_grid, 0, Npoints * sizeof(ui08)); /* * load up comp grid */ comp_base = cart_comp_base(cart); comp_top = cart_comp_top(cart); run = s_handle->runs; for (irun = 0; irun < gprops->n_runs; irun++, run++) { if (run->iz < comp_base || run->iz > comp_top) { continue; } dbz_plane = v_handle->field_plane[Glob->params.dbz_field][run->iz]; grid_index = run->iy * grid->nx + run->ix; layer = dbz_plane + grid_index; comp = Comp_grid + grid_index; for (ix = 0; ix < run->n; ix++, comp++, layer++) { if (*comp < *layer) { *comp = *layer; } } /* ix */ } /* irun */ /* * remap comp grid onto forecast grid, accounting for the * storm movement and change in size. Accumulate the precip * depth */ bounding_minx = gprops->bounding_min_ix * fcast_cart.dx + fcast_cart.minx; cart_min_ix = (long) ((bounding_minx - grid->minx) / grid->dx + 0.5); cart_min_ix = MAX(0, cart_min_ix); cart_min_ix = MIN(grid->nx - 1, cart_min_ix); bounding_miny = gprops->bounding_min_iy * fcast_cart.dy + fcast_cart.miny; cart_min_iy = (long) ((bounding_miny - grid->miny) / grid->dy + 0.5); cart_min_iy = MAX(0, cart_min_iy); cart_min_iy = MIN(grid->ny - 1, cart_min_iy); bounding_maxx = gprops->bounding_max_ix * fcast_cart.dx + fcast_cart.minx; cart_max_ix = (long) ((bounding_maxx - grid->minx) / grid->dx + 0.5); cart_max_ix = MAX(0, cart_max_ix); cart_max_ix = MIN(grid->nx - 1, cart_max_ix); bounding_maxy = gprops->bounding_max_iy * fcast_cart.dy + fcast_cart.miny; cart_max_iy = (long) ((bounding_maxy - grid->miny) / grid->dy + 0.5); cart_max_iy = MAX(0, cart_max_iy); cart_max_iy = MIN(grid->ny - 1, cart_max_iy); y = cart_min_iy * grid->dy + grid->miny; yratio = grid->dy / fcast_cart.dy; forecast_iy = (y - fcast_cart.miny) / fcast_cart.dy; for (iy = cart_min_iy; iy <= cart_max_iy; iy++, forecast_iy += yratio) { jy = (long) (forecast_iy + 0.5); x = cart_min_ix * grid->dx + grid->minx; xratio = grid->dx / fcast_cart.dx; forecast_ix = (x - fcast_cart.minx) / fcast_cart.dx; for (ix = cart_min_ix; ix <= cart_max_ix; ix++, forecast_ix += xratio) { jx = (long) (forecast_ix + 0.5); comp_val = Comp_grid[jy * grid->nx + jx]; grid_index = iy * grid->nx + ix; delta_precip = precip_lookup[comp_val] * contrib_time; Precip_grid[grid_index] += delta_precip; } /* ix */ } /* iy */ } /* i_int */ } /* ientry */ /* * scale the precip array and copy into the first field * of the volume index */ precip_fparams = map_v_handle->field_params[0]; scale_data(Precip_grid, Forecast_grid, Npoints, precip_fparams->factor, &precip_fparams->scale, &precip_fparams->bias); return (Forecast_grid); }
void freepsgold(int* sampsize, double* xdata, double* ydata, int* ord, int* numknot, int* initseed, int* initstream, double* lambda, double* optknot, double* trace_hat, double* GCV, double* GSJS_value) { int i, order, n, nobs, inform, seed, stream; double bestlambda; double shift, scale; struct L2_1D_DATA * origdat, * sortdat; struct SP_1D * best_sp; /* set order and dimension for spline, seed, and stream */ order = *ord; n = *numknot; n += order; seed = *initseed; stream = *initstream; /* load data into data struct */ origdat = data_1d_initialize(*sampsize, &inform); for(i=0; i<*sampsize; i++) { origdat->xdata[i] = xdata[i]; origdat->ydata[i] = ydata[i]; } nobs = origdat->nobs; sortdat = data_1d_initialize(nobs, &inform); dat_psgold = data_1d_initialize(nobs, &inform); sort_data(origdat, sortdat); scale_data(sortdat, dat_psgold, &shift, &scale); sp_psgold = sp_1d_initialize( order, n, &inform); sp_1d_set_knots(sp_psgold, 0., 1.); best_sp = sp_1d_initialize( order, n, &inform); sp_1d_set_knots(best_sp, 0., 1.); bestlambda = slave_psgold(shift, scale, seed, stream); /* Save information */ /* Lambda */ *lambda = bestlambda * pow(scale, 2*sp_psgold->order-2); /* Optimal knots */ for (i = 0; i <*numknot; i++) optknot[i] = shift + scale * sp_psgold->knot[i+*ord]; /* trace of hat matrix */ *trace_hat = trace_hat_matrix(dat_psgold, sp_psgold, bestlambda); /* GCV */ *GCV = gcv(dat_psgold, sp_psgold, bestlambda, MIN_SPACE_PSGOLD); /* GSJS */ *GSJS_value = GSJS(dat_psgold); }
f_all_sol opt_melt_diff_conc(f_info *f_inp, int nf, fit_params f_parm, bool bVerbose ,const char *out ) { // // Scale data // fit all melting curves taking into account that // we want to have a straight line 1/Tm vs log(conc) // int i=0; int iter=0; real *t_min; real *t_max; real *t_min_opt; real *t_max_opt; real r_number=0; real r_sq_min=0; FILE *f_fit; f_all_sol mf; real *fduplex; bool bUpdate; snew(fduplex,nf); snew(t_min,nf); snew(t_max,nf); snew(t_min_opt,nf); snew(t_max_opt,nf); // remember to free max_abs and t_height and c_fix find_c_fix(f_inp,nf); scale_data(f_inp,nf); compute_derivatives(f_inp,nf); suggest_tm2_dh2(f_inp,nf,bVerbose); if(bVerbose){ print_multifit_info(f_inp, nf); } // we do a number of iterations // and find the boundary conditions // that give the best r_2 // We do not update the tm2 guess in order to have // reproduceble fitting results. Once we have found // the best fit, we can keep the results bUpdate = FALSE; for(iter=0; iter<f_parm.max_MC_iter; iter++){ // printf("\rProgress %3.2f", (real)iter/(real) f_parm.max_MC_iter); // fflush(stdout); for(i=0; i<nf; i++){ r_number = (real)rand()/(real)RAND_MAX; t_min[i] = (f_inp[i].bf - f_parm.wt) + r_number*2*f_parm.wt ; r_number = (real)rand()/(real)RAND_MAX; t_max[i] = (f_inp[i].ef - f_parm.wt) + r_number*2*f_parm.wt ; } // Do the fit // Here you have to add the straight line fitting, besides working with all // It is here that we select based on the tmin/tmax mf=do_fit_duplex(f_inp, nf, t_min, t_max, bVerbose, bUpdate); if ( mf.r2 > 0 && mf.r2 > r_sq_min ){ for(i=0; i<nf; i++){ t_min_opt[i] = t_min[i]; t_max_opt[i] = t_max[i]; } r_sq_min = mf.r2; } } printf("\n"); // We should have found the optimals, keep the fit bUpdate = TRUE; mf=do_fit_duplex(f_inp, nf, t_min_opt, t_max_opt, bVerbose, bUpdate); eval_fit_straight(mf, f_inp, nf, fduplex); //create_fit_curve(f_inp[i].x,ftrip,f_inp[i].nts,solution); // Output experimental vs fitted if(out) { f_fit = ffopen(out,"w"); for(j=0; j<nf; j++){ fprintf(f_fit,"%f %f \n", log(f_inp[j].conc), 1.0/f_inp[j].tm2); } fprintf(f_fit,"&\n"); for(j=0; j<nf; j++){ fprintf(f_fit,"%f %f \n", log(f_inp[j].conc), fduplex[j]); } fclose(f_fit); } //Optim T solutions printf("Opt fit DG %4.2f DH %4.2f DS %4.2f - R_2 %4.2f\n",mf.dg2, mf.dh2, mf.ds2, mf.r2) ; if (bVerbose){ printf("\n") ; for(i=0; i<nf; i++){ printf(" %d - T_min %4.2f T_max %4.2f\n",i,t_min_opt[i], t_max_opt[i]) ; } } sfree(t_min); sfree(t_max); sfree(t_min_opt); sfree(t_max_opt); return mf; }
int main ( ) /******************************************************************************/ /* Purpose: MAIN is the main program for TRIANGULATE. Modified: 02 January 2013 Author: Joseph O'Rourke Reference: Joseph ORourke, Computational Geometry, Second Edition, Cambridge, 1998, ISBN: 0521649765, LC: QA448.D38. */ { tVertex a; double area; int nvertices; tVertex p; int scale; int xmax; int xmin; int ymax; int ymin; read_vertices ( &nvertices ); scale_data ( &xmin, &xmax, &ymin, &ymax, &scale ); print_vertices ( nvertices, xmin, xmax, ymin, ymax, scale ); area = 0.5 * ( double ) area_poly2 ( ); printf ( "%%Area of polygon = %g\n", area ); if ( area == 0.0 ) { fprintf ( stderr, "\n" ); fprintf ( stderr, "TRIANGULATE - Fatal error!\n" ); fprintf ( stderr, " Computed area of polygon is zero.\n" ); exit ( 1 ); } if ( area < 0.0 ) { fprintf ( stderr, "\n" ); fprintf ( stderr, "TRIANGULATE - Fatal error!\n" ); fprintf ( stderr, " Computed area of polygon is negative.\n" ); fprintf ( stderr, " The vertices are probably listed in clockwise order.\n" ); fprintf ( stderr, " Revise the input file by reversing the vertex order.\n" ); exit ( 1 ); } /* Refuse to accept input if two consecutive vertices are equal. */ p = vertices; a = p->next; do { if ( p->v[X] == a->v[X] && p->v[Y] == a->v[Y] ) { fprintf ( stderr, "\n" ); fprintf ( stderr, "TRIANGULATE - Fatal error!\n" ); fprintf ( stderr, " Vertices %d and %d are equal.\n", p->vnum, a->vnum ); exit ( 1 ); } p = a; a = p->next; } while ( a->next != vertices ); /* Compute the triangulation. */ triangulate ( nvertices, xmin, xmax, ymin, ymax, scale ); printf ( "showpage\n%%%%EOF\n" ); /* Terminate. */ return 0; }