int main(int argc, char *argv[]) { fitsfile *infptr = 0, *outfptr = 0; /* FITS file pointers */ int status = 0; /* CFITSIO status value MUST be initialized to zero! */ int icol = 0, incols = 0, outcols = 0, intype = 0, outtype = 0, check = 1; long inrep = 0, outrep = 0, width = 0, inrows = 0, outrows = 0, ii = 0, jj = 0; unsigned char *buffer = 0; int printhelp = (argc == 2 && (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help") == 0)); if (printhelp || argc != 3) { fprintf(stderr, "Usage: %s infile1[ext][filter] outfile[ext]\n", argv[0]); fprintf(stderr, "\n"); fprintf(stderr, "Merge 2 tables by copying all the rows from the 1st table\n"); fprintf(stderr, "into the 2nd table. The 2 tables must have identical\n"); fprintf(stderr, "structure, with the same number of columns with the same\n"); fprintf(stderr, "datatypes. This program modifies the output file in place,\n"); fprintf(stderr, "rather than creating a whole new output file.\n"); fprintf(stderr, "\n"); fprintf(stderr, "Examples: \n"); fprintf(stderr, "\n"); fprintf(stderr, "1. %s intab.fit+1 outtab.fit+2\n", argv[0]); fprintf(stderr, "\n"); fprintf(stderr, " merge the table in the 1st extension of intab.fit with\n"); fprintf(stderr, " the table in the 2nd extension of outtab.fit.\n"); fprintf(stderr, "\n"); fprintf(stderr, "2. %s 'intab.fit+1[PI > 45]' outab.fits+2\n", argv[0]); fprintf(stderr, "\n"); fprintf(stderr, " Same as the 1st example, except only rows that have a PI\n"); fprintf(stderr, " column value > 45 will be merged into the output table.\n"); fprintf(stderr, "\n"); return (0); } /* open both input and output files and perform validity checks */ if (fits_open_file(&infptr, argv[1], READONLY, &status) || fits_open_file(&outfptr, argv[2], READWRITE, &status)) { fprintf(stderr, " Couldn't open both files\n"); } else if (fits_get_hdu_type(infptr, &intype, &status) || fits_get_hdu_type(outfptr, &outtype, &status)) { fprintf(stderr, "couldn't get the type of HDU for the files\n"); } else if (intype == IMAGE_HDU) { fprintf(stderr, "The input HDU is an image, not a table\n"); } else if (outtype == IMAGE_HDU) { fprintf(stderr, "The output HDU is an image, not a table\n"); } else if (outtype != intype) { fprintf(stderr, "Input and output HDUs are not the same type of table.\n"); } else if (fits_get_num_cols(infptr, &incols, &status) || fits_get_num_cols(outfptr, &outcols, &status)) { fprintf(stderr, "Couldn't get number of columns in the tables\n"); } else if (incols != outcols) { fprintf(stderr, "Input and output HDUs don't have same # of columns.\n"); } else if (fits_read_key(infptr, TLONG, "NAXIS1", &width, NULL, &status)) { fprintf(stderr, "Couldn't get width of input table\n"); } else if (!(buffer = (unsigned char *) malloc(width))) { fprintf(stderr, "memory allocation error\n"); } else if (fits_get_num_rows(infptr, &inrows, &status) || fits_get_num_rows(outfptr, &outrows, &status)) { fprintf(stderr, "Couldn't get the number of rows in the tables\n"); } else { /* check that the corresponding columns have the same datatypes */ for (icol = 1; icol <= incols; icol++) { fits_get_coltype(infptr, icol, &intype, &inrep, NULL, &status); fits_get_coltype(outfptr, icol, &outtype, &outrep, NULL, &status); if (intype != outtype || inrep != outrep) { fprintf(stderr, "Column %d is not the same in both tables\n", icol); check = 0; } } if (check && !status) { /* insert 'inrows' empty rows at the end of the output table */ fits_insert_rows(outfptr, outrows, inrows, &status); for (ii = 1, jj = outrows +1; ii <= inrows; ii++, jj++) { /* read row from input and write it to the output table */ fits_read_tblbytes(infptr, ii, 1, width, buffer, &status); fits_write_tblbytes(outfptr, jj, 1, width, buffer, &status); if (status) { break; /* jump out of loop if error occurred */ } } /* all done; now free memory and close files */ fits_close_file(outfptr, &status); fits_close_file(infptr, &status); } } if (buffer) { free(buffer); } if (status) { fits_report_error(stderr, status); /* print any error message */ } return (status); }
int main(int argc, char *argv[]) { static struct option long_opts[] = { {"start", 1, NULL, 's'}, {"end", 1, NULL, 'e'}, {0,0,0,0} }; fitsfile *infptr, *outfptr; /* FITS file pointers defined in fitsio.h */ int opt, opti; int i, status = 0, ii = 1, jj=1; /* status must always be initialized = 0 */ int lo_row, hi_row; unsigned char *buffer = 0; long nrows; int ncols; int tfields; long pcount; char extname[FLEN_VALUE]; char *ttype[NFIELDS_I]; char *tform[NFIELDS_I]; char *tunit[NFIELDS_I]; char *tcomm[NFIELDS_I]; float start=0.0, end=1.0; while ((opt=getopt_long(argc,argv,"s:e:h",long_opts,&opti))!=-1) { switch (opt) { case 's': start = atof(optarg); break; case 'e': end = atof(optarg); break; case 'h': default: usage(); exit(0); break; } } if (argc < 3) { usage(); return(0); } //printf("> %s %s\n", argv[1], argv[2]); //printf("> %s %s\n", argv[optind], argv[2]); /* Allocate space for the table parameters and initialize */ for (i=0; i<NFIELDS_I; i++) { ttype[i] = (char *) malloc(FLEN_VALUE*sizeof(char)); tform[i] = (char *) malloc(FLEN_VALUE*sizeof(char)); tunit[i] = (char *) malloc(FLEN_VALUE*sizeof(char)); tcomm[i] = (char *) malloc(FLEN_CARD*sizeof(char)); strcpy(ttype[i], " "); } /* Open the input file */ if ( !fits_open_file(&infptr, argv[optind], READONLY, &status) ) { /* Create the output file */ if ( !fits_create_file(&outfptr, argv[optind+1], &status) ) { /* Copy every HDU until we get an error */ //while( !fits_movabs_hdu(infptr, ii++, NULL, &status) ) /* Copy first HDU */ fits_copy_hdu(infptr, outfptr, 0, &status); fits_movnam_hdu(infptr, BINARY_TBL, "SUBINT", 0, &status); fits_get_num_rows(infptr, &nrows, &status); printf("Number of rows = %ld\n", nrows); fits_get_num_cols(infptr, &ncols, &status); printf("Number of cols = %d\n", ncols); long width; fits_read_key(infptr, TLONG, "NAXIS1", &width, NULL, &status); printf("Width = %ld\n", width); buffer = (unsigned char *) malloc(width); /* Copy the fits header */ fits_copy_header(infptr, outfptr, &status); //#if 0 /* Determine low and high row number ot copy */ lo_row = 1 + start * nrows; hi_row = end * nrows; printf("lo_row=%d hi_row=%d\n", lo_row, hi_row); for (ii=lo_row, jj=1; ii<=hi_row; ii++, jj++) { printf("\rRead %d/%ld", ii, nrows); fits_read_tblbytes( infptr, ii, 1, width, buffer, &status); fits_write_tblbytes(outfptr, jj, 1, width, buffer, &status); } nrows = (hi_row-lo_row)+1; fits_update_key(outfptr, TLONG, "NAXIS2", &nrows, 0, &status); printf("\nDone\n"); //#endif /* Reset status after normal error */ //if (status == END_OF_FILE) status = 0; fits_close_file(outfptr, &status); } fits_close_file(infptr, &status); } /* if error occured, print out error message */ if (status) fits_report_error(stderr, status); return(status); }