frameT *read_csv (char *file_name) { FILE *fp; unsigned char buf[CSV_BUF_SIZE]; unsigned char header_buf[CSV_BUF_SIZE]; unsigned char *row[MAX_COLUMNS]; unsigned char *header[MAX_COLUMNS]; int ncol, cols; int n, i; frameT *frame; csvT *csv; csv = open_csv (file_name); fp = csv->fp; // Parse header n = csv_row_fread (fp, header_buf, CSV_BUF_SIZE, header, MAX_COLUMNS, ',', CSV_TRIM | CSV_QUOTES, &ncol); if (n <= 0) { fprintf (stderr, "Failed to read header from CSV file\n"); exit (-1); } // Parse the first data row to determine data types n = csv_row_fread (fp, buf, CSV_BUF_SIZE, row, ncol, ',', CSV_TRIM | CSV_QUOTES, &cols); if (n <= 0) { fprintf (stderr, "File has no data\n"); exit (-1); } frame = new_frame (file_name, ncol); frame->csv = csv; for (i=0; i<ncol; i++) init_column (frame, i, (char *)header[i], guess_type((char *) row[i])); for (i=0; i<ncol; i++) column_init_data (frame, i, csv->est_rows); frame->allocated_rows = csv->est_rows; frame->region_rows = (unsigned char *) malloc (frame->allocated_rows); if (!frame->region_rows) { fprintf (stderr, "Failed to allocate region_rows\n"); exit (-1); } frame->allocated_region_rows = frame->allocated_rows; if (csv->est_rows < 10000) { load_all_rows (frame); } else { load_random_rows (frame, 1.0); } return (frame); }
static int save_as_csv(ImageInfo *ii, const char *out_file, int what_to_save, int strict_boundary, int load) { int i,j; assert (g_poly->n > 0); assert (crosshair_line > 0 && crosshair_samp > 0); meta_parameters *meta = ii->meta; int line_min, line_max, samp_min, samp_max, nl, ns; compute_extent(meta, &line_min, &line_max, &samp_min, &samp_max, &nl, &ns); if (nl>500 || ns>500) { // too big for csv -- Excel etc. will choke char errbuf[1024]; snprintf(errbuf, 1024, "\nRegion is too large (%dx%d) to export as CSV (500x500 max)\n\n", nl, ns); message_box(errbuf); printf("%s", errbuf); return FALSE; // failure } FILE *outFp = fopen(out_file, "w"); if (!outFp) { // failed to open the output file! char errbuf[1024]; snprintf(errbuf, 1024, "Failed to open %s: %s", out_file, strerror(errno)); message_box(errbuf); strcat(errbuf, "\n"); printf("%s", errbuf); return FALSE; // failure } printf("Generating %s...\n", out_file); // define clipping region, if necessary double xp[MAX_POLY_LEN+2], yp[MAX_POLY_LEN+2]; int n=0; if (strict_boundary) define_clipping_region(meta, &n, xp, yp); // generate csv fprintf(outFp, ","); for (j=0; j<ns; ++j) { if (what_to_save==LAT_LON_2_BAND) fprintf(outFp, "%d,%s", samp_min+j, j==ns-1 ? "\n" : ","); else fprintf(outFp, "%d%s", samp_min+j, j==ns-1 ? "\n" : ","); } if (what_to_save==LAT_LON_2_BAND) { fprintf(outFp, ","); for (j=0; j<ns; ++j) { fprintf(outFp, "Lat,Lon%s", j==ns-1 ? "\n" : ","); } } for (i=0; i<nl; ++i) { int l = line_min+i; fprintf(outFp, "%d,", l); for (j=0; j<ns; ++j) { int s = samp_min+j; if (what_to_save==LAT_LON_2_BAND) { float lat, lon; if (!strict_boundary || pnpoly(n, xp, yp, s, l)) { double dlat, dlon; meta_get_latLon(meta, l, s, 0, &dlat, &dlon); lat = (float)dlat; lon = (float)dlon; } else { lat = lon = 0.; } fprintf(outFp, "%f,%f%s", lat, lon, j==ns-1 ? "\n" : ","); } else { float val; if (!strict_boundary || pnpoly(n, xp, yp, s, l)) { val = get_data(ii, what_to_save, l, s); } else { val = 0; } fprintf(outFp, "%f%s", val, j==ns-1 ? "\n" : ","); } } asfLineMeter(i,nl); } fclose(outFp); // if requested, open up the csv with an external viewer if (load) open_csv(out_file); return TRUE; }