time_t forecast_generate(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, date_time_t *scan_times, si32 scan_num, double lead_time_requested) { ui08 *forecast; char rdata_file_path[MAX_PATH_LEN]; char map_file_dir[MAX_PATH_LEN]; char map_file_path[MAX_PATH_LEN]; int n_scans; double lead_time_used, lead_time_hr; double precip_lookup[256]; double coeff, expon; time_t time_this_scan, time_latest_scan; time_t scan_time_diff; time_t forecast_time; date_time_t *stime, ftime; cart_params_t *cart; mdv_grid_t grid; field_params_t *dbz_fparams; PMU_auto_register("In forecast_generate"); lock_and_read_headers(s_handle, t_handle); /* * set up times */ stime = scan_times + scan_num; time_this_scan = stime->unix_time; scan_time_diff = time_latest_scan - time_this_scan; n_scans = s_handle->header->n_scans; time_latest_scan = scan_times[n_scans - 1].unix_time; /* * compute forecast time */ forecast_time = time_this_scan + lead_time_requested; if (Glob->params.round_forecast_times) { forecast_time = (time_t) ((int) floor((double) forecast_time / Glob->params.rounding_interval + 0.5) * Glob->params.rounding_interval); } lead_time_used = forecast_time - time_this_scan; lead_time_hr = lead_time_used / 3600.0; initialize(lead_time_used); /* * get path name for map file */ ftime.unix_time = stime->unix_time; uconvert_from_utime(&ftime); sprintf(map_file_dir, "%s%s%.4ld%.2ld%.2ld", Glob->params.map_dir, PATH_DELIM, (long) ftime.year, (long) ftime.month, (long) ftime.day); sprintf(map_file_path, "%s%s%.2ld%.2ld%.2ld.%s", map_file_dir, PATH_DELIM, (long) ftime.hour, (long) ftime.min, (long) ftime.sec, Glob->params.output_file_ext); /* * Check if we should write this file. If not, return -1 */ if (!check_write(map_file_path)) { unlock_files(s_handle, t_handle); return (-1); } sprintf(rdata_file_path, "%s%s%.4ld%.2ld%.2ld%s%.2ld%.2ld%.2ld.%s", Glob->params.rdata_dir, PATH_DELIM, (long) stime->year, (long) stime->month, (long) stime->day, PATH_DELIM, (long) stime->hour, (long) stime->min, (long) stime->sec, Glob->params.output_file_ext); if (Glob->params.debug >= DEBUG_NORM) { fprintf(stderr, "\nScan %ld, time %s\n", (long) scan_num, utimestr(stime)); fprintf(stderr, "Rdata path: %s\n", rdata_file_path); fprintf(stderr, "Map path: %s\n", map_file_path); fprintf(stderr, "lead_time_requested: %g\n", lead_time_requested); fprintf(stderr, "lead_time_used: %g\n", lead_time_used); } /* if (Glob->params.debug >= DEBUG_NORM) */ /* * read in radar volume */ v_handle->vol_file_path = rdata_file_path; if (RfReadVolume(v_handle, "forecast_generate")) { fprintf(stderr, "ERROR - %s:forecast_generate\n", Glob->prog_name); fprintf(stderr, "Could not read in Mdv radar volume\n"); perror(rdata_file_path); unlock_files(s_handle, t_handle); return (-1); } /* * get ZR params */ if (Glob->params.get_zr_from_file) { if (RfGetZrClosest(Glob->params.zr_dir, stime->unix_time, 7200, &coeff, &expon)) { coeff = Glob->params.ZR.coeff; expon = Glob->params.ZR.expon; } } else { coeff = Glob->params.ZR.coeff; expon = Glob->params.ZR.expon; } dbz_fparams = v_handle->field_params[Glob->params.dbz_field]; compute_precip_lookup(precip_lookup, dbz_fparams, coeff, expon); /* * check geometry */ cart = &v_handle->vol_params->cart; if (check_cart_geom(cart, &s_handle->scan->grid)) { fprintf(stderr, "ERROR - %s:forecast_generate\n", Glob->prog_name); fprintf(stderr, "Radar and storm file cart geometry does not match\n"); unlock_files(s_handle, t_handle); return (-1); } cart_params_to_mdv_grid(cart, &grid, s_handle->scan->grid.proj_type); /* * read in storm file scan */ if (RfReadStormScan(s_handle, scan_num, "forecast_generate")) { unlock_files(s_handle, t_handle); return (-1); } init_map_index(map_v_handle, v_handle, &ftime); /* * compute forecast */ if (Glob->params.thresholded_forecast) { forecast = thresholded_forecast(s_handle, t_handle, v_handle, map_v_handle, &grid, scan_num, precip_lookup); } else { /* if (Glob->params.thresholded_forecast) */ forecast = unthresholded_forecast(s_handle, t_handle, v_handle, map_v_handle, &grid, scan_num, precip_lookup); } /* if (Glob->params.thresholded_forecast) */ if (forecast != NULL) { map_v_handle->field_plane[0][0] = forecast; /* * write the map file */ sprintf(map_v_handle->vol_params->note, "%s\n%s%g\n%s%g\n%s%g\n", "Precipitation forecast", "Refl threshold: ", s_handle->header->params.low_dbz_threshold, "Z-R coeff : ", coeff, "Z-R expon : ", expon); sprintf(map_v_handle->field_params[0]->name, "%g hr forecast ahead of time", lead_time_hr); RfWriteDobson(map_v_handle, FALSE, Glob->params.debug, Glob->params.map_dir, Glob->params.output_file_ext, Glob->prog_name, "forecast_generate"); } unlock_files(s_handle, t_handle); return (ftime.unix_time); }
/* * unlock_cfs - unlock the fs related process on current process */ static void unlock_cfs(void) { unlock_files(current->filesp); }