void update_location_block(meta_parameters *meta) { if (!meta->projection) asfPrintWarning("Image does not have a projection block.\n" "Not updating location block!\n"); else asfPrintWarning("Location block is updated based on the projection " "information.\nThis might no reflect the actual corner " "coordinates as it could\ninclude background values.\n"); double startX = meta->projection->startX; double startY = meta->projection->startY; double endX = startX + meta->general->sample_count*meta->projection->perX; double endY = startY + meta->general->line_count*meta->projection->perY; double lat, lon, height; proj_to_latlon(meta->projection, startX, startY, 0.0, &lat, &lon, &height); meta->location->lat_start_near_range = lat*R2D; meta->location->lon_start_near_range = lon*R2D; proj_to_latlon(meta->projection, startX, endY, 0.0, &lat, &lon, &height); meta->location->lat_end_near_range = lat*R2D; meta->location->lon_end_near_range = lon*R2D; proj_to_latlon(meta->projection, endX, startY, 0.0, &lat, &lon, &height); meta->location->lat_start_far_range = lat*R2D; meta->location->lon_start_far_range = lon*R2D; proj_to_latlon(meta->projection, endX, endY, 0.0, &lat, &lon, &height); meta->location->lat_end_far_range = lat*R2D; meta->location->lon_end_far_range = lon*R2D; }
void save_subset(ImageInfo *ii) { if (g_poly->n > 0) { if (crosshair_line > 0 && crosshair_samp > 0) { // clamp crosshair to image extent, if needed if (crosshair_line >= ii->meta->general->line_count) crosshair_line = ii->meta->general->line_count - 1; if (crosshair_samp >= ii->meta->general->sample_count) crosshair_samp = ii->meta->general->sample_count - 1; show_save_subset_window(); set_defaults(ii); g_poly->show_extent = TRUE; fill_big(ii); update_save_subset_info(); } else { asfPrintWarning("Can't save subset: No crosshair.\n"); } } else { // shouldn't ever get in here... asfPrintWarning("Can't save subset: Polygon not defined.\n"); } }
/* dirwalk: * from "The C Programming Language" 2nd edition by Kernigan & Ritchie. pg. 182 * Apply fcn to all files in dir */ static void dirwalk(const char *dir, int (*fcn)(const char*)) { char name[1024]; struct dirent *dp; DIR *dfd; if ((dfd = opendir(dir)) == NULL) { asfPrintWarning("dirwalk: cannot open %s\n",dir); return; // error } while ((dp = readdir(dfd)) != NULL) { if (strcmp(dp->d_name, ".")==0 || strcmp(dp->d_name, "..")==0) { continue; } if (strlen(dir)+strlen(dp->d_name)+2 > sizeof(name)) { asfPrintWarning("dirwalk: name %s/%s exceeds buffersize.\n", dir, dp->d_name); return; // error } else { sprintf(name, "%s/%s", dir, dp->d_name); (*fcn)(name); } } closedir(dfd); }
// create a directory. acts like 'mkdir -p'. return 0 on success, -1 on fail. int create_dir(const char *dir) { int keep_going=TRUE; int ret=-1; char *ptr1, *ptr2; char *dir_tmp = (char*)MALLOC((strlen(dir)+1)*sizeof(char)); #ifndef mingw // big list of S_*'s equivalent to mode = 0777 int mode = S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IWOTH | S_IXOTH; #else // Windows path length limit: 260 chars // This is supposed to be defined as MAX_PATH (which is 260) // However it looks like the actual limit is 247... not sure why if (strlen(dir) >= 247) { asfPrintWarning("Path too long:\n%s\n", dir); return -1; } #endif ptr1 = strcpy(dir_tmp, dir); do { if ((ptr2=strpbrk(ptr1,DIR_SEPARATOR_STR)) != NULL) { *ptr2 = '\0'; ptr1 = ptr2+1; } else { keep_going = FALSE; } if (strlen(dir_tmp) > 0) { #ifdef mingw ret = mkdir(dir_tmp); #else ret = mkdir(dir_tmp, mode); #endif if (ret != 0 && errno != EEXIST) { asfPrintWarning("create_dir failed to create: %s\nErr: %d %s\n", dir_tmp, errno, strerror(errno)); } } if (keep_going) { *ptr2 = DIR_SEPARATOR; } } while (keep_going); FREE(dir_tmp); return ret; }
void test_geoid(void) { float f = get_geoid_height(0,0); if (fabs(f - 17.16) > .0001) asfPrintWarning("Unexpected value at 0,0 should be 17.15: %f\n", f); int w = geoid_get_width(); int h = geoid_get_height(); float *geoid_heights = geoid_get_height_array(); int i, j; for (i=0; i<w; ++i) { for (j=0; j<h; ++j) { float a0 = *(geoid_heights + j*w + i); float a1 = geoid_height_at(i,j); if (fabs(a0-a1) > .0001) asfPrintWarning("test_geoid #1 failed: %d %d %f %f\n", i, j, a0, a1); CU_ASSERT(fabs(a0-a1)<.0001); if (i<w-1 && j<h-1) { double ii = i+.5; double jj = j+.5; double lat = 90.0 - jj/4.0; double lon = ii/4.0; float b0 = get_geoid_height(lat,lon); float b1 = .25*(geoid_height_at(i,j)+geoid_height_at(i+1,j)+ geoid_height_at(i,j+1)+geoid_height_at(i+1,j+1)); if (fabs(b0-b1) > .0001) asfPrintWarning("test_geoid #2 failed: %d %d %f %f\n", i, j, b0, b1); CU_ASSERT(fabs(b0-b1)<.0001); ii = i+.25; jj = j+.75; lat = 90.0 - jj/4.0; lon = ii/4.0; float c0 = get_geoid_height(lat,lon); float u = .75*geoid_height_at(i,j) + .25*geoid_height_at(i+1,j); float v = .75*geoid_height_at(i,j+1) + .25*geoid_height_at(i+1,j+1); float c1 = .25*u + .75*v; if (fabs(c0-c1) > .0001) asfPrintError("test_geoid #3 failed: %d %d %f %f\n", i, j, c0, c1); CU_ASSERT(fabs(c0-c1)<.0001); } } } }
// process all things in a directory. Calls "process" to decide // if the "thing" is a dir (process_dir) or a file (process_file) static void process_dir(const char *dir, int top, int recursive, char *overlapping_dems[], int *next_dem_number, meta_parameters *meta, int *n_dems_total) { char name[1024]; struct dirent *dp; DIR *dfd; if ((dfd = opendir(dir)) == NULL) { asfPrintStatus(" Cannot open %s\n",dir); return; // error } while ((dp = readdir(dfd)) != NULL) { // skip current, parent dir entries if (strcmp(dp->d_name, ".")==0 || strcmp(dp->d_name, "..")==0) { continue; } if (strlen(dir)+strlen(dp->d_name)+2 > sizeof(name)) { asfPrintWarning("dirwalk: name %s/%s exceeds buffersize.\n", dir, dp->d_name); return; // error } else { // process this entry sprintf(name, "%s%c%s", dir, DIR_SEPARATOR, dp->d_name); process(name, top, recursive, overlapping_dems, next_dem_number, meta, n_dems_total); } } closedir(dfd); }
static void add_line_samp(double line, double samp) { if (line_samp_ok(line, samp)) { center_line = line; center_samp = samp; if (g_poly->n < MAX_POLY_LEN) { g_poly->line[g_poly->n] = line; g_poly->samp[g_poly->n] = samp; ++g_poly->n; g_poly->c = g_poly->n-1; } else { asfPrintWarning("Exceeded maximum polygon length.\n" "No more points can be added.\n"); } // now arrow keys will move the just-added point last_was_crosshair = FALSE; update_pixel_info(curr); fill_small(curr); fill_big(curr); } }
char *get_terrasar_browse_file(const char *xml_file_name) { xmlDoc *doc = xmlReadFile(xml_file_name, NULL, 0); if (!doc) { asfPrintWarning("Could not parse file %s\n", xml_file_name); return NULL; } char preview_file[2048]; char *path = get_dirname(xml_file_name); if (strlen(path)>0) { strcpy(preview_file, path); if (preview_file[strlen(preview_file)-1] != '/') strcat(preview_file, "/"); } else strcpy(preview_file, ""); free(path); strcat(preview_file, xml_get_string_value(doc, "level1Product.productComponents.browseImage.file.location.path")); strcat(preview_file, "/"); strcat(preview_file, xml_get_string_value(doc, "level1Product.productComponents.browseImage.file.location.filename")); return STRDUP(preview_file); }
void ASF_TIFF_WarningHandler(const char* module, const char* fmt, va_list ap) { char buf[4096]; vsnprintf(buf, sizeof(buf), fmt, ap); asfPrintWarning("%s: %s\n", module, buf); return; }
static int test_nad27_ll(double lat, double lon, double expected_lat, double expected_lon) { double lats[1]; double lons[1]; double hts[1]; char input_projection_info[255]; char output_projection_info[255]; projPJ input_projection, output_projection; sprintf(input_projection_info, "+proj=latlong +datum=NAD27"); sprintf(output_projection_info, "+proj=latlong +datum=NAD83"); input_projection = pj_init_plus(input_projection_info); output_projection = pj_init_plus(output_projection_info); lats[0] = lat * D2R; lons[0] = lon * D2R; hts[0] = 0; pj_transform (input_projection, output_projection, 1, 1, lats, lons, hts); if (pj_errno != 0) { asfPrintWarning("libproj error: %s\n", pj_strerrno(pj_errno)); } pj_free(input_projection); pj_free(output_projection); lats[0] *= R2D; lons[0] *= R2D; CU_ASSERT(double_equals(lats[0], expected_lat, 6)); CU_ASSERT(double_equals(lons[0], expected_lon, 6)); if (double_equals(lats[0], expected_lat, 6) && double_equals(lons[0], expected_lon, 6)) { //asfPrintStatus("Proj (fwd): %f, %f ... OK\n", lat, lon); return TRUE; } else { asfPrintStatus("Proj (fwd): %f, %f ... ERROR\n", lat, lon); asfPrintStatus("Result: %.10f %.10f (%.10f)\n", lats[0], lons[0], hts[0]); asfPrintStatus("Expected: %.10f %.10f\n", expected_lat, expected_lon); return FALSE; } }
int remove_file(const char *file) { if (is_dir(file)) { int ret = rmdir(file); if (ret < 0) { asfPrintWarning("Could not remove directory '%s': %s\n", file, strerror(errno)); } return ret; } else if (fileExists(file)) { int ret = unlink(file); if (ret < 0) { asfPrintWarning("Could not remove file '%s': %s\n", file, strerror(errno)); } return ret; } return 0; // success, I guess }
// Convert metadata to text // NOTE: inFile will either be a leader data file, a geotiff, // or an ASF metadata file void meta2text(char *inFile, FILE *outFP) { double ulLong=0.0, urLong=0.0, lrLong=0.0, llLong=0.0; // Clockwise polygon double ulLat=0.0, urLat=0.0, lrLat=0.0, llLat=0.0; int no_location_info=1; meta_parameters *meta = NULL; if (isgeotiff(inFile)) { int i, ignore[MAX_BANDS]; for (i=0; i<MAX_BANDS; i++) ignore[i] = 0; // Default to ignoring no bands meta = read_generic_geotiff_metadata(inFile, ignore, NULL); } else if (isleader(inFile)) { meta = meta_create(inFile); } else if (ismetadata(inFile)) { meta = meta_read(inFile); } if (meta && meta->location) { meta_location *ml = meta->location; // Convenience pointer no_location_info = 0; // false ...location info was found ulLong = ml->lon_start_near_range; ulLat = ml->lat_start_near_range; urLong = ml->lon_start_far_range; urLat = ml->lat_start_far_range; lrLong = ml->lon_end_far_range; lrLat = ml->lat_end_far_range; llLong = ml->lon_end_near_range; llLat = ml->lat_end_near_range; } meta_free(meta); if (no_location_info) asfPrintWarning("No location coordinates found in %s\n", inFile); fprintf(outFP, "# File type , polygon\n"); // Use inFile for name ...for lack of a better idea fprintf(outFP, "# Polygon ID (name), %s\n", inFile); fprintf(outFP, "#\n"); fprintf(outFP, "# Latitude, Longitude\n"); if (no_location_info) { fprintf(outFP, "# WARNING: No location information found in " "source file (%s)\n", inFile); fprintf(outFP, "# Values shown below are invalid\n"); } fprintf(outFP, "%f, %f\n", ulLat, ulLong); fprintf(outFP, "%f, %f\n", urLat, urLong); fprintf(outFP, "%f, %f\n", lrLat, lrLong); fprintf(outFP, "%f, %f\n", llLat, llLong); fprintf(outFP, "\n"); // FCLOSE() is called by the calling function return; }
static int latlon_getls(double *line, double *samp) { int ok = FALSE; meta_parameters *meta = curr->meta; if (meta->projection || (meta->sar&&meta->state_vectors) || meta->transform || meta->airsar) { double lat, lon; char *lat_str = get_string_from_entry("go_to_lat_entry"); if (strchr(lat_str, ',') == NULL) { lat = get_double_from_entry("go_to_lat_entry"); lon = get_double_from_entry("go_to_lon_entry"); } else { char *latlon = STRDUP(lat_str); char *comma = strchr(latlon, ','); *comma = '\0'; lat = atof(latlon); lon = atof(comma+1); // points to character after comma, longitude } if (lat < -90 || lat > 90) { asfPrintWarning("Illegal latitude value: %f\n", lat); } else if (lon < -360 || lon > 360) { asfPrintWarning("Illegal longitude value: %f\n", lon); } else { int bad = meta_get_lineSamp(curr->meta, lat, lon, 0, line, samp); ok = !bad; } } else { asfPrintWarning("No geolocation information available -- GoTo will only\n" "work for GoTo by Line/Sample.\n"); } return ok; }
static int line_samp_ok(double line, double samp) { if (!meta_is_valid_double(line) || !meta_is_valid_double(samp)) { asfPrintWarning("Invalid line/sample values: line %f, sample %f.\n", line, samp); return FALSE; } else { int nl = curr->meta->general->line_count; int ns = curr->meta->general->sample_count; if (line < 0 || line > nl || samp < 0 || samp > ns) { // we could actually plot the point if it isn't "too crazy" // we define "too crazy" as more than an image width/height out if (line < -nl || line > 2*nl || samp <-ns || samp > 2*ns) { asfPrintWarning("Point is outside the image: line %f, sample %f.\n" "Ignoring this point -- too far outside the image.\n", line, samp); return FALSE; } else { asfPrintWarning("Point is outside the image: line %f, sample %f.\n", line, samp); return TRUE; } } else { // normal case: inside the image return TRUE; } } // not reached assert(0); return FALSE; }
static char *get_arclist_from_settings(char *sensor) { if (strcmp_case(sensor, "ERS-1") == 0 || strcmp_case(sensor, "ERS-2") == 0) { char *settings_file = find_in_share("mapready_settings.cfg"); if (!settings_file) { asfPrintWarning("Could not find mapready_settings.cfg"); return NULL; } char match_str[256]; char *arclist = NULL; sprintf(match_str, "precise orbits %s", sensor); FILE *fSettings = FOPEN(settings_file, "r"); if (fSettings) { char line[1024]; while (fgets(line, 1024, fSettings) != NULL) { if (strncmp(line, match_str, strlen(match_str)) == 0) { arclist = read_str(line, match_str); if (strcmp_case(arclist, "<location of arclist file>") == 0) strcpy(arclist, ""); break; } } FCLOSE(fSettings); if (!arclist || strlen(arclist) == 0) { // not an error, user probably does not have precision state vectors asfPrintStatus("No precise orbits found in mapready_settings.cfg for %s\n", sensor); } FREE(settings_file); return arclist; } else { asfPrintError("Could not open mapready_settings.cfg"); return NULL; } } else { asfPrintError("Internal error, bad sensor: %s\n", sensor); return NULL; } // not reached return NULL; }
int open_uavsar_data(const char *filename, int multilook, meta_parameters *meta, ClientInterface *client) { ReadUavsarClientInfo *info = MALLOC(sizeof(ReadUavsarClientInfo)); info->ml = multilook; char *ext = findExt(filename); if (strcmp_case(ext, ".grd") == 0 || strcmp_case(ext, ".mlc") == 0) { // UAVSAR .grd/.mlc files are real for HHHH, HVHV, and VVVV info->is_complex = strstr(filename, "HHHV_") != NULL || strstr(filename, "HHVV_") != NULL || strstr(filename, "HVVV_") != NULL; } else if (strcmp_case(ext, ".hgt") == 0) { // UAVSAR .hgt files are real info->is_complex = FALSE; } asfPrintStatus("Reading UAVSAR data as %s\n", info->is_complex ? "complex" : "real"); if (info->is_complex) meta->general->data_type = COMPLEX_REAL32; info->fp = fopen(filename, "rb"); if (!info->fp) { asfPrintWarning("Failed to open UAVSAR data file %s: %s\n", filename, strerror(errno)); return FALSE; } client->read_client_info = info; client->read_fn = read_uavsar_client; client->thumb_fn = get_uavsar_thumbnail_data; client->free_fn = free_uavsar_client_info; if (meta->general->data_type == ASF_BYTE) client->data_type = GREYSCALE_BYTE; else client->data_type = GREYSCALE_FLOAT; return TRUE; }
static int proj_getls(double *line, double *samp) { meta_parameters *meta = curr->meta; if (meta->projection || (meta->sar&&meta->state_vectors) || meta->transform || meta->airsar) { double x = get_double_from_entry("go_to_projx_entry"); double y = get_double_from_entry("go_to_projy_entry"); double lat, lon; if (curr->meta->projection) { double h; proj_to_latlon(curr->meta->projection, x, y, 0, &lat, &lon, &h); if (lat==y && lon==x) { // this is usually indicative of failure... an error will have // been printed out already by libproj return FALSE; } lat *= R2D; lon *= R2D; } else { int zone = utm_zone(curr->meta->general->center_longitude); asfPrintStatus("Unprojected data -- assuming UTM (zone: %d)\n", zone); UTM2latLon(x, y, 0, zone, &lat, &lon); if (lat==y*R2D && lon==x*R2D) { // this is usually indicative of failure... an error will have // been printed out already by libproj return FALSE; } } int bad = meta_get_lineSamp(curr->meta, lat, lon, 0, line, samp); return !bad; } else { asfPrintWarning("No geolocation information available -- GoTo will only\n" "work for GoTo by Line/Sample.\n"); return FALSE; } }
const char *get_cal_band_name(meta_parameters *meta, char *base) { static char rad_name[32]; const char *rad; switch (meta->general->radiometry) { case r_AMP: rad = ""; break; case r_SIGMA: rad = "SIGMA-"; break; case r_BETA: rad = "BETA-"; break; case r_GAMMA: rad = "GAMMA-"; break; case r_SIGMA_DB: rad = "SIGMA_DB-"; break; case r_BETA_DB: rad = "BETA_DB-"; break; case r_GAMMA_DB: rad = "GAMMA_DB-"; break; case r_POWER: rad = "POWER-"; break; default: asfPrintWarning("Unexpected radiometry: %d\n", meta->general->radiometry); rad = ""; break; } if(!strcmp(meta->general->sensor, "UAVSAR")) // UAVSAR band names do not have the radiometry encoded in them strncpy(rad_name, base, 32); else sprintf(rad_name, "%s%s", rad, base); return rad_name; }
FILE * fopen_share_file(const char * filename, const char * mode) { char * full_name; const char * share_dir; FILE * fp; share_dir = get_asf_share_dir(); full_name = (char *) MALLOC (sizeof(char) * (strlen(filename) + strlen(share_dir) + 10)); sprintf(full_name, "%s%c%s", share_dir, DIR_SEPARATOR, filename); fp = fopen(full_name, mode); if (!fp) asfPrintWarning("Could not open share file: %s\n", filename); free(full_name); return fp; }
double gsl_spline_eval_check(gsl_spline *s, double x, gsl_interp_accel *a) { double x_orig = x; int len = s->size; if (x < s->x[0] || x > s->x[len-1]) { if (x < s->x[0]) { //asfPrintStatus("Spline extrapolation: %.12f < %.12f\n", // x, s->x[0]); x = s->x[0]; } else if (x > s->x[len-1]) { //asfPrintStatus("Spline extrapolation: %.12f > %.12f\n", // x, s->x[len-1]); x = s->x[len-1]; } if (fabs(x_orig - x) > .001) { asfPrintWarning("Attempt to extrapolate with spline: %.10f (%.10f, %.10f)\n", x_orig, s->x[0], s->x[len-1]); } } return gsl_spline_eval(s, x, a); }
void create_cal_params(const char *inSAR, meta_parameters *meta, report_level_t level) { int ii, kk; struct dataset_sum_rec dssr; // Data set summary record double *noise; char sarName[512], *facilityStr, *processorStr, *error; ceos_description *ceos; meta->calibration = meta_calibration_init(); strcpy (sarName, inSAR); if (isCEOS(sarName, &error)) { // Check for the various processors get_dssr(sarName, &dssr); facilityStr = trim_spaces(dssr.fac_id); processorStr = trim_spaces(dssr.sys_id); ceos = get_ceos_description_ext(inSAR, REPORT_LEVEL_NONE, FALSE); if (strncmp(facilityStr , "ASF" , 3) == 0 && strncmp(processorStr, "FOCUS", 5) != 0 && ((meta->projection && meta->projection->type == SCANSAR_PROJECTION) || (meta->projection && (strcmp_case(meta->general->mode, "SWA") == 0 || strcmp_case(meta->general->mode, "SWB") == 0)) ) ) { // ASF internal processor (PP or SSP), ScanSar data asf_scansar_cal_params *asf = MALLOC(sizeof(asf_scansar_cal_params)); meta->calibration->type = asf_scansar_cal; meta->calibration->asf_scansar = asf; // Get values for calibration coefficients and LUT struct VRADDR rdr; // Radiometric data record get_raddr(sarName, &rdr); // hardcodings for not-yet-calibrated fields if (rdr.a[0] == -99.0 || rdr.a[1]==0.0 ) { asf->a0 = 1.1E4; asf->a1 = 2.2E-5; asf->a2 = 0.0; } else { asf->a0 = rdr.a[0]; asf->a1 = rdr.a[1]; asf->a2 = rdr.a[2]; } // Set the Noise Correction Vector to correct version if (strncmp(dssr.cal_params_file,"SSPSWB010.CALPARMS",18)==0) { asfPrintStatus("\n Substituting hardcoded noise vector sspswb010\n"); noise = sspswb010_noise_vec; } else if (strncmp(dssr.cal_params_file,"SSPSWB011.CALPARMS",18)==0) { asfPrintStatus("\n Substituting hardcoded noise vector sspswb011\n"); noise = sspswb011_noise_vec; } else if (strncmp(dssr.cal_params_file,"SSPSWB013.CALPARMS",18)==0) { asfPrintStatus("\n Substituting hardcoded noise vector sspswb013\n"); noise = sspswb013_noise_vec; } else if (strncmp(dssr.cal_params_file,"SSPSWB014.CALPARMS",18)==0) { asfPrintStatus("\n Substituting hardcoded noise vector sspswb014\n"); noise = sspswb014_noise_vec; } else if (strncmp(dssr.cal_params_file,"SSPSWB015.CALPARMS",18)==0) { asfPrintStatus("\n Substituting hardcoded noise vector sspswb015\n"); noise = sspswb015_noise_vec; } else if (strncmp(dssr.cal_params_file,"SSPSWB016.CALPARMS",18)==0) { asfPrintStatus("\n Substituting hardcoded noise vector sspswb016\n"); noise = sspswb015_noise_vec; // 16 and 15 were identical antenna patterns, only metadata fields were // changed, so the noise vector for 16 is the same and that for 15. JBN } else noise = rdr.noise; for (kk=0; kk<256; ++kk) asf->noise[kk] = noise[kk]; } else if (strncmp(facilityStr, "ASF", 3)== 0 && strncmp(processorStr, "FOCUS", 5) != 0) { // ASF internal processor (PP or SSP) (non-Scansar) asf_cal_params *asf = (asf_cal_params *) MALLOC(sizeof(asf_cal_params)); meta->calibration->type = asf_cal; meta->calibration->asf = asf; // Get values for calibration coefficients and LUT struct VRADDR rdr; // Radiometric data record get_raddr(sarName, &rdr); // hardcodings for not-yet-calibrated fields if (rdr.a[0] == -99.0 || rdr.a[1]==0.0 ) { asf->a0 = 1.1E4; asf->a1 = 2.2E-5; asf->a2 = 0.0; } else { asf->a0 = rdr.a[0]; asf->a1 = rdr.a[1]; asf->a2 = rdr.a[2]; } if (ceos->product == SLC && ceos->sensor == ERS && meta->general->radiometry > r_AMP && meta->general->radiometry < r_POWER) { asfPrintStatus("Applying calibration adjustment of x1.3 for SLC data." "\n"); asf->a1 *= 1.3; } // grab the noise vector for (kk=0; kk<256; ++kk) asf->noise[kk] = rdr.noise[kk]; asf->sample_count = meta->general->sample_count; } else if ((strncmp(facilityStr, "ASF", 3) == 0 && strncmp(dssr.sys_id, "FOCUS", 5) == 0) || (strncmp(facilityStr, "CDPF", 4) == 0 || strncmp(facilityStr, "RSI", 3) == 0 || ((strncmp(facilityStr, "CSTARS", 6) == 0 || strncmp(facilityStr, "TRNS", 4) == 0) && strncmp(dssr.mission_id, "RSAT", 4) == 0))) { // Radarsat style calibration rsat_cal_params *rsat = (rsat_cal_params *) MALLOC(sizeof(rsat_cal_params)); meta->calibration->type = rsat_cal; meta->calibration->rsat = rsat; rsat->slc = FALSE; rsat->focus = FALSE; if (strncmp(dssr.product_type, "SLANT RANGE COMPLEX", 19) == 0 || strncmp(dssr.product_type, "SPECIAL PRODUCT(SINGL-LOOK COMP)", 32) == 0) { rsat->slc = TRUE; } if (strncmp(dssr.sys_id, "FOCUS", 5) == 0) rsat->focus = TRUE; // Read lookup up table from radiometric data record struct RSI_VRADDR radr; get_rsi_raddr(sarName, &radr); rsat->n = radr.n_samp; //rsat->lut = (double *) MALLOC(sizeof(double) * rsat->n); for (ii=0; ii<rsat->n; ii++) { if (strncmp(dssr.sys_id, "FOCUS", 5) == 0) rsat->lut[ii] = radr.lookup_tab[0]; else rsat->lut[ii] = radr.lookup_tab[ii]; } rsat->samp_inc = radr.samp_inc; rsat->a3 = radr.offset; } else if (strncmp(facilityStr, "ES", 2) == 0 || strncmp(facilityStr, "D-PAF", 5) == 0 || strncmp(facilityStr, "I-PAF", 2) == 0 || strncmp(facilityStr, "Beijing", 7) == 0 || (strncmp(facilityStr, "CSTARS", 6) == 0 && (strncmp(dssr.mission_id, "E", 1) == 0 || strncmp(dssr.mission_id, "J", 1) == 0)) ) { // ESA style calibration esa_cal_params *esa = (esa_cal_params *) MALLOC(sizeof(esa_cal_params)); meta->calibration->type = esa_cal; meta->calibration->esa = esa; // Read calibration coefficient and reference incidence angle struct ESA_FACDR facdr; get_esa_facdr(sarName, &facdr); esa->k = facdr.abs_cal_const; esa->ref_incid = dssr.incident_ang; } else if (strncmp(facilityStr, "EOC", 3) == 0) { // ALOS processor struct alos_rad_data_rec ardr; // ALOS Radiometric Data record alos_cal_params *alos = (alos_cal_params *) MALLOC(sizeof(alos_cal_params)); meta->calibration->type = alos_cal; meta->calibration->alos = alos; // Determine beam mode int beam = dssr.ant_beam_num; int beam_count = dssr.nchn; if (beam >= 0 && beam <= 35 && beam_count == 2) beam += 36; // actually dual-pol data (HH+HV or VV+VH) else if (beam == 3 && beam_count == 4) beam = 127; // actually PLR 21.5 else if (beam_count == 1 && dssr.product_id[3] == 'S') { // some fine backwards engineering here to figure out the correct beam // number - off nadir angle and processing bandwidth required // don't care for the HH versus VV at the moment if (dssr.off_nadir_angle < 25.0) { if (dssr.bnd_rng < 20000.0) beam = 72; // WB1 HH3scan else beam = 73; // WB2 HH3scan } else if (dssr.off_nadir_angle > 25.0 && dssr.off_nadir_angle < 26.0) { if (dssr.off_nadir_angle < 25.0) { if (dssr.bnd_rng < 20000.0) beam = 76; // WB1 HH4scan else beam = 77; // WB2 HH4scan } } else { if (dssr.bnd_rng < 20000.0) beam = 80; // WB1 HH5scan else beam = 81; // WB2 HH5scan } } // Reading calibration coefficient get_ardr(sarName, &ardr); if (strncmp(dssr.lev_code, "1.1", 3) == 0) { // SLC // HH polarization if ((beam >= 0 && beam <= 17) || // FBS HH polarization (beam >= 36 && beam <= 53) || // FBD HH+HV polarization (beam >= 72 && beam <= 73) || // WB HH3scan (beam >= 76 && beam <= 77) || // WB HH4scan (beam >= 80 && beam <= 81) || // WB HH5scan (beam >= 84 && beam <= 101) || // DSN HH polarization (beam >= 120 && beam <= 131)) // PLR alos->cf_hh = ardr.calibration_factor - 32; else alos->cf_hh = MAGIC_UNSET_DOUBLE; // HV polarization if ((beam >= 36 && beam <= 53) || // FBD HH+HV polarization (beam >= 120 && beam <= 131)) // PLR alos->cf_hv = ardr.calibration_factor - 32; else alos->cf_hv = MAGIC_UNSET_DOUBLE; // VH polarization if ((beam >= 54 && beam <= 71) || // FBD VV+VH polarization (beam >= 120 && beam <= 131)) // PLR alos->cf_vh = ardr.calibration_factor - 32; else alos->cf_vh = MAGIC_UNSET_DOUBLE; // VV polarization if ((beam >= 18 && beam <= 35) || // FBS VV polarization (beam >= 54 && beam <= 71) || // FBD VV+VH polarization (beam >= 74 && beam <= 75) || // WB VV3scan (beam >= 78 && beam <= 79) || // WB VV4scan (beam >= 82 && beam <= 83) || // WB VV5scan (beam >= 102 && beam <= 119) || // DSN VV polarization (beam >= 120 && beam <= 131)) // PLR alos->cf_vv = ardr.calibration_factor - 32; else alos->cf_vv = MAGIC_UNSET_DOUBLE; } else if (strncmp(dssr.lev_code, "1.5", 3) == 0) { // regular detected // HH polarization if ((beam >= 0 && beam <= 17) || // FBS HH polarization (beam >= 36 && beam <= 53) || // FBD HH+HV polarization (beam >= 72 && beam <= 73) || // WB HH3scan (beam >= 76 && beam <= 77) || // WB HH4scan (beam >= 80 && beam <= 81) || // WB HH5scan (beam >= 84 && beam <= 101) || // DSN HH polarization (beam >= 120 && beam <= 131)) // PLR alos->cf_hh = ardr.calibration_factor; else alos->cf_hh = MAGIC_UNSET_DOUBLE; // HV polarization if ((beam >= 36 && beam <= 53) || // FBD HH+HV polarization (beam >= 120 && beam <= 131)) // PLR alos->cf_hv = ardr.calibration_factor; else alos->cf_hv = MAGIC_UNSET_DOUBLE; // VH polarization if ((beam >= 54 && beam <= 71) || // FBD VV+VH polarization (beam >= 120 && beam <= 131)) // PLR alos->cf_vh = ardr.calibration_factor; else alos->cf_vh = MAGIC_UNSET_DOUBLE; // VV polarization if ((beam >= 18 && beam <= 35) || // FBS VV polarization (beam >= 54 && beam <= 71) || // FBD VV+VH polarization (beam >= 74 && beam <= 75) || // WB VV3scan (beam >= 78 && beam <= 79) || // WB VV4scan (beam >= 82 && beam <= 83) || // WB VV5scan (beam >= 102 && beam <= 119) || // DSN VV polarization (beam >= 120 && beam <= 131)) // PLR alos->cf_vv = ardr.calibration_factor; else alos->cf_vv = MAGIC_UNSET_DOUBLE; } // Check on processor version // Prior to processor version 9.02 some calibration parameters have been // determined again. double version; if (!get_alos_processor_version(inSAR, &version)) asfReport(level, "Could not find workreport file!\n" "Calibration parameters applied to the data might be " "inaccurate!\n"); else if (version < 9.02) { char str[512]; switch (beam) { case 0: alos->cf_hh = -83.16; // FBS 9.9 HH sprintf(str, "HH: %.2lf\n", alos->cf_hh); recalibration(str, level); if (strncmp(dssr.lev_code, "1.1", 3) == 0) alos->cf_hh -= 32; break; case 3: alos->cf_hh = -83.55; // FBS 21.5 HH sprintf(str, "HH: %.2lf\n", alos->cf_hh); recalibration(str, level); if (strncmp(dssr.lev_code, "1.1", 3) == 0) alos->cf_hh -= 32; break; case 7: alos->cf_hh = -83.40; // FBS 34.3 HH sprintf(str, "HH: %.2lf\n", alos->cf_hh); recalibration(str, level); if (strncmp(dssr.lev_code, "1.1", 3) == 0) alos->cf_hh -= 32; break; case 10: alos->cf_hh = -83.65; // FBS 41.5 HH sprintf(str, "HH: %.2lf\n", alos->cf_hh); recalibration(str, level); if (strncmp(dssr.lev_code, "1.1", 3) == 0) alos->cf_hh -= 32; break; case 35: alos->cf_hh = -83.30; // FBS 50.8 HH sprintf(str, "HH: %.2lf\n", alos->cf_hh); recalibration(str, level); if (strncmp(dssr.lev_code, "1.1", 3) == 0) alos->cf_hh -= 32; break; case 43: alos->cf_hh = -83.20; // FBD 34.3 HH alos->cf_hv = -80.20; // FBD 34.3 HV sprintf(str, "HH: %.2lf\nHV: %.2lf\n", alos->cf_hh, alos->cf_hv); recalibration(str, level); if (strncmp(dssr.lev_code, "1.1", 3) == 0) { alos->cf_hh -= 32; alos->cf_hv -= 32; } break; case 46: alos->cf_hh = -83.19; // FBD 41.5 HH alos->cf_hv = -80.19; // FBD 41.5 HV sprintf(str, "HH: %.2lf\nHV: %.2lf\n", alos->cf_hh, alos->cf_hv); recalibration(str, level); if (strncmp(dssr.lev_code, "1.1", 3) == 0) { alos->cf_hh -= 32; alos->cf_hv -= 32; } break; case 127: alos->cf_hh = -83.40; // PLR 21.5 HH alos->cf_hv = -83.40; // PLR 21.5 HV alos->cf_vh = -83.40; // PLR 21.5 VH alos->cf_vv = -83.40; // PLR 21.5 VV sprintf(str, "HH: %.2lf\nHV: %.2lf\nVH: %.2lf\nVV: %.2lf", alos->cf_hh, alos->cf_hv, alos->cf_vh, alos->cf_vv); recalibration(str, level); if (strncmp(dssr.lev_code, "1.1", 3) == 0) { alos->cf_hh -= 32; alos->cf_hv -= 32; alos->cf_vh -= 32; alos->cf_vv -= 32; } break; } } } } else if (isTerrasar(sarName, &error)) { // TSX style calibration tsx_cal_params *tsx = (tsx_cal_params *) MALLOC(sizeof(tsx_cal_params)); meta->calibration->type = tsx_cal; meta->calibration->tsx = tsx; terrasar_meta *terrasar = read_terrasar_meta(sarName); tsx->k = terrasar->cal_factor; // calibration factor in beta naught FREE(terrasar); } else // should never get here asfPrintWarning("Unknown calibration parameter scheme!\n"); }
uavsar_insar * read_uavsar_insar_params(const char *dataFile, uavsar_type_t type) { uavsar_insar *params = (uavsar_insar *) MALLOC(sizeof(uavsar_insar)); char time1[50]="", time2[50]=""; // Determine ID char *dirName = (char *) MALLOC(sizeof(char) * 1024); char *fileName = (char *) MALLOC(sizeof(char) * 1024); split_dir_and_file(dataFile, dirName, fileName); sprintf(params->id, "%s", stripExt(fileName)); // Read annotation file char line[MAX_LINE], key[MAX_LINE], value[MAX_LINE]; FILE *fp = FOPEN(dataFile, "r"); while (fgets(line, MAX_LINE, fp)) { if(!parse_annotation_line(line, key, value)) { asfPrintWarning("Unable to parse line in annotation file: %s", line); continue; } if(!strcmp(key, "")) continue; if (!strcmp(key, "Site Description")) strcpy(params->site, value); if (!strcmp(key, "Processing Mode")) strcpy(params->processing_mode, value); if (!strcmp(key, "Polarization")) strcpy(params->polarization, value); if (!strcmp(key, "Number of Looks in Range")) params->range_look_count = atoi(value); if (!strcmp(key, "Number of Looks in Azimuth")) params->azimuth_look_count = atoi(value); if (type == INSAR_INT) { params->type = INSAR_INT; if (!strcmp(key, "Interferogram Bytes Per Pixel")) params->bytes_per_pixel = atoi(value); else if (!strcmp(key, "Interferogram Pixel Format")) strcpy(params->value_format, value); else if (!strcmp(key, "Interferogram Units")) strcpy(params->data_units, value); } else if (type == INSAR_UNW) { params->type = INSAR_UNW; if (!strcmp(key, "Unwrapped Phase Bytes Per Pixel")) params->bytes_per_pixel = atoi(value); else if (!strcmp(key, "Unwrapped Phase Pixel Format")) strcpy(params->value_format, value); else if (!strcmp(key, "Unwrapped Phase Units")) strcpy(params->data_units, value); } else if (type == INSAR_COR) { params->type = INSAR_COR; if (!strcmp(key, "Correlation Bytes Per Pixel")) params->bytes_per_pixel = atoi(value); else if (!strcmp(key, "Correlation Pixel Format")) strcpy(params->value_format, value); else if (!strcmp(key, "Correlation Units")) strcpy(params->data_units, value); } else if (type == INSAR_AMP) { params->type = INSAR_AMP; if (!strcmp(key, "Amplitude Bytes Per Pixel")) params->bytes_per_pixel = atoi(value); else if (!strcmp(key, "Amplitude Pixel Format")) strcpy(params->value_format, value); else if (!strcmp(key, "Amplitude Units")) strcpy(params->data_units, value); } if (type >= INSAR_AMP && type <= INSAR_COR) { if (!strcmp(key, "Slant Range Data Azimuth Lines")) params->row_count = atoi(value); else if (!strcmp(key, "Slant Range Data Range Samples")) params->column_count = atoi(value); else if (!strcmp(key, "slt.set_proj")) strcpy(params->projection, value); else if (!strcmp(key, "slt.row_addr")) params->along_track_offset = atof(value); else if (!strcmp(key, "slt.col_addr")) params->cross_track_offset = atof(value); else if (!strcmp(key, "Slant Range Data at Near Range")) { params->slant_range_first_pixel = atof(value); params->slant_range_first_pixel /= 1000.0; } else if (!strcmp(key, "Slant Range Data Azimuth Spacing")) params->azimuth_pixel_spacing = atof(value); else if (!strcmp(key, "Slant Range Data Range Spacing")) params->range_pixel_spacing = atof(value); } else if (type >= INSAR_AMP_GRD && type <= INSAR_HGT_GRD) { if (!strcmp(key, "Ground Range Data Latitude Lines")) params->row_count = atoi(value); else if (!strcmp(key, "Ground Range Data Longitude Samples")) params->column_count = atoi(value); else if (!strcmp(key, "grd.set_proj")) strcpy(params->projection, value); else if (!strcmp(key, "grd.row_addr")) params->along_track_offset = atof(value); else if (!strcmp(key, "grd.col_addr")) params->cross_track_offset = atof(value); else if (!strcmp(key, "Ground Range Data Latitude Spacing")) params->azimuth_pixel_spacing = atof(value); else if (!strcmp(key, "Ground Range Data Longitude Spacing")) params->range_pixel_spacing = atof(value); } if (type == INSAR_INT_GRD) { params->type = INSAR_INT_GRD; if (!strcmp(key, "Interferogram Bytes Per Pixel")) params->bytes_per_pixel = atoi(value); else if (!strcmp(key, "Interferogram Pixel Format")) strcpy(params->value_format, value); else if (!strcmp(key, "Interferogram Units")) strcpy(params->data_units, value); } else if (type == INSAR_UNW_GRD) { params->type = INSAR_UNW_GRD; if (!strcmp(key, "Unwrapped Phase Bytes Per Pixel")) params->bytes_per_pixel = atoi(value); else if (!strcmp(key, "Unwrapped Phase Pixel Format")) strcpy(params->value_format, value); else if (!strcmp(key, "Unwrapped Phase Units")) strcpy(params->data_units, value); } else if (type == INSAR_COR_GRD) { params->type = INSAR_COR_GRD; if (!strcmp(key, "Correlation Bytes Per Pixel")) params->bytes_per_pixel = atoi(value); else if (!strcmp(key, "Correlation Pixel Format")) strcpy(params->value_format, value); else if (!strcmp(key, "Correlation Units")) strcpy(params->data_units, value); } else if (type == INSAR_AMP_GRD) { params->type = INSAR_AMP_GRD; if (!strcmp(key, "Amplitude Bytes Per Pixel")) params->bytes_per_pixel = atoi(value); else if (!strcmp(key, "Amplitude Pixel Format")) strcpy(params->value_format, value); else if (!strcmp(key, "Amplitude Units")) strcpy(params->data_units, value); } else if (type == INSAR_HGT_GRD) { params->type = INSAR_HGT_GRD; if (!strcmp(key, "DEM Bytes Per Pixel")) params->bytes_per_pixel = atoi(value); else if (!strcmp(key, "DEM Pixel Format")) strcpy(params->value_format, value); else if (!strcmp(key, "DEM Units")) strcpy(params->data_units, value); } if (!strcmp(key, "set_hddr")) params->header_bytes = atoi(value); else if (!strcmp(key, "set_tail")) params->tail_bytes = atoi(value); else if (!strcmp(key, "set_plat")) params->lat_peg_point = atof(value); else if (!strcmp(key, "set_plon")) params->lon_peg_point = atof(value); else if (!strcmp(key, "set_phdg")) params->head_peg_point = atof(value); else if (!strcmp(key, "val_endi")) strcpy(params->endianess, value); else if (!strcmp(key, "val_mult")) params->data_scale = atof(value); else if (!strcmp(key, "val_addr")) params->data_shift = atof(value); else if (!strcmp(key, "val_minv")) params->min_value = atof(value); else if (!strcmp(key, "val_maxv")) params->max_value = atof(value); else if (!strcmp(key, "Center Wavelength")) params->wavelength = atof(value); else if (!strcmp(key, "Ellipsoid Semi-major Axis")) params->semi_major = atof(value); else if (!strcmp(key, "Ellipsoid Eccentricity Squared")) params->eccentricity = atof(value); else if (!strcmp(key, "Look Direction")) strcpy(params->look_direction, value); else if (!strcmp(key, "Range Spacing per Bin")) params->range_spacing = atof(value); else if (!strcmp(key, "Azimuth Spacing")) params->azimuth_spacing = atof(value); else if (!strcmp(key, "Image Starting Range")) params->slant_range_first_pixel = atof(value); else if (!strcmp(key, "Global Average Yaw")) params->yaw = atof(value); else if (!strcmp(key, "Global Average Pitch")) params->pitch = atof(value); else if (!strcmp(key, "Global Average Roll")) params->roll = atof(value); else if (!strcmp(key, "Global Average ESA")) params->steering_angle = atof(value); else if (!strcmp(key, "Global Average Altitude")) params->altitude = atof(value); else if (!strcmp(key, "Average GPS Altitude")) params->altitude = atof(value); else if (!strcmp(key, "Global Average Terrain Height")) params->terrain_height = atof(value); else if (!strcmp(key, "Global Average Squint Angle")) params->squint_angle = atof(value); else if (!strcmp(key, "Pulse Length")) params->pulse_length = atof(value); else if (!strcmp(key, "Bandwidth")) params->bandwidth = atof(value); else if (!strcmp(key, "Approximate Upper Left Latitude")) { if (strcmp_case(value, "N/A") == 0) params->lat_upper_left = MAGIC_UNSET_DOUBLE; else params->lat_upper_left = atof(value); } else if (!strcmp(key, "Approximate Upper Left Longitude")) { if (strcmp_case(value, "N/A") == 0) params->lon_upper_left = MAGIC_UNSET_DOUBLE; else params->lon_upper_left = atof(value); } else if (!strcmp(key, "Approximate Upper Right Latitude")) { if (strcmp_case(value, "N/A") == 0) params->lat_upper_right = MAGIC_UNSET_DOUBLE; else params->lat_upper_right = atof(value); } else if (!strcmp(key, "Approximate Upper Right Longitude")) { if (strcmp_case(value, "N/A") == 0) params->lon_upper_right = MAGIC_UNSET_DOUBLE; else params->lon_upper_right = atof(value); } else if (!strcmp(key, "Approximate Lower Left Latitude")) { if (strcmp_case(value, "N/A") == 0) params->lat_lower_left = MAGIC_UNSET_DOUBLE; else params->lat_lower_left = atof(value); } else if (!strcmp(key, "Approximate Lower Left Longitude")) { if (strcmp_case(value, "N/A") == 0) params->lon_lower_left = MAGIC_UNSET_DOUBLE; else params->lon_lower_left = atof(value); } else if (!strcmp(key, "Approximate Lower Right Latitude")) { if (strcmp_case(value, "N/A") == 0) params->lat_lower_right = MAGIC_UNSET_DOUBLE; else params->lat_lower_right = atof(value); } else if (!strcmp(key, "Approximate Lower Right Longitude")) { if (strcmp_case(value, "N/A") == 0) params->lon_lower_right = MAGIC_UNSET_DOUBLE; else params->lon_lower_right = atof(value); } else if (!strcmp(key, "Time of Acquisition for Pass 1")) strcpy(time1, value); else if (!strcmp(key, "Time of Acquisition for Pass 2")) strcpy(time2, value); else if (!strcmp(key, "Processor Version Number")) strcpy(params->processor, value); } FCLOSE(fp); sprintf(params->acquisition_date, "%s, %s", time1, time2); return params; }
static void test_proj_direct(const char *input_projection_info, const char *proj_name, double projX, double projY, double height, double expected_lat, double expected_lon, const char *datum) { double x[1]; double y[1]; double hts[1]; char output_projection_info[255]; sprintf(output_projection_info, "+proj=latlong +datum=WGS84"); projPJ input_projection, output_projection; input_projection = pj_init_plus(input_projection_info); output_projection = pj_init_plus(output_projection_info); y[0] = projX; x[0] = projY; hts[0] = height; pj_transform (input_projection, output_projection, 1, 1, x, y, hts); if (pj_errno != 0) { asfPrintWarning("libproj error: %s\n", pj_strerrno(pj_errno)); } x[0] *= R2D; y[0] *= R2D; CU_ASSERT(double_equals(x[0], expected_lat, 6)); CU_ASSERT(double_equals(y[0], expected_lon, 6)); if (double_equals(x[0], expected_lat, 6) && double_equals(y[0], expected_lon, 6)) { //asfPrintStatus("%s %s (fwd): %f, %f ... OK\n", // proj_name, datum, projX, projY); ++n_ok; } else { asfPrintStatus("%s (fwd): %s %f, %f ... ERROR\n", proj_name, datum, projX, projY); asfPrintStatus("Result: %.10f %.10f (%.10f)\n", x[0], y[0], hts[0]); asfPrintStatus("Expected: %.10f %.10f\n", expected_lat, expected_lon); ++n_bad; } // now project the other way y[0] = expected_lon*D2R; x[0] = expected_lat*D2R; hts[0] = height; pj_transform(output_projection, input_projection, 1, 1, x, y, hts); if (pj_errno != 0) { asfPrintWarning("libproj error: %s\n", pj_strerrno(pj_errno)); } pj_free(input_projection); pj_free(output_projection); CU_ASSERT(double_equals(y[0], projX, 5)); CU_ASSERT(double_equals(x[0], projY, 5)); if (double_equals(y[0], projX, 5) && double_equals(x[0], projY, 5)) { //asfPrintStatus("%s %s (rev): %f, %f ... OK\n", // proj_name, datum, expected_lon, expected_lat); ++n_ok; } else { asfPrintStatus("%s (rev): %s %f, %f ... ERROR\n", proj_name, datum, expected_lon, expected_lat); asfPrintStatus("Result: %.10f %.10f (%.10f)\n", y[0], x[0], hts[0]); asfPrintStatus("Expected: %.10f %.10f\n", projX, projY); ++n_bad; } }
uavsar_polsar * read_uavsar_polsar_params(const char *dataFile, uavsar_type_t type) { uavsar_polsar *params = (uavsar_polsar *) MALLOC(sizeof(uavsar_polsar)); // Determine ID char *dirName = (char *) MALLOC(sizeof(char) * 1024); char *fileName = (char *) MALLOC(sizeof(char) * 1024); split_dir_and_file(dataFile, dirName, fileName); sprintf(params->id, "%s", stripExt(fileName)); // Read annotation file char line[MAX_LINE], key[MAX_LINE], value[MAX_LINE]; FILE *fp = FOPEN(dataFile, "r"); while (fgets(line, MAX_LINE, fp)) { if(!parse_annotation_line(line, key, value)) { asfPrintWarning("Unable to parse line in annotation file: %s", line); continue; } if (!strcmp(key, "")) continue; if (!strcmp(key, "Site Description")) strcpy(params->site, value); if (!strcmp(key, "Acquisition Mode")) strcpy(params->acquisition_mode, value); if (type == POLSAR_SLC) { params->type = POLSAR_SLC; if (!strcmp(key, "slc_mag.set_rows")) params->row_count = atoi(value); else if (!strcmp(key, "slc_mag.set_cols")) params->column_count = atoi(value); else if (!strcmp(key, "slc_mag.set_proj")) strcpy(params->projection, value); else if (!strcmp(key, "slc_mag.row_addr")) params->along_track_offset = atof(value); else if (!strcmp(key, "slc_mag.col_addr")) params->cross_track_offset = atof(value); else if (!strcmp(key, "slc_mag.row_mult")) params->azimuth_pixel_spacing = atof(value); else if (!strcmp(key, "slc_mag.col_mult")) params->range_pixel_spacing = atof(value); else if (!strcmp(key, "slc_mag.val_size")) params->bytes_per_pixel = atoi(value); else if (!strcmp(key, "slc_mag.val_frmt")) strcpy(params->value_format, value); params->range_look_count = 1; params->azimuth_look_count = 1; if (!strcmp(key, "SLC Data Units")) strcpy(params->data_units, value); } else if (type == POLSAR_MLC) { params->type = POLSAR_MLC; if (!strcmp(key, "mlc_pwr.set_rows")) params->row_count = atoi(value); else if (!strcmp(key, "mlc_pwr.set_cols")) params->column_count = atoi(value); else if (!strcmp(key, "mlc_pwr.set_proj")) strcpy(params->projection, value); else if (!strcmp(key, "mlc_pwr.row_addr")) params->along_track_offset = atof(value); else if (!strcmp(key, "mlc_pwr.col_addr")) params->cross_track_offset = atof(value); else if (!strcmp(key, "mlc_pwr.row_mult")) params->azimuth_pixel_spacing = atof(value); else if (!strcmp(key, "mlc_pwr.col_mult")) params->range_pixel_spacing = atof(value); else if (!strcmp(key, "mlc_pwr.val_size")) params->bytes_per_pixel = atoi(value); else if (!strcmp(key, "mlc_pwr.val_frmt")) strcpy(params->value_format, value); else if (!strcmp(key, "Number of Range Looks in MLC")) params->range_look_count = atoi(value); else if (!strcmp(key, "Number of Azimuth Looks in MLC")) params->azimuth_look_count = atoi(value); else if (!strcmp(key, "MLC Data Units")) strcpy(params->data_units, value); params->range_look_count = 1; params->azimuth_look_count = 1; } else if (type == POLSAR_DAT) { params->type = POLSAR_DAT; if (!strcmp(key, "dat.set_rows")) params->row_count = atoi(value); else if (!strcmp(key, "dat.set_cols")) params->column_count = atoi(value); else if (!strcmp(key, "dat.set_proj")) strcpy(params->projection, value); else if (!strcmp(key, "dat.row_addr")) params->along_track_offset = atof(value); else if (!strcmp(key, "dat.col_addr")) params->cross_track_offset = atof(value); else if (!strcmp(key, "dat.row_mult")) params->azimuth_pixel_spacing = atof(value); else if (!strcmp(key, "dat.col_mult")) params->range_pixel_spacing = atof(value); else if (!strcmp(key, "dat.val_size")) params->bytes_per_pixel = atoi(value); else if (!strcmp(key, "dat.val_frmt")) strcpy(params->value_format, value); params->range_look_count = 1; params->azimuth_look_count = 1; strcpy(params->data_units, MAGIC_UNSET_STRING); } else if (type == POLSAR_GRD) { params->type = POLSAR_GRD; if (!strcmp(key, "grd_pwr.set_rows")) params->row_count = atoi(value); else if (!strcmp(key, "grd_pwr.set_cols")) params->column_count = atoi(value); else if (!strcmp(key, "grd_pwr.set_proj")) strcpy(params->projection, value); else if (!strcmp(key, "grd_pwr.row_addr")) params->along_track_offset = atof(value); else if (!strcmp(key, "grd_pwr.col_addr")) params->cross_track_offset = atof(value); else if (!strcmp(key, "grd_pwr.row_mult")) params->azimuth_pixel_spacing = atof(value); else if (!strcmp(key, "grd_pwr.col_mult")) params->range_pixel_spacing = atof(value); else if (!strcmp(key, "grd_pwr.val_size")) params->bytes_per_pixel = atoi(value); else if (!strcmp(key, "grd_pwr.val_frmt")) strcpy(params->value_format, value); else if (!strcmp(key, "Number of Range Looks in MLC")) params->range_look_count = atoi(value); else if (!strcmp(key, "Number of Azimuth Looks in MLC")) params->azimuth_look_count = atoi(value); else if (!strcmp(key, "GRD Data Units")) strcpy(params->data_units, value); } else if (type == POLSAR_HGT) { params->type = POLSAR_HGT; if (!strcmp(key, "hgt.set_rows")) params->row_count = atoi(value); else if (!strcmp(key, "hgt.set_cols")) params->column_count = atoi(value); else if (!strcmp(key, "hgt.set_proj")) strcpy(params->projection, value); else if (!strcmp(key, "hgt.row_addr")) params->along_track_offset = atof(value); else if (!strcmp(key, "hgt.col_addr")) params->cross_track_offset = atof(value); else if (!strcmp(key, "hgt.row_mult")) params->azimuth_pixel_spacing = atof(value); else if (!strcmp(key, "hgt.col_mult")) params->range_pixel_spacing = atof(value); else if (!strcmp(key, "hgt.val_size")) params->bytes_per_pixel = atoi(value); else if (!strcmp(key, "hgt.val_frmt")) strcpy(params->value_format, value); params->range_look_count = 1; params->azimuth_look_count = 1; if (!strcmp(key, "HGT Data Units")) strcpy(params->data_units, value); } if (!strcmp(key, "set_hddr")) params->header_bytes = atoi(value); else if (!strcmp(key, "set_tail")) params->tail_bytes = atoi(value); else if (!strcmp(key, "set_plat")) params->lat_peg_point = atof(value); else if (!strcmp(key, "set_plon")) params->lon_peg_point = atof(value); else if (!strcmp(key, "set_phdg")) params->head_peg_point = atof(value); else if (!strcmp(key, "val_endi")) strcpy(params->endianess, value); else if (!strcmp(key, "val_mult")) params->data_scale = atof(value); else if (!strcmp(key, "val_addr")) params->data_shift = atof(value); else if (!strcmp(key, "val_minv")) params->min_value = atof(value); else if (!strcmp(key, "val_maxv")) params->max_value = atof(value); else if (!strcmp(key, "Center Wavelength")) params->wavelength = atof(value); else if (!strcmp(key, "Ellipsoid Semi-major Axis")) params->semi_major = atof(value); else if (!strcmp(key, "Ellipsoid Eccentricity Squared")) params->eccentricity = atof(value); else if (!strcmp(key, "Look Direction")) strcpy(params->look_direction, value); else if (!strcmp(key, "Range Spacing per Bin")) params->range_spacing = atof(value); else if (!strcmp(key, "Azimuth Spacing")) params->azimuth_spacing = atof(value); else if (!strcmp(key, "Image Starting Range")) params->slant_range_first_pixel = atof(value); else if (!strcmp(key, "Global Average Yaw")) params->yaw = atof(value); else if (!strcmp(key, "Global Average Pitch")) params->pitch = atof(value); else if (!strcmp(key, "Global Average Roll")) params->roll = atof(value); else if (!strcmp(key, "Global Average ESA")) params->steering_angle = atof(value); else if (!strcmp(key, "Global Average Altitude")) params->altitude = atof(value); else if (!strcmp(key, "Average GPS Altitude")) params->altitude = atof(value); else if (!strcmp(key, "Global Average Terrain Height")) params->terrain_height = atof(value); else if (!strcmp(key, "Global Average Squint Angle")) params->squint_angle = atof(value); else if (!strcmp(key, "Pulse Length")) params->pulse_length = atof(value); else if (!strcmp(key, "Bandwidth")) params->bandwidth = atof(value); else if (!strcmp(key, "Approximate Upper Left Latitude")) params->lat_upper_left = atof(value); else if (!strcmp(key, "Approximate Upper Left Longitude")) params->lon_upper_left = atof(value); else if (!strcmp(key, "Approximate Upper Right Latitude")) params->lat_upper_right = atof(value); else if (!strcmp(key, "Approximate Upper Right Longitude")) params->lon_upper_right = atof(value); else if (!strcmp(key, "Approximate Lower Left Latitude")) params->lat_lower_left = atof(value); else if (!strcmp(key, "Approximate Lower Left Longitude")) params->lon_lower_left = atof(value); else if (!strcmp(key, "Approximate Lower Right Latitude")) params->lat_lower_right = atof(value); else if (!strcmp(key, "Approximate Lower Right Longitude")) params->lon_lower_right = atof(value); else if (!strcmp(key, "Date of Acquisition")) strcpy(params->acquisition_date, value); else if (!strcmp(key, "Processor Version Number")) strcpy(params->processor, value); } FCLOSE(fp); return params; }
unsigned char *download_url(const char *url_in, int verbose, int *length) { char *url=STRDUP(url_in); // parse the url char *protocol, *host, *path; int port; parse_url(url, &protocol, &host, &port, &path); if (verbose) printf("Connecting to URL: %s://%s:%d%s\n", protocol, host, port, path); int timeout = 60; if (verbose) printf("Looking up IP Address for: %s\n", host); skt_ip_t hostIP = skt_lookup_ip(host); if (verbose) printf("Connecting to %s\n", host); SOCKET s = skt_connect(hostIP, port, timeout); char *send_get = MALLOC(strlen(url)+128); sprintf(send_get, "GET %s HTTP/1.1\r\n" "Host: %s\r\n" "User-Agent: Mozilla/5.0\r\n" "\r\n", path, host); // send our get request skt_sendN(s, send_get, strlen(send_get)); // wait... if (verbose) printf("Waiting for a response from %s\n", host); char *status = skt_recv_line(s); if (verbose) printf("Received status: %s\n", status); //char *status_trim = trim_spaces(status); free(status); // now can get the response code //int code = atoi(status_trim); //free(status_trim); // a zero-length line indicates the end of the HTTP headers... we ignore int len=-1; while (1) { char *line = skt_recv_line(s); if (strlen(line)==0) break; if (strstr(line, "Content-Length") != 0) { char *l = line + strlen("Content-Length") + 2; len=atoi(l); } } if (verbose) printf("Content Length: %d\n", len); if (len==-1) { asfPrintWarning("No Content-Length specified in the HTTP headers.\n"); return NULL; } // receiving data... unsigned char *data = CALLOC(len+12, sizeof(char)); int curr=0, chunkSize=1024; while (1) { int amt = chunkSize < len-curr ? chunkSize : len-curr; skt_recvN(s,data+curr,amt); curr += amt; if (verbose) printf("Retrieved %d Kb so far.\n", curr); if (curr==len) break; else if (curr>len) asfPrintError("Invalid Content-Length?\n"); } if(verbose) printf("Done.\n"); //if (verbose) // printf("Received data:\n" // "-------------------------------------------------------------\n" // "%s\n" // "-------------------------------------------------------------\n", // data); free(protocol); free(host); free(path); free(url); free(send_get); skt_close(s); *length = len; return data; }
int open_google_earth() { char *kml_filename = appendExt(curr->filename, ".kml"); char *basename = get_basename(curr->filename); char *dirname = get_dirname(curr->filename); char *arg; if (strlen(dirname)==0) { char *tmpdir = g_get_current_dir(); dirname = escapify(tmpdir); arg = MALLOC(sizeof(char)*(strlen(dirname)+strlen(kml_filename)+20)); sprintf(arg, "%s/%s", dirname, kml_filename); //free(tmpdir); } else { arg = STRDUP(kml_filename); } char *png_file = appendExt(arg, ".png"); printf("png file: %s\n", png_file); printf("Temporary kml file: %s\n", arg); FILE *kml_file = fopen(arg, "w"); if (!kml_file) { asfPrintWarning("Couldn't open kml file!\n"); return FALSE; } dbf_header_t *dbf; int nAttr, nCoords; double *lat, *lon, center_lat, center_lon; char configFile[255], *name; meta_parameters *meta; sprintf(configFile, "%s/convert2vector.config", get_asf_share_dir()); c2v_config *cfg = read_c2v_config(configFile); kml_header(kml_file); meta = meta2vector(curr->filename, &dbf, &nAttr, &lat, &lon, &nCoords); //meta_parameters *meta = curr->meta; if (meta && meta->general && meta_is_valid_double(meta->general->center_latitude) && meta_is_valid_double(meta->general->center_longitude)) { pixbuf2png(pixbuf_small, png_file); //kml_entry_with_overlay(kml_file, meta, basename, png_file, dirname); name = get_basename(kml_filename); center_lat = meta->general->center_latitude; center_lon = meta->general->center_longitude; write_kml_placemark(kml_file, name, center_lat, center_lon, png_file, dbf, nAttr, lat, lon, nCoords, cfg); FREE(lat); FREE(lon); FREE(dbf); } else { asfPrintWarning( "Failed, metadata doesn't contain valid lat/lon info.\n"); return FALSE; } kml_footer(kml_file); fclose(kml_file); gchar *ge; printf("kml: %s\n", kml_filename); printf("dir: %s\n", dirname); #ifdef win32 char path[1024]; FindExecutable((LPCTSTR)kml_filename, (LPCTSTR)dirname, (LPTSTR)path); ge = escapify(path); printf("Path to google earth: %s\n", ge); asfSystem("\"%s\" \"%s\"", ge, arg); #else ge = find_in_path("googleearth"); if (!ge) { message_box("Couldn't find googleearth! Is it installed?"); return FALSE; } int pid = fork(); if (pid == 0) { asfSystem("\"%s\" \"%s\"", ge, arg); //unlink(kml_filename); exit(EXIT_SUCCESS); } #endif free(kml_filename); free(basename); free(dirname); free(arg); return TRUE; }
meta_parameters *read_meta_gridfloat_ext(char *inBaseName, char *flt_file, int *column_count, int *row_count) { // all three of these must be present char *hdr_file = appendExt(inBaseName, ".hdr"); char *prj_file = appendExt(inBaseName, ".prj"); flt_file = appendExt(inBaseName, ".flt"); if (!fileExists(flt_file) || !fileExists(hdr_file) || !fileExists(prj_file)) { asfPrintError( "The BIL and/or associated metadata files were not found:\n" " FLT File: %s %s\n" " PRJ File: %s %s\n" " HDR File: %s %s\n", flt_file, fileExists(flt_file) ? "Found" : "NOT FOUND", prj_file, fileExists(prj_file) ? "Found" : "NOT FOUND", hdr_file, fileExists(hdr_file) ? "Found" : "NOT FOUND"); } asfPrintStatus("Parsing %s ...\n", hdr_file); // Info we'll get from the header file int nrows=-1; int ncols=-1; double cell_size = 0; double lat_ll = -999; double lon_ll = -999; double nodata = -9999; int msbfirst = FALSE; // Read header file char line[1024], value[1024]; FILE *fp = FOPEN(hdr_file, "r"); while (NULL!=fgets(line, 1024, fp)) { if (matches(line, "ncols")) { get_value(line, value); ncols = atoi(value); } else if (matches(line, "nrows")) { get_value(line, value); nrows = atoi(value); } else if (matches(line, "xllcorner")) { get_value(line, value); lon_ll = atof(value); } else if (matches(line, "yllcorner")) { get_value(line, value); lat_ll = atof(value); } else if (matches(line, "cellsize")) { get_value(line, value); cell_size = atof(value); } else if (matches(line, "NODATA_value")) { get_value(line, value); nodata = atof(value); } else if (matches(line, "byteorder")) { get_value(line, value); if (strcmp(value, "LSBFIRST") == 0) { msbfirst = FALSE; } else if (strcmp(value, "MSBFIRST") == 0) { msbfirst = TRUE; } else { asfPrintError("Unsupported byte order (should be 'LSBFIRST' or 'MSBFIRST'): %s\n", value); } } } fclose(fp); if (nrows < 0 || ncols < 0) { asfPrintError( "Header file did not contain Row/Column infomation.\n" "It is a valid .HDR file?\n"); } // PRJ File asfPrintStatus("Parsing %s ...\n", prj_file); datum_type_t datum=UNKNOWN_DATUM; spheroid_type_t spheroid=UNKNOWN_SPHEROID; fp = FOPEN(prj_file, "r"); while (NULL!=fgets(line, 1024, fp)) { if (matches(line, "Projection")) { get_value(line, value); if (strcmp(value, "GEOGRAPHIC") != 0) asfPrintError("Unsupported byte order (should be GEOGRAPHIC): %s\n", value); } else if (matches(line, "Units")) { get_value(line, value); if (strcmp(value, "DD") != 0) asfPrintError("Unsupported Units (should be DD): %s\n", value); } else if (matches(line, "Zunits")) { get_value(line, value); if (strcmp(value, "METERS") != 0) asfPrintError("Unsupported Zunits (should be METERS): %s\n", value); } else if (matches(line, "Datum")) { get_value(line, value); if (strcmp_case(value, "NAD27") == 0) datum = NAD27_DATUM; else if (strcmp_case(value, "NAD83") == 0) datum = NAD83_DATUM; else if (strcmp_case(value, "WGS83") == 0) datum = WGS84_DATUM; else asfPrintError( "Unsupported Datum (should be NAD27, NAD83, or WGS84): %s\n", value); } else if (matches(line, "Spheroid")) { get_value(line, value); if (strcmp_case(value, "CLARKE1866") == 0) spheroid = CLARKE1866_SPHEROID; else if (strcmp_case(value, "BESSEL") == 0) spheroid = BESSEL_SPHEROID; else if (strcmp_case(value, "CLARKE1880") == 0) spheroid = CLARKE1880_SPHEROID; else if (strcmp_case(value, "GEM6") == 0) spheroid = GEM6_SPHEROID; else if (strcmp_case(value, "GEM10C") == 0) spheroid = GEM10C_SPHEROID; else if (strcmp_case(value, "GRS1980") == 0) spheroid = GRS1980_SPHEROID; else if (strcmp_case(value, "WGS72") == 0) spheroid = WGS72_SPHEROID; else if (strcmp_case(value, "WGS84") == 0) spheroid = WGS84_SPHEROID; else if (strcmp_case(value, "INTERNATIONAL1924") == 0) spheroid = INTERNATIONAL1924_SPHEROID; else if (strcmp_case(value, "INTERNATIONAL1967") == 0) spheroid = INTERNATIONAL1967_SPHEROID; else asfPrintError("Unsupported Spheroid: %s\n", value); } } fclose(fp); meta_parameters *meta = raw_init(); meta->projection = meta_projection_init(); meta->location = meta_location_init(); // aliases meta_general *mg = meta->general; meta_projection *mp = meta->projection; meta_location *ml = meta->location; // populate general block info mg->data_type = REAL32; mg->image_data_type = DEM; //presumably!? mg->no_data = nodata; // these are supposed to be in meters // currently, cell_size_* is in arcsecs... so here is a kludgey // calculation, to round to the nearest 10m. usgs data is either // 30, 60, or 90 meter. // Note that the projection block will have the actual pixel size int cell_size_m = 10 * (int)(11131.95 * cell_size + .5); if (cell_size_m != 30 && cell_size_m != 60 && cell_size_m != 90) { asfPrintWarning("Unexpected pixel size of %dm (%.10f degree) detected.\n" "USGS Seamless data should be 30, 60, or 90 meter.\n", cell_size_m, cell_size); } mg->x_pixel_size = cell_size_m; mg->y_pixel_size = cell_size_m; mg->line_count = nrows; mg->sample_count = ncols; mg->band_count = 1; mg->start_line = 0; mg->start_sample = 0; char *basename = get_basename(inBaseName); strcpy(mg->basename, basename); free(basename); strcpy(mg->sensor, "USGS Seamless data (e.g., NED, STRM)"); strcpy(mg->mode, "N/A"); strcpy(mg->processor, "Unknown"); mg->center_latitude = lat_ll + cell_size * ncols / 2.0; mg->center_longitude = lon_ll + cell_size * nrows / 2.0; // populate projection block info mp->type = LAT_LONG_PSEUDO_PROJECTION; mp->startX = lon_ll; mp->startY = lat_ll + cell_size * nrows; mp->perX = cell_size; mp->perY = -cell_size; strcpy(mp->units, "degrees"); mp->hem = mg->center_latitude > 0 ? 'N' : 'S'; // the datum for NED is NAD83, for SRTM it is WGS84 mp->datum = datum; mp->spheroid = spheroid; spheroid_axes_lengths(spheroid, &mg->re_major, &mg->re_minor); mp->re_major = mg->re_major; mp->re_minor = mg->re_minor; // location block ml->lon_start_near_range = mp->startX; ml->lat_start_near_range = mp->startY; ml->lon_start_far_range = mp->startX + mp->perX * ncols; ml->lat_start_far_range = mp->startY; ml->lon_end_near_range = mp->startX; ml->lat_end_near_range = mp->startY + mp->perY * nrows; ml->lon_end_far_range = mp->startX + mp->perX * ncols; ml->lat_end_far_range = mp->startY + mp->perY * nrows; free(hdr_file); free(prj_file); *column_count = ncols; *row_count = nrows; return meta; }
int open_asf_data(const char *filename, const char *band, int multilook, meta_parameters *meta, ClientInterface *client) { ReadAsfClientInfo *info = MALLOC(sizeof(ReadAsfClientInfo)); info->is_rgb = FALSE; info->band_gs = info->band_r = info->band_g = info->band_b = 0; info->ml = multilook; // special hack for Avnir data! if (!band && strcmp_case(meta->general->sensor_name, "AVNIR") == 0 && meta->general->band_count >= 3) { // no band was specifed -- show true color (3,2,1) asfPrintStatus("Avnir data: defaulting to TRUE color -- " "Red=3, Green=2, Blue=1\n"); band = "03,02,01"; } if (band) { char *r, *b, *g; if (split3(band, &r, &g, &b, ',')) { // Looks like we were given 3 bands -- so, we are doing rgb info->band_r = get_band_number(meta->general->bands, meta->general->band_count, r); if (info->band_r < 0) asfPrintWarning("Red band '%s' not found.\n", r); else asfPrintStatus("Red band is band #%d: %s\n", info->band_r+1, r); info->band_g = get_band_number(meta->general->bands, meta->general->band_count, g); if (info->band_g < 0) asfPrintWarning("Green band '%s' not found.\n", g); else asfPrintStatus("Green band is band #%d: %s\n", info->band_g+1, g); info->band_b = get_band_number(meta->general->bands, meta->general->band_count, b); if (info->band_b < 0) asfPrintWarning("Blue band '%s' not found.\n", b); else asfPrintStatus("Blue band is band #%d: %s\n", info->band_b+1, b); if (info->band_r < 0 && info->band_g < 0 && info->band_b < 0) { // none of the bands were found return FALSE; } info->is_rgb = TRUE; FREE(r); FREE(g); FREE(b); set_bands_rgb(info->band_r, info->band_g, info->band_b); } else { // Single band name given info->band_gs = get_band_number(meta->general->bands, meta->general->band_count, (char*)band); if (info->band_gs < 0) { asfPrintWarning("Band '%s' not found.\n", band); return FALSE; } else { asfPrintStatus("Reading band #%d: %s\n", info->band_gs+1, band); } set_bands_greyscale(info->band_gs); } } info->fp = fopen(filename, "rb"); if (!info->fp) { asfPrintWarning("Failed to open ASF Internal file %s: %s\n", filename, strerror(errno)); return FALSE; } client->read_client_info = info; client->read_fn = read_asf_client; client->thumb_fn = get_asf_thumbnail_data; client->free_fn = free_asf_client_info; if (meta->general->data_type == ASF_BYTE) client->data_type = info->is_rgb ? RGB_BYTE : GREYSCALE_BYTE; else client->data_type = info->is_rgb ? RGB_FLOAT : GREYSCALE_FLOAT; return TRUE; }
static int pixbuf2png(GdkPixbuf *pb, const char *output_png) { int i; int width = gdk_pixbuf_get_width(pb); int height = gdk_pixbuf_get_height(pb); int n_channels = gdk_pixbuf_get_n_channels(pb); int rowstride = gdk_pixbuf_get_rowstride(pb); guchar *pixels = gdk_pixbuf_get_pixels(pb); guchar *pixels_out = MALLOC(sizeof(guchar)*width*height*4); //printf("pixbuf2png> opening: %s\n", output_png); FILE *fout = FOPEN(output_png, "wb"); png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); if (!png_ptr) { asfPrintWarning("Couldn't open png file: %s\n", output_png); return FALSE; } //printf("pixbuf2png> png_create_info_struct\n"); png_infop info_ptr = png_create_info_struct(png_ptr); if (!info_ptr) { png_destroy_write_struct(&png_ptr, (png_infopp)NULL); fclose(fout); asfPrintWarning("Couldn't open png info for %s\n", output_png); return FALSE; } //printf("pixbuf2png> setjmp\n"); if (setjmp(png_jmpbuf(png_ptr))) { png_destroy_write_struct(&png_ptr, &info_ptr); fclose(fout); asfPrintWarning("Error writing the png: %s\n", output_png); return FALSE; } //printf("pixbuf2png> png_init_io\n"); png_init_io(png_ptr, fout); //printf("pixbuf2png> png_set_IHDR\n"); png_set_IHDR(png_ptr, info_ptr, width, height, 8, PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT); //printf("pixbuf2png> png_write_info\n"); png_write_info(png_ptr, info_ptr); // add a transparency byte to each pixel in the pixels_out buffer for (i=0; i<height; ++i) { int j; for (j=0; j<width; ++j) { // output: red=k, green=k+1, blue=k+2, alpha=k+3 int out_k = 4*(j + i*width); // input: red=k, green=k+1, blue=k+2 int in_k = j*n_channels + i*rowstride; // make it transparent, if the pixel is black // (i.e., all channels are 0) int trans = pixels[in_k] == 0 && pixels[in_k+1] == 0 && pixels[in_k+2] == 0; pixels_out[out_k] = pixels[in_k]; pixels_out[out_k+1] = pixels[in_k+1]; pixels_out[out_k+2] = pixels[in_k+2]; pixels_out[out_k+3] = trans ? 0 : 255; } } //printf("pixbuf2png> row_pointers\n"); png_bytep *row_pointers = MALLOC(sizeof(png_bytep)*height); for (i=0; i<height; ++i) row_pointers[i] = pixels_out + i*width*4; //printf("pixbuf2png> png_write_image\n"); png_write_image(png_ptr, row_pointers); //printf("pixbuf2png> png_write_end\n"); png_write_end(png_ptr, NULL); //printf("pixbuf2png> png_destroy_write_struct\n"); png_destroy_write_struct(&png_ptr, &info_ptr); //printf("pixbuf2png> fclose\n"); fclose(fout); //printf("pixbuf2png> freeing row pointers\n"); FREE(row_pointers); FREE(pixels_out); return TRUE; }
int asf_export_bands(output_format_t format, scale_t sample_mapping, int rgb, int true_color, int false_color, char *look_up_table_name, int use_pixel_is_point, char *in_base_name, char *output_name, char **band_name, int *noutputs, char ***output_names) { char *in_meta_name=NULL, *in_data_name=NULL, *out_name=NULL; asfPrintStatus("Exporting ...\n"); meta_parameters *md = NULL; int i, nouts = 0, is_polsarpro = 0, is_matrix = 0; char **outs = NULL; if (format != HDF && format != NC) { in_data_name = appendExt(in_base_name, ".img"); in_meta_name = appendExt(in_base_name, ".meta"); md = meta_read(in_meta_name); is_polsarpro = (md->general->image_data_type == POLARIMETRIC_SEGMENTATION) ? 1 : 0; is_matrix = ((md->general->image_data_type >= POLARIMETRIC_C2_MATRIX && md->general->image_data_type <= POLARIMETRIC_STOKES_MATRIX) || md->general->image_data_type == POLARIMETRIC_DECOMPOSITION) ? 1 : 0; if (md->general->image_data_type == RGB_STACK) rgb = TRUE; } // Do that exporting magic! if ( format == ENVI ) { //in_data_name = appendExt(in_base_name, ".img"); //in_meta_name = appendExt(in_base_name, ".meta"); out_name = appendExt(output_name, ".bsq"); export_as_envi (in_meta_name, in_data_name, out_name); } else if ( format == ESRI ) { //in_data_name = appendExt(in_base_name, ".img"); //in_meta_name = appendExt(in_base_name, ".meta"); out_name = appendExt(output_name, ".esri"); export_as_esri (in_meta_name, in_data_name, out_name); } else if ( format == TIF ) { //in_data_name = appendExt(in_base_name, ".img"); //in_meta_name = appendExt(in_base_name, ".meta"); out_name = MALLOC(sizeof(char)*(strlen(output_name)+32)); strcpy(out_name, output_name); if (!is_matrix) append_ext_if_needed(out_name, ".tif", ".tiff"); export_band_image(in_meta_name, in_data_name, out_name, sample_mapping, band_name, rgb, true_color, false_color, look_up_table_name, TIF, 0, &nouts, &outs); } else if ( format == GEOTIFF ) { //in_data_name = appendExt(in_base_name, ".img"); //in_meta_name = appendExt(in_base_name, ".meta"); out_name = MALLOC(sizeof(char)*(strlen(output_name)+32)); strcpy(out_name, output_name); if (!is_matrix) append_ext_if_needed(out_name, ".tif", ".tiff"); export_band_image(in_meta_name, in_data_name, out_name, sample_mapping, band_name, rgb, true_color, false_color, look_up_table_name, GEOTIFF, use_pixel_is_point, &nouts, &outs); } else if ( format == JPEG ) { //in_data_name = appendExt(in_base_name, ".img"); //in_meta_name = appendExt(in_base_name, ".meta"); out_name = MALLOC(sizeof(char)*(strlen(output_name)+32)); strcpy(out_name, output_name); if (!is_matrix) append_ext_if_needed(out_name, ".jpg", ".jpeg"); export_band_image(in_meta_name, in_data_name, out_name, sample_mapping, band_name, rgb, true_color, false_color, look_up_table_name, JPEG, 0, &nouts, &outs); } else if ( format == PNG ) { //in_data_name = appendExt(in_base_name, ".img"); //in_meta_name = appendExt(in_base_name, ".meta"); out_name = MALLOC(sizeof(char)*(strlen(output_name)+32)); strcpy(out_name, output_name); if (!is_matrix) append_ext_if_needed(out_name, ".png", NULL); export_band_image(in_meta_name, in_data_name, out_name, sample_mapping, band_name, rgb, true_color, false_color, look_up_table_name, PNG, 0, &nouts, &outs); } else if ( format == PNG_ALPHA ) { //in_data_name = appendExt(in_base_name, ".img"); //in_meta_name = appendExt(in_base_name, ".meta"); out_name = MALLOC(sizeof(char)*(strlen(output_name)+32)); strcpy(out_name, output_name); if (!is_matrix) append_ext_if_needed(out_name, ".png", NULL); export_band_image(in_meta_name, in_data_name, out_name, sample_mapping, band_name, rgb, true_color, false_color, look_up_table_name, PNG_ALPHA, 0, &nouts, &outs); } else if ( format == PNG_GE ) { //in_data_name = appendExt(in_base_name, ".img"); //in_meta_name = appendExt(in_base_name, ".meta"); out_name = MALLOC(sizeof(char)*(strlen(output_name)+32)); strcpy(out_name, output_name); if (!is_matrix) append_ext_if_needed(out_name, ".png", NULL); export_band_image(in_meta_name, in_data_name, out_name, sample_mapping, band_name, rgb, true_color, false_color, look_up_table_name, PNG_GE, 0, &nouts, &outs); } else if ( format == PGM ) { //in_data_name = appendExt(in_base_name, ".img"); //in_meta_name = appendExt(in_base_name, ".meta"); if (rgb || true_color || false_color || is_polsarpro) { asfPrintWarning( "Greyscale PGM output is not compatible with color options:\n" "(RGB, True Color, False Color, color look-up tables, PolSARpro\n" "classifications, etc) ...\n" "Defaulting to producing separate greyscale PGM files for " "available bands.\n"); rgb = 0; true_color = 0; false_color = 0; } out_name = MALLOC(sizeof(char)*(strlen(output_name)+32)); strcpy(out_name, output_name); if (!is_matrix) append_ext_if_needed(out_name, ".pgm", NULL); export_band_image(in_meta_name, in_data_name, out_name, sample_mapping, band_name, rgb, true_color, false_color, look_up_table_name, 0, PGM, &nouts, &outs); } else if ( format == POLSARPRO_HDR ) { out_name = MALLOC(sizeof(char)*(strlen(output_name)+32)); strcpy(out_name, output_name); export_polsarpro(in_meta_name, in_data_name, out_name, &nouts, &outs); } else if ( format == HDF ) { out_name = MALLOC(sizeof(char)*(strlen(output_name)+32)); strcpy(out_name, output_name); export_hdf(in_base_name, out_name, &nouts, &outs); } else if ( format == NC ) { out_name = MALLOC(sizeof(char)*(strlen(output_name)+32)); strcpy(out_name, output_name); export_netcdf(in_base_name, out_name, &nouts, &outs); } if (format != HDF && format != NC) { if (should_write_insar_rgb(md->general->bands)) { write_insar_rgb(format, in_meta_name, in_data_name, out_name); } if (should_write_insar_xml_meta(md)) { char *xml_meta = get_insar_xml_string(md, FALSE); char *xml_output_file_name = (char *) MALLOC(sizeof(char)*(strlen(out_name)+10)); sprintf(xml_output_file_name, "%s.xml", stripExt(out_name)); write_insar_xml_to_file(xml_output_file_name, xml_meta); FREE(xml_meta); FREE(xml_output_file_name); } else if (should_write_dem_xml_meta(md)) { char *xml_meta = get_dem_xml_string(md, FALSE); char *xml_output_file_name = (char *) MALLOC(sizeof(char)*(strlen(out_name)+10)); sprintf(xml_output_file_name, "%s.xml", stripExt(out_name)); write_dem_xml_to_file(xml_output_file_name, xml_meta); FREE(xml_meta); FREE(xml_output_file_name); } } if (noutputs && output_names) { asfPrintStatus("\n\nExport complete.\nGenerated %d output file%s:\n", nouts, nouts==1?"":"s"); for (i=0; i<nouts; ++i) asfPrintStatus(" %s\n", outs[i]); asfPrintStatus("\n"); *noutputs = nouts; *output_names = outs; } else { for (i=0; i<nouts; ++i) FREE(outs[i]); FREE(outs); } if (in_data_name) FREE(in_data_name); if (in_meta_name) FREE(in_meta_name); if (out_name) FREE(out_name); if (md) meta_free(md); asfPrintStatus("Export successful!\n\n"); return (EXIT_SUCCESS); }