Ejemplo n.º 1
0
int ieee_grib_out(unsigned char **sec, float *data, unsigned int ndata, FILE *out) {

    unsigned int n_defined, i;
    int j;
    unsigned char *p, *sec0, *sec1, *sec2, *sec3, *sec4, *sec5, *sec6, *sec7;
#ifdef IEEE_BITMAP
    float *data_tmp;
#endif

    /* required passed sections */
    sec0 = sec[0];
    sec1 = sec[1];
    sec2 = sec[2];
    sec3 = sec[3];
    sec4 = sec[4];

    /* make a new section 6 */

#ifdef IEEE_BITMAP
    data_tmp = (float *) malloc(ndata * sizeof(float));
    for (i = 0; i < ndata; i++) {
	data_tmp[i] = data[i];
    }
    n_defined = ndata;
    sec6 = mk_bms(data_tmp, &n_defined);			// make bitmap section
    if (sec6 == NULL) fatal_error("grib_out ieee memory allocation sec6","");
#else
    n_defined = ndata;
    sec6 = (unsigned char *) malloc(6);
    if (sec6 == NULL) fatal_error("grib_out ieee memory allocation sec6","");
    uint_char(6 * sizeof (unsigned char), sec6);
    sec6[4] = 6;			// section 5
    sec6[5] = 255;			// no bitmap
#endif
    
    /* data representation section */

    sec5 = (unsigned char *) malloc(12 * sizeof(unsigned char));
    if (sec5 == NULL) fatal_error("grib_out ieee memory allocation sec5","");
    uint_char(12 * sizeof (unsigned char), sec5);
    sec5[4] = 5;			// section 5
    uint_char(ndata, sec5+5);		// number of points
    uint2_char(4,sec5+9);		// data template 4
    sec5[11] = 1;			// precision: ieee 32-bit


    /* data section */
    sec7 = (unsigned char *) malloc(5 + n_defined * 4);
    if (sec7 == NULL) fatal_error("grib_out ieee memory allocation sec7","");
    uint_char(5+n_defined*4, sec7);
    sec7[4] = 7;
    p = sec7 + 5;
    j = 0;
    for (i = 0; i < n_defined; i++) {
#ifdef IEEE_BITMAP
	flt2ieee_nan(data_tmp[i], p);
#else
	flt2ieee_nan(data[i], p);
#endif
	p += 4;
    }
#ifdef IEEE_BITMAP
    free(data_tmp);
#endif

    j = wrt_sec(sec0, sec1, sec2, sec3, sec4, sec5, sec6, sec7, out);

    free(sec5);
    free(sec6);
    free(sec7);

    return j;

}
Ejemplo n.º 2
0
int f_submsg_uv(ARG1) {

    struct local_struct {
        unsigned char *sec[9];
	const char *vname;    
        FILE *output;
    };

    struct local_struct *save;
    int i, j, is_v;
    unsigned long int size;
    char name[NAMELEN];
    unsigned char s[8];

    if (mode == -1) {		// initialization

        // allocate static structure

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

        if ((save->output = ffopen(arg1, file_append ? "ab" : "wb")) == NULL) {
            fatal_error("submsg_uv: could not open file %s", arg1);
        }
	save->vname = NULL;
        init_sec(save->sec);
	return 0;
    }

    save = (struct local_struct *) *local;

    if (mode == -2)  {		// cleanup
	if (save->vname != NULL) {		// write out cached field
	   i = wrt_sec(save->sec[0], save->sec[1], save->sec[2], save->sec[3], 
		save->sec[4], save->sec[5], save->sec[6], save->sec[7], save->output);
	   if (i) fatal_error_i("submsg_uv: last record problem %i",i);
           free_sec(save->sec);
	}
	ffclose(save->output);
        free(save);
	return 0;
    }

    if (mode >= 0 )  {					// processing
        i = getName(sec, mode, NULL, name, NULL, NULL);

	/* see if name == expected vname */
	is_v = 0;
	if (save->vname != NULL && strcmp(name, save->vname) == 0) {
	    is_v = 1;
            if (same_sec0(sec,save->sec) == 0) is_v = 0;
            if (same_sec1(sec,save->sec) == 0) is_v = 0;
            if (same_sec2(sec,save->sec) == 0) is_v = 0;
            if (same_sec3(sec,save->sec) == 0) is_v = 0;

	    i = GB2_ParmNum(sec);
	    j = GB2_ParmCat(sec);
	    GB2_ParmNum(sec) = GB2_ParmNum(save->sec);
	    GB2_ParmCat(sec) = GB2_ParmCat(save->sec);
            if (same_sec4(sec,save->sec) == 0) is_v = 0;
	    GB2_ParmNum(sec) = i;
	    GB2_ParmCat(sec) = j;
	}

	/* finished tests for U/V sequence */

	/* is U/V sequence */
	if (is_v) {
	    size = (unsigned long int) GB2_Sec0_size + GB2_Sec8_size +
	    (sec[1] ? uint4(sec[1]) : 0) +
	    (sec[2] ? uint4(sec[2]) : 0) +
	    (sec[3] ? uint4(sec[3]) : 0) +
	    (sec[4] ? uint4(sec[4]) : 0) +
	    (sec[5] ? uint4(sec[5]) : 0) +
	    (sec[6] ? uint4(sec[6]) : 0) +
	    (sec[7] ? uint4(sec[7]) : 0) +
	    (save->sec[4] ? uint4(save->sec[4]) : 0) +
	    (save->sec[5] ? uint4(save->sec[5]) : 0) +
	    (save->sec[6] ? uint4(save->sec[6]) : 0) +
	    (save->sec[7] ? uint4(save->sec[7]) : 0);

	    fwrite((void *) sec[0], sizeof(char), 8, save->output);
            uint8_char(size, s);
            fwrite((void *) s, sizeof(char), 8, save->output);

            if (sec[1]) {
                i = uint4(sec[1]);
                if (fwrite((void *)sec[1], sizeof(char), i, save->output) != i) return 1;
            }
            if (sec[2]) {
                i = uint4(sec[2]);
                if (fwrite((void *)sec[2], sizeof(char), i, save->output) != i) return 1;
            }
            if (sec[3]) {
                i = uint4(sec[3]);
                if (fwrite((void *)sec[3], sizeof(char), i, save->output) != i) return 1;
            }
            if (save->sec[4]) {
                i = uint4(save->sec[4]);
                if (fwrite((void *)save->sec[4], sizeof(char), i, save->output) != i) return 1;
            }
            if (save->sec[5]) {
                i = uint4(save->sec[5]);
                if (fwrite((void *)save->sec[5],sizeof(char), i, save->output) != i) return 1;
            }
            if (save->sec[6]) {
                i = uint4(save->sec[6]);
                if (fwrite((void *)save->sec[6],sizeof(char), i, save->output) != i) return 1;
            }
            if (save->sec[7]) {
                i = uint4(save->sec[7]);
                if (fwrite((void *)save->sec[7],sizeof(char), i, save->output) != i) return 1;
            }
            if (sec[4]) {
                i = uint4(sec[4]);
                if (fwrite((void *)sec[4], sizeof(char), i, save->output) != i) return 1;
            }
            if (sec[5]) {
                i = uint4(sec[5]);
                if (fwrite((void *)sec[5], sizeof(char), i, save->output) != i) return 1;
            }
            if (sec[6]) {
                i = uint4(sec[6]);
                if (fwrite((void *)sec[6], sizeof(char), i, save->output) != i) return 1;
            }
            if (sec[7]) {
                i = uint4(sec[7]);
                if (fwrite((void *)sec[7], sizeof(char), i, save->output) != i) return 1;
            }

            s[0] = s[1] = s[2] = s[3] = 55; /* s = "7777" */
            if (fwrite((void *) s, sizeof(char), 4, save->output) != 4)
	           fatal_error("submsg_uv: write record problem","");

	    save->vname = NULL;
            free_sec(save->sec);
            return 0;
	}

	/* has U but not V, write U */    

	if (save->vname != NULL) {
	   i = wrt_sec(save->sec[0], save->sec[1], save->sec[2], save->sec[3], 
		save->sec[4], save->sec[5], save->sec[6], save->sec[7], save->output);
	   if (i) fatal_error_i("submsg_uv: write record problem %i",i);
	   free_sec(save->sec);
	   save->vname = NULL;
	}

	/* check to see if new field is a U */

	save->vname = is_u(name);

	/* if U, cache it */
	if (save->vname != NULL) {
	    copy_sec(sec,save->sec);
	    return 0;
	}

	/* not U, write it out */
	i = wrt_sec(sec[0], sec[1], sec[2], sec[3], sec[4], sec[5], sec[6], sec[7], save->output);
	if (i) fatal_error_i("submsg_uv: write problem %i",i);
	return 0;
    }
    return 0;
}