Exemple #1
0
int f_min(ARG0) {
    double mx;
    int ok;
    unsigned int i;

    if (mode == -1) {
        decode = 1;
    }
    else if (mode >= 0) {
        mx = 0.0;
        ok = 0;
        for (i = 0; i < ndata; i++) {
            if (!UNDEFINED_VAL(data[i])) {
                if (ok) {
                    mx = mx < data[i] ? mx : data[i];
                }
                else {
                    ok = 1;
                    mx = data[i];
               }
            }
        }
        if (ok) sprintf(inv_out,"min=%lg",mx);
        else sprintf(inv_out,"min=undefined");

    }
    return 0;
}
Exemple #2
0
const char *WxLabel(float f) {
    int j;
    if (UNDEFINED_VAL(f)) return "Undefined";
    j = (int) floor(f + 0.5);
    if (f < 0 || WxNum == 0 || j >= WxNum) fatal_error("WxLabel: program error","");
    return WxKeys[j];
}
Exemple #3
0
/*
 * HEADER:100:spread:output:1:write text - spread sheet format into X (WxText enabled)
 */
int f_spread(ARG1) {
    unsigned int i;

    if (mode == -1) {
        if ((*local = (void *) ffopen(arg1,file_append ? "a" : "w")) == NULL)
	        fatal_error("Could not open %s", arg1);
        WxText = latlon = decode = 1;
    }
    else if (mode == -2) {
	ffclose((FILE *) *local);
    }
    else if (mode >= 0) {
	if (lat == NULL || lon == NULL || data == NULL) {
	    fprintf(stderr,"no code to determine lat-lon information, no spread sheet output\n");
	    return 0;
	}
	set_mode(0);
	f_var(call_ARG0(inv_out,NULL));
	fprintf((FILE *) *local,"lon,lat,%s",inv_out);
	f_lev(call_ARG0(inv_out,NULL));
	fprintf((FILE *) *local," %s", inv_out);
	f_t(call_ARG0(inv_out,NULL));
	fprintf((FILE *) *local," %s", inv_out);
	f_ftime(call_ARG0(inv_out,NULL));
	fprintf((FILE *) *local," %s\n", inv_out);

	if (WxNum > 0) {
	    for (i = 0; i < ndata; i++) {
	        if(!UNDEFINED_VAL(data[i])) 
	            fprintf((FILE *) *local,"%lf,%lf,\"%s\"\n",lon[i],lat[i],WxLabel(data[i]));
	    }
	}
	else {
	    for (i = 0; i < ndata; i++) {
	        if(!UNDEFINED_VAL(data[i])) 
	            fprintf((FILE *) *local,"%lf,%lf,%g\n",lon[i],lat[i],data[i]);
	    }
	}
        if (flush_mode) fflush((FILE *) *local);
	inv_out[0] = 0;
	set_mode(mode);
    }

    return 0;
}
Exemple #4
0
void update_data(float *sum, int *count, float *data, int ndata) 
{
int i;

    for (i = 0; i < ndata; i++) {
	if (! UNDEFINED_VAL(data[i])) {
	    sum[i] += data[i];
	    count[i]++;
	}
    }
}
Exemple #5
0
int main(int argc, char **argv) {

    long int len_grib, pos = 0;
    unsigned char *pds, *gds;
    FILE *input, *output, *dx_file;
    int count, cnvt, scale10, scale2;
    int ndata;
    float *array = NULL, dx[NXNY];
    int i, fld;


    /* preliminaries .. open up all files */

    if (argc != 4) {
	fprintf(stderr, "%s [in gribfile] [out gribfile] [AWIP32_dx.grib]\n", argv[0]);
	exit(8);
    }
    if ((input = fopen(argv[1],"rb")) == NULL) {
	fprintf(stderr,"could not open file: %s\n", argv[1]);
	exit(7);
    }
    if ((output = fopen(argv[2],"wb")) == NULL) {
	fprintf(stderr,"could not open file: %s\n", argv[2]);
	exit(7);
    }
    if ((dx_file = fopen(argv[3],"rb")) == NULL) {
	fprintf(stderr,"could not open file: %s\n", argv[3]);
	exit(7);
    }

    /* read the scaling factors */

    pos = 0;
    len_grib = rd_grib_rec(dx_file, pos, &pds, &gds, &array, &ndata);
    if (len_grib <= 0 || ndata != NXNY) {
        fprintf(stderr,"bad dx file\n");
        exit(9);
    }
    for (i = 0; i < NXNY; i++) {
	if (UNDEFINED_VAL(array[i])) {
	    dx[i] = 0.0;
	}
	else {
            dx[i] = 1.0 / array[i];
	}
    }
    fclose(dx_file);


    set_BDSMinBits(-1);	/* ETA-style mode */

    pos = count = cnvt = 0;
    for(;;) {

	/* read the grib file */
        len_grib = rd_grib_rec2(input, pos, &pds, &gds, &array, &ndata, 
          &scale10, &scale2);
        set_def_power2(-scale2);

	if (len_grib <= 0) break;
	if (ndata != NXNY) {
	    fprintf(stderr,"error in grid size\n");
	    exit(8);
	}
        fld = PDS_PARAM(pds);
	if (fld == WVCONV || fld == WCCONV) {
	    for (i = 0; i < NXNY; i++) {
		if (UNDEFINED_VAL(array[i]) || dx[i] == 0.0) {
		    array[i] = UNDEFINED;
		}
		else {
		    array[i] *= dx[i];
		}
	    }
	    pds = PDStool(pds, P_dec_scale(4), P_end);
	    cnvt++;
	}        
	wrt_grib_rec(pds, gds, array, ndata, output);

	pos += len_grib;
	count++;
    }
    fclose(output);
    fclose(input);
    fprintf(stderr,"fixed %d records out of %d\n", cnvt, count);
    return 0;
}
Exemple #6
0
int f_csv(ARG1) {

    char new_inv_out[STRING_SIZE];
    char name[100], desc[100], unit[100];
    FILE *out;

    unsigned int j;
    char vt[20],rt[20];
    int year, month, day, hour, minute, second;
	
    /* initialization phase */

    if (mode == -1) {
        WxText = decode = latlon = 1;
        if ((*local = (void *) ffopen(arg1,file_append ? "a" : "w")) == NULL)
		fatal_error("csv could not open file %s", arg1);  
	return 0;
    }

    /* cleanup phase */

    if (mode == -2) return 0;

    /* processing phase */

    if (lat == NULL || lon == NULL) {
	fprintf(stderr,"csv: latitude/longitude not defined, record skipped\n");
	return 0;
    }

    out = (FILE *) *local;

    /*Collect runtime and validtime into vt and rt*/

    reftime(sec, &year, &month, &day, &hour, &minute, &second);
    sprintf(rt, "%4.4d-%2.2d-%2.2d %2.2d:%2.2d:%2.2d", year,month,day,hour,minute,second);

    vt[0] = 0;
    if (verftime(sec, &year, &month, &day, &hour, &minute, &second) == 0) {
        sprintf(vt,"%4.4d-%2.2d-%2.2d %2.2d:%2.2d:%2.2d", year,month,day,hour,minute,second);
    }

    /*Get levels, parameter name, description and unit*/

    *new_inv_out = 0;
    f_lev(call_ARG0(new_inv_out,NULL));

    if (strcmp(new_inv_out, "reserved")==0) return 0;
//    getName(sec, mode, NULL, name, desc, unit);
    getExtName(sec, mode, NULL, name, desc, unit,".","_");
//	fprintf(stderr,"Start processing of %s at %s\n", name, new_inv_out);
//	fprintf(stderr,"Gridpoints in data: %d\n", ndata);
//	fprintf(stderr,"Description: %s, Unit %s\n", desc,unit);

     /* Lage if-setning rundt hele som sjekker om alt eller deler skal ut*/

    if (WxNum > 0) {
        for (j = 0; j < ndata; j++) {
            if (!UNDEFINED_VAL(data[j])) {
	        fprintf(out,"\"%s\",\"%s\",\"%s\",\"%s\",%g,%g,\"%s\"\n",rt,vt,name,
		    new_inv_out,lon[j] > 180.0 ?  lon[j]-360.0 : lon[j],lat[j],WxLabel(data[j]));
	    }
	}
    }
    else {
        for (j = 0; j < ndata; j++) {
            if (!UNDEFINED_VAL(data[j])) {
	        fprintf(out,"\"%s\",\"%s\",\"%s\",\"%s\",%g,%g,%lg\n",rt,vt,name,
		    new_inv_out,lon[j] > 180.0 ?  lon[j]-360.0 : lon[j],lat[j],data[j]);
	    }
	}
    }
    if (flush_mode) fflush(out);
    return 0;
}
Exemple #7
0
int f_mysql(ARG5) {

    char sql[300];
    char server[100];
    char user[100];
    char password[100]; 
    char database[100];
    char table[100];

    char name[100], desc[100], unit[100], level_buf[100];
    int year, month, day, hour, minute, second;
	
    char vt[20],rt[20];
//    unsigned char *p;
	
    int j;
    double longitude,latitude;
	
    struct local_struct {
        MYSQL *conn;
        char filename[200];
    };
    struct local_struct *save;

    FILE *file;
    int tempfile;

    strcpy(server,arg1);
    strcpy(user,arg2);
    strcpy(password,arg3);
    strcpy(database,arg4);
    strcpy(table,arg5);


    /* initialization phase */

    if (mode == -1) {
        decode = latlon = 1;
            
	*local = save = (struct local_struct *) malloc(sizeof(struct local_struct));
	if (save == NULL) fatal_error("mysql memory allocation ","");

	save->conn = mysql_init(NULL);

	/* Connect to database */
	if (!mysql_real_connect(save->conn, server, user, password, database, 0, NULL, 0))
	    fatal_error("mysql error %s", mysql_error(save->conn));
    }

    /* cleanup phase */
    else if (mode == -2) {
    	save = *local;
    	mysql_close(save->conn);
	free(save);
    }

    /* processing phase */
    else if (mode >= 0) {
	save = *local;

        /*Collect runtime and validtime into vt and rt*/
        reftime(sec, &year, &month, &day, &hour, &minute, &second);
//	p = sec[1];
//	sprintf(rt, "%4.4d-%2.2d-%2.2d %2.2d:%2.2d:%2.2d", (p[12]<<8)+p[13], p[14],p[15],p[16],p[17],p[18]);
        sprintf(rt, "%4.4d-%2.2d-%2.2d %2.2d:%2.2d:%2.2d", year,month,day,hour,minute,second);

        if (verftime(sec, &year, &month, &day, &hour, &minute, &second) == 0) {
            sprintf(vt,"%4.4d-%2.2d-%2.2d %2.2d:%2.2d:%2.2d", year,month,day,hour,minute,second);
        }

	/* Get levels, parameter name, description and unit*/
        f_lev(mode, sec, data, ndata, level_buf, local);
   
        if (strcmp(level_buf, "reserved") == 0) return 0;

        strcpy(save->filename, "/tmp/wgrib2_mysqlXXXXXX");
        if ((tempfile = mkstemp(save->filename)) == -1) fatal_error("mysql filename setup","");

        fprintf(stdout, "\nUsing temporary pathname %s\n", save->filename);
        if ((file = fdopen(tempfile, "w")) == NULL ) fatal_error("mysql: error opening temporary filename: %s", save->filename);

        getName(sec, mode, NULL, name, desc, unit);
	fprintf(stderr,"Start processing of %s at %s\n", name, level_buf);
	fprintf(stderr,"Gridpoints in data: %d\n", ndata);
	fprintf(stderr,"Description: %s, Unit %s\n", desc,unit);
	
	printf("Inserting into database, table=%s, runtime=%s, validtime=%s, parameter=%s, level=%s \n",table,rt,vt,name,level_buf);
	
	for (j = 0; j < ndata; j++) {
	    if (!UNDEFINED_VAL(data[j])) {
		longitude = lon[j];
		latitude = lat[j];
		if (longitude > 180) longitude -= 360;
		fprintf(file,"\"%s\",\"%s\",%g,%g,\"%s\",\"%s\",%lg\n",rt,vt,latitude,longitude,name,level_buf,data[j]);
	    }
	}
	fclose(file);
	sprintf(sql,"LOAD DATA LOCAL INFILE '%s' INTO TABLE %s FIELDS TERMINATED BY ',' ENCLOSED BY '\"' LINES TERMINATED BY '\\n'",save->filename,table);
	printf("%s \n",sql);
	/* send SQL query */
	if (mysql_query(save->conn, sql)) fatal_error("mysql: %s", mysql_error(save->conn));

//      unlink = POSIX, remove = ANSI
//        unlink(save->filename);
        remove(save->filename);
    }
    return 0;
}
Exemple #8
0
int f_stats(ARG0) {
    double sum, sum_wt, wt, min, max, t;
    int do_wt, i;
    unsigned int n;

    if (mode == -1) {
        latlon = decode = 1;
    }
    else if (mode >= 0) {
	do_wt = lat != NULL;
	sum = wt = sum_wt = 0.0;
	n = 0;

#pragma omp parallel private(i,t)
{
        double sum_thread, sum_wt_thread, wt_thread, min_thread, max_thread;
        int n_thread;

        max_thread = min_thread = 0.0;
	sum_thread = 0.0;
	wt_thread = sum_wt_thread = 0.0;
        n_thread = 0;
	
	if (do_wt) { 
#pragma omp for nowait
            for (i = 0; i < ndata; i++) {
                if (!UNDEFINED_VAL(data[i])) {
		   { t = cos(CONV*lat[i]);  wt_thread += t; sum_wt_thread += data[i]*t; }
	        }
	    }
        }


#pragma omp for nowait
        for (i = 0; i < ndata; i++) {
            if (!UNDEFINED_VAL(data[i])) {
		if (n_thread++ == 0) {
                    sum_thread = min_thread = max_thread = data[i];
		}
		else {
                    max_thread = max_thread < data[i]? data[i] : max_thread;
                    min_thread = min_thread > data[i]? data[i] : min_thread;
                    sum_thread += data[i];
		}
            }
        }
#pragma omp critical
	{
	    if (n_thread) {
		if (n == 0) {
		    min = min_thread;
		    max = max_thread;
		}
		else {
		    min = min > min_thread ? min_thread : min;
		    max = max < max_thread ? max_thread : max;
		}
		sum += sum_thread;
		wt += wt_thread;
		sum_wt += sum_wt_thread;
		n += n_thread;
	    }
	}
}

        if (n) sum /= n;
        sprintf(inv_out,"ndata=%u:undef=%u:mean=%lg:min=%lg:max=%lg", ndata, ndata-n, sum, min, max);
	if (wt > 0) {
	    sum_wt = sum_wt/wt;
	    inv_out += strlen(inv_out);
            sprintf(inv_out,":cos_wt_mean=%lg", sum_wt);
	}
    }
    return 0;
}
Exemple #9
0
int f_cress_lola(ARG4) {

    int n, nx, ny, nxny, ix, iy, i, j, k, m, iradius;
    double x0,dx, y0,dy, x, y, z, r_sq, sum;
    unsigned char *new_sec[8];
    double *cos_lon, *sin_lon, s, c, tmp, *tmpv, *inc, *wt;
    float *background;

    struct local_struct {
        int nlat, nlon, nRadius;
        double lat0, lon0, dlat, dlon, latn, lonn;
        FILE *out;
	int last_GDS_change_no;
	double Radius[MAX_SCANS];
	double R_earth;
        double *in_x, *in_y, *in_z;
        double *out_x, *out_y, *out_z;
	char *mask;
    };
    struct local_struct *save;

    /* initialization phase */

    if (mode == -1) {
        decode = latlon = 1;	/* request decode of data, lat and lon */

        *local = save = (struct local_struct *) malloc( sizeof(struct local_struct));
        if (save == NULL) fatal_error("cress_lola memory allocation ","");

	/* parse command line arguments */

        if (sscanf(arg1,"%lf:%d:%lf", &x0, &nx, &dx) != 3)
            fatal_error("cress_lola parsing longitudes lon0:nx:dlon  %s", arg1);

        if (dx < 0) fatal_error("cress_lola: dlon < 0", "");
        if (nx <= 0) fatal_error_i("cress_lola: bad nlon %d", nx);
        if (x0 < 0.0) x0 += 360.0;
        if (x0 < 0.0 || x0 >= 360.0) fatal_error("cress_lola: bad initial longitude","");
        save->nlon = nx;
        save->lon0 = x0;
        save->dlon = dx;
        save->lonn = x0 + (nx-1) * dx;

        if (sscanf(arg2,"%lf:%d:%lf", &y0, &ny, &dy) != 3) 
            fatal_error("cress_lola parsing latitudes lat0:nx:dlat  %s", arg2);

        if (dy < 0) fatal_error("cress_lola: dlat < 0","");
        if (ny <= 0) fatal_error_i("cress_lola: bad nlat %d", ny);
        save->nlat = ny;
        save->lat0 = y0;
        save->dlat = dy;
        save->latn = y0 + (ny-1)*dy;
        if (save->latn > 90.0 || save->lat0 < -90.0) fatal_error("cress_lola: bad latitude","");
        nxny = nx*ny;

        if ((save->out = ffopen(arg3,file_append ? "ab" : "wb")) == NULL) 
              fatal_error("cress_lola could not open file %s", arg3);

	iradius = 0;
	save->mask = NULL;
	k = sscanf(arg4, "%lf%n", &tmp, &m);
        while (k == 1) {
            if (iradius >= MAX_SCANS) fatal_error("cres_lola: too many radius parameters","");
            save->Radius[iradius++] = tmp;
	    if (tmp < 0.0 && save->mask == NULL) {
		save->mask = (char *) malloc(nxny * sizeof(char));
		if (save->mask == NULL) fatal_error("cress_lola memory allocation ","");
	    }
            arg4 += m;
            k = sscanf(arg4, ":%lf%n", &tmp, &m);
	}
	save->nRadius = iradius;

fprintf(stderr,"nRadius=%d nx=%d ny=%d\n",save->nRadius, nx, ny);

	save->out_x = (double *) malloc(nxny * sizeof(double));
	save->out_y = (double *) malloc(nxny * sizeof(double));
	save->out_z = (double *) malloc(nxny * sizeof(double));
	if (save->out_x == NULL || save->out_y == NULL || save->out_z == NULL) 
		fatal_error("cress_lola: memory allocation","");

	save->in_x = save->in_y = save->in_z = NULL;
	save->last_GDS_change_no = 0;

	/* out_x, out_y, out_z have the 3-d coordinates of the lola grid */

	cos_lon = (double *) malloc(nx * sizeof(double));
	sin_lon = (double *) malloc(nx * sizeof(double));
	if (cos_lon == NULL || sin_lon == NULL) fatal_error("cress_lola: memory allocation","");
	for (i = 0; i < nx; i++) {
	    x = (x0 + i*dx) * (M_PI / 180.0);
	    cos_lon[i] = cos(x);
	    sin_lon[i] = sin(x);
	}

        for (k = j = 0; j < ny; j++) {
	    y = (y0 + j*dy) * (M_PI / 180.0);
            s = sin(y);
            c = sqrt(1.0 - s * s);
	    for (i = 0; i < nx; i++) {
                save->out_z[k] = s;
                save->out_x[k] = c * cos_lon[i];
                save->out_y[k] = c * sin_lon[i];
		k++;
	    }
	}
	free(cos_lon);
	free(sin_lon);
        return 0;
    }

    save = (struct local_struct *) *local;
    if (mode == -2) {
	ffclose(save->out);
        return 0;
    }

    /* processing phase */
fprintf(stderr,">>processing\n");

    nx = save->nlon;
    ny = save->nlat;
    nxny = nx*ny;

    background = (float *) malloc(nxny * sizeof(float));
    tmpv = (double *) malloc(nxny * sizeof(double));
    inc = (double *) malloc(nxny * sizeof(double));
    wt = (double *) malloc(nxny * sizeof(double));
    if (background == NULL || tmpv == NULL || wt == NULL || inc == NULL) fatal_error("cress_lola: memory allocation","");

    /* Calculate x, y and z of input grid if new grid */
    if (save->last_GDS_change_no != GDS_change_no || save->in_x == NULL) {
	save->last_GDS_change_no = GDS_change_no;
        if (lat == NULL || lon == NULL || data == NULL) fatal_error("cress_lola: no lat, lon, or data","");

	save->R_earth  =  radius_earth(sec);

	if (save->in_x) free(save->in_x);
	if (save->in_y) free(save->in_y);
	if (save->in_z) free(save->in_z);

	save->in_x = (double *) malloc(npnts * sizeof(double));
	save->in_y = (double *) malloc(npnts * sizeof(double));
	save->in_z = (double *) malloc(npnts * sizeof(double));
	if (save->in_x == NULL || save->in_y == NULL || save->in_z == NULL)
	    fatal_error("cress_lola: memory allocation","");

	for (i = 0; i < npnts; i++) {
	    tmp = lon[i];
	    if (tmp < save->lon0) tmp += 360.0;
	    if (lat[i] >= 999.0 || lat[i] > save->latn || lat[i] < save->lat0 || tmp > save->lonn) {
		save->in_x[i] = 999.9;
	    }
	    else {
                s = sin(lat[i] * (M_PI / 180.0));
                c = sqrt(1.0 - s * s);
                save->in_z[i] = s;
                save->in_x[i] = c * cos(lon[i] * (M_PI / 180.0));
                save->in_y[i] = c * sin(lon[i] * (M_PI / 180.0));
            }
        }
fprintf(stderr,"done new gds processing npnts=%d\n", npnts);
    }

    /* at this point x, y, and z of input and output grids have been made */

    /* make new_sec[] with new grid definition */
    for (i = 0; i < 8; i++) new_sec[i] = sec[i];
    new_sec[3] = sec3_lola(nx, save->lon0, save->dlon, ny, save->lat0, save->dlat, sec);

    /* set background to average value of data */

    n = 0;
    sum = 0.0;
    /* make background = ave value */
    for (i = 0; i < npnts; i++) {
        if (save->in_x[i] < 999.0  && ! UNDEFINED_VAL(data[i]) ) {
	    n++;
	    sum += data[i];
	}
    }
    if (n == 0) {
	/* write undefined grid */
	for (i = 0; i < nxny; i++) background[i] = UNDEFINED;
        grib_wrt(new_sec, background, nxny, nx, ny, use_scale, dec_scale, bin_scale,
                wanted_bits, max_bits, grib_type, save->out);
        if (flush_mode) fflush(save->out);
	free(background);
	free(tmpv);
	free(inc);
	free(wt);
	return 0;
    }
    sum /= n;
    for (i = 0; i < nxny; i++) background[i] = sum;
fprintf(stderr,">>sum=%lf n %d background[1] %lf\n", sum, n, background[1]);

    for (iradius = 0; iradius < save->nRadius; iradius++) {
fprintf(stderr,">>radias=%lf nxny %d npnts %d\n", save->Radius[iradius],nxny, npnts);
	/* save->Radius has units of km */
	/* normalize to a sphere of unit radius */
	r_sq = save->Radius[iradius] / (save->R_earth / 1000.0);
	r_sq = r_sq * r_sq;
	/* wt = inc = 0.0; */
	for (k = 0; k < nxny; k++) inc[k] = wt[k] = 0.0;

	for (j = 0; j < npnts; j++) {
	    if (save->in_x[j] > 999.0 || UNDEFINED_VAL(data[j]) ) continue;

	    /* find the background value */
	    x = lon[j] - save->lon0;
	    x = (x < 0.0) ? (x + 360.0) / save->dlon : x / save->dlon;
	    y = (lat[j] - save->lat0) / save->dlat;

	    ix = floor(x);
	    iy = floor(y);
	    if ((double) ix == x && ix == nx-1) ix--;
	    if ((double) iy == y && iy == ny-1) iy--;

	    if (ix < 0 || iy < 0 || ix >= nx || iy >= ny) fatal_error("cress_lola: prog error ix, iy","");

	    x = x - ix;
	    y = y - iy;

	    /* find background value */

	    tmp = background[ix+iy*nx] * (1-x)*(1-y) +
		  background[ix+1+iy*nx] * (x)*(1-y) +
		  background[ix+(iy+1)*nx] * (1-x)*(y) +
		  background[(ix+1)+(iy+1)*nx] * (x)*(y);

// fprintf(stderr,"obs: lat/lon %lf %lf, ix %d / %d iy %d data %lf, background %lf\n", lat[j],lon[j], ix, nx, iy, data[j], tmp);
	    /* data increment */
	    tmp = data[j] - tmp;

	    x = save->in_x[j];
	    y = save->in_y[j];
	    z = save->in_z[j];

	    for (k = 0; k < nxny; k++) {
		tmpv[k] = DIST_SQ(x-save->out_x[k], y-save->out_y[k], z-save->out_z[k]);
		if (tmpv[k] < r_sq) {
		    tmpv[k] = (r_sq - tmpv[k]) / (r_sq + tmpv[k]);
		    wt[k] += tmpv[k];
		    inc[k] += tmpv[k] * tmp;
		}
	    }
	}		

	/* make mask or update background */

	if (save->Radius[iradius] < 0.0) {
	    for (k = 0; k < nxny; k++) save->mask[k] = (wt[k] > 0) ? 1 : 0;
	}
	for (k = 0; k < nxny; k++) {
	    if (wt[k] > 0) background[k] += inc[k]/wt[k];
	}
    }

    if (save->mask) {
	for (k = 0; k < nxny; k++) {
	    if (save->mask[k] == 0) background[k] = UNDEFINED;
	}
    }
    grib_wrt(new_sec, background, nxny, nx, ny, use_scale, dec_scale, bin_scale,
	wanted_bits, max_bits, grib_type, save->out);

    if (flush_mode) fflush(save->out);
    free(background);
    free(tmpv);
    free(wt);
    free(inc);
    return 0;
}
Exemple #10
0
int f_wind_speed(ARG1) {

    struct local_struct {
        float *val;
        int has_u;
        unsigned char *clone_sec[9];
        FILE *output;
    };
    struct local_struct *save;

    unsigned int i;
    int is_u;
    float *d1, *data_tmp;
    int discipline, mastertab, parmcat, parmnum;

    if (mode == -1) {			// initialization
        save_translation = decode = 1;

	// allocate static variables

        *local = save = (struct local_struct *) malloc( sizeof(struct local_struct));
        if (save == NULL) fatal_error("memory allocation -wind_speed","");

        if ((save->output = (void *) ffopen(arg1, file_append ? "ab" : "wb")) == NULL) {
	    fatal_error("-wind_speed: could not open file %s", arg1);
	}
	save->has_u = 0;
	init_sec(save->clone_sec);
	return 0;
    }

    save = *local;

    if (mode == -2) {			// cleanup
	if (save->has_u == 1) {
	    free(save->val);
	    free_sec(save->clone_sec);
	}
	return 0;
    }

    if (mode >= 0) {			// processing

	// get variable name parameters

        discipline = GB2_Discipline(sec);
        mastertab = GB2_MasterTable(sec);
        parmcat = GB2_ParmCat(sec);
        parmnum = GB2_ParmNum(sec);

	if (mode == 99) fprintf(stderr,"-wind_speed %d %d %d %d\n",mastertab,discipline,parmcat,parmnum);

	is_u = (mastertab <= 6) && (discipline == 0) && (parmcat == 2) && (parmnum == 2);
	if (mode == 99 && is_u) fprintf(stderr,"\n-wind_speed: is u\n");
	if (is_u) {		// save data
	    if (save->has_u) {
	        free(save->val);
	        free_sec(save->clone_sec);
	    }
            copy_sec(sec, save->clone_sec);
	    copy_data(data,ndata,&(save->val));
            GB2_ParmNum(save->clone_sec) = 3;		// set id to V
	    save->has_u = 1;
	    return 0;
	}

	// check for V

        if (same_sec0(sec,save->clone_sec) == 1 &&
            same_sec1(sec,save->clone_sec) == 1 &&
            same_sec3(sec,save->clone_sec) == 1 &&
            same_sec4(sec,save->clone_sec) == 1) {

	    // calculate wind speed

	    if (mode == 99) fprintf(stderr,"\n-wind_speed: calc wind speed\n");

            d1= save->val;
	    for (i = 0; i < ndata; i++) {
                if (!UNDEFINED_VAL(data[i]) && !UNDEFINED_VAL(*d1)) {
	            *d1 = sqrt(data[i]*data[i] + *d1 * *d1);
		}
	        else *d1 = UNDEFINED;
	        d1++;
	    }
            GB2_ParmNum(save->clone_sec) = 1;		// set id to wind speed

	    // copy data to temp space

            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);

            // cleanup
            free(save->val);
            free_sec(save->clone_sec);
	    save->has_u = 0;
	}
    }
    return 0;
}
Exemple #11
0
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;
}
int f_mysql_speed(ARG7) {

    char temp_pathname[STRING_SIZE];
    char sql[1500];
    char server[100];
    char user[100];
    char password[100]; 
    char database[100];
    char table[100];
    MYSQL_RES *res;
    MYSQL_ROW row;

    char name[100], desc[100], unit[100], level_buf[100];
    int year, month, day, hour, minute, second;
	
    char vt[20],rt[20];
//    unsigned char *p;
   		
    int i,j;
    double longitude,latitude,value;

    char new_level[50];
    char new_name[50];
    char conv[50];
    char precision[10];
    char last;
    char param[50];
    int ctr;
    int level;
    unsigned int load_local_infile;

    struct local_struct {
        MYSQL *conn;
        FILE *temp_fileptr;
        unsigned int npts;
        char *rows[MAX_NXNY];
	char has_value[MAX_NXNY];
        char *params;
        unsigned int isset;
        unsigned int wlon;
        unsigned int remove_unlikely;
        char runtime[20], validtime[20];
	int last_GDS_change_no;
    };
    struct local_struct *save;

    strcpy(server,arg1);
    strcpy(user,arg2);
    strcpy(password,arg3);
    strcpy(database,arg4);
    strcpy(table,arg5);

    /* initialization phase */

    if (mode == -1) {
        decode = latlon = 1;
        
	*local = save = (struct local_struct *) malloc( sizeof(struct local_struct));
	if (save == NULL) fatal_error("mysql_speed memory allocation ","");

        for (i = 0; i < MAX_NXNY; i++) {
	    save->rows[i] = NULL;
	    save->has_value[i] = 0;
	}
	save->last_GDS_change_no = 0;
	save->conn = mysql_init(NULL);
	save->temp_fileptr= NULL;
	save->params = (char *)  malloc(1500*sizeof(char));
	sprintf(save->params,"%s","");
	save->isset = 0;
	save->runtime[0] = 0;
	save->validtime[0] = 0;
	load_local_infile = 1;		// 1 = LOAD LOCAL INFILE,  0 = do not LOAD LOCAL INFILE

	if (sscanf(arg6,"%d", &save->wlon) != 1) {
            fatal_error("Argument 6, use western longitudes, has to be 0 or 1, error parsing %s", arg6);
        }
        if (sscanf(arg7,"%d", &save->remove_unlikely) != 1) {
            fatal_error("Argument 7, remove unlikely values, has to be 0 or 1, error parsing %s", arg7);
        }
	
	/* Set options for database */
	mysql_options(save->conn,MYSQL_OPT_LOCAL_INFILE, (char *) &load_local_infile);

	/* Connect to database */
	if (!mysql_real_connect(save->conn, server, user, password, database, 0, NULL, 0)) {
	   fatal_error("f_mysql_speed: could not connect to %s", mysql_error(save->conn));
	} 
	return 0;
    }

    /* cleanup phase */

    if (mode == -2) {
    	save = (struct local_struct *) *local;
    	
    	strcpy(temp_pathname, "/tmp/wgrib2_mysqlXXXXXX");
    	if ( -1 == (load_local_infile = mkstemp(temp_pathname)) ) {
	    fatal_error("f_mysql_speed: error making temporary filename","");
	}
	if ( !(save->temp_fileptr = fdopen(load_local_infile, "w")) ) {
	    fatal_error("f_mysql_speed: error making temporary filename","");
	}

    	fprintf(stdout, "Columns to insert: rt,vt,lat,lon%s\n", save->params);
    	fprintf(save->temp_fileptr,"rt,vt,lat,lon%s\n",save->params);
    	for (j = 0; j < save->npts; j++) {
	    if (save->has_value[j] == 1) fprintf(save->temp_fileptr,"%s\n",save->rows[j]);
	}
	fflush(save->temp_fileptr);
	sprintf(sql,"LOAD DATA LOCAL INFILE '%s' INTO TABLE %s FIELDS TERMINATED BY ',' ENCLOSED BY '\"' LINES TERMINATED BY '\\n' IGNORE 1 LINES (rt,vt,lat,lon%s)",temp_pathname,table,save->params);
	printf("Inserting into database, table=%s \n",table);
	printf("%s\n",sql);
	/* send SQL query */
	if (mysql_query(save->conn, sql)) {	
	    fatal_error("f_mysql_speed: connection error %s", mysql_error(save->conn));
	}
	fclose(save->temp_fileptr);
	remove(temp_pathname);
    	mysql_close(save->conn);
    	return 0;
    }

    /* processing phase */

    save = (struct local_struct *) *local;
  	
//    if (new_GDS && save->isset == 0 ) {
    if ((save->last_GDS_change_no != GDS_change_no) && save->isset == 0 ) {
  	//save->npts = GB2_Sec3_npts(sec);
  	save->npts = ndata;
  	if (ndata > MAX_NXNY) fatal_error_i("f_mysql_speed: MAX_NXNY exceeded %d", MAX_NXNY);

	for (i = 0; i < ndata; i++) {
	    if (save->rows[i] == NULL) {
		save->rows[i] = (char *)  malloc(1500*sizeof(char));
		if (save->rows[i] == NULL) fatal_error("f_mysql_speed: memory allocation problem","");
	    }
	    save->rows[i][0] = '\0';
	}

	save->isset = 1;
//    } else if (new_GDS) {
    } else if (save->last_GDS_change_no != GDS_change_no) {
	fatal_error("f_mysql_speed, grid definition has to be the same for all fields","");
   }
   save->last_GDS_change_no = GDS_change_no;
 
    /*Collect runtime and validtime into vt and rt*/

    reftime(sec, &year, &month, &day, &hour, &minute, &second);
//  p = sec[1];
//  sprintf(rt, "%4.4d-%2.2d-%2.2d %2.2d:%2.2d:%2.2d", (p[12]<<8)+p[13], p[14],p[15],p[16],p[17],p[18]);
    sprintf(rt, "%4.4d-%2.2d-%2.2d %2.2d:%2.2d:%2.2d", year,month,day,hour,minute,second);

    if (verftime(sec, &year, &month, &day, &hour, &minute, &second) == 0) {
        sprintf(vt,"%4.4d-%2.2d-%2.2d %2.2d:%2.2d:%2.2d", year,month,day,hour,minute,second);
    }
    /*Check that runtimes are equal and validtimes are equal*/
    if (save->runtime[0] != 0 && strcmp(save->runtime,rt) != 0) {
	fprintf(stderr, "Error, runtime has to be equal for all fields\n");
    }
    strcpy(save->runtime,rt);

    if (save->validtime[0] != 0 && strcmp(save->validtime,vt) != 0) {
	fprintf(stderr, "Error, validtime has to be equal for all fields\n");
    }
    strcpy(save->validtime,vt);
	
    /*Get levels, parameter name, description and unit*/
    // f_lev(mode, sec, data, ndata, level_buf, local);
    f_lev(call_ARG0(level_buf, NULL));
    //if (ndata != save->npts && save->npts>0) fprintf(stderr,"ERROR: fields do not contain equally many gridpoints, %d , %d \n",save->npts,ndata);
 	   
    if (strcmp(level_buf, "reserved") == 0) return(0);
    getName(sec, mode, NULL, name, desc, unit);
    fprintf(stderr,"Start processing of %s at %s, runtime=%s, validtime=%s \n", name, level_buf,rt,vt);

    ctr = GB2_Center(sec);

    sprintf(sql,"select * from wgrib2_parameter_mapping where center_id=%d and wgrib_param_name='%s' and wgrib_param_levelname='%s'", ctr, name, level_buf);
    fprintf(stderr,"SQL: %s\n", sql);
    if (mysql_query(save->conn, sql)) {	
	fatal_error("f_mysql_speed: mysql error %s", mysql_error(save->conn));
    }
    sprintf(new_level,"%s", "None");
    sprintf(conv,"%s", "None");
    sprintf(precision, "%s", "2");
    printf("\nCenter_id: %d \n", ctr);
    res = mysql_use_result(save->conn);
    while ( (row = mysql_fetch_row(res) ) != NULL) {
	printf("our_name: %s \n", row[3]);
	sprintf(new_name,"%s", row[3]);
	sprintf(new_level,"%s", row[4]);
	sprintf(conv,"%s", row[5]);
	sprintf(precision, "%s", row[6]);
    }

    printf("val_precision: %s \n", precision);
    printf("conversion: %s \n", conv);
    printf("our_levelname: %s \n", new_level);
	
    if (strcmp(new_level,"None") != 0) {
	fprintf(stderr,"Sets level %s as level %s\n", level_buf, new_level);
	sprintf(param, "%s_%s", new_name, new_level);
    } else {
	if (strcmp(level_buf,"surface") == 0) {
	    sprintf(param, "%s_0", name);
	} else {		
	    sscanf(level_buf,"%d %s", &level, &last);
 	    sprintf(param, "%s_%d", name, level);
	}
    }
	
    mysql_free_result(res);

    strcat(save->params,",");
    strcat(save->params,param);
	
    fprintf(stderr,"Gridpoints in data: %d\n", ndata);
    fprintf(stderr, "Gridpoints for insert: %d\n", save->npts);

    fprintf(stderr, "Remove unlikely: %d\n", save->remove_unlikely);
    fprintf(stderr, "Western longitudes: %d\n", save->wlon);

    for (j = 0; j < ndata; j++) {
	longitude = lon[j];
	if (longitude > 180 && save->wlon==1) longitude-=360;
	latitude = lat[j];
	value = data[j];
	if (save->remove_unlikely == 1 && value>0 && value<10e-8) value=0; 			
	if (save->remove_unlikely == 1 && (strcmp(name,"APCP")==0 || strcmp(name,"ASNOW")==0 || strcmp(name,"ACPCP")==0 ) && value==1) value=0;
	value = convunit(value, conv);
	if (strlen(save->rows[j]) < 2) 	{
	    if (!UNDEFINED_VAL(data[j])) {
		save->has_value[j] = 1;
		sprintf(save->rows[j],"\"%s\",\"%s\",%g,%g,%lg",rt,vt,latitude,longitude,value);
	    } else {
		sprintf(save->rows[j],"\"%s\",\"%s\",%g,%g,NULL",rt,vt,latitude,longitude);
	    }
	} else {
	    if (!UNDEFINED_VAL(data[j])) {
		save->has_value[j] = 1;
		sprintf(sql,",%lg",value);
	    } else {
		sprintf(sql,",NULL");
	    }
	    strcat(save->rows[j],sql);
	}

    }
    return 0;
}
Exemple #13
0
int main(int argc, char **argv) {

    unsigned char *pds, *gds;
    FILE *input, *inv, *output;
    int i, iscale;

    float *data, *tmp;
    double shmax, shmin;
    int ndata, nmax, date;

    long int pos_tmp, pos_rh, len_grib;
    int lev, press;
    char *levels[] = {"1000 mb","925 mb","850 mb",
	"700 mb","600 mb","500 mb","400 mb","300 mb"};

    /* preliminaries .. open up all files */

    if (argc != 4) {
	fprintf(stderr, "%s [in gribfile] [inv] [out gribfile]\n", argv[0]);
	exit(8);
    }
    if ((input = fopen(argv[1],"rb")) == NULL) {
        fprintf(stderr,"could not open file: %s\n", argv[1]);
        exit(7);
    }
    if ((inv = fopen(argv[2],"rb")) == NULL) {
        fprintf(stderr,"could not open file: %s\n", argv[2]);
        exit(7);
    }
    if ((output = fopen(argv[3],"wb+")) == NULL) {
        fprintf(stderr,"could not open file: %s\n", argv[3]);
        exit(7);
    }

    nmax = 0;
    tmp = NULL;

    for (lev = 0; lev < sizeof(levels) / sizeof(char *); lev++) {
	rewind(inv);

	/* read the temperature */

	pos_tmp = scan3(inv, 0, "TMP", levels[lev]);
	if (pos_tmp < 0) continue;

	len_grib = rd_grib_rec(input, pos_tmp, &pds, &gds, &data, &ndata);
	if (len_grib <= 0) {
	    fprintf(stderr,"bad record\n");
	    exit(8);
	}
	if (ndata > nmax) {
	    if (tmp != NULL) free(tmp);    
	    nmax = ndata;
	    tmp = malloc(nmax*sizeof (float));
	    if (tmp == NULL) {
		fprintf(stderr,"malloc error\n");
		exit(8);
	    }
	}
	for (i = 0; i < ndata; i++) tmp[i] = data[i];

	/* read for the same date */

	date = PDS_Hour(pds) + 100*PDS_Day(pds) + 10000*PDS_Month(pds) +
		1000000*PDS_Year4(pds);

	/* read RH */

	pos_rh = scan3(inv, date, "RH", levels[lev]);
	if (pos_rh < 0) {
	    rewind(inv);
	    pos_rh = scan3(inv, 0, "RH", levels[lev]);
	}
	printf("%ld %ld\n", pos_tmp, pos_rh);
	len_grib = rd_grib_rec(input, pos_rh, &pds, &gds, &data, &ndata);
	if (len_grib <= 0) {
	    fprintf(stderr,"bad record\n");
	    exit(8);
	}

	/* compute SPFH */
	press = atoi(levels[lev]);
	for (i = 0; i < ndata; i++) {
	    if(!UNDEFINED_VAL(data[i]) && !UNDEFINED_VAL(tmp[i])) 
	        data[i] = spfh(tmp[i], data[i], (float) press);
	}

	/* figure out a good decimal scaling
	 * use what was used in NCEP/NCAR Reanalysis
	 * assume data is all defined
	 */
	shmax = 0;
	shmin = 1e20;
	for (i = 1; i < ndata; i++) {
	    if (!UNDEFINED_VAL(data[i])) {
	        shmin = shmin <= data[i] ? shmin : data[i];
	        shmax = shmax >= data[i] ? shmax : data[i];
	    }
	}
	shmax = shmax - shmin;
	iscale = 0;
	if (shmax != 0.0) {
	    while (shmax < 999.9) {
		iscale++;
		shmax *= 10.0;
	    }
	    while (shmax > 9999.9) {
		iscale--;
		shmax /= 10.0;
	    }
	}

	pds = PDStool(pds,P_param(SPFH),P_dec_scale(iscale), P_end);
	wrt_grib_rec(pds, gds, data, ndata, output);
    }

    fclose(output);
    fclose(inv);
    fclose(input);
    return 0;
}
Exemple #14
0
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 j, pdt, 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;

        // allocate static variables

        *local = save = (struct local_struct *) malloc( sizeof(struct local_struct));
        if (save == NULL) fatal_error("memory allocation f_normalize","");

        if ((save->output = (void *) 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 = *local;

    if (mode == -2) {			// cleanup
        if (save->has_val == 1) {
            free(save->val);
            free_sec(save->clone_sec);
            free(save);
        }
        return 0;
    }

    if (mode >= 0) {			// processing

        // only hande PDT = 8
        pdt = GB2_ProdDefTemplateNo(sec);
        if (pdt != 8) return 0;

        // only process averages or accumulations

        // check for code table 4.8

        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


        // only process when fcst time units == ave/acc time units
        if (sec[4][17] != sec[4][48]) {
            if (int4(sec[4]+18) != 0) {
                return 0;
            }
        }

        fhr_2 = int4(sec[4]+18);			// start time
        dt2 = int4(sec[4]+49);				// delta-time
        if (dt2 == 0) return 0;	 			// dt == 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 (grib_type == simple) grib_out(sec, data_tmp, ndata, save->output);
                        else if (grib_type == jpeg) jpeg_grib_out(sec, data_tmp, ndata, nx,
                                 ny, use_scale, dec_scale, bin_scale, save->output);
                        else if (grib_type == ieee) ieee_grib_out(sec, data_tmp, ndata,
                            save->output);
            	    else fatal_error("NCEP_norm: unsupported grib type 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 = int4(save->clone_sec[4]+18);             // start time of save message
        dt1 = int4(save->clone_sec[4]+49);               // 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) {
                new_type = 1;
            }
        }

        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 (grib_type == simple) grib_out(sec, data_tmp, ndata, save->output);
                        else if (grib_type == jpeg) jpeg_grib_out(sec, data_tmp, ndata, nx,
                                 ny, use_scale, dec_scale, bin_scale, save->output);
                        else if (grib_type == ieee) ieee_grib_out(sec, data_tmp, ndata,
                            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]+49);		//  dt = dt2 - dt1
        save->clone_sec[4][17] = sec[4][48];                    // new forecast time unit
        int_char(dt1+fhr_1, save->clone_sec[4]+18);             // fhr = fhr + dt1

        for (i = 34; i < 41; 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 (grib_type == simple) grib_out(save->clone_sec, data_tmp, ndata, save->output);
                else if (grib_type == jpeg) jpeg_grib_out(save->clone_sec, data_tmp, ndata, nx,
                             ny, use_scale, dec_scale, bin_scale, save->output);
                else if (grib_type == ieee) ieee_grib_out(save->clone_sec, data_tmp, ndata,
                        save->output);
                else if (grib_type == complex1) complex_grib_out(save->clone_sec, data_tmp, ndata,
        		use_scale, dec_scale, bin_scale, wanted_bits, max_bits, 1, save->output);
                else if (grib_type == complex2) complex_grib_out(save->clone_sec, data_tmp, ndata,
        		use_scale, dec_scale, bin_scale, wanted_bits, max_bits, 2, save->output);
                else if (grib_type == complex3) complex_grib_out(save->clone_sec, data_tmp, ndata,
        		use_scale, dec_scale, bin_scale, wanted_bits, max_bits, 3, save->output);
        	else fatal_error("NCEP_norm: unsupported grib output type","");
        */

        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;
}