/* * HEADER:400:Sec5:inv:0:Sec 5 values (Data representation section) */ int f_Sec5(ARG0) { if (mode >= 0) { sprintf(inv_out,"Sec5 len=%d #defined data points=%d Data Repr. Template=5.%d", uint4(&(sec[5][0])), uint4(&(sec[5][5])), uint2(&(sec[5][9]))); } return 0; }
/* check for same section .. only for sections 1..7 */ static int same_sec(unsigned char *seca, unsigned char *secb) { unsigned int a, b, i; if (seca == NULL && secb == NULL) return 1; if (seca == NULL || secb == NULL) return 0; a = uint4(seca); b = uint4(secb); if (a != b) return 0; for (i = 0; i < a; i++) { if (seca[i] != secb[i]) return 0; } return 1; }
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; }
uint4 Generate_0(float2 cacheCoord) // R4 { float2 blockCoord = floor(cacheCoord / float2(2,1)); float2 blockUL = blockCoord * float2(8,8); float2 subBlockUL = blockUL + float2(0, 4*(cacheCoord.x%2)); float4 sample[32]; for (uint y = 0; y < 4; ++y) { for (uint x = 0; x < 8; ++x) { sample[y*8+x] = SampleEFB(subBlockUL+float2(x,y)); } } uint dw[4]; for (uint i = 0; i < 4; ++i) { dw[i] = UINT_44444444_BE( Float8ToUint4(sample[8*i+0].r), Float8ToUint4(sample[8*i+1].r), Float8ToUint4(sample[8*i+2].r), Float8ToUint4(sample[8*i+3].r), Float8ToUint4(sample[8*i+4].r), Float8ToUint4(sample[8*i+5].r), Float8ToUint4(sample[8*i+6].r), Float8ToUint4(sample[8*i+7].r) ); } return uint4(dw[0], dw[1], dw[2], dw[3]); }
int f_grib(ARG1) { unsigned long int size; int i; unsigned char s[8]; if (mode == -1) { if ((*local = (void *) ffopen(arg1, file_append ? "ab" : "wb")) == NULL) { fatal_error("Could not open %s", arg1); } } else if (mode >= 0) { /* figure out size of grib file */ size = (unsigned long int) GB2_Sec0_size + GB2_Sec1_size(sec) + GB2_Sec2_size(sec) + + GB2_Sec3_size(sec) + GB2_Sec4_size(sec) + GB2_Sec5_size(sec) + GB2_Sec6_size(sec) + GB2_Sec7_size(sec) + GB2_Sec8_size; /* section 0 */ fwrite((void *) sec[0], sizeof(char), 8, (FILE *) *local); uint8_char(size, s); fwrite((void *) s, sizeof(char), 8, (FILE *) *local); for (i = 1; i <= 7; i++) { if (sec[i]) { size = uint4(sec[i]); fwrite((void *) sec[i], sizeof(char), size, (FILE *) *local); } } s[0] = s[1] = s[2] = s[3] = 55; /* s = "7777" */ fwrite((void *) s, sizeof(char), 4, (FILE *) *local); // fwrite((void *) "7777", sizeof(char), 4, (FILE *) *local); if (flush_mode) fflush((FILE *) *local); } return 0; }
unsigned int pds_fcst_time(unsigned char **sec) { int p; p = GB2_ProdDefTemplateNo(sec); if (p <= 10 || p == 1000 || p == 1001 || p == 1002 || p == 1100 || p == 1101) { return uint4(sec[4]+18); } return 0xffffffff; }
void gvcst_map_build(uint4 *array, sm_uc_ptr_t base_addr, cw_set_element *cs, trans_num ctn) { boolean_t status; uint4 (*bml_func)(); uint4 bitnum, ret; #ifdef DEBUG int4 prev_bitnum, actual_cnt = 0; #endif DEBUG_ONLY(VALIDATE_BM_BLK(cs->blk, (blk_hdr_ptr_t)base_addr, cs_addrs, gv_cur_region, status);)
int parse_next_msg(unsigned char **sec) { unsigned char *p, *end_of_msg; int i; end_of_msg = Msg + GB2_MsgLen(sec); p = sec[7]; if (p[4] != 7) { fatal_error("parse_next_msg: parsing error",""); } p += uint4(p); if (p > end_of_msg) fatal_error("bad grib fill",""); while (p < Sec[8]) { Sec[p[4]] = p; // code to handle code table 6.0 if (p[4] == 6) { if (p[5] == 0) { Sec6_bitmap = p; } else if (p[5] >= 1 && p[5] <= 253) { fatal_error("parse_next_msg: predefined bitmaps are not handled",""); } else if (p[5] == 254) { if (Sec6_bitmap == NULL) { fatal_error("parse_1st_msg: illegal grib msg, bitmap not defined code, table 6.0=254",""); } Sec[6] = Sec6_bitmap; } } if (p[4] == 7) { // end of message .. save on sec[] for (i = 0; i < 9; i++) { sec[i] = Sec[i]; } return 0; } p += uint4(p); if (p > end_of_msg) fatal_error("bad grib fill",""); } return 1; }
int f_Sec_len(ARG0) { const char *new_sec2, *new_sec3; if (mode >= 0) { sprintf(inv_out,"Sec size msg=%lu", (unsigned long int) GB2_MsgLen(sec)); inv_out += strlen(inv_out); new_sec2=new_sec3=""; if (submsg != 1) { if (sec[3] + uint4(sec[3]) != sec[4]) { new_sec2 = new_sec3 = "*"; } else { if ((sec[2] != NULL) && (sec[2] + uint4(sec[2]) == sec[3])) { new_sec2 = "*"; } } } sprintf(inv_out," id(1)=%u", uint4(sec[1])); inv_out += strlen(inv_out); if (sec[2] != NULL) sprintf(inv_out," local(2)=%u%s", uint4(sec[2]),new_sec2); else sprintf(inv_out," local(2)=0"); inv_out += strlen(inv_out); sprintf(inv_out," grid(3)=%u%s", uint4(sec[3]),new_sec3); inv_out += strlen(inv_out); sprintf(inv_out," product(4)=%u", uint4(sec[4])); inv_out += strlen(inv_out); sprintf(inv_out," data-rep(5)=%u", uint4(sec[5])); inv_out += strlen(inv_out); if (sec[5] != NULL) sprintf(inv_out," bitmap(6)=%u", uint4(sec[6])); else sprintf(inv_out," bitmap(6)=0"); inv_out += strlen(inv_out); sprintf(inv_out," data(7)=%u", uint4(sec[7])); inv_out += strlen(inv_out); } return 0; }
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; }
int parse_1st_msg(unsigned char **sec) { unsigned char *p, *end_of_msg; int i; if (Msg == NULL) fatal_error("parse_1st_msg .. Msg == NULL",""); Sec[0] = Msg; Sec[1] = Sec[2] = Sec[3] = Sec[4] = Sec[5] = Sec[6] = Sec[7] = Sec6_bitmap = NULL; end_of_msg = Msg + GB2_MsgLen(Sec); p = Msg + 16; while (Sec[8] - p > 0) { if (p[4] > 8) fatal_error_i("parse_1st_msg illegal section %d", (int) p[4]); Sec[p[4]] = p; /* Section 6: bitmap */ if (p[4] == 6) { if (p[5] == 0) { Sec6_bitmap = p; } else if (p[5] >= 1 && p[5] <= 253) { fatal_error("parse_1st_msg: predefined bitmaps are not handled",""); } else if (p[5] == 254) { fatal_error("parse_1st_msg: illegal grib msg, bitmap not defined code, table 6.0=254",""); } } /* last section */ if (p[4] == 7) { for (i = 0; i < 9; i++) { sec[i] = Sec[i]; } return 0; } p += uint4(p); if (p > end_of_msg) fatal_error("bad grib fill",""); } fatal_error("parse_1st_msg illegally format grib",""); return 1; }
int fix_ncep_3(unsigned char **sec) { int j; if (GB2_Center(sec) != NCEP) return 0; // only for NCEP files j = code_table_5_0(sec); if (j == 0 || j == 1 || j == 40 || j == 41 || j == 40000 || j == 40010) { // simple, jpeg, png if (sec[5][19] == 0) { sec[5][17] = sec[5][18] = 0; } } else if (j == 2 || j == 3) { // complex packing if (uint4(sec[5]+31) == 0) { // number of groups sec[5][17] = sec[5][18] = 0; } } else if (j == 50 || j == 51) { // spectral if (sec[5][19] == 0) { sec[5][17] = sec[5][18] = 0; } } return 0; }
unsigned char *rd_grib2_msg_seq(FILE *input, long int *pos, unsigned long int *len, int *num_submsgs) { int i, j, c, c1, c2, c3, c4; long int len_grib; unsigned char *p, *end_of_msg; /* setup grib buffer */ if (buffer == NULL) { if ((buffer = (unsigned char *) malloc(BUFF_ALLOC0)) == NULL) { fatal_error("not enough memory: rd_grib2_msg",""); } buffer_size = BUFF_ALLOC0; } /* search for GRIB...2 */ while (1) { c = get_input(input); if (c == EOF) { *len = 0; return NULL; } if (c != G) continue; if ( (c = get_input(input)) != R) { unget_input(c); continue; } if ( (c = get_input(input)) != I) { unget_input(c); continue; } if ( (c = get_input(input)) != B) { unget_input(c); continue; } c1 = get_input(input); c2 = get_input(input); c3 = get_input(input); c4 = get_input(input); if (c4 == 1) { fprintf(stderr,"grib1 message ignored (use wgrib)\n"); continue; } if (c4 != 2) { unget_input(c4); unget_input(c3); unget_input(c2); unget_input(c1); continue; } buffer[0] = G; buffer[1] = R; buffer[2] = I; buffer[3] = B; buffer[4] = c1; buffer[5] = c2; buffer[6] = c3; buffer[7] = c4; /* fill in the size 8-15, unget buffer is empty */ for (i = 0; i < 8; i++) { buffer[8+i] = c = get_input(input); if (c == EOF) { *len = 0; return NULL; } } break; } *len = len_grib = uint8(buffer+8); *pos = pos_input - 16; if (buffer_size < len_grib) { buffer_size = len_grib + len_grib / 10; buffer = (unsigned char *) realloc((void *) buffer, buffer_size); } if (io_buffer_cnt) fatal_error("rd_grib2_msg_seq: program error: io_buffer is not flushed",""); j=fread(buffer+16, sizeof (unsigned char), len_grib-16, input); pos_input += j; if (j != len_grib-16) fatal_error("rd_grib2_msg_seq, read outside of file, bad grib file",""); Msg = buffer; Sec[8] = Msg + len_grib - 4; if (Sec[8][0] != 55 || Sec[8][1] != 55 || Sec[8][2] != 55 || Sec[8][3] != 55) { fatal_error("rd_grib2_msg_seq, missing end section ('7777')",""); } Sec[0] = Msg; /* scan message for number of submessages and perhaps for errors */ p = Msg + GB2_Sec0_size; end_of_msg = Msg + len_grib; i = 0; while (p < Sec[8]) { if (p[4] == 7) i++; if (uint4(p) < 5) fatal_error_i("rd_grib2_msg: illegal grib: section length, section %i", p[4]); p += uint4(p); if (p > end_of_msg) fatal_error("bad grib format",""); } if (p != Sec[8]) { fatal_error("rd_grib2_msg: illegal format, end section expected",""); } *num_submsgs = i; *len = len_grib; return Msg; }
void GpuFftCS5::Execute(TexturePtr const & out_real, TexturePtr const & out_imag, TexturePtr const & in_real, TexturePtr const & in_imag) { RenderFactory& rf = Context::Instance().RenderFactoryInstance(); RenderEngine& re = rf.RenderEngineInstance(); FrameBufferPtr old_fb = re.CurFrameBuffer(); re.BindFrameBuffer(FrameBufferPtr()); int index = 0; // X direction { uint32_t istride = width_ / 8; float phase_base = -PI2 / width_; *(effect_->ParameterByName("ostride2")) = uint2(width_ / 8, 0); *(effect_->ParameterByName("iscale2")) = uint2(8, 1); *(effect_->ParameterByName("istride2")) = uint4(istride, 0, istride - 1, static_cast<uint32_t>(-1)); *(effect_->ParameterByName("phase_base2")) = phase_base; this->Radix008A(tmp_real_tex_[index], tmp_imag_tex_[index], in_real, in_imag, width_ / 8, height_, 1 == istride, false); index = !index; uint32_t t = width_; while (t > 8) { istride /= 8; phase_base *= 8; *(effect_->ParameterByName("istride2")) = uint4(istride, 0, istride - 1, static_cast<uint32_t>(-1)); *(effect_->ParameterByName("phase_base2")) = phase_base; this->Radix008A(tmp_real_tex_[index], tmp_imag_tex_[index], tmp_real_tex_[!index], tmp_imag_tex_[!index], width_ / 8, height_, 1 == istride, false); index = !index; t /= 8; } } // Y direction { uint32_t istride = height_ / 8; float phase_base = -PI2 / height_; *(effect_->ParameterByName("ostride2")) = uint2(0, height_ / 8); *(effect_->ParameterByName("iscale2")) = uint2(1, 8); *(effect_->ParameterByName("istride2")) = uint4(0, istride, static_cast<uint32_t>(-1), istride - 1); *(effect_->ParameterByName("phase_base2")) = phase_base; if (1 == istride) { this->Radix008A(out_real, out_imag, tmp_real_tex_[!index], tmp_imag_tex_[!index], width_, height_ / 8, false, 1 == istride); } else { this->Radix008A(tmp_real_tex_[index], tmp_imag_tex_[index], tmp_real_tex_[!index], tmp_imag_tex_[!index], width_, height_ / 8, false, 1 == istride); } index = !index; uint32_t t = height_; while (t > 8) { istride /= 8; phase_base *= 8; *(effect_->ParameterByName("istride2")) = uint4(0, istride, static_cast<uint32_t>(-1), istride - 1); *(effect_->ParameterByName("phase_base2")) = phase_base; if (1 == istride) { this->Radix008A(out_real, out_imag, tmp_real_tex_[!index], tmp_imag_tex_[!index], width_, height_ / 8, false, 1 == istride); } else { this->Radix008A(tmp_real_tex_[index], tmp_imag_tex_[index], tmp_real_tex_[!index], tmp_imag_tex_[!index], width_, height_ / 8, false, 1 == istride); } index = !index; t /= 8; } } re.BindFrameBuffer(old_fb); }
int f_new_grid(ARG4) { struct local_struct *save; unsigned int i; int is_u, is_v, ftn_npnts, ftn_nout; int kgds[200], km; float *data_in, *data_out; double x0, y0, dx, dy, xn, yn; double lov, lad, latin1, latin2; int proj; // projection: for LC 0 = NP, 128 = SP char name[NAMELEN]; int j, ibi, ibo, iret, nnx, nny, n_out; unsigned char *new_sec[8], *s, *bitmap, *bitmap_out, *p; /* for lambertc */ double r_maj, r_min, ref_lon, ref_lat; if (mode == -1) { // initialization decode = 1; output_order_wanted = raw; // in raw order #ifdef G95 // initialize g95 runtime library if (g95_runstop == 0) { g95_runtime_start(0,NULL); g95_runstop = 1; } #endif // if ( (sizeof(vectors) / sizeof (vectors[0])) % 2 == 1) fatal_error("new_grid: program error in vectors[]",""); // allocate static variables *local = save = (struct local_struct *) malloc( sizeof(struct local_struct)); if (save == NULL) fatal_error("memory allocation -wind_speed",""); if ((save->out = ffopen(arg4, file_append ? "ab" : "wb")) == NULL) { fatal_error("-new_grid: could not open file %s", arg1); } save->has_u = 0; save->radius_major = save->radius_minor = 0.0; init_sec(save->clone_sec); s = NULL; // parse NCEP grids */ ncep_grids(&arg1, &arg2, &arg3); // for each output grid if (strcmp(arg1,"latlon") == 0) { if (sscanf(arg2,"%lf:%d:%lf", &x0, &nnx, &dx) != 3) fatal_error("new_grid: XDEF wrong:%s",arg2); if (sscanf(arg3,"%lf:%d:%lf", &y0, &nny, &dy) != 3) fatal_error("new_grid: YDEF wrong:%s",arg3); if (x0 < 0.0) x0 += 360.0; save->nx = nnx; save->ny = nny; save->npnts_out = n_out = nnx*nny; if (n_out <= 0) fatal_error("new_grid: bad nx, ny",""); // make a new section 3 s = sec3_lola(nnx, x0, dx, nny, y0, dy, sec); } else if (strncmp(arg1,"mercator:",9) == 0) { if (sscanf(arg1,"mercator:%lf", &lad) != 1) fatal_error("new_grid: LaD (latitude interesection) not specified",""); if (sscanf(arg2,"%lf:%d:%lf:%lf", &x0, &nnx, &dx, &xn) != 4) fatal_error("new_grid: XDEF wrong:%s",arg2); if (sscanf(arg3,"%lf:%d:%lf:%lf", &y0, &nny, &dy, &yn) != 4) if (x0 < 0.0) x0 += 360.0; save->nx = nnx; save->ny = nny; save->npnts_out = n_out = nnx*nny; if (n_out <= 0) fatal_error("new_grid: bad nx, ny",""); // make a new section 3 s = sec3_mercator(lad, nnx, x0, dx, xn, nny, y0, dy, yn, sec); } else if (strcmp(arg1,"gaussian") == 0) { if (sscanf(arg2,"%lf:%d:%lf", &x0, &nnx, &dx) != 3) fatal_error("new_grid: XDEF wrong:%s",arg2); if (sscanf(arg3,"%lf:%d", &y0, &nny) != 2) fatal_error("new_grid: YDEF wrong:%s",arg3); if (x0 < 0.0) x0 += 360.0; save->nx = nnx; save->ny = nny; save->npnts_out = n_out = nnx*nny; if (n_out <= 0) fatal_error("new_grid: bad nx, ny",""); // make a new section 3 s = sec3_gaussian(nnx, x0, dx, nny, y0, sec); } else if (strncmp(arg1,"lambert:",8) == 0) { i = sscanf(arg1,"lambert:%lf:%lf:%lf:%lf", &lov, &latin1, &latin2, &lad); if (i < 2) fatal_error("new_grid: arg1 wrong:%s",arg1); if (lov < 0.0) lov += 360.0; if (i < 3) latin2 = latin1; if (i < 4) lad = latin2; proj = 0; if (latin2 < 0.0) proj = 128; if (sscanf(arg2,"%lf:%d:%lf", &x0, &nnx, &dx) != 3) fatal_error("new_grid: XDEF wrong:%s",arg2); if (sscanf(arg3,"%lf:%d:%lf", &y0, &nny, &dy) != 3) fatal_error("new_grid: YDEF wrong:%s",arg3); if (x0 < 0.0) x0 += 360.0; save->nx = nnx; save->ny = nny; save->npnts_out = n_out = nnx*nny; if (n_out <= 0) fatal_error("new_grid: bad nx, ny",""); // make a new section 3 s = sec3_lc(lov, lad, latin1, latin2, proj, nnx, x0, dx, nny, y0, dy, sec); } /* for lambertc, input is the lon-lat of center point */ /* can not calc grid until radius is given, so do lambert code to check args */ else if (strncmp(arg1,"lambertc:",9) == 0) { i = sscanf(arg1,"lambertc:%lf:%lf:%lf:%lf", &lov, &latin1, &latin2, &lad); if (i < 2) fatal_error("new_grid: arg1 wrong:%s",arg1); if (lov < 0.0) lov += 360.0; if (i < 3) latin2 = latin1; if (i < 4) lad = latin2; proj = 0; if (latin2 < 0.0) proj = 128; if (sscanf(arg2,"%lf:%d:%lf", &x0, &nnx, &dx) != 3) fatal_error("new_grid: XDEF wrong:%s",arg2); if (sscanf(arg3,"%lf:%d:%lf", &y0, &nny, &dy) != 3) fatal_error("new_grid: YDEF wrong:%s",arg3); if (x0 < 0.0) x0 += 360.0; save->nx = nnx; save->ny = nny; save->npnts_out = n_out = nnx*nny; if (n_out <= 0) fatal_error("new_grid: bad nx, ny",""); // make a new section 3 s = sec3_lc(lov, lad, latin1, latin2, proj, nnx, x0, dx, nny, y0, dy, sec); } else if (strncmp(arg1,"nps:",4) == 0 || strncmp(arg1,"sps:",4) == 0) { if (sscanf(arg1,"%*[ns]ps:%lf:%lf", &lov, &lad) != 2) fatal_error("new_grid: arg1 wrong:%s",arg1); if (lad != 60.0) fatal_error("New_grid: only LatD = 60 is supported",""); proj = 0; if (arg1[0] == 's') proj = 128; if (sscanf(arg2,"%lf:%d:%lf", &x0, &nnx, &dx) != 3) fatal_error("new_grid: XDEF wrong:%s",arg2); if (sscanf(arg3,"%lf:%d:%lf", &y0, &nny, &dy) != 3) fatal_error("new_grid: YDEF wrong:%s",arg3); if (lov < 0.0) lov += 360.0; if (x0 < 0.0) x0 += 360.0; save->nx = nnx; save->ny = nny; save->npnts_out = n_out = nnx*nny; if (n_out <= 0) fatal_error("new_grid: bad nx, ny",""); // make a new section 3 s = sec3_polar_stereo(lov, lad, proj, nnx, x0, dx, nny, y0, dy, sec); } else fatal_error("new_grid: unsupported output grid %s", arg1); // save new section 3 i = (int) uint4(s); // size of section 3 new_sec[3] = save->sec3 = (unsigned char *) malloc(i * sizeof(unsigned char)); for (j = 0; j < i; j++) save->sec3[j] = s[j]; // apply wind rotation .. change flag 3.3 if (wind_rotation == undefined) { fprintf(stderr,"Warning: -new_grid wind orientation undefined, " "use \"-new_grid_winds (grid|earth)\", earth used (N=North Pole)\n"); if ( (p = flag_table_3_3_location(new_sec)) ) *p = *p & (255 - 8); } if (wind_rotation == grid && (p = flag_table_3_3_location(new_sec))) *p = *p | 8; if (mk_kgds(new_sec, save->kgds_out)) fatal_error("new_grid: encoding output kgds",""); /* some vectors need by interpolation routines */ if ((save->rlat = (float *) malloc(n_out * sizeof(float))) == NULL) fatal_error("new_grid memory allocation",""); if ((save->rlon = (float *) malloc(n_out * sizeof(float))) == NULL) fatal_error("new_grid memory allocation",""); if ((save->crot = (float *) malloc(n_out * sizeof(float))) == NULL) fatal_error("new_grid memory allocation",""); if ((save->srot = (float *) malloc(n_out * sizeof(float))) == NULL) fatal_error("new_grid memory allocation",""); return 0; } save = (struct local_struct *) *local; if (mode == -2) { // cleanup #ifdef G95 if (g95_runstop == 1) { g95_runtime_stop(); g95_runstop = 0; } #endif if (save->has_u > 0) { fprintf(stderr,"-new_grid: last field %s was not interpolated (missing V)\n", save->name); free(save->u_val); free_sec(save->clone_sec); } free(save->rlon); free(save->rlat); free(save->crot); free(save->srot); free(save->sec3); ffclose(save->out); free(save); return 0; } if (mode >= 0) { // processing /* The kgds of some output grids will change depending on input grid */ /* for example, radius of earth is not known grib file is read, */ /* and mass vs wind fields */ /* right nowm, only affects lambertc */ if (strncmp(arg1,"lambertc:",8) == 0) { // lambertc depends on the radius of the earth which is // set by the input grib file /* read earth radius */ i = axes_earth(sec, &r_maj, &r_min); if (i) fatal_error_i("axes_earth: error code %d", i); if (save->radius_major != r_maj || save->radius_minor != r_min) { // update sec3 and kgds i = sscanf(arg1,"lambertc:%lf:%lf:%lf:%lf", &lov, &latin1, &latin2, &lad); if (i < 2) fatal_error("new_grid: arg1 wrong:%s",arg1); if (lov < 0.0) lov += 360.0; if (i < 3) latin2 = latin1; if (i < 4) lad = latin2; proj = 0; if (latin2 < 0.0) proj = 128; if (sscanf(arg2,"%lf:%d:%lf", &x0, &nnx, &dx) != 3) fatal_error("new_grid: XDEF wrong:%s",arg2); if (sscanf(arg3,"%lf:%d:%lf", &y0, &nny, &dy) != 3) fatal_error("new_grid: YDEF wrong:%s",arg3); if (x0 < 0.0) x0 += 360.0; save->nx = nnx; save->ny = nny; save->npnts_out = n_out = nnx*nny; if (n_out <= 0) fatal_error("new_grid: bad nx, ny",""); ref_lon = x0; ref_lat = y0; i = new_grid_lambertc(nnx, nny, ref_lon, ref_lat, latin1, latin2, lov, lad, r_maj, r_min, dx, dy, &x0, &y0); if (i) fatal_error_i("new_grid_lambertc: error code %d", i); // make a new section 3 s = sec3_lc(lov, lad, latin1, latin2, proj, nnx, x0, dx, nny, y0, dy, sec); // save new section 3 i = (int) uint4(s); // size of section 3 for (j = 0; j < i; j++) save->sec3[j] = s[j]; // make kgds new_sec[3] = save->sec3; if (mk_kgds(new_sec, save->kgds_out)) fatal_error("new_grid: encoding output kgds",""); // save radius of earth, to show sec3 and kgds has been done save->radius_major = r_maj; save->radius_minor = r_min; } } if (output_order != raw) fatal_error("new_grid: must be in raw output order",""); i = getName(sec, mode, NULL, name, NULL, NULL); is_u = is_v = 0; // for (j = 0 ; j < sizeof(vectors) / sizeof(vectors[0]); j++) { for (j = 0; vectors[j] != NULL; j++) { if (strcmp(name,vectors[j]) == 0) { if (j % 2 == 0) is_u = 1; else is_v = 1; break; } } // fprintf(stderr, " %s isu %d isv %d has_u %d\n", name, is_u, is_v, save->has_u); // for (i = 0; i < 12; i++) { printf("kgds_out[%d] = %d ",i,save->kgds_out[i]); } // check if V matches expectation if (is_v && (save->has_u == 0 || (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) )) { fprintf(stderr,"-new_grid: %s doesn't pair with previous vector field, field ignored\n", name); return 0; } // if U field - save if (is_u) { if (save->has_u > 0) { fprintf(stderr,"-new_grid: missing V, %s not interpolated\n",save->name); free(save->u_val); free_sec(save->clone_sec); } copy_sec(sec, save->clone_sec); copy_data(data,ndata,&(save->u_val)); GB2_ParmNum(save->clone_sec) = GB2_ParmNum(sec) + 1; save->has_u = 1; strncpy(save->name, name,NAMELEN-1); save->name[NAMELEN-2]=0; return 0; } // at this point will call polates with either a scalar or vector n_out = save->npnts_out; nnx = save->nx; nny = save->ny; km = 1; // only one field if (mk_kgds(sec, kgds)) fatal_error("new_grid: encoding input kgds",""); data_in = (float *) malloc(npnts * (1 + (is_v != 0)) * sizeof(float)); bitmap = (unsigned char *) malloc(npnts * sizeof(unsigned char)); bitmap_out = (unsigned char *) malloc(n_out * sizeof(unsigned char)); data_out = (float *) malloc(n_out * (1 + (is_v != 0)) * sizeof(float)); if (data_in == NULL || data_out == NULL || bitmap == NULL || bitmap_out == NULL) fatal_error("new_grid: memory allocation problem",""); ibi = 0; // input bitmap is not used if (is_v) { for (i = 0; i < npnts; i++) { if (DEFINED_VAL(data[i]) && DEFINED_VAL(save->u_val[i])) { data_in[i] = save->u_val[i]; data_in[i+npnts] = data[i]; bitmap[i] = 1; } else { data_in[i] = data_in[i + npnts] = 0.0; bitmap[i] = 0; ibi = 1; // input bitmap is used } } if (mode == 98) fprintf(stderr," UV interpolation %s , %s\n", save->name, name); } else { for (i = 0; i < npnts; i++) { if (DEFINED_VAL(data[i])) { data_in[i] = data[i]; bitmap[i] = 1; } else { data_in[i] = 0.0; bitmap[i] = 0; ibi = 1; // input bitmap is used } } } // interpolate // for (i = 0; i < 12; i++) { printf("\nkgds_in[%d] = %d out=%d ",i,kgds[i],save->kgds_out[i]); } ftn_npnts = (int) npnts; ftn_nout = (int) n_out; if (is_v) { IPOLATEV(&interpol_type, ipopt,kgds,save->kgds_out, &ftn_npnts, &n_out, &km, &ibi, bitmap, data_in, data_in+npnts, &ftn_nout,save->rlat,save->rlon, save->crot, save->srot, &ibo, bitmap_out, data_out, data_out + n_out, &iret); } else { IPOLATES(&interpol_type, ipopt,kgds,save->kgds_out, &ftn_npnts, &n_out, &km, &ibi, bitmap, data_in, &ftn_nout, save->rlat,save->rlon, &ibo, bitmap_out, data_out, &iret); } if (iret != 0) { for (i = 0; i < 12; i++) { fprintf(stderr," IPOLATES error: kgds[%d] input %d output %d\n", i+1,kgds[i],save->kgds_out[i]); } if (iret == 2) fatal_error("IPOLATES failed, unrecognized input grid or no grid overlap",""); if (iret == 3) fatal_error("IPOLATES failed, unrecognized output grid",""); fatal_error_i("IPOLATES failed, error %d",iret); } n_out = (unsigned int) ftn_nout; /* use bitmap to set UNDEFINED values */ if (ibo == 1) { // has a bitmap if (is_v) { for (i = 0; i < n_out; i++) { if (bitmap_out[i] == 0) data_out[i] = data_out[i+n_out] = UNDEFINED; } } else { for (i = 0; i < n_out; i++) { if (bitmap_out[i] == 0) data_out[i] = UNDEFINED; } } } // now to write out the grib file for (i = 0; i < 8; i++) new_sec[i] = sec[i]; new_sec[3] = save->sec3; if (is_v != 0) { GB2_ParmNum(new_sec) = GB2_ParmNum(new_sec) - 1; grib_wrt(new_sec, data_out, n_out, nnx, nny, use_scale, dec_scale, bin_scale, wanted_bits, max_bits, grib_type, save->out); GB2_ParmNum(new_sec) = GB2_ParmNum(new_sec) + 1; grib_wrt(new_sec, data_out+n_out, n_out, nnx, nny, use_scale, dec_scale, bin_scale, wanted_bits, max_bits, grib_type, save->out); } else { grib_wrt(new_sec, data_out, n_out, nnx, nny, use_scale, dec_scale, bin_scale, wanted_bits, max_bits, grib_type, save->out); } if (flush_mode) fflush(save->out); free(data_in); free(bitmap); free(bitmap_out); free(data_out); if (is_v != 0) { save->has_u = 0; free(save->u_val); free_sec(save->clone_sec); } } return 0; }
int unpk_complex(unsigned char **sec, float *data, unsigned int ndata) { unsigned int i, j, n; int k, nbits, ref_group_length; unsigned char *p, *d, *mask_pointer; double ref_val,factor_10, factor_2, factor; float missing1, missing2; int n_sub_missing; int pack, offset; unsigned clocation; unsigned int ngroups, ref_group_width, nbit_group_width, len_last, npnts; int nbits_group_len, group_length_factor; int *group_refs, *group_widths, *group_lengths, *group_offset, *udata; unsigned int *group_clocation, *group_location; int m1, m2, mask, last, penultimate; int extra_vals[2]; int min_val; int ctable_5_4, ctable_5_6, bitmap_flag, extra_octets; extra_vals[0] = extra_vals[1] = 0; pack = code_table_5_0(sec); if (pack != 2 && pack != 3) return 0; p = sec[5]; ref_val = ieee2flt(p+11); factor_2 = Int_Power(2.0, int2(p+15)); factor_10 = Int_Power(10.0, -int2(p+17)); ref_val *= factor_10; factor = factor_2 * factor_10; nbits = p[19]; ngroups = uint4(p+31); bitmap_flag = code_table_6_0(sec); ctable_5_6 = code_table_5_6(sec); if (pack == 3 && (ctable_5_6 != 1 && ctable_5_6 != 2)) fatal_error_i("unsupported: code table 5.6=%d", ctable_5_6); extra_octets = (pack == 2) ? 0 : sec[5][48]; if (ngroups == 0) { if (bitmap_flag == 255) { for (i = 0; i < ndata; i++) data[i] = ref_val; return 0; } if (bitmap_flag == 0 || bitmap_flag == 254) { mask_pointer = sec[6] + 6; mask = 0; for (i = 0; i < ndata; i++) { if ((i & 7) == 0) mask = *mask_pointer++; data[i] = (mask & 128) ? ref_val : UNDEFINED; mask <<= 1; } return 0; } fatal_error("unknown bitmap", ""); } ctable_5_4 = code_table_5_4(sec); ref_group_width = p[35]; nbit_group_width = p[36]; ref_group_length = uint4(p+37); group_length_factor = p[41]; len_last = uint4(p+42); nbits_group_len = p[46]; #ifdef DEBUG fprintf(stderr,"ctable 5.4 %d ref_group_width %u nbit_group_width %u ref_group_length %u group_length_factor %d\n", ctable_5_4, ref_group_width, nbit_group_width, ref_group_length, group_length_factor); fprintf(stderr,"len_last %u nbit_group_len %u\n", len_last, nbits_group_len); #endif npnts = GB2_Sec5_nval(sec); // number of defined points n_sub_missing = sub_missing_values(sec, &missing1, &missing2); // allocate group widths and group lengths group_refs = (int *) malloc(sizeof (unsigned int) * (size_t) ngroups); group_widths = (int *) malloc(sizeof (unsigned int) * (size_t) ngroups); group_lengths = (int *) malloc(sizeof (unsigned int) * (size_t) ngroups); group_location = (unsigned int *) malloc(sizeof (unsigned int) * (size_t) ngroups); group_clocation = (unsigned int *) malloc(sizeof (unsigned int) * (size_t) ngroups); group_offset = (int *) malloc(sizeof (unsigned int) * (size_t) ngroups); udata = (int *) malloc(sizeof (unsigned int) * (size_t) npnts); if (group_refs == NULL || group_widths == NULL || group_lengths == NULL || group_location == NULL || group_clocation == NULL || group_offset == NULL || udata == NULL) fatal_error("unpk_complex: memory allocation",""); // read any extra values d = sec[7]+5; min_val = 0; if (extra_octets) { extra_vals[0] = uint_n(d,extra_octets); d += extra_octets; if (ctable_5_6 == 2) { extra_vals[1] = uint_n(d,extra_octets); d += extra_octets; } min_val = int_n(d,extra_octets); d += extra_octets; } if (ctable_5_4 != 1) fatal_error_i("internal decode does not support code table 5.4=%d", ctable_5_4); #pragma omp parallel { #pragma omp sections { #pragma omp section { // read the group reference values rd_bitstream(d, 0, group_refs, nbits, ngroups); } #pragma omp section { unsigned int i; // read the group widths rd_bitstream(d+(nbits*ngroups+7)/8,0,group_widths,nbit_group_width,ngroups); for (i = 0; i < ngroups; i++) group_widths[i] += ref_group_width; } #pragma omp section { unsigned int i; // read the group lengths if (ctable_5_4 == 1) { rd_bitstream(d+(nbits*ngroups+7)/8+(ngroups*nbit_group_width+7)/8, 0,group_lengths, nbits_group_len, ngroups-1); for (i = 0; i < ngroups-1; i++) { group_lengths[i] = group_lengths[i] * group_length_factor + ref_group_length; } group_lengths[ngroups-1] = len_last; } } } #pragma omp single { d += (nbits*ngroups + 7)/8 + (ngroups * nbit_group_width + 7) / 8 + (ngroups * nbits_group_len + 7) / 8; // do a check for number of grid points and size clocation = offset = n = j = 0; } #pragma omp sections { #pragma omp section { unsigned int i; for (i = 0; i < ngroups; i++) { group_location[i] = j; j += group_lengths[i]; n += group_lengths[i]*group_widths[i]; } } #pragma omp section { unsigned int i; for (i = 0; i < ngroups; i++) { group_clocation[i] = clocation; clocation = clocation + group_lengths[i]*(group_widths[i]/8) + (group_lengths[i]/8)*(group_widths[i] % 8); } } #pragma omp section { unsigned int i; for (i = 0; i < ngroups; i++) { group_offset[i] = offset; offset += (group_lengths[i] % 8)*(group_widths[i] % 8); } } } } if (j != npnts) fatal_error_u("bad complex packing: n points %u",j); if (d + (n+7)/8 - sec[7] != GB2_Sec7_size(sec)) fatal_error("complex unpacking size mismatch old test",""); if (d + clocation + (offset + 7)/8 - sec[7] != GB2_Sec7_size(sec)) fatal_error("complex unpacking size mismatch",""); #pragma omp parallel for private(i) schedule(static) for (i = 0; i < ngroups; i++) { group_clocation[i] += (group_offset[i] / 8); group_offset[i] = (group_offset[i] % 8); rd_bitstream(d + group_clocation[i], group_offset[i], udata+group_location[i], group_widths[i], group_lengths[i]); } // handle substitute, missing values and reference value if (n_sub_missing == 0) { #pragma omp parallel for private(i,k,j) for (i = 0; i < ngroups; i++) { j = group_location[i]; for (k = 0; k < group_lengths[i]; k++) { udata[j++] += group_refs[i]; } } } else if (n_sub_missing == 1) { #pragma omp parallel for private(i,m1,k,j) for (i = 0; i < ngroups; i++) { j = group_location[i]; if (group_widths[i] == 0) { m1 = (1 << nbits) - 1; if (m1 == group_refs[i]) { for (k = 0; k < group_lengths[i]; k++) udata[j++] = INT_MAX; } else { for (k = 0; k < group_lengths[i]; k++) udata[j++] += group_refs[i]; } } else { m1 = (1 << group_widths[i]) - 1; for (k = 0; k < group_lengths[i]; k++) { if (udata[j] == m1) udata[j] = INT_MAX; else udata[j] += group_refs[i]; j++; } } } } else if (n_sub_missing == 2) { #pragma omp parallel for private(i,j,k,m1,m2) for (i = 0; i < ngroups; i++) { j = group_location[i]; if (group_widths[i] == 0) { m1 = (1 << nbits) - 1; m2 = m1 - 1; if (m1 == group_refs[i] || m2 == group_refs[i]) { for (k = 0; k < group_lengths[i]; k++) udata[j++] = INT_MAX; } else { for (k = 0; k < group_lengths[i]; k++) udata[j++] += group_refs[i]; } } else { m1 = (1 << group_widths[i]) - 1; m2 = m1 - 1; for (k = 0; k < group_lengths[i]; k++) { if (udata[j] == m1 || udata[j] == m2) udata[j] = INT_MAX; else udata[j] += group_refs[i]; j++; } } } } // post processing if (pack == 3) { if (ctable_5_6 == 1) { last = extra_vals[0]; i = 0; while (i < npnts) { if (udata[i] == INT_MAX) i++; else { udata[i++] = extra_vals[0]; break; } } while (i < npnts) { if (udata[i] == INT_MAX) i++; else { udata[i] += last + min_val; last = udata[i++]; } } } else if (ctable_5_6 == 2) { penultimate = extra_vals[0]; last = extra_vals[1]; i = 0; while (i < npnts) { if (udata[i] == INT_MAX) i++; else { udata[i++] = extra_vals[0]; break; } } while (i < npnts) { if (udata[i] == INT_MAX) i++; else { udata[i++] = extra_vals[1]; break; } } for (; i < npnts; i++) { if (udata[i] != INT_MAX) { udata[i] = udata[i] + min_val + last + last - penultimate; penultimate = last; last = udata[i]; } } } else fatal_error_i("Unsupported: code table 5.6=%d", ctable_5_6); } // convert to float if (bitmap_flag == 255) { #pragma omp parallel for schedule(static) private(i) for (i = 0; i < ndata; i++) { data[i] = (udata[i] == INT_MAX) ? UNDEFINED : ref_val + udata[i] * factor; } } else if (bitmap_flag == 0 || bitmap_flag == 254) { n = 0; mask = 0; mask_pointer = sec[6] + 6; for (i = 0; i < ndata; i++) { if ((i & 7) == 0) mask = *mask_pointer++; if (mask & 128) { if (udata[n] == INT_MAX) data[i] = UNDEFINED; else data[i] = ref_val + udata[n] * factor; n++; } else data[i] = UNDEFINED; mask <<= 1; } } else fatal_error_i("unknown bitmap: %d", bitmap_flag); free(group_refs); free(group_widths); free(group_lengths); free(group_location); free(group_clocation); free(group_offset); free(udata); return 0; }
int space_view_init(unsigned char **sec) { double major, minor, orient_angle, angular_size; int x0, y0; int nres, nscan; unsigned int nnpnts; fprintf(stderr,"ALPHA: experimental space_view2ij\n"); if (sec == NULL || sec[3] == NULL) fatal_error("space_view_init: sec/sec[3] == NULL",""); if (code_table_3_1(sec) != 90) fatal_error("space_view_init: not space view grid",""); if (output_order != wesn) fatal_error("space_view_init: order must be we:sn",""); //printf("space_view_init >>>> table 3.1 %d\n", code_table_3_1(sec)); get_nxny(sec, &nnx, &nny, &nnpnts, &nres, &nscan); axes_earth(sec, &major, &minor); //fprintf(stderr,">> axes %lf minor %lf\n", major,minor); r_eq = major * 0.001; r_pol = minor * 0.001; r_pol_eq = (r_pol * r_pol) / (r_eq * r_eq); // factor_10 = (r_eq*r_eq - r_pol*r_pol) / (r_eq * r_eq); factor_10 = 1.0 - r_pol_eq; //fprintf(stderr,">> r_eq %lf r_pol %lf rad_factor %lf \n",r_eq, r_pol, factor_10); angular_size = 2.0 * asin(1e6/ uint4(sec[3]+68)); sat_height = uint4(sec[3]+68) * 1e-6 * r_eq; //fprintf(stderr,">> sat height %lf\n",sat_height); lap = int4(sec[3]+38); lop = int4(sec[3]+38); /* I am guessing that a scale factor has to be added */ lap *= 1e-6; lop *= 1e-6; // fprintf(stderr,">> lap %lf lop %lf degrees\n",lap, lop); if (lap != 0.0) return 0; // need to extend code /* convert to radians */ lap *= (180.0/M_PI); lop *= (180.0/M_PI); orient_angle = int4(sec[3]+64); /* I am guessing that a scale factor has to be added */ orient_angle *= 1e-6; // fprintf(stderr,">> orientation angle %lf\n", orient_angle); if (orient_angle != 0.0) return 0; // need to extend code xp = int4(sec[3]+55) * 0.001; yp = int4(sec[3]+59) * 0.001; //fprintf(stderr,">> xp %lf yp %lf pixels\n",xp, yp); x0 = int4(sec[3]+72); y0 = int4(sec[3]+76); //fprintf(stderr,">> origin x0 %d yo %d pixels\n",x0, y0); /* find center point in we:sn coordinate */ if (GDS_Scan_x(nscan)) { xp = xp - x0; } else { xp = (nnx-1) - (xp - x0); } if (GDS_Scan_y(nscan)) { yp = yp - y0; } else { yp = (nny-1) - (yp - y0); } //fprintf(stderr,">> new center point point x/y=%lf %lf nnx=%d nny=%d\n", xp,yp,nnx, nny); dx = int4(sec[3]+47); dy = int4(sec[3]+51); //fprintf(stderr,">> dia: dx %lf dy %lf pixels\n",dx, dy); inv_rx = dx / angular_size; inv_ry = (r_eq/r_pol) * dy / angular_size; // fprintf(stderr,">> factor %.18lf %.18lf, q: %.19lf %.18lf\n", 256*256.0/(-781648343.0), 256*256.0/(-781648343.0), 1/inv_rx, 1/inv_ry); return 0; }
unsigned char *rd_grib2_msg_seq_file(unsigned char **sec, struct seq_file *input, long int *pos, unsigned long int *len, int *num_submsgs) { int i, j, c, c1, c2, c3, c4; long int len_grib; unsigned char *p, *end_of_msg; /* setup grib buffer */ if (input->buffer == NULL) { if ((input->buffer = (unsigned char *) malloc(BUFF_ALLOC0)) == NULL) { fatal_error("not enough memory: rd_grib2_msg",""); } input->buffer_size = BUFF_ALLOC0; } /* search for GRIB...2 */ while (1) { c = getchar_seq_file(input); if (c == EOF) { *len = 0; return NULL; } if (c != G) continue; if ( (c = getchar_seq_file(input)) != R) { unget_seq_file(input, c); continue; } if ( (c = getchar_seq_file(input)) != I) { unget_seq_file(input, c); continue; } if ( (c = getchar_seq_file(input)) != B) { unget_seq_file(input, c); continue; } c1 = getchar_seq_file(input); c2 = getchar_seq_file(input); c3 = getchar_seq_file(input); c4 = getchar_seq_file(input); if (c4 == 1) { fprintf(stderr,"grib1 message ignored (use wgrib)\n"); continue; } if (c4 != 2) { unget_seq_file(input, c4); unget_seq_file(input, c3); unget_seq_file(input, c2); unget_seq_file(input, c1); continue; } input->buffer[0] = G; input->buffer[1] = R; input->buffer[2] = I; input->buffer[3] = B; input->buffer[4] = c1; input->buffer[5] = c2; input->buffer[6] = c3; input->buffer[7] = c4; /* fill in the size 8-15, unget buffer is empty */ for (i = 0; i < 8; i++) { input->buffer[8+i] = c = getchar_seq_file(input); if (c == EOF) { *len = 0; return NULL; } } break; } *len = len_grib = uint8(input->buffer+8); *pos = input->pos - 16; if (input->buffer_size < len_grib) { input->buffer_size = (len_grib*5)/4; input->buffer = (unsigned char *) realloc((void *) input->buffer, input->buffer_size); } j=fread(input->buffer+16, sizeof (unsigned char), len_grib-16, input->input); input->pos += j; if (j != len_grib-16) fatal_error("rd_grib2_msg_seq_file, read outside of file, bad grib file",""); sec[0] = input->buffer; sec[8] = sec[0] + len_grib - 4; if (sec[8][0] != 55 || sec[8][1] != 55 || sec[8][2] != 55 || sec[8][3] != 55) { fatal_error("rd_grib2_msg_seq_file, missing end section ('7777')",""); } /* scan message for number of submessages and perhaps for errors */ p = sec[0] + GB2_Sec0_size; end_of_msg = sec[0] + len_grib; i = 0; while (p < sec[8]) { if (p[4] == 7) i++; if (uint4(p) < 5) fatal_error_i("rd_grib2_msg_file: illegal grib: section length, section %i", p[4]); p += uint4(p); if (p > end_of_msg) fatal_error("bad grib format",""); } if (p != sec[8]) { fatal_error("rd_grib2_msg: illegal format, end section expected",""); } *num_submsgs = i; *len = len_grib; return sec[0]; }
int grib_ieee(unsigned char **sec, float *data, unsigned int ndata, FILE *out, FILE *head, FILE *tail, FILE *c) { int i; unsigned int n_defined, j; // int flag; unsigned long int size; unsigned char *p, *sec0, *sec1, *sec2, *sec3, *sec4, *sec5, *sec6, *sec7; unsigned char s[8]; float *new_data; /* required passed sections */ sec0 = sec[0]; sec1 = sec[1]; sec2 = sec[2]; sec3 = sec[3]; sec4 = sec[4]; /* change scan mode */ // flag = flag_table_3_4(sec); // set_order(sec, output_order); /* make a new section 6 */ 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 /* 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 */ new_data = (float *) malloc(n_defined * sizeof(float)); if (new_data == NULL) fatal_error("grib_out ieee memory allocation data",""); undo_output_order(data, new_data, n_defined); 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; for (j = 0; j < n_defined; j++) { flt2ieee_nan(new_data[j], p); p += 4; } free(new_data); size = (unsigned long int) GB2_Sec0_size + GB2_Sec8_size + (sec1 ? uint4(sec1) : 0) + (sec2 ? uint4(sec2) : 0) + (sec3 ? uint4(sec3) : 0) + (sec4 ? uint4(sec4) : 0) + (sec5 ? uint4(sec5) : 0) + (sec6 ? uint4(sec6) : 0) + (sec7 ? uint4(sec7) : 0); fprintf(c,"unsigned char head[] = {"); /* section 0 */ fwrite((void *) sec0, sizeof(char), 8, out); fwrite((void *) sec0, sizeof(char), 8, head); output_c(c, sec0, 8); uint8_char(size, s); fwrite((void *) s, sizeof(char), 8, out); fwrite((void *) s, sizeof(char), 8, head); output_c(c, s, 8); fwrite((void *)sec1, sizeof(char), uint4(sec1), out); fwrite((void *)sec1, sizeof(char), uint4(sec1), head); output_c(c, sec1, uint4(sec1)); if (sec2) fwrite((void *)sec2, sizeof(char), uint4(sec2), out); if (sec2) fwrite((void *)sec2, sizeof(char), uint4(sec2), head); if (sec2) output_c(c, sec2, uint4(sec2)); if (sec3) fwrite((void *)sec3, sizeof(char), uint4(sec3), out); if (sec3) fwrite((void *)sec3, sizeof(char), uint4(sec3), head); if (sec3) output_c(c, sec3, uint4(sec3)); if (sec4) fwrite((void *)sec4, sizeof(char), uint4(sec4), out); if (sec4) fwrite((void *)sec4, sizeof(char), uint4(sec4), head); if (sec4) output_c(c, sec4, uint4(sec4)); if (sec5) fwrite((void *)sec5, sizeof(char), uint4(sec5), out); if (sec5) fwrite((void *)sec5, sizeof(char), uint4(sec5), head); if (sec5) output_c(c, sec5, uint4(sec5)); if (sec6) fwrite((void *)sec6, sizeof(char), uint4(sec6), out); if (sec6) fwrite((void *)sec6, sizeof(char), uint4(sec6), head); if (sec6) output_c(c, sec6, uint4(sec6)); if (sec7) fwrite((void *)sec7, sizeof(char), uint4(sec7), out); if (sec7) fwrite((void *)sec7, sizeof(char), 5, head); if (sec7) output_c(c, sec7, 5); fprintf(c,"};\n\n"); s[0] = s[1] = s[2] = s[3] = 55; /* s = "7777" */ fprintf(c,"unsigned char tail[4] = {55, 55, 55, 55};\n\n"); fwrite((void *) s, sizeof(char), 4, out); fwrite((void *) s, sizeof(char), 4, tail); fprintf(c,"#define NDATA %u\n", ndata); i = 0; fprintf(c,"#define SEC0 0\n"); fprintf(c,"#define DISCIPLINE %d\n",6); fprintf(c,"#define EDITION %d\n",7); i = 16; fprintf(c,"#define SEC1 %d\n",i); fprintf(c,"#define CENTER %d\n",i+5); fprintf(c,"#define SUBCENTER %d\n",i+7); fprintf(c,"#define MASTERTABLE %d\n",i+9); fprintf(c,"#define LOCALTABLE %d\n",i+9); fprintf(c,"#define YEAR %d\n",i+12); fprintf(c,"#define MONTH %d\n",i+14); fprintf(c,"#define DAY %d\n",i+15); fprintf(c,"#define HOUR %d\n",i+16); fprintf(c,"#define MINUTE %d\n",i+17); fprintf(c,"#define SECOND %d\n",i+18); i = i + uint4(sec1); if (sec2) { fprintf(c,"#define SEC2 %d\n",i); i = i + uint4(sec2); } if (sec3) { fprintf(c,"#define SEC3 %d\n",i); i = i + uint4(sec3); } if (sec4) { fprintf(c,"#define SEC4 %d\n",i); fprintf(c,"#define PRODUCTDEFTEMPLATENUM %d\n",i+7); fprintf(c,"#define PRODUCTDEFTEMPLATE %d\n",i+9); fprintf(c,"#define PRODUCTCATEGORY %d\n",i+9); fprintf(c,"#define PRODUCTNUMBER %d\n",i+9); i = i + uint4(sec4); } if (sec5) { fprintf(c,"#define SEC5 %d\n",i); i = i + uint4(sec5); } if (sec6) { fprintf(c,"#define SEC6 %d\n",i); i = i + uint4(sec6); } if (sec7) { fprintf(c,"#define SEC7 %d\n",i); i = i + uint4(sec4); } free(sec5); free(sec6); free(sec7); // /* set scan mode to original order */ // set_flag_table_3_4(sec, flag); return 0; }
/* * return number of grid points with values */ unsigned int nval(unsigned char **sec) { return uint4(sec[5]+5); }
bus_model* model_loader::load(const string& elements_filename, const string& vertices_filename, const string& tex_coords_filename, const string& bone_weights_filename, const string& bone_indices_filename, const string& matrices_filename) { stringstream elements_buffer, vertices_buffer, tc_buffer, bweights_buffer, bindices_buffer, bm_buffer; if(!f->file_to_buffer(elements_filename, elements_buffer)) { a2e_error("couldn't open elements file: %s!", elements_filename); return nullptr; } if(!f->file_to_buffer(vertices_filename, vertices_buffer)) { a2e_error("couldn't open vertices file: %s!", vertices_filename); return nullptr; } if(!f->file_to_buffer(tex_coords_filename, tc_buffer)) { a2e_error("couldn't open tex coords file: %s!", tex_coords_filename); return nullptr; } if(!f->file_to_buffer(bone_weights_filename, bweights_buffer)) { a2e_error("couldn't open bone weights file: %s!", bone_weights_filename); return nullptr; } if(!f->file_to_buffer(bone_indices_filename, bindices_buffer)) { a2e_error("couldn't open bone indices file: %s!", bone_indices_filename); return nullptr; } if(!f->file_to_buffer(matrices_filename, bm_buffer)) { a2e_error("couldn't open matrices file: %s!", matrices_filename); return nullptr; } stringstream normals_buffer; if(!f->file_to_buffer(e->data_path("NI-Nrms.txt"), normals_buffer)) { a2e_error("couldn't open normals file: %s!", "NI-Nrms.txt"); return nullptr; } // bus_model* bmodel = new bus_model(); static const float model_scale = 1.0f/10.0f; // read elements while(!elements_buffer.eof() && elements_buffer.good()) { unsigned int i1, i2, i3; elements_buffer >> i1; elements_buffer >> i2; elements_buffer >> i3; bmodel->indices.push_back(index3(i1, i2, i3)); } // read vertices while(!vertices_buffer.eof() && vertices_buffer.good()) { float x, y, z; vertices_buffer >> x; vertices_buffer >> y; vertices_buffer >> z; bmodel->vertices.push_back(float3(x, y, z)); } // read tex coords while(!tc_buffer.eof() && tc_buffer.good()) { float u, v; tc_buffer >> u; tc_buffer >> v; bmodel->tex_coords.push_back(coord(u, v)); } // read normals while(!normals_buffer.eof() && normals_buffer.good()) { float x, y, z; normals_buffer >> x; normals_buffer >> y; normals_buffer >> z; bmodel->normals.push_back(float3(x, y, z)); } // read bone weights while(!bweights_buffer.eof() && bweights_buffer.good()) { float wx, wy, wz, ww; bweights_buffer >> wx; bweights_buffer >> wy; bweights_buffer >> wz; bweights_buffer >> ww; bmodel->bone_weights.push_back(float4(wx, wy, wz, ww)); } // read bone indices while(!bindices_buffer.eof() && bindices_buffer.good()) { unsigned int ix, iy, iz, iw; bindices_buffer >> ix; bindices_buffer >> iy; bindices_buffer >> iz; bindices_buffer >> iw; bmodel->bone_indices.push_back(uint4(ix, iy, iz, iw)); } // read inverse matrices size_t m_index = 0; while(!bm_buffer.eof() && bm_buffer.good()) { if(m_index >= PLAYERS_BONE_COUNT) { a2e_error("too many matrices!"); return nullptr; } matrix4f m; for(size_t i = 0; i < 16; i++) { bm_buffer >> m.data[i]; } bmodel->matrices[m_index] = m; bmodel->matrices[m_index][3] *= model_scale; bmodel->matrices[m_index][7] *= model_scale; bmodel->matrices[m_index][11] *= model_scale; bmodel->joint_positions[m_index].set(-m.data[3], -m.data[7], -m.data[11], 1.0f); m_index++; } if(m_index != PLAYERS_BONE_COUNT) { a2e_error("too few matrices!"); return nullptr; } for(auto& vertex : bmodel->vertices) { vertex *= model_scale; } a2e_debug("model (%s, %s, %s) read: #indices: %u, #vertices: %u, #normals, %u, #tex coords: %u", elements_filename, vertices_filename, tex_coords_filename, bmodel->indices.size(), bmodel->vertices.size(), bmodel->normals.size(), bmodel->tex_coords.size()); return bmodel; }
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; }
/* * HEADER:400:JMA:inv:0:inventory for JMA locally defined PDT */ int f_JMA(ARG0) { int pdt, i, nr; int center; if (mode < 0) return 0; center = GB2_Center(sec); if (center != JMA1 && center != JMA2) return 0; pdt = GB2_ProdDefTemplateNo(sec); if (pdt < 50000) return 0; if (pdt == 51022) { sprintf(inv_out,"site=%c%c%c%c", sec[4][24], sec[4][25], sec[4][26], sec[4][27]); inv_out += strlen(inv_out); if (mode >= 1) { sprintf(inv_out,":site_id#=%u", uint2(sec[4]+28)); inv_out += strlen(inv_out); sprintf(inv_out,":site_lon=%lf:site_lat=%lf:site_elev=%.1lfm",int4(sec[4]+18)*1e-6, int4(sec[4]+14)*1e-6,int2(sec[4]+22)*0.1) ; inv_out += strlen(inv_out); sprintf(inv_out,":mag_dec=%.2fdeg", int2(sec[4]+30)*0.001); inv_out += strlen(inv_out); sprintf(inv_out,":freq=%ukHz", uint4(sec[4]+32)); inv_out += strlen(inv_out); sprintf(inv_out,":pol=%d:opn_mode=%d:reflec=%d", (int) sec[4][36], (int) sec[4][37], (int) sec[4][38]); inv_out += strlen(inv_out); sprintf(inv_out,":qc=%d:cluster_filter=%d", (int) sec[4][39], (int) sec[4][40]); inv_out += strlen(inv_out); sprintf(inv_out,":angle_elev_constant=%.2f", int2(sec[4]+41)*0.01); inv_out += strlen(inv_out); } nr = GB2_Sec4_size(sec); nr = (nr % 4) ? -1 : (nr - 60) / 4; if (mode >= 1) { sprintf(inv_out,":Ntheta=%d", nr); inv_out += strlen(inv_out); i = uint_n(sec[4]+55, 3); if (i != 0xffffff) { sprintf(inv_out,":bin_size=%dm", i); inv_out += strlen(inv_out); } if ((sec[4][58] != 255) || (sec[4][59] != 255)) { sprintf(inv_out,":d_theta=%.1lfdeg", int2(sec[4]+58)*0.1); inv_out += strlen(inv_out); } } if (mode < 2) { sprintf(inv_out,":ele(1)=%.2fdeg", int2(sec[4]+60)*0.01); inv_out += strlen(inv_out); sprintf(inv_out,":ele(%d)=%.2fdeg", nr/2,int2(sec[4]+60+((nr-1)/2)*4)*0.01); inv_out += strlen(inv_out); } if (mode >= 2) { sprintf(inv_out,"\n:elevation angle(1..Ntheta)="); inv_out += strlen(inv_out); for (i = 0; i < nr; i++) { sprintf(inv_out,"%.2f ", int2(sec[4]+60+i*4)*0.01); inv_out += strlen(inv_out); } sprintf(inv_out,"\n:pulse repetion freq(1..Ntheta)="); inv_out += strlen(inv_out); for (i = 0; i < nr; i++) { sprintf(inv_out,"%.2f ", int2(sec[4]+62+i*4)*0.01); inv_out += strlen(inv_out); } } } return 0; }
unsigned char *rd_grib2_msg(FILE *input, long int *pos, unsigned long int *len, int *num_submsgs){ unsigned long int len_grib; long int position, i; unsigned long int tmp; long int n_bytes; int j; unsigned char *p, *end_of_msg; /* setup grib buffer */ if (buffer == NULL) { if ((buffer = (unsigned char *) malloc(BUFF_ALLOC0)) == NULL) { fatal_error("not enough memory: rd_grib2_msg",""); } buffer_size = BUFF_ALLOC0; } /* find Msg and length of message */ #ifdef DEBUG fprintf(stderr,"rd_grib2_msg: start looking at pos %ld\n", *pos); #endif position = *pos; Msg = seek_grib2(input, &position, &len_grib, buffer, MSEEK, &n_bytes); #ifdef DEBUG fprintf(stderr,"rd_grib2_msg: found at %ld .. n_bytes %ld\n", position, n_bytes); #endif *pos = position; if (Msg == NULL) { *len = 0; return NULL; } // for (i = 0; i <4; i++) fprintf(stderr," (%c) ", Msg[i]); /* read all whole grib record .. to save I/O time, add to end of buffer */ if (len_grib + Msg - buffer > buffer_size) { tmp = Msg - buffer; buffer_size = len_grib + Msg - buffer + 5000; buffer = (unsigned char *) realloc((void *) buffer, buffer_size); if (buffer == NULL) fatal_error("rd_grib2_msg: ran out of memory",""); Msg = buffer + tmp; } if (fseek(input, *pos+n_bytes-(Msg-buffer), SEEK_SET) == -1) { fatal_error("rd_grib2_msg seek, outside the file, bad grib file",""); } i = len_grib + Msg - buffer - n_bytes; /* no. of bytes need to read */ if (i > 0 && ((j=fread(buffer+n_bytes, sizeof (unsigned char), i, input)) != i)) { fatal_error("rd_grib2_msg, read outside of file, bad grib file",""); } // for (i = 0; i <4; i++) fprintf(stderr," (%c) ", Msg[i+len_grib-4]); Sec[8] = Msg + len_grib - 4; if (Sec[8][0] != 55 || Sec[8][1] != 55 || Sec[8][2] != 55 || Sec[8][3] != 55) { fatal_error("rd_grib2_msg, missing end section ('7777')",""); } Sec[0] = Msg; /* scan message for number of submessages and perhaps for errors */ p = Msg + GB2_Sec0_size; end_of_msg = Msg + len_grib; i = 0; while (p < Sec[8]) { #ifdef DEBUG fprintf(stderr,"rd_grib2_msg: section %d found\n", p[4]); #endif if (p[4] == 7) i++; if (uint4(p) < 5) fatal_error_i("rd_grib2_msg: illegal grib: section length, section %i", p[4]); p += uint4(p); if (p > end_of_msg) fatal_error("bad grib format",""); } if (p != Sec[8]) { fatal_error("rd_grib2_msg: illegal format, end section expected",""); } *num_submsgs = i; *len = len_grib; #ifdef DEBUG fprintf(stderr,"rd_grib2_msg: found at %ld .. len_grib %ld\n", position, len_grib); #endif return Msg; }