int f_set_date(ARG1) { int year, month, day, hour, minute, second, i, j, units, n; int code_4_11,idx; unsigned int dtime; if (mode < 0) return 0; reftime(sec, &year, &month, &day, &hour, &minute, &second); i=strlen(arg1); if (i < 4 || i % 2 == 1) fatal_error("set_date: bad date code %s",arg1); i = sscanf(arg1,"%4d%2d%2d%2d%2d%2d" , &year, &month, &day, &hour, &minute, &second); if (i < 1) fatal_error("set_date: bad date code %s",arg1); if (check_datecode(year, month, day) != 0 || hour < 0 || hour >= 24 || minute < 0 || minute >= 60 || second < 0 || second >= 60) fatal_error("set_date: bad date code %s",arg1); // set reference time save_time(year,month,day,hour,minute,second, sec[1]+12); idx = stat_proc_n_time_ranges_index(sec); if (idx < 0) return 0; // not a stat processed template n = (int) sec[4][idx]; // number of stat proc elements j = idx + 35 - 42; // add forecast time to time units = code_table_4_4(sec); dtime = forecast_time_in_units(sec); add_time(&year, &month, &day, &hour, &minute, &second, dtime, units); for (i = 0; i < n; i++) { // add statistical processing time to time code_4_11 = (int) sec[4][47-34+j+i*12]; units = (int) sec[4][48-34+j+i*12]; dtime = uint4(sec[4]+49-34+j+i*12); if (code_4_11 == 3 || code_4_11 == 4) continue; if (code_4_11 == 1 || code_4_11 == 2) { add_time(&year, &month, &day, &hour, &minute, &second, dtime, units); } else { fatal_error_i("set_date: code 4.11=%d is not supported", code_4_11); } } save_time(year,month,day,hour,minute,second, sec[4]+j); return 0; }
/* * HEADER:-1:code_table_4.4:inv:0:code table 4.4 */ int f_code_table_4_4(ARG0) { int val; const char *string; if (mode >= 0) { val = code_table_4_4(sec); string = NULL; switch(val) { #include "CodeTable_4.4.dat" } if (string == NULL) sprintf(inv_out,"code table 4.4=%d", val); else sprintf(inv_out,"code table 4.4=%d (%s)", val, string); } return 0; }
int verftime(unsigned char **sec, int *year, int *month, int *day, int *hour, int *minute, int *second) { unsigned char *p; int units; unsigned int dtime; p = sec[1]; *year = (p[12] << 8) | p[13]; *month = p[14]; *day = p[15]; *hour = p[16]; *minute = p[17]; *second = p[18]; units = code_table_4_4(sec); if (units == -1) return 1; dtime = pds_fcst_time(sec); return add_time(year, month, day, hour, minute, second, dtime, units); }
int f_ncep_norm(ARG1) { struct local_struct { float *val; int has_val; unsigned char *clone_sec[9]; FILE *output; }; struct local_struct *save; int idx, j, fhr_1, fhr_2, dt1, dt2, new_type, is_ave; unsigned int i; float *d1, *data_tmp; if (mode == -1) { // initialization save_translation = decode = 1; // 1/2015 use_scale = 0; // allocate static variables *local = save = (struct local_struct *) malloc( sizeof(struct local_struct)); if (save == NULL) fatal_error("memory allocation f_ncep_norm",""); if ((save->output = ffopen(arg1, file_append ? "ab" : "wb")) == NULL) { fatal_error("f_ncep_norm: could not open file %s", arg1); } save->has_val = 0; init_sec(save->clone_sec); return 0; } save = (struct local_struct *) *local; if (mode == -2) { // cleanup ffclose(save->output); if (save->has_val == 1) { free(save->val); free_sec(save->clone_sec); } free(save); return 0; } if (mode >= 0) { // processing idx = stat_proc_n_time_ranges_index(sec); // only process stat processed fields if (idx < 0) return 0; // n_time_ranges has to be one if (sec[4][idx] != 1) return 0; // only process averages or accumulations j = code_table_4_10(sec); if (mode == 99) fprintf(stderr,"\nncep_norm: code table 4.10 (ave/acc/etc)=%d\n",j); if (j == 0) is_ave = 1; // average else if (j == 1) is_ave = 0; // accumulation else return 0; // only process average or accumulations fhr_2 = forecast_time_in_units(sec); // start time dt2 = int4(sec[4]+idx+50-42); // delta-time if (mode == 99) fprintf(stderr,"\nncep_norm: fhr_2=%d dt2=%d index of dt2=%d\n",fhr_2, dt2, idx+50-42); if (dt2 == 0) return 0; // dt == 0 // units of fcst and stat proc should be the same if fcst time != 0 if (fhr_2 != 0 && code_table_4_4(sec) != sec[4][49-42+idx]) return 0; if (save->has_val == 0) { // new data: write and save if ((data_tmp = (float *) malloc(ndata * sizeof(float))) == NULL) fatal_error("memory allocation - data_tmp",""); undo_output_order(data, data_tmp, ndata); grib_wrt(sec, data_tmp, ndata, nx, ny, use_scale, dec_scale, bin_scale, wanted_bits, max_bits, grib_type, save->output); if (flush_mode) fflush(save->output); free(data_tmp); if (save->has_val == 1) { // copy data to save free(save->val); // save = new field free_sec(save->clone_sec); } copy_sec(sec, save->clone_sec); copy_data(data,ndata,&(save->val)); save->has_val = 1; if (mode == 99) fprintf(stderr," ncep_norm: saved as new field\n"); return 0; } new_type = 0; fhr_1 = forecast_time_in_units(save->clone_sec); // start_time of save message dt1 = int4(save->clone_sec[4]+idx+50-42); // delta-time if (mode == 99) fprintf(stderr,"ncep_norm: is_ave = %d\n fhr_1 %d dt1 %d fhr_2 %d dt2 %d\n",is_ave, fhr_1, dt1, fhr_2, dt2); if (fhr_1 != fhr_2) new_type = 1; if (new_type == 0) { if (same_sec0(sec,save->clone_sec) == 0 || same_sec1(sec,save->clone_sec) == 0 || same_sec3(sec,save->clone_sec) == 0 || same_sec4_diff_ave_period(sec,save->clone_sec) == 0) { if (mode == 99) fprintf(stderr,"ncep_norm: new_type sec test %d %d %d %d\n", same_sec0(sec,save->clone_sec), same_sec1(sec,save->clone_sec), same_sec3(sec,save->clone_sec), same_sec4_diff_ave_period(sec,save->clone_sec)); new_type = 1; } } if (mode == 99) fprintf(stderr,"ncep_norm: new_type=%d write and save\n",new_type); if (new_type == 1) { // fields dont match: write and save if ((data_tmp = (float *) malloc(ndata * sizeof(float))) == NULL) fatal_error("memory allocation - data_tmp",""); undo_output_order(data, data_tmp, ndata); grib_wrt(sec, data_tmp, ndata, nx, ny, use_scale, dec_scale, bin_scale, wanted_bits, max_bits, grib_type, save->output); if (flush_mode) fflush(save->output); free(data_tmp); if (save->has_val == 1) { // copy data to save free(save->val); // save = new field free_sec(save->clone_sec); } copy_sec(sec, save->clone_sec); copy_data(data,ndata,&(save->val)); save->has_val = 1; if (mode == 99) fprintf(stderr," ncep_norm: saved as new type/sequence\n"); return 0; } /* now do stuff */ if (dt1 == dt2) return 0; // same ending time // change metadata int_char(dt2-dt1, save->clone_sec[4]+50-42+idx); // dt = dt2 - dt1 save->clone_sec[4][17] = sec[4][49-42+idx]; // new forecast time unit int_char(dt1+fhr_1, save->clone_sec[4]+18); // fhr = fhr + dt1 if (mode == 99) fprintf(stderr,"ncep_norm new fcst time %d + %d\n", dt1,fhr_1); for (i = idx-7; i < idx; i++) { // ending time from pds2 save->clone_sec[4][i] = sec[4][i]; } if (mode == 99) { if (is_ave) fprintf(stderr," process: factor: NEW*%g - OLD*%g\n", dt2/ (double) (dt2 - dt1), dt1/ (double) (dt2-dt1)); else fprintf(stderr," process: current-last\n"); } // change floating point data d1= save->val; for (i = 0; i < ndata; i++) { if (!UNDEFINED_VAL(data[i]) && !UNDEFINED_VAL(*d1) ) { if (is_ave) { *d1 = (data[i]*dt2 - *d1*dt1) / (double) (dt2 - dt1); } else { // accumulation *d1 = data[i] - *d1; } } else *d1 = UNDEFINED; d1++; } // write grib output if ((data_tmp = (float *) malloc(ndata * sizeof(float))) == NULL) fatal_error("memory allocation - data_tmp",""); undo_output_order(save->val, data_tmp, ndata); grib_wrt(save->clone_sec, data_tmp, ndata, nx, ny, use_scale, dec_scale, bin_scale, wanted_bits, max_bits, grib_type, save->output); if (flush_mode) fflush(save->output); free(data_tmp); // save data free(save->val); // save = new field free_sec(save->clone_sec); copy_sec(sec, save->clone_sec); copy_data(data,ndata,&(save->val)); return 0; } return 0; }