unsigned char *code_table_3_20_location(unsigned char **sec) { int grid_def; grid_def = code_table_3_1(sec); if (grid_def == 1000) return sec[3]+59; if (grid_def == 1100) return sec[3]+59; return NULL; }
int code_table_3_21(unsigned char **sec) { int grid_def; grid_def = code_table_3_1(sec); if (grid_def == 1000) return sec[3][63]; if (grid_def == 1200) return sec[3][39]; return -1; }
int grid_id(GRID_ID_ARGS) { int gdt, res, scan, nx, ny, n; unsigned int npnts; if (axes_earth(sec, r_major, r_minor)) fatal_error("grid_id: axes undefined",""); gdt = code_table_3_1(sec); switch (gdt) { case 0: *proj_id = p_latlon; break; case 1: *proj_id = p_rotated_latlon; break; case 10: *proj_id = p_mercator; break; case 20: *proj_id = p_polar_stereographic; break; case 30: *proj_id = p_lambert_conic; break; default: *proj_id = p_unknown; break; } /* get nx, ny n */ get_nxny(sec, &nx, &ny, &npnts, &res, &scan); n = (int) npnts; grid_defn->nx = nx; grid_defn->ny = ny; grid_defn->n = npnts; /* get xy_list */ grid_defn->valid_xy_list = 0; return 0; }
int closest(unsigned char **sec, double plat, double plon) { int i, nnpts; int grid_type, j; double t, xx, yy, zz, small; if (use_gctpc && output_order == wesn && nx > 0 && ny > 0) { if (gctpc_ll2i(1, &plon, &plat, &j) == 0) return j; } grid_type = code_table_3_1(sec); // if grid with (lat,lon) -> (i,j) /l.. insert code here if (grid_type == 0 && nx > 0 && ny > 0 && output_order == wesn) return latlon_closest(sec, plat, plon); if (grid_type == 90 && nx > 0 && ny > 0 && output_order == wesn) return space_view_closest(sec, plat, plon); nnpts = (int) GB2_Sec3_npts(sec); if (x == NULL || nnpts <= 0) return -1; zz = sin(plat * (M_PI / 180.0)); t = sqrt(1.0 - zz*zz); xx = t * cos(plon * (M_PI / 180.0)); yy = t * sin(plon * (M_PI / 180.0)); small = 0.0; j = -1; #pragma omp parallel private(i,t) { double t_thread, small_thread; int j_thread; small_thread = 0.0; j_thread = -1; #pragma omp for schedule(static) nowait for (i = 0; i < nnpts; i++) { if (x[i] >= 999.0) continue; t_thread = (x[i]-xx)*(x[i]-xx)+(y[i]-yy)*(y[i]-yy)+(z[i]-zz)*(z[i]-zz); if (j_thread == -1 || t_thread < small_thread) { small_thread = t_thread; j_thread = i; } } #pragma omp critical { if (j == -1 || small_thread < small) { small = small_thread; j = j_thread; } } } return j; }
int cyclic(unsigned char **sec) { int grid_template, nx, ny, res, scan, flag_3_3, no_dx, basic_ang, sub_ang; unsigned int npnts; unsigned char *gds; double dlon, units, lon1, lon2; get_nxny(sec, &nx, &ny, &npnts, &res, &scan); if (GDS_Scan_staggered(scan)) return 0; if (nx <= 1 || ny <= 0) return 0; grid_template = code_table_3_1(sec); gds = sec[3]; flag_3_3 = flag_table_3_3(sec); no_dx = 0; if (flag_3_3 != -1) { if ((flag_3_3 & 0x20) == 0) no_dx = 1; } if (no_dx) return 0; if (grid_template == 0) { basic_ang = GDS_LatLon_basic_ang(gds); sub_ang = GDS_LatLon_sub_ang(gds); units = basic_ang == 0 ? 0.000001 : (double) basic_ang / (double) sub_ang; /* dlon has to be defined */ dlon = units * GDS_LatLon_dlon(gds); return (fabs(nx*dlon-360.0) < ERROR); } if (grid_template == 10) { if (output_order != wesn) return 0; // only works with we:sn order lon1 = GDS_Mercator_lon1(gds); lon2 = GDS_Mercator_lon2(gds); if (lon2 < lon1) lon2 += 360.0; dlon = (lon2-lon1)*nx/(nx-1.0); return (fabs(dlon-360.0) < ERROR); } if (grid_template == 40) { basic_ang = GDS_Gaussian_basic_ang(gds); sub_ang = GDS_Gaussian_sub_ang(gds); units = basic_ang == 0 ? 0.000001 : (double) basic_ang / (double) sub_ang; /* dlon has to be defined */ dlon = units * GDS_Gaussian_dlon(gds); return (fabs(nx*dlon-360.0) < ERROR); } return 0; }
int f_code_table_3_1(ARG0) { const char *string; int val; if (mode >= 0) { val = code_table_3_1(sec); string = NULL; switch(val) { #include "CodeTable_3.1.dat" } if (string == NULL) sprintf(inv_out,"code table 3.1=%d", val); else sprintf(inv_out,"code table 3.1=%d %s", val, string); } return 0; }
int closest_init(unsigned char **sec) { int i, nnpts; double s, c; int grid_type; if (use_gctpc && output_order == wesn && nx > 0 && ny > 0) { if (gctpc_ll2xy_init(sec, lon, lat) == 0) return 0; } grid_type = code_table_3_1(sec); if (!GDS_Scan_staggered(scan) && nx > 0 && ny > 0) { /* if grids with (lat,lon) -> (i,j) insert code here */ if (grid_type == 0 && output_order == wesn) return latlon_init(sec, nx, ny); if (grid_type == 90 && output_order == wesn) return space_view_init(sec); } nnpts = (int) GB2_Sec3_npts(sec); if (x) { free(x); free(y); free(z); x = y = z = NULL; } if (lat && lon) { x = (double *) malloc(nnpts * sizeof(double)); y = (double *) malloc(nnpts * sizeof(double)); z = (double *) malloc(nnpts * sizeof(double)); if (x == NULL || y == NULL || z == NULL) fatal_error("memory allocation closest_init",""); #pragma omp parallel for private(i,s,c) schedule(static) for (i = 0; i < nnpts; i++) { if (lat[i] >= 999.0 || lon[i] >= 999.0) { /* x[i] = sin() .. cannot be bigger than 1 */ x[i] = y[i] = z[i] = 999.0; } else { s = sin(lat[i] * (M_PI / 180.0)); c = sqrt(1.0 - s * s); z[i] = s; x[i] = c * cos(lon[i] * (M_PI / 180.0)); y[i] = c * sin(lon[i] * (M_PI / 180.0)); } } } return 0; }
int cyclic(unsigned char **sec) { int grid_template, nx, ny, res, scan, flag_3_3, no_dx, basic_ang, sub_ang; unsigned int npnts; unsigned char *gds; double dlon, units; get_nxny(sec, &nx, &ny, &npnts, &res, &scan); if ((unsigned) (nx * ny) != npnts) return 0; if (nx <= 0 || ny <= 0) return 0; grid_template = code_table_3_1(sec); gds = sec[3]; flag_3_3 = flag_table_3_3(sec); no_dx = 0; if (flag_3_3 != -1) { if ((flag_3_3 & 0x20) == 0) no_dx = 1; } if (grid_template == 0) { basic_ang = GDS_LatLon_basic_ang(gds); sub_ang = GDS_LatLon_sub_ang(gds); units = basic_ang == 0 ? 0.000001 : (double) basic_ang / (double) sub_ang; dlon = units * GDS_LatLon_dlon(gds); if (no_dx) dlon = 0.0; dlon = nx * dlon; return (fabs(dlon-360.0) < ERROR); } if (grid_template == 10) { dlon = GDS_Mercator_dx(gds); if (no_dx) dlon = 0.0; dlon = nx * dlon; return (fabs(dlon-360.0) < ERROR); } // need to add gaussian grid - a bit more complicated return 0; }
unsigned char *flag_table_3_3_location(unsigned char **sec) { int grid_template; unsigned char *gds; grid_template = code_table_3_1(sec); gds = sec[3]; switch (grid_template) { case 0: case 1: case 2: case 3: case 40: case 41: case 42: case 43: case 140: case 204: return gds+54; break; case 4: case 5: case 10: case 12: case 20: case 30: case 31: case 90: case 110: return gds+46; break; case 32768: if (GB2_Center(sec) == NCEP) return gds+54; return NULL; case 32769: if (GB2_Center(sec) == NCEP) return gds+54; return NULL; break; default: return NULL; break; } return NULL; }
unsigned char *code_table_3_2_location(unsigned char **sec) { int grid_def; grid_def = code_table_3_1(sec); if (grid_def < 50) return sec[3]+14; switch (grid_def) { case 90: case 110: case 130: case 140: case 204: case 1000: case 1100: return sec[3]+14; break; default: break; } if (GB2_Center(sec) == NCEP) { if (grid_def == 32768 || (grid_def == 32769)) return sec[3]+14; } return NULL; }
int f_AAIGlong(ARG0) { size_t i, j; double cellsize, dlon, dlat; char file[STRING_SIZE], name[STRING_SIZE]; FILE *out; if (mode == -1) { decode = latlon = 1; return 0; } if (mode < 0) return 0; if (lat == NULL || lon == NULL) { fprintf(stderr,"AAIGlong does nothing, no lat-lon information\n"); return 0; } if (output_order != wesn) { fprintf(stderr,"AAIGlong does nothing, not in we:sn order\n"); return 0; } if (code_table_3_1(sec) != 0) { // check grid template fprintf(stderr,"AAIGlong does nothing, not lat-lon grid\n"); return 0; } if (nx_ == 0 || ny_ == 0) { fprintf(stderr,"AAIGlong does nothing, found thinned lat-lon grid\n"); return 0; } cellsize = dlon = lon[1] - lon[0]; dlat = lat[nx_] - lat[0]; if ( fabs(dlat - dlon) > 0.0001*dlon) cellsize = 0.0; f_S(call_ARG0(name,NULL)); name[STRING_SIZE-1] = 0; /* get rid of trailing semicolon */ i = strlen(name); if (name[i-1] == ':') name[i-1] = 0; /* get rid of bad characters for a filename */ j = i = 0; while (name[i]) { if (name[i] == '/') { file[j++] = ' '; file[j++] = 'D'; file[j++] = 'I'; file[j++] = 'V'; file[j++] = ' '; } else if (name[i] == '\\') { file[j++] = ' '; file[j++] = 'B'; file[j++] = 'S'; file[j++] = ' '; } else if (name[i] == ':') { file[j++] = '_'; } else if (name[i] == '"' || name[i] == '\'') { file[j++] = ' '; file[j++] = 'Q'; file[j++] = ' '; } else file[j++] = name[i]; i++; } file[j++] = '.'; file[j++] = 'a'; file[j++] = 's'; file[j++] = 'c'; file[j] = 0; if ((out = ffopen(file,"w")) == NULL) { fprintf(stderr,"AAIG could not open raster file %s\n",file); return 0; } fprintf(stderr, "raster file: %s\n", file); fprintf(out,"ncols %u\n", nx_); fprintf(out,"nrows %u\n", ny_); fprintf(out,"xllcenter %lf\n", lon[0] > 180.0 ? lon[0]-360.0 : lon[0]); fprintf(out,"yllcenter %lf\n", lat[0]); if (cellsize > 0.0) { fprintf(out,"cellsize %lf\n", cellsize); } else { fprintf(out,"dx %lf\n", dlon); fprintf(out,"dy %lf\n", dlat); } fprintf(out,"NODATA_VALUE 9.999e20\n"); for (j = 0; j < ny_; j++) { for (i = 0; i < nx_; i++) { fprintf(out,"%f\n", data[i+(ny_ - 1 - j)*nx_]); } } ffclose(out); return 0; }
int proj4_initialize(unsigned char **sec, struct proj4_struct *projection) { unsigned int gdt; unsigned char *gds; int has_np; double r_maj, r_min, c_lon, c_lat, x_0, y_0, lat1, lon1; double latsp1, latsp2; char proj4_def[1000]; gdt = code_table_3_1(sec); gds = sec[3]; axes_earth(sec, &r_maj, &r_min); projection->radius_minor = r_min; projection->radius_minor = r_min; if (gdt == 0) { projection->proj_is_nop = 1; projection->x_0 = projection->y_0 = projection->lat_0 = projection->lon_0 = 0.0; return 0; } projection->proj_is_nop = 0; sprintf(proj4_def,"+proj=latlong +a=%lf +b=%lf",r_maj, r_min); if ( (projection->pj_latlon = pj_init_plus(proj4_def)) == NULL) fatal_error("proj4_initialize: pj_init_plus %s failed", proj4_def); if (gdt == 10 && (GDS_Mercator_ori_angle(gds) == 0.0) ) { // mercator no rotation /* central point */ c_lon = 0.0; c_lat = GDS_Mercator_latD(gds); sprintf(proj4_def,"+proj=merc +lat_ts=%lf +lat_0=0 +lon_0=0 +x_0=0 +y_0=0 +a=%lf +b=%lf", c_lat, r_maj, r_min); if ((projection->pj_grid = pj_init_plus(proj4_def)) == NULL) fatal_error("proj4_initialize: pj_init_plus %s failed", proj4_def); /* longitude, latitude of first grid point */ projection->lat_0 = lat1 = GDS_Mercator_lat1(gds); projection->lon_0 = lon1 = GDS_Mercator_lon1(gds); x_0 = lon1 * DEG_TO_RAD; y_0 = lat1 * DEG_TO_RAD; if ( pj_transform(projection->pj_latlon, projection->pj_grid, 1, 1, &x_0, &y_0, NULL) != 0 ) fatal_error("proj4_initialize: Proj4 transform to lat-lon",""); projection->x_0 = x_0; projection->y_0 = y_0; } else if (gdt == 20) { // polar stereographic /* central point */ c_lon = GDS_Polar_lov(gds); c_lat = GDS_Polar_lad(gds); /* strange but np/sp flag is used by proj4 but not gctpc */ has_np = ((flag_table_3_5(sec) & 128) == 0); sprintf(proj4_def,"+proj=stere +lat_ts=%lf +lat_0=%s +lon_0=%lf +k_0=1 +x_0=0 +y_0=0 +a=%lf +b=%lf", c_lat, has_np ? "90" : "-90", c_lon, r_maj,r_min); if ((projection->pj_grid = pj_init_plus(proj4_def)) == NULL) fatal_error("Proj4: pj_init_plus %s failed", proj4_def); /* longitude, latitude of first grid point */ projection->lon_0 = lon1 = GDS_Polar_lon1(gds); projection->lat_0 = lat1 = GDS_Polar_lat1(gds); x_0 = lon1 * DEG_TO_RAD; y_0 = lat1 * DEG_TO_RAD; if ( pj_transform(projection->pj_latlon, projection->pj_grid, 1, 1, &x_0, &y_0, NULL) != 0 ) fatal_error("proj4_init: Proj4 transform to lat-lon",""); projection->x_0 = x_0; projection->y_0 = y_0; } else if (gdt == 30) { // lambert conformal conic /* latitudes of tangent/intersection */ latsp1 = GDS_Lambert_Latin1(gds); latsp2 = GDS_Lambert_Latin2(gds); /* central point */ c_lon = GDS_Lambert_Lov(gds); c_lat = GDS_Lambert_LatD(gds); sprintf(proj4_def,"+proj=lcc +lon_0=%lf +lat_0=%lf +lat_1=%lf +lat_2=%lf +a=%lf +b=%lf",c_lon, c_lat,latsp1,latsp2,r_maj,r_min); if ((projection->pj_grid = pj_init_plus(proj4_def)) == NULL) fatal_error("Proj4: pj_init_plus %s failed", proj4_def); /* longitude, latitude of first grid point */ lon1 = GDS_Lambert_Lo1(gds); lat1 = GDS_Lambert_La1(gds); x_0 = lon1 * DEG_TO_RAD; y_0 = lat1 * DEG_TO_RAD; /* longitude, latitude of first grid point */ projection->lon_0 = lon1 = GDS_Lambert_Lo1(gds); projection->lat_0 = lat1 = GDS_Lambert_La1(gds); x_0 = lon1 * DEG_TO_RAD; y_0 = lat1 * DEG_TO_RAD; if ( pj_transform(projection->pj_latlon, projection->pj_grid, 1, 1, &x_0, &y_0, NULL) != 0 ) fatal_error("proj4_init: Proj4 transform to lat-lon",""); projection->x_0 = x_0; projection->y_0 = y_0; } else { return 1; } return 0; }
int gctpc_get_latlon(unsigned char **sec, double **lon, double **lat) { int gdt; unsigned char *gds; double r_maj; /* major axis */ double r_min; /* minor axis */ double lat1; /* first standard parallel */ double lat2; /* second standard parallel */ double c_lon; /* center longitude */ double c_lat; /* center latitude */ double false_east; /* x offset in meters */ double false_north; double dx, dy; double x0, y0; long int (*inv_fn)(); double *llat, *llon, rlon, rlat; int i, nnx, nny, nres, nscan; unsigned int nnpnts; long long_i; gdt = code_table_3_1(sec); gds = sec[3]; /* only process certain grids */ if (gdt != 10 && gdt != 20 && gdt != 30 && gdt != 31) return 1; get_nxny(sec, &nnx, &nny, &nnpnts, &nres, &nscan); /* potentially staggered */ if (nnx < 1 || nny < 1) return 1; llat = *lat; llon = *lon; if (llat != NULL) { free(llat); free(llon); *lat = *lon = llat = llon = NULL; } inv_fn = NULL; dx = dy = 0.0; if (gdt == 10) { // mercator /* get earth axis */ axes_earth(sec, &r_maj, &r_min); dy = GDS_Mercator_dy(gds); dx = GDS_Mercator_dx(gds); /* central point */ c_lon = GDS_Mercator_ori_angle(gds) * (M_PI/180.0); c_lat = GDS_Mercator_latD(gds) * (M_PI/180.0); /* find the eastling and northing of of the 1st grid point */ false_east = false_north = 0.0; long_i = merforint(r_maj,r_min,c_lon,c_lat,false_east,false_north); rlon = GDS_Mercator_lon1(gds) * (M_PI/180.0); rlat = GDS_Mercator_lat1(gds) * (M_PI/180.0); long_i = merfor(rlon, rlat, &x0, &y0); /* initialize for 1st grid point */ x0 = -x0; y0 = -y0; long_i = merinvint(r_maj,r_min,c_lon,c_lat,x0,y0); inv_fn = &merinv; } else if (gdt == 20) { // polar stereographic /* get earth axis */ axes_earth(sec, &r_maj, &r_min); dy = GDS_Polar_dy(gds); dx = GDS_Polar_dx(gds); /* central point */ c_lon = GDS_Polar_lov(gds) * (M_PI/180.0); c_lat = GDS_Polar_lad(gds) * (M_PI/180.0); /* find the eastling and northing of of the 1st grid point */ false_east = false_north = 0.0; long_i = psforint(r_maj,r_min,c_lon,c_lat,false_east,false_north); rlon = GDS_Polar_lon1(gds) * (M_PI/180.0); rlat = GDS_Polar_lat1(gds) * (M_PI/180.0); long_i = psfor(rlon, rlat, &x0, &y0); /* initialize for 1st grid point */ x0 = -x0; y0 = -y0; long_i = psinvint(r_maj,r_min,c_lon,c_lat,x0,y0); inv_fn = &psinv; } else if (gdt == 30) { // lambert conformal conic /* get earth axis */ axes_earth(sec, &r_maj, &r_min); dy = GDS_Lambert_dy(gds); dx = GDS_Lambert_dx(gds); //printf(">>> gctpc dx %lf, dy %lf\n", dx, dy); /* latitudes of tangent/intersection */ lat1 = GDS_Lambert_Latin1(gds) * (M_PI/180.0); lat2 = GDS_Lambert_Latin2(gds) * (M_PI/180.0); /* central point */ c_lon = GDS_Lambert_Lov(gds) * (M_PI/180.0); c_lat = GDS_Lambert_LatD(gds) * (M_PI/180.0); /* find the eastling and northing of of the 1st grid point */ false_east = false_north = 0.0; long_i = lamccforint(r_maj,r_min,lat1,lat2,c_lon,c_lat,false_east,false_north); rlon = GDS_Lambert_Lo1(gds) * (M_PI/180.0); rlat = GDS_Lambert_La1(gds) * (M_PI/180.0); long_i = lamccfor(rlon, rlat, &x0, &y0); /* initialize for 1st grid point */ x0 = -x0; y0 = -y0; long_i = lamccinvint(r_maj,r_min,lat1,lat2,c_lon,c_lat,x0,y0); inv_fn = &lamccinv; } else if (gdt == 31) { // albers equal area /* get earth axis */ axes_earth(sec, &r_maj, &r_min); dy = GDS_Albers_dy(gds); dx = GDS_Albers_dx(gds); /* latitudes of tangent/intersection */ lat1 = GDS_Albers_Latin1(gds) * (M_PI/180.0); lat2 = GDS_Albers_Latin2(gds) * (M_PI/180.0); /* central point */ c_lon = GDS_Albers_Lov(gds) * (M_PI/180.0); c_lat = GDS_Albers_LatD(gds) * (M_PI/180.0); /* find the eastling and northing of of the 1st grid point */ false_east = false_north = 0.0; long_i = alberforint(r_maj,r_min,lat1,lat2,c_lon,c_lat,false_east,false_north); rlon = GDS_Albers_Lo1(gds) * (M_PI/180.0); rlat = GDS_Albers_La1(gds) * (M_PI/180.0); long_i = alberfor(rlon, rlat, &x0, &y0); /* initialize for 1st grid point */ x0 = -x0; y0 = -y0; long_i = alberinvint(r_maj,r_min,lat1,lat2,c_lon,c_lat,x0,y0); inv_fn = &alberinv; } if (inv_fn == NULL) return 1; if ((*lat = llat = (double *) malloc(nnpnts * sizeof(double))) == NULL) { fatal_error("gctpc_get_latlon memory allocation failed",""); } if ((*lon = llon = (double *) malloc(nnpnts * sizeof(double))) == NULL) { fatal_error("gctpc_get_latlon memory allocation failed",""); } /* put x[] and y[] values in lon and lat */ if (stagger(sec, nnpnts, llon, llat)) fatal_error("gctpc: stagger problem",""); printf(">> stagger gctpc x00 %lf y00 %lf\n",llon[0], llat[0]); #pragma omp parallel for schedule(static) private(i) for (i = 0; i < nnpnts; i++) { inv_fn(llon[i]*dx, llat[i]*dy, llon+i, llat+i); llat[i] *= (180.0 / M_PI); llon[i] *= (180.0 / M_PI); if (llon[i] < 0.0) llon[i] += 360.0; } return 0; }
int small_grib(unsigned char **sec, int mode, float *data, double *lon, double *lat, unsigned int ndata, int ix0, int ix1, int iy0, int iy1, FILE *out) { int can_subset, grid_template; int nx, ny, res, scan, new_nx, new_ny, i, j; unsigned int sec3_len, new_ndata, k, npnts; unsigned char *sec3, *new_sec[9]; double units; int basic_ang, sub_ang, cyclic_grid; float *new_data; get_nxny(sec, &nx, &ny, &npnts, &res, &scan); /* get nx, ny, and scan mode of grid */ grid_template = code_table_3_1(sec); // make a copy of the gds (sec3) sec3_len = GB2_Sec3_size(sec); sec3 = (unsigned char *) malloc(sec3_len); for (k = 0; k < sec3_len; k++) sec3[k] = sec[3][k]; // make a copy of the sec[] with new sec3 new_sec[0] = sec[0]; new_sec[1] = sec[1]; new_sec[2] = sec[2]; new_sec[3] = sec3; new_sec[4] = sec[4]; new_sec[5] = sec[5]; new_sec[6] = sec[6]; new_sec[7] = sec[7]; // new_sec[8] = sec[8]; not needed by writing routines can_subset = 1; if (lat == NULL || lon == NULL) can_subset = 0; new_nx = ix1-ix0+1; new_ny = iy1-iy0+1; if (new_nx <= 0) fatal_error("small_grib, new_nx is <= 0",""); if (new_ny <= 0) fatal_error("small_grib, new_ny is <= 0",""); new_ndata = new_nx * new_ny; cyclic_grid = 0; if (can_subset) { cyclic_grid = cyclic(sec); // lat-lon grid - no thinning if ((grid_template == 0 && sec3_len == 72) || (grid_template == 1 && sec3_len == 04)) { uint_char(new_nx,sec3+30); // nx uint_char(new_ny,sec3+34); // ny basic_ang = GDS_LatLon_basic_ang(sec3); sub_ang = GDS_LatLon_sub_ang(sec3); if (basic_ang != 0) { units = (double) basic_ang / (double) sub_ang; } else { units = 0.000001; } i = lat[ idx(ix0,iy0,nx,ny,cyclic_grid) ] / units; // lat1 int_char(i,sec3+46); i = lon[ idx(ix0,iy0,nx,ny,cyclic_grid) ] / units; // lon1 int_char(i,sec3+50); i = lat[ idx(ix1,iy1,nx,ny,cyclic_grid) ] / units; // lat2 int_char(i,sec3+55); i = lon[ idx(ix1,iy1,nx,ny,cyclic_grid) ] / units; // lon2 int_char(i,sec3+59); } else if ((grid_template == 40 && sec3_len == 72)) { // full Gaussian grid uint_char(new_nx,sec3+30); // nx uint_char(new_ny,sec3+34); // ny basic_ang = GDS_Gaussian_basic_ang(sec3); sub_ang = GDS_Gaussian_sub_ang(sec3); if (basic_ang != 0) { units = (double) basic_ang / (double) sub_ang; } else { units = 0.000001; } i = lat[ idx(ix0,iy0,nx,ny,cyclic_grid) ] / units; // lat1 int_char(i,sec3+46); i = lon[ idx(ix0,iy0,nx,ny,cyclic_grid) ] / units; // lon1 int_char(i,sec3+50); i = lat[ idx(ix1,iy1,nx,ny,cyclic_grid) ] / units; // lat2 int_char(i,sec3+55); i = lon[ idx(ix1,iy1,nx,ny,cyclic_grid) ] / units; // lon2 int_char(i,sec3+59); } // polar-stereo graphic, lambert conformal , no thinning else if ((grid_template == 20 && sec3_len == 65) || // polar stereographic (grid_template == 30 && sec3_len == 81)) { // lambert conformal uint_char(new_nx,sec3+30); // nx uint_char(new_ny,sec3+34); // ny i = (int) (lat[ idx(ix0,iy0,nx,ny,cyclic_grid) ] * 1000000.0); // lat1 int_char(i,sec3+38); i = (int) (lon[ idx(ix0,iy0,nx,ny,cyclic_grid) ] * 1000000.0); // lon1 int_char(i,sec3+42); } // mercator, no thinning else if (grid_template == 10 && sec3_len == 72) { // mercator uint_char(new_nx,sec3+30); // nx uint_char(new_ny,sec3+34); // ny units = 0.000001; i = lat[ idx(ix0,iy0,nx,ny,cyclic_grid) ] / units; // lat1 int_char(i,sec3+38); i = lon[ idx(ix0,iy0,nx,ny,cyclic_grid) ] / units; // lon1 int_char(i,sec3+42); i = lat[ idx(ix1,iy1,nx,ny,cyclic_grid) ] / units; // lat2 int_char(i,sec3+51); i = lon[ idx(ix1,iy1,nx,ny,cyclic_grid) ] / units; // lon2 int_char(i,sec3+55); } else { can_subset = 0; } } // copy data to a new array if (can_subset) { uint_char(new_ndata, sec3+6); new_data = (float *) malloc(new_ndata * sizeof(float)); #pragma omp parallel for private(i,j,k) for(j = iy0; j <= iy1; j++) { k = (j-iy0)*(ix1-ix0+1); for(i = ix0; i <= ix1; i++) { new_data[(i-ix0) + k ] = data[ idx(i,j,nx,ny,cyclic_grid) ]; } } } else { new_ndata = ndata; new_data = (float *) malloc(new_ndata * sizeof(float)); for (k = 0; k < ndata; k++) new_data[k] = data[k]; new_nx = nx; new_ny = ny; } set_order(new_sec, output_order); grib_wrt(new_sec, new_data, new_ndata, new_nx, new_ny, use_scale, dec_scale, bin_scale, wanted_bits, max_bits, grib_type, out); if (flush_mode) fflush(out); free(new_data); free(sec3); return 0; }
int code_table_3_7(unsigned char **sec) { int grid_def; grid_def = code_table_3_1(sec); if (grid_def >= 50 && grid_def <= 53) return GDS_Harmonic_code_3_7(sec[3]); return -1; }
int main(int argc, char **argv) { FILE *in; unsigned char *msg, *sec[9]; long int last_pos; struct stat stat_buf; /* for type of grib input file */ int file_arg, i, j, num_submsgs; int n_arg; unsigned int k, ndata; float *data; double *ddata, ref; #ifdef USE_G2CLIB float missing_c_val_1, missing_c_val_2; g2int *bitmap, has_bitmap; g2float *g2_data; int ii; #endif struct ARGLIST arglist[N_ARGLIST]; int narglist = 0; const char *new_argv[N_ARGLIST]; void *local[N_ARGLIST]; int has_inv_option, last_submsg; int err, new_GDS, gdt, pdt, center; unsigned char dscale[2] = {0,0}; inv_file = stdout; // jas_init(); // gctpc initialiation init(-1,-1,"gctpc_1,txt", "gctpc_2.txt"); data = NULL; ndata = 0; /* no arguments .. help screen */ if (argc == 1) { // f_help(-1,NULL,NULL,0,inv_out,local,"most"); mode = -1; data = NULL; ndata = 0; *inv_out = 0; f_h(call_ARG0(inv_out,NULL)); fprintf(inv_file, "%s\n", inv_out); eof_bin(); eof_string(); exit(8); } setup_user_gribtable(); /* copy argv */ for (i = 0; i < argc; i++) { new_argv[i] = argv[i]; } /* scan for "inv" and input file */ has_inv_option = 0; file_arg = 0; for (i = 1; i < argc; i++) { if (new_argv[i][0] != '-') { /* must be filename */ file_arg = i; continue; } /* must be an option */ for (j = 0; j < nfunctions; j++) { if (strcmp(&(new_argv[i][1]),functions[j].name) == 0) { if (functions[j].type == inv) has_inv_option = 1; i += functions[j].nargs; break; } } } /* if no inv option, use default inventory .. put it at end */ if (has_inv_option == 0) { for (i = 0; i < argc; i++) { new_argv[i] = new_argv[i]; } new_argv[argc++] = "-s"; } /* parse parameters */ file_arg = 0; for (i = 1; i < argc; i++) { if (new_argv[i][0] != '-' || (strcmp(new_argv[i],"-") == 0) ) { /* must be filename */ if (file_arg == 0) { file_arg = i; continue; } else { fatal_error("too many grib files .. 2nd=%s", new_argv[i]); } } /* must be an option */ for (j = 0; j < nfunctions; j++) { if (strcmp(&(new_argv[i][1]),functions[j].name) == 0) { #ifdef DEBUG fprintf(stderr,"match .. -%s %d args\n", functions[j].name, functions[j].nargs); #endif /* add to function argument list */ arglist[narglist].fn = j; arglist[narglist].i_argc = i+1; if (functions[j].type == inv) has_inv_option = 1; i += functions[j].nargs; if (i >= argc) fatal_error("missing arguments option=%s",functions[j].name); narglist++; break; } } if (j == nfunctions) { fatal_error("unknown option %s", new_argv[i]); } } if (has_inv_option == 0) { fatal_error("missing arguments on last option",""); } /* initialize options mode = -1 */ #ifdef DEBUG fprintf(stderr,"init options narglist %d\n",narglist); #endif for (j = 0; j < narglist; j++) { inv_out[0] = 0; n_arg = functions[arglist[j].fn].nargs; err = 0; if (n_arg == 0) err = functions[arglist[j].fn].fn(-1,NULL,NULL,0, inv_out,local+j); else if (n_arg == 1) err = functions[arglist[j].fn].fn(-1,NULL,NULL,0, inv_out,local+j, new_argv[arglist[j].i_argc]); else if (n_arg == 2) err = functions[arglist[j].fn].fn(-1,NULL,NULL,0, inv_out,local+j, new_argv[arglist[j].i_argc],new_argv[arglist[j].i_argc+1]); else if (n_arg == 3) err = functions[arglist[j].fn].fn(-1,NULL,NULL,0, inv_out,local+j, new_argv[arglist[j].i_argc],new_argv[arglist[j].i_argc+1],new_argv[arglist[j].i_argc+2]); else if (n_arg == 4) err = functions[arglist[j].fn].fn(-1,NULL,NULL,0, inv_out,local+j, new_argv[arglist[j].i_argc],new_argv[arglist[j].i_argc+1], new_argv[arglist[j].i_argc+2], new_argv[arglist[j].i_argc+3]); else if (n_arg == 5) err = functions[arglist[j].fn].fn(-1,NULL,NULL,0, inv_out,local+j, new_argv[arglist[j].i_argc],new_argv[arglist[j].i_argc+1], new_argv[arglist[j].i_argc+2], new_argv[arglist[j].i_argc+3], new_argv[arglist[j].i_argc+4]); else if (n_arg == 6) err = functions[arglist[j].fn].fn(-1,NULL,NULL,0, inv_out,local+j, new_argv[arglist[j].i_argc],new_argv[arglist[j].i_argc+1], new_argv[arglist[j].i_argc+2], new_argv[arglist[j].i_argc+3], new_argv[arglist[j].i_argc+4], new_argv[arglist[j].i_argc+5]); else if (n_arg == 7) err = functions[arglist[j].fn].fn(-1,NULL,NULL,0, inv_out,local+j, new_argv[arglist[j].i_argc],new_argv[arglist[j].i_argc+1], new_argv[arglist[j].i_argc+2], new_argv[arglist[j].i_argc+3], new_argv[arglist[j].i_argc+4], new_argv[arglist[j].i_argc+5], new_argv[arglist[j].i_argc+6]); else if (n_arg == 8) err = functions[arglist[j].fn].fn(-1,NULL,NULL,0, inv_out,local+j, new_argv[arglist[j].i_argc],new_argv[arglist[j].i_argc+1], new_argv[arglist[j].i_argc+2], new_argv[arglist[j].i_argc+3], new_argv[arglist[j].i_argc+4], new_argv[arglist[j].i_argc+5], new_argv[arglist[j].i_argc+6], new_argv[arglist[j].i_argc+7]); if(inv_out[0] != 0) fprintf(inv_file, "%s", inv_out); if (err) { err_bin(); err_string(); exit(8); } } if (file_arg == 0 && argc > 1) fatal_error("no input file", ""); if (latlon == 1 && output_order_wanted != wesn) fatal_error("latitude-longitude information is only available with -order we:sn",""); /* open input file */ seq_input = 0; if (strcmp(new_argv[file_arg],"-") == 0) { seq_input = 1; in = stdin; if (mode == 98) fprintf(stderr, "grib input is stdin\n"); } else { if (stat(new_argv[file_arg], &stat_buf) != -1) { if (S_ISREG(stat_buf.st_mode)) { if (mode == 98) fprintf(stderr, "grib input is a regular file\n"); } else if (S_ISDIR(stat_buf.st_mode)) { fatal_error("grib input is a directory: %s",new_argv[file_arg]); } else if (S_ISCHR(stat_buf.st_mode)) { seq_input = 1; if (mode == 98) fprintf(stderr, "grib input is a char device\n"); } else if (S_ISBLK(stat_buf.st_mode)) { seq_input = 1; if (mode == 98) fprintf(stderr, "grib input is a block device\n"); } else if (S_ISFIFO(stat_buf.st_mode)) { seq_input = 1; if (mode == 98) fprintf(stderr, "grib input is a fifo device\n"); } else { if (mode == 98) fprintf(stderr, "grib input has an unknown type\n"); } } if ((in = fopen(new_argv[file_arg],"rb")) == NULL) { fatal_error("could not open file: %s", new_argv[file_arg]); } } /* sequential input - can not do random access */ if (seq_input && input == inv_mode) fatal_error("wgrib2 cannot random access grib input file",""); ndata = 0; data = NULL; ddata = NULL; msg_no = 1; inv_no = 0; len = pos = 0; submsg = 0; msg = NULL; if ((old_gds = (unsigned char *) malloc(GDS_max_size * sizeof(char)) ) == NULL) { fatal_error("memory allocation problem old_gds in wgrib2.main",""); } last_pos = -1; last_submsg = -1; /* if dump mode .. position io stream */ if (input == dump_mode) { while (msg_no < dump_msg) { msg = seq_input ? rd_grib2_msg_seq(in, &pos, &len, &num_submsgs) : rd_grib2_msg(in, &pos, &len, &num_submsgs); if (msg == NULL) fatal_error_i("record %d not found", dump_msg); last_pos = pos; pos += len; msg_no++; } #ifdef DEBUG printf("dump mode msg=%d\n", msg_no); #endif } /* * submsg = 0 .. beginning of unread record * submsg = i .. start at ith submsg * num_submsgs = number of submessages in grib message */ /* inventory loop */ for (;last_message == 0;) { /* need position and submessage number of message */ if (input == inv_mode || input == dump_mode) { if (input == inv_mode) { if (rd_inventory(&msg_no,&submsg, &pos)) break; } else if (input == dump_mode) { if (dump_msg == -1) break; submsg = dump_submsg; dump_msg = -1; } if (pos != last_pos) { msg = seq_input ? rd_grib2_msg_seq(in, &pos, &len, &num_submsgs) : rd_grib2_msg(in, &pos, &len, &num_submsgs); if (msg == NULL) { fatal_error_i("grib message #%d not found", msg_no); break; } last_pos = pos; last_submsg = -1; } if (pos == last_pos && submsg == last_submsg + 1) { /* read previous submessage */ if (parse_next_msg(sec) != 0) { fprintf(stderr,"\n*** grib message #%d.%d not found ***\n\n", msg_no, submsg); break; } } else { /* need to get desired submessage into sec */ if (parse_1st_msg(sec) != 0) { fprintf(stderr,"\n*** grib message #%d.1 not found ***\n\n", msg_no); break; } for (i = 2; i <= submsg; i++) { if (parse_next_msg(sec) != 0) { fprintf(stderr,"\n*** grib message #%d.%d not found ***\n\n", msg_no, i); break; } } } last_submsg = submsg; } else if (input == all_mode) { if (submsg == 0) { msg = seq_input ? rd_grib2_msg_seq(in, &pos, &len, &num_submsgs) : rd_grib2_msg(in, &pos, &len, &num_submsgs); if (msg == NULL) break; submsg = 1; } else if (submsg > num_submsgs) { pos += len; msg_no++; msg = seq_input ? rd_grib2_msg_seq(in, &pos, &len, &num_submsgs) : rd_grib2_msg(in, &pos, &len, &num_submsgs); if (msg == NULL) break; submsg = 1; } if (submsg == 1) { if (parse_1st_msg(sec) != 0) { fprintf(stderr,"illegal format: parsing 1st submessage\n"); } } else { if (parse_next_msg(sec) != 0) { fprintf(stderr,"illegal format: parsing submessages\n"); } } } if (only_submsg > 0 && only_submsg != submsg) { submsg++; continue; } if (for_mode) { if (msg_no < for_start || msg_no > for_end || ((msg_no - for_start) % for_step) != 0) { if (msg_no > for_end && input != inv_mode) last_message = 1; submsg++; continue; } } #ifdef USE_REGEX /* move inv_no++ before match_inv is made */ inv_no++; if (match) { inv_out[0] = 0; if (num_submsgs > 1) { sprintf(inv_out,"%d.%d:", msg_no, submsg); } else { sprintf(inv_out,"%d:", msg_no); } // f_match_inv(0, sec, NULL, 0, inv_out+strlen(inv_out), NULL); f_match_inv(call_ARG0(inv_out+strlen(inv_out), NULL)); if (is_match(inv_out) != 0) { submsg++; inv_no--; continue; } } #endif match_flag = 0; if (for_n_mode) { if (inv_no < for_n_start || inv_no > for_n_end || ((inv_no - for_n_start) % for_n_step) != 0) { if (inv_no > for_n_end) last_message = 1; submsg++; continue; } } /* see if new GDS */ if ((i = GB2_Sec3_size(sec)) != old_GDS_size) { new_GDS = 1; } else { new_GDS = 0; for (j = 0; j < i; j++) { if (old_gds[j] != sec[3][j]) new_GDS = 1; } } if (new_GDS) { GDS_change_no++; if (i > GDS_max_size) { free(old_gds); GDS_max_size = i; if ((old_gds = (unsigned char *) malloc(GDS_max_size) ) == NULL) { fatal_error("memory allocation problem old_gds in wgrib2.main",""); } } for (j = 0; j < i; j++) { old_gds[j] = sec[3][j]; } old_GDS_size = i; /* update grid information */ get_nxny(sec, &nx, &ny, &npnts, &res, &scan); /* get nx, ny, and scan mode of grid */ output_order = (nx == -1 || ny == -1) ? raw : output_order_wanted; if (latlon) { i = 1; if (use_gctpc && output_order == wesn) { /* use gctpc to get lat lon values */ i = gctpc_get_latlon(sec, &lon, &lat); } if (i) get_latlon(sec, &lon, &lat); /* get lat lon of grid points */ } } /* Decode NDFD WxText */ if (WxText) mk_WxKeys(sec); // any fixes to raw grib message before decode need to be placed here if (fix_ncep_2_flag) fix_ncep_2(sec); if (fix_ncep_3_flag) fix_ncep_3(sec); if (fix_ncep_4_flag) fix_ncep_4(sec); #ifdef CHECK j = code_table_5_0(sec); // type of compression /* yes this can be simplified but want to split it up in case other decoders have problems */ if (j == 0 && sec[5][19] == 0 && int2(sec[5] + 17) != 0 && ieee2flt(sec[5]+11) != 0.0) fprintf(stderr,"Warning: g2lib/g2clib/grib-api simple decode may differ from WMO standard, use -g2clib 0 for WMO standard\n"); if ((j == 2 || j == 3) && int2(sec[5]+17) != 0 && int4(sec[5] + 31) == 0 && ieee2flt(sec[5]+11) != 0.0) fprintf(stderr,"Warning: g2lib/g2clib complex decode may differ from WMO standard, use -g2clib 0 for WMO standard\n"); if (j == 40 && sec[5][19] == 0 && int2(sec[5] + 17) != 0 && ieee2flt(sec[5]+11) != 0.0) fprintf(stderr,"Warning: g2lib/g2clib jpeg deocde may differ from WMO standard, use use -g2clib 0 for WMO standard\n"); if (j == 41 && sec[5][19] == 0 && int2(sec[5] + 17) != 0 && ieee2flt(sec[5]+11) != 0.0) fprintf(stderr,"Warning: g2lib/g2clib/grib-api png decode may differ from WMO standard, use -g2clib 0 for WMO standard\n"); #endif #ifdef CHECK /* check the size of Section 7 */ /* code to check the other sizes needs to be placed in decode routines */ j = code_table_5_0(sec); // type of compression if (j == 0) { /* simple */ /* to avoid overflow on 32 bit machines */ /* old: k = (GB2_Sec5_nval(sec) * sec[5][19] + 7) / 8 + 5; */ k = 5 + (GB2_Sec5_nval(sec)/8) * sec[5][19] + (GB2_Sec5_nval(sec)%8) * (sec[5][19]/8) + ( (GB2_Sec5_nval(sec)%8) * (sec[5][19]%8) + 7) / 8; if (k != GB2_Sec7_size(sec)) { fprintf(stderr,"Detected a size mismatch, Section 7, wanted %d found %d\n", k, GB2_Sec7_size(sec)); if (decode) fatal_error("Section 7 size, mismatch, simple packing",""); } } else if (j == 4) { /* IEEE */ k = GB2_Sec5_nval(sec) * 4 + 5; if (k != GB2_Sec7_size(sec)) { fprintf(stderr,"Detected a size mismatch, Section 7, wanted %d found %d\n", k, GB2_Sec7_size(sec)); if (decode) fatal_error("Section 7 size, mismatch, IEEE packing",""); } } #endif if (decode) { #ifdef CHECK if (code_table_6_0(sec) == 0) { // has bitmap k = GB2_Sec3_npts(sec) - GB2_Sec5_nval(sec); if (k != missing_points(sec[6]+6, GB2_Sec3_npts(sec))) fatal_error_ii("inconsistent number of bitmap points sec3-sec5: %d sec6: %d", k, missing_points(sec[6]+6, GB2_Sec3_npts(sec))); } else if (code_table_6_0(sec) == 255) { // no bitmap if (GB2_Sec3_npts(sec) != GB2_Sec5_nval(sec)) fatal_error_ii("inconsistent number of data points sec3: %d sec5: %d", GB2_Sec3_npts(sec), GB2_Sec5_nval(sec)); } #endif /* allocate data */ if (GB2_Sec3_npts(sec) != ndata) { if (ndata) free(data); ndata = GB2_Sec3_npts(sec); if (ndata) { data = (float *) malloc(ndata * sizeof(float)); if (data == NULL) fatal_error("main: memory allocation failed data",""); } else { data = NULL; } } j = code_table_5_0(sec); // type of compression gdt = code_table_3_1(sec); // grid type pdt = GB2_ProdDefTemplateNo(sec); // product defintion template /* USE G2CLIB */ #ifdef USE_G2CLIB if (use_g2clib == 2) { err = g2_getfld(msg,submsg,1,1,&grib_data); if (err != 0) fatal_error_ii("Fatal g2clib decode err=%d msg=%d", err, msg_no); free_gribfield = 1; has_bitmap = grib_data->ibmap; g2_data = &(grib_data->fld[0]); if (has_bitmap == 0 || has_bitmap == 254) { bitmap = grib_data->bmap; for (i = 0; i < ndata; i++) { data[i] = (bitmap[i] == 0) ? UNDEFINED : g2_data[i]; } } else { for (i = 0; i < ndata; i++) { data[i] = g2_data[i]; } } /* complex packing uses special values for undefined */ ii = sub_missing_values(sec, &missing_c_val_1, &missing_c_val_2); if (ii == 1) { for (i = 0; i < ndata; i++) { if (data[i] == missing_c_val_1) data[i] = UNDEFINED; } } else if (ii == 2) { for (i = 0; i < ndata; i++) { if (data[i] == missing_c_val_1) data[i] = UNDEFINED; if (data[i] == missing_c_val_2) data[i] = UNDEFINED; } } } #endif /* USE INTERNAL DECODER */ if (use_g2clib != 2) { center = GB2_Center(sec); if (use_g2clib == 1) { // introduce g2clib constant field error /* g2clib ignores decimal scaling for constant fields make internal decoders look like g2clib*/ if ( (j == 0 && sec[5][19] == 0) || ((j == 2 || j == 3) && int4(sec[5] + 31) == 0) || (j == 40 && sec[5][19] == 0) || (j == 41 && sec[5][19] == 0) || (center == NCEP && j == 40000 && sec[5][19] == 0) || (center == NCEP && j == 40010 && sec[5][19] == 0) ) { dscale[0] = sec[5][17]; dscale[1] = sec[5][18]; sec[5][17] = sec[5][18] = 0; } } err = unpk_grib(sec, data); if (err != 0) fatal_error_i("Fatal decode packing type %d",err); if (use_g2clib == 1) { // fix up data /* restore decimal scaling */ if ( (j == 0 && sec[5][19] == 0) || ((j == 2 || j == 3) && int4(sec[5] + 31) == 0) || (j == 40 && sec[5][19] == 0) || (j == 41 && sec[5][19]) || (center == NCEP && j == 40000 && sec[5][19] == 0) || (center == NCEP && j == 40010 && sec[5][19] == 0) ) { sec[5][17] = dscale[0]; sec[5][18] = dscale[1]; } } } /* convert to standard output order we:sn */ if (output_order_wanted == wesn) to_we_sn_scan(data); else if (output_order_wanted == wens) to_we_ns_scan(data); } else { if (ndata) free(data); ndata = 0; data = NULL; } /* get scaling parameters */ use_scale = scaling(sec, &ref, &dec_scale, &bin_scale, &i) == 0; if (num_submsgs > 1) { fprintf(inv_file, "%d.%d%s%ld", msg_no, submsg, ":", pos); } else { fprintf(inv_file, "%d%s%ld", msg_no, ":", pos); } for (j = 0; j < narglist; j++) { /* skip execution if match_flag == 1 */ /* an output option acts as endif for match_flag */ if (match_flag == 1) { if (functions[arglist[j].fn].type == output) match_flag = 0; continue; } // if (functions[arglist[j].fn].type == inv) fprintf(inv_file, item_deliminator); if (functions[arglist[j].fn].type == inv) fprintf(inv_file, "%s", item_deliminator); if (functions[arglist[j].fn].type != setup) { inv_out[0] = 0; n_arg = functions[arglist[j].fn].nargs; if (n_arg == 0) functions[arglist[j].fn].fn(mode, sec, data, ndata, inv_out, local+j); else if (n_arg == 1) functions[arglist[j].fn].fn(mode, sec, data, ndata, inv_out, local+j, new_argv[arglist[j].i_argc]); else if (n_arg == 2) functions[arglist[j].fn].fn(mode, sec, data, ndata, inv_out, local+j, new_argv[arglist[j].i_argc], new_argv[arglist[j].i_argc+1]); else if (n_arg == 3) functions[arglist[j].fn].fn(mode, sec, data, ndata, inv_out, local+j, new_argv[arglist[j].i_argc], new_argv[arglist[j].i_argc+1], new_argv[arglist[j].i_argc+2]); else if (n_arg == 4) functions[arglist[j].fn].fn(mode, sec, data, ndata, inv_out, local+j, new_argv[arglist[j].i_argc], new_argv[arglist[j].i_argc+1], new_argv[arglist[j].i_argc+2], new_argv[arglist[j].i_argc+3]); else if (n_arg == 5) functions[arglist[j].fn].fn(mode, sec, data, ndata, inv_out, local+j, new_argv[arglist[j].i_argc], new_argv[arglist[j].i_argc+1], new_argv[arglist[j].i_argc+2], new_argv[arglist[j].i_argc+3], new_argv[arglist[j].i_argc+4]); else if (n_arg == 6) functions[arglist[j].fn].fn(mode, sec, data, ndata, inv_out, local+j, new_argv[arglist[j].i_argc], new_argv[arglist[j].i_argc+1], new_argv[arglist[j].i_argc+2], new_argv[arglist[j].i_argc+3], new_argv[arglist[j].i_argc+4], new_argv[arglist[j].i_argc+5]); else if (n_arg == 7) functions[arglist[j].fn].fn(mode, sec, data, ndata, inv_out, local+j, new_argv[arglist[j].i_argc], new_argv[arglist[j].i_argc+1], new_argv[arglist[j].i_argc+2], new_argv[arglist[j].i_argc+3], new_argv[arglist[j].i_argc+4], new_argv[arglist[j].i_argc+5], new_argv[arglist[j].i_argc+6]); else if (n_arg == 8) functions[arglist[j].fn].fn(mode, sec, data, ndata, inv_out, local+j, new_argv[arglist[j].i_argc], new_argv[arglist[j].i_argc+1], new_argv[arglist[j].i_argc+2], new_argv[arglist[j].i_argc+3], new_argv[arglist[j].i_argc+4], new_argv[arglist[j].i_argc+5], new_argv[arglist[j].i_argc+6], new_argv[arglist[j].i_argc+7]); if(inv_out[0] != 0) fprintf(inv_file, "%s", inv_out); } } #ifdef CHECK if (!decode) { if (code_table_6_0(sec) == 0) { // has bitmap k = GB2_Sec3_npts(sec) - GB2_Sec5_nval(sec); if (k != missing_points(sec[6]+6, GB2_Sec3_npts(sec))) fatal_error_ii("inconsistent number of bitmap points sec3-sec5: %d sec6: %d", k, missing_points(sec[6]+6, GB2_Sec3_npts(sec))); } else if (code_table_6_0(sec) == 255) { // no bitmap if (GB2_Sec3_npts(sec) != GB2_Sec5_nval(sec)) fatal_error_ii("inconsistent number of data points sec3: %d sec5: %d", GB2_Sec3_npts(sec), GB2_Sec5_nval(sec)); } } #endif submsg++; #ifdef USE_G2CLIB if (free_gribfield) { g2_free(grib_data); free_gribfield = 0;} #endif // fprintf(inv_file, "\n"); fprintf(inv_file, "%s",end_inv); if (flush_mode) fflush(inv_file); if (dump_msg > 0) break; } /* finalize all functions, call with mode = -2 */ for (j = 0; j < narglist; j++) { if (functions[arglist[j].fn].type != setup) { n_arg = functions[arglist[j].fn].nargs; inv_out[0] = 0; if (n_arg == 0) functions[arglist[j].fn].fn(-2, NULL, NULL, 0, inv_out, local+j); else if (n_arg == 1) functions[arglist[j].fn].fn(-2, NULL, NULL, 0, inv_out, local+j, new_argv[arglist[j].i_argc]); else if (n_arg == 2) functions[arglist[j].fn].fn(-2, NULL, NULL, 0, inv_out, local+j, new_argv[arglist[j].i_argc], new_argv[arglist[j].i_argc+1]); else if (n_arg == 3) functions[arglist[j].fn].fn(-2, NULL, NULL, 0, inv_out, local+j, new_argv[arglist[j].i_argc], new_argv[arglist[j].i_argc+1], new_argv[arglist[j].i_argc+2]); else if (n_arg == 4) functions[arglist[j].fn].fn(-2, NULL, NULL, 0, inv_out, local+j, new_argv[arglist[j].i_argc], new_argv[arglist[j].i_argc+1], new_argv[arglist[j].i_argc+2], new_argv[arglist[j].i_argc+3]); else if (n_arg == 5) functions[arglist[j].fn].fn(-2, NULL, NULL, 0, inv_out, local+j, new_argv[arglist[j].i_argc], new_argv[arglist[j].i_argc+1], new_argv[arglist[j].i_argc+2], new_argv[arglist[j].i_argc+3], new_argv[arglist[j].i_argc+4]); else if (n_arg == 6) functions[arglist[j].fn].fn(-2, NULL, NULL, 0, inv_out, local+j, new_argv[arglist[j].i_argc], new_argv[arglist[j].i_argc+1], new_argv[arglist[j].i_argc+2], new_argv[arglist[j].i_argc+3], new_argv[arglist[j].i_argc+4], new_argv[arglist[j].i_argc+5]); else if (n_arg == 7) functions[arglist[j].fn].fn(-2, NULL, NULL, 0, inv_out, local+j, new_argv[arglist[j].i_argc], new_argv[arglist[j].i_argc+1], new_argv[arglist[j].i_argc+2], new_argv[arglist[j].i_argc+3], new_argv[arglist[j].i_argc+4], new_argv[arglist[j].i_argc+5], new_argv[arglist[j].i_argc+6]); else if (n_arg == 8) functions[arglist[j].fn].fn(-2, NULL, NULL, 0, inv_out, local+j, new_argv[arglist[j].i_argc], new_argv[arglist[j].i_argc+1], new_argv[arglist[j].i_argc+2], new_argv[arglist[j].i_argc+3], new_argv[arglist[j].i_argc+4], new_argv[arglist[j].i_argc+5], new_argv[arglist[j].i_argc+6], new_argv[arglist[j].i_argc+7]); // if (inv_out[0]) fprintf(stderr, "%s\n", inv_out); if (inv_out[0]) fprintf(stderr, "%s%s", inv_out, end_inv); } } eof_bin(); eof_string(); exit(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; }
int code_table_3_8(unsigned char **sec) { int grid_def; grid_def = code_table_3_1(sec); if (grid_def == 100) return sec[3][31]; return -1; }
int f_AAIG(ARG0) { int grid_template; double cellsize; char *save_inv_out, level[STRING_SIZE], file[STRING_SIZE], name[STRING_SIZE]; int year0, month0, day0, hour0, minute0, second0; int year, month, day, hour, minute, second, i, j; FILE *out; if (mode == -1) { decode = latlon = 1; return 0; } if (mode < 0) return 0; if (lat == NULL || lon == NULL) { fprintf(stderr,"f_AAIG does nothing, no lat-lon information\n"); return 0; } if (output_order != wesn) { fprintf(stderr,"f_AAIG does nothing, not in we:sn order\n"); return 0; } grid_template = code_table_3_1(sec); if (grid_template != 0) { fprintf(stderr,"f_AAIG does nothing, not lat-lon grid\n"); return 0; } if (nx == -1 || ny == -1) { fprintf(stderr,"f_AAIG does nothing, found thinned lat-lon grid\n"); return 0; } cellsize = -1.0; if (nx > 1) { cellsize = lon[1] - lon[0]; if ( fabs(lat[nx]-lat[0] - cellsize) > 0.0001*cellsize) { fprintf(stderr,"f_AAIG does nothing, dlon != dlat\n"); return 0; } } else { cellsize = lat[nx] - lat[0]; } if (getName(sec, mode, NULL, name, NULL, NULL) != 0) { fprintf(stderr,"f_AAIG does nothing, no name\n"); return 0; } save_inv_out = inv_out; inv_out = level; f_lev(CALL_ARG0); inv_out = save_inv_out; save_inv_out = level; while (*save_inv_out) { if (*save_inv_out == ' ') *save_inv_out = '_'; save_inv_out++; } reftime(sec, &year0, &month0, &day0, &hour0, &minute0, &second0); if (verftime(sec, &year, &month, &day, &hour, &minute, &second) != 0) { fprintf(stderr,"f_AAIG no verf time\n"); year = year0; month = month0; day = day0; hour = hour0; } if (year == year0 && month == month0 && day == day0 && hour == hour0) { sprintf(file,"%s.%s.%4.4d%2.2d%2.2d%2.2d.asc",name,level,year,month,day,hour); } else { sprintf(file,"%s.%s.%4.4d%2.2d%2.2d%2.2d_%4.4d%2.2d%2.2d%2.2d.asc", name,level,year0,month0,day0,hour0,year,month,day,hour); } if ((out = fopen(file,"w")) == NULL) { fprintf(stderr,"f_AAIG could not open raster file %s\n",file); return 0; } fprintf(stderr, "raster file: %s\n", file); fprintf(out,"ncols %d\n", nx); fprintf(out,"nrows %d\n", ny); fprintf(out,"xllcenter %lf\n", lon[0] > 180.0 ? lon[0]-180.0 : lon[0]); fprintf(out,"yllcenter %lf\n", lat[0]); fprintf(out,"cellsize %lf\n", cellsize); fprintf(out,"NODATA_VALUE 9.999e20\n"); for (j = ny-1; j >= 0; j--) { for (i = 0; i < nx; i++) { fprintf(out,"%f\n", data[i+j*nx]); } } fclose(out); return 0; }