Esempio n. 1
0
unsigned int forecast_time_in_units(unsigned char **sec) {

    int p;
    p = code_table_4_0(sec);
    if (p <= 15 || p == 1000 || p == 1001 || p == 1002 || p == 1100 || p == 1101)  {
        return uint4(sec[4]+18);
    }
    if (p >= 40 && p <= 43)  {
        return uint4(sec[4]+20);
    }
    return 0xffffffff;
}
Esempio n. 2
0
int f_code_table_4_0(ARG0) {
    int val;
    const char *string;
    if (mode >= 0) {
        val = code_table_4_0(sec);
        string = NULL;
        switch(val) {
#include "CodeTable_4.0.dat"
        }
        if (string == NULL) sprintf(inv_out,"code table 4.0=%d", val);
        else sprintf(inv_out,"code table 4.0=%d %s", val, string);
    }
    return 0;
}
Esempio n. 3
0
int perturbation_number(unsigned char **sec) {
    int pdt, n;

    pdt = code_table_4_0(sec);
    switch(pdt) {
	case 1:
	case 11:
		n = sec[4][35]; break;
	case 41:
	case 43:
		n = sec[4][37]; break;
	default: n = -1; break;
    }
    return n;
}
Esempio n. 4
0
int f_set_prob(ARG5) {

    int pdt, val, scale;
    double value;
    unsigned char *p;

    if (mode < 0) return 0;
    pdt = code_table_4_0(sec);

    switch(pdt) {
	case 0:
	case 1:
	case 2:
	case 4:
	case 6:
	    f_set_pdt(call_ARG1(inv_out, NULL, "+5"));
	    break;
	case 8:
	case 10:
	case 11:
	case 12:
	case 13:
	case 14:
	case 15:
	    f_set_pdt(call_ARG1(inv_out, NULL, "+9"));
	    break;
    }
    p = code_table_4_9_location(sec);
    p[-2] = (unsigned char) atoi(arg1);
    p[-1] = (unsigned char) atoi(arg2);
    p[0] = (unsigned char) atoi(arg3);

    // encode lower limit
    value = atof(arg4);
    best_scaled_value(value, &scale,&val);
    p[1] = scale & 255;
    uint_char(val, p+2);

    // encode upper limit
    value = atof(arg5);
    best_scaled_value(value, &scale,&val);
    p[6] = scale & 255;
    uint_char(val, p+7);
    return 0;
}
Esempio n. 5
0
int number_of_forecasts_in_the_ensemble(unsigned char **sec) {
    int pdt, n;

    pdt = code_table_4_0(sec);
    switch(pdt) {
	case 1:
	case 11:
		n = sec[4][36]; break;
	case 2:
	case 3:
	case 4:
	case 12:
	case 13:
		n = sec[4][35]; break;
	case 41:
	case 43:
		n = sec[4][38]; break;
	default: n=-1; break;
    }
    return n;
}
Esempio n. 6
0
int stat_proc_verf_time(unsigned char **sec, int *year, int *month, int *day, int *hour, int *minute, int *second)
{
    int i, j;
    i = code_table_4_0(sec);
    j = 0;
    if (i == 8) j = 34;
    else if (i == 9) j = 47;
    else if (i == 10) j = 35;
    else if (i == 11) j = 37;
    else if (i == 12) j = 36;
    else if (i == 13) j = 68;
    else if (i == 14) j = 64;

    if (j) {
	get_time(sec[4]+j, year, month, day, hour, minute, second);
	return 0;
    }
    else {
	*year = *month = *day = *hour = *minute = *second = 0;
    }
    return 1;
}
Esempio n. 7
0
int f_set_percentile(ARG1) {

    int i,pdt, percent;
    unsigned char *p;

    if (mode < 0) return 0;

    pdt = code_table_4_0(sec);
    percent = atoi(arg1);
    if (percent < 0) percent = 0;
    if (percent > 100 ) percent = 100;

    switch(pdt) {
	case 0:
	case 1:
	case 2:
	case 4:
	case 5:
	    f_set_pdt(call_ARG1(inv_out, NULL, "+6"));
	    break;
	case 8:
	case 9:
	case 11:
	case 12:
	case 13:
	case 14:
	case 15:
	    f_set_pdt(call_ARG1(inv_out, NULL, "+10"));
	    break;
    }

    p = percentile_value_location(sec);
    if (p != NULL) *p = (unsigned char) percent;

    return 0;
}
Esempio n. 8
0
void fixed_surfaces(unsigned char **sec, int *type1, float *surface1, 
	int *undef_val1, int *type2, float *surface2, int *undef_val2) {

    int pdt;
    unsigned char *p1, *p2;
    pdt = code_table_4_0(sec);
    *undef_val1 = *undef_val2 = 1;
    *surface1 = *surface2 = UNDEFINED;
    *type1 = *type2 = 255;

    switch (pdt) {
    case 0:
    case 1:
    case 2:
    case 3:
    case 4:
    case 5:
    case 6:
    case 7:
    case 8:
    case 9:
    case 10:
    case 11:
    case 12:
    case 13:
    case 14:
    case 15:
    case 1100:
    case 1101:
	p1 = sec[4]+22; p2 = sec[4]+28; break;
    case 40:
    case 41:
    case 42:
    case 43:
	p1 = sec[4]+24; p2 = sec[4]+30; break;
    case 20:
    case 30:
    case 31:
    case 1000:
    case 1001:
    case 1002:
    case 254:
	return; break;
    default: 
	fprintf(stderr,"levels: product definition template #%d not supported\n", pdt);
	return;
	break;
    }

    if (*p1 != 255) {
	*type1 = *p1;
        if (p1[1] != 255) {
	    if (p1[2] != 255 || p1[3] != 255 || p1[4] != 255 || p1[5] != 255) {
		*undef_val1 = 0;
                *surface1 = scaled2flt(INT1(p1[1]), int4(p1+2));
	    }
	}
    }
    if (*p2 != 255) {
	*type2 = *p2;
        if (p2[1] != 255) {
	    if (p2[2] != 255 || p2[3] != 255 || p2[4] != 255 || p2[5] != 255) {
		*undef_val2 = 0;
                *surface2 = scaled2flt(INT1(p2[1]), int4(p2+2));
	    }
	}
    }
    return ;
}
Esempio n. 9
0
File: cname.c Progetto: mmase/wgrib2
int getName(unsigned char **sec, int mode, char *inv_out, char *name, char *desc, char *unit) {

    int discipline, center, mastertab, localtab, parmcat, parmnum;
    int pdt;
    struct gribtable_s *p;
    const char *p_unit;

    p = NULL;
    if (user_gribtable != NULL) p = search_gribtable(user_gribtable, sec);

#ifdef USE_TIGGE
    if (tigge && p == NULL) p = search_gribtable(tigge_gribtable, sec);		/* tigge is default table */
#endif
    if (p == NULL) p = search_gribtable(gribtable, sec);
#ifdef USE_TIGGE
    /* if undefined and a tigge file */
    if (p == NULL && !tigge && (code_table_1_3(sec) == 4 || code_table_1_3(sec) == 5)) p = search_gribtable(tigge_gribtable, sec);
#endif

    p_unit = "unit";
    if (p) {
        p_unit = p->unit;
        pdt = code_table_4_0(sec);
	if (pdt == 5 || pdt == 9) p_unit = "prob";
    }

    if (p) {
        if (name) strcpy(name, p->name);
	if (desc) strcpy(desc, p->desc);
	if (unit) strcpy(unit, p_unit);

	if (inv_out) {
	    sprintf(inv_out, "%s", p->name);
	    inv_out += strlen(inv_out);
            if (mode) sprintf(inv_out," %s [%s]", p->desc, p_unit);
        }
    }
    else {
        discipline = GB2_Discipline(sec);
        center = GB2_Center(sec);
        mastertab = GB2_MasterTable(sec);
        localtab = GB2_LocalTable(sec);
        parmcat = GB2_ParmCat(sec);
        parmnum = GB2_ParmNum(sec);

        if (name) sprintf(name,"var%d_%d_%d",discipline,parmcat,parmnum);
	if (desc) strcpy(desc,"desc");
	if (unit) strcpy(unit,p_unit);

	if (inv_out) {
            if (parmnum < 192 && parmcat < 192) {
                sprintf(inv_out,"var discipline=%d master_table=%d parmcat=%d parm=%d", 
                  discipline, mastertab, parmcat, parmnum);
            }
            else {
	        sprintf(inv_out,"var discipline=%d center=%d local_table=%d parmcat=%d parm=%d",
                  discipline, center, localtab, parmcat, parmnum);
            }
	}
    }

    return 0;
}
Esempio n. 10
0
/*
 * HEADER:100:fix_CFSv2_fcst:misc:3:fixes CFSv2 monthly fcst  X=daily or 00/06/12/18 Y=pert no. Z=number ens fcsts  v1.0
 */
int f_fix_CFSv2_fcst(ARG3) {

    int subcenter, n_time_ranges, fcst_time_0, fcst_time_1, i;
    int dtime, unit, id;

    int ref_year, ref_month, ref_day, ref_hour, ref_minute, ref_second;
    int fcst0_year, fcst0_month, fcst0_day, fcst0_hour, fcst0_minute, fcst0_second;
    int fcst1_year, fcst1_month, fcst1_day, fcst1_hour, fcst1_minute, fcst1_second;

    static unsigned char new_sec4[61];

    struct local_struct {
        int fixed;			// number of fields processed
	int dt;				// dt either 6 or 24
	int hour0;			// initial hour
        int pert;			// perturbation no, 0 = ctl
        int num_ensemble_members;
    };
    struct local_struct *save;

    if (mode == -1) {
	*local = save = (struct local_struct *)malloc( sizeof(struct local_struct));
	save->fixed = 0;
	save->dt = 24;
	if (strcmp(arg1,"daily") == 0) {
	    save->dt = 6;
	    save->hour0 = 0;
	}
	else if (strcmp(arg1,"00") == 0) {
	    save->hour0 = 0;
	}
	else if (strcmp(arg1,"06") == 0) {
	    save->hour0 = 6;
	}
	else if (strcmp(arg1,"12") == 0) {
	    save->hour0 = 12;
	}
	else if (strcmp(arg1,"18") == 0) {
	    save->hour0 = 1;
	}
	else fatal_error("fix_CFSv2_fcst: bad arg %s, wanted daily,00,06,12 or 18",arg1);

	save->pert = atoi(arg2);
	save->num_ensemble_members = atoi(arg2);

    }
    save = *local;

    if (mode == -2) sprintf(inv_out,"fix_CFSRv2_fcst %d fields fixed" , save->fixed);

    if (mode < 0) return 0;

    // only process NCEP CFSv2 monthly forecasts

    // must be NCEP generated
    if (GB2_Center(sec) != NCEP) return 0;

    // subcenter should be 0, 1 (reanalysis), 3 (EMC) or 4 (NCO)
    subcenter = GB2_Subcenter(sec);
    if (subcenter != 0 && subcenter != 1 && subcenter != 3 && subcenter != 4) return 0;

    // must have process id = 82 or 98
    id = analysis_or_forecast_generating_process_identifier(sec);
    if (id != 82 && id != 98) return 0;

    // product defn table must be 8 (fcst average)
    if ( code_table_4_0(sec) != 8) return 0;

    // n_time_ranges should be 1
    if ( (n_time_ranges = sec[4][41]) != 1) {
        fprintf(stderr,"unexected CFSv2 type forecast field .. pdt=8, n=%d\n", n_time_ranges);
	return 0;
    }

    // forecast time units should be months
    if (sec[4][17] != 3 || sec[4][48] != 3) return 0;

    fcst_time_0  = int4(sec[4]+18);
    fcst_time_1  = int4(sec[4]+49) + fcst_time_0;

    // get reference time
    reftime(sec, &ref_year, &ref_month, &ref_day, &ref_hour, &ref_minute, &ref_second);

    if (ref_minute != 0 || ref_second != 0) {
        fprintf(stderr,"unexected CFSv2 minute/second value != 0\n");
	return 0;
    }

    if (fcst_time_0 != 0) {	// start at day=1 hour=save->hour0 of proper month
	fcst0_year = ref_year;
	fcst0_month = ref_month + fcst_time_0;
        if (fcst0_month > 12) {
	    i = (fcst0_month - 1) / 12;
	    fcst0_year += i;
	    fcst0_month -= (i*12);
	}
	fcst0_day = 1;
	fcst0_hour = save->hour0;
	fcst0_minute = 0;
	fcst0_second= 0;
   }
   else {			// start at current month 
	fcst0_year = ref_year;
	fcst0_month = ref_month;
	fcst0_day = ref_day;
	fcst0_hour = ref_hour;
	fcst0_minute = ref_minute;
	fcst0_second= ref_second;
	if (save->dt == 24) {
	    i = save->hour0 - fcst0_hour;
	    if (i < 0) i += 24;
	    add_time(&fcst0_year, &fcst0_month, &fcst0_day, &fcst0_hour, &fcst0_minute, &fcst0_second, i, HOUR);
	}
    }

    sub_time(fcst0_year, fcst0_month, fcst0_day, fcst0_hour, fcst0_minute, fcst0_second, 
		ref_year, ref_month, ref_day, ref_hour, ref_minute, ref_second,
		&dtime, &unit);
    // set forecast time.
    if (dtime == 0) unit = MONTH;
    sec[4][17] = unit;
    int_char(dtime, sec[4]+18);

    if (fcst_time_1 == 0) fatal_error("fix-CFSv2_fcst: unexpected end_ft","");

    if (save->dt == 6) {		// ends at 18Z of last day of month
	fcst1_year = ref_year;
	fcst1_month = ref_month + fcst_time_1-1;
	if (fcst1_month > 12) {
            i = (fcst1_month - 1) / 12;
            fcst1_year += i;
            fcst1_month -= (i*12);
        }
        fcst1_day = num_days_in_month(fcst1_year, fcst1_month);
        fcst1_hour = 18;
        fcst1_minute = 0;
        fcst1_second= 0;

	// time increment = 6 hours
	sec[4][53] = (unsigned char) HOUR;
	int_char(6, sec[4]+54);
    }
    else {
	fcst1_year = ref_year;
	fcst1_month = ref_month + fcst_time_1-1;
	if (fcst1_month > 12) {
            i = (fcst1_month - 1) / 12;
            fcst1_year += i;
            fcst1_month -= (i*12);
        }
        fcst1_day = num_days_in_month(fcst1_year, fcst1_month);
        fcst1_hour = save->hour0;
        fcst1_minute = 0;
        fcst1_second= 0;

	// time increment = 24 hours
	sec[4][53] = (unsigned char)  HOUR;
	int_char(24, sec[4]+54);
    }

    // find stat processing time
    sub_time(fcst1_year, fcst1_month, fcst1_day, fcst1_hour, fcst1_minute, fcst1_second, 
		fcst0_year, fcst0_month, fcst0_day, fcst0_hour, fcst0_minute, fcst0_second, 
		&dtime, &unit);

    // change length of processing
    sec[4][48] = (unsigned char) unit;
    int_char(dtime, sec[4]+49);

// fprintf(stderr,"length of processing %d unit (%d)\n", dtime, unit);

    // save end-of-processing time
    save_time(fcst1_year, fcst1_month, fcst1_day, fcst1_hour, fcst1_minute, fcst1_second, sec[4]+34);

    // make sure that some basic info is correct
    sec[4][47] = 2;	// fcst_time++


    // now to add ensemble information
    for (i = 0; i < 34; i++) new_sec4[i] = sec[4][i];
    for (i = 34; i <= 57; i++) new_sec4[i+3] = sec[4][i];

    uint_char(61, new_sec4);			// length of new sec[4[
    new_sec4[7] = 0;				// pdt = 11
    new_sec4[8] = 11;

    // add perturbation info
    if (save->pert == 0) {			// pert == 0 means control forecast
        new_sec4[34] = 0;
        new_sec4[35] = 0;
    }
    else { 
        new_sec4[34] = 3;
        new_sec4[35] = save->pert;
    }
    new_sec4[35] = save->num_ensemble_members;
   
    sec[4] = new_sec4;
 
    save->fixed = save->fixed + 1;
    return 0;
}