int FitsFile::GetColumnCount() { CheckOpen(); int rowCount, status = 0; fits_get_num_cols(_fptr, &rowCount, &status); CheckStatus(status); return rowCount; }
int InputFileFITS::getNCols() { int status = 0; if(!isOpened()) throwException("Error in InputFileFITS::getNCols() ", status); int ncols; fits_get_num_cols(infptr, &ncols, &status); if (status) throwException("Error in InputFileFITS::getNCols() ", status); return ncols; }
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); }
Kst::Object::UpdateType LFIIOSource::update() { Kst::Object::UpdateType updateType = Kst::Object::NO_CHANGE; QString strTemplate; QString strName; fitsfile* ffits; char charTemplate[FLEN_CARD]; char charName[FLEN_CARD]; long lNumFrames; long lMaxRepeat = 1; long lRepeat; long lWidth; int iColNumber; int iNumCols; int iStatus = 0; int iResult = 0; int iTypeCode; int i; int newNF = 0; _valid = false; if(!_filename.isNull() && !_filename.isEmpty()) { iResult = fits_open_table( &ffits, _filename.ascii(), READONLY, &iStatus ); if(iResult == 0) { // determine size of data... iResult = fits_get_num_cols( ffits, &iNumCols, &iStatus ); if(iResult == 0) { iResult = fits_get_num_rows( ffits, &lNumFrames, &iStatus ); if(iResult == 0) { _fieldList.clear(); _fieldList.append("INDEX"); _valid = true; _bHasTime = false; // need to multiply lNumFrames by the maximum value of the vector repeat value... for(i = 0; i<iNumCols; i++) { iStatus = 0; sprintf(charTemplate, "%d", i+1); iResult = fits_get_colname( ffits, CASEINSEN, charTemplate, charName, &iColNumber, &iStatus ); if(iResult == 0) { int iOffset = i; strName = charName; // ensure that we don't add duplicates to the _fieldList... while(_fieldList.findIndex(strName) != -1) { strName = QString("%1[%2]").arg(charName).arg(iOffset); iOffset++; } } else { strName.setNum(i); } _fieldList.append(strName); iStatus = 0; iResult = fits_get_coltype( ffits, i+1, &iTypeCode, &lRepeat, &lWidth, &iStatus ); if(iResult == 0) { if(lRepeat > lMaxRepeat) { lMaxRepeat = lRepeat; } } } // check if we have a time field defined by the header keys TIMEZERO and DELTA_T. // If so then we create a new field called $TIME_FIELD, unless such a field already // exists, in which case we do nothing... char charTimeZero[] = "TIMEZERO"; iStatus = 0; iResult = fits_read_key( ffits, TDOUBLE, charTimeZero, &_dTimeZero, 0L, &iStatus ); if(iResult == 0) { char charTimeDelta[] = "DELTA_T"; iResult = fits_read_key( ffits, TDOUBLE, charTimeDelta, &_dTimeDelta, 0L, &iStatus ); if(iResult == 0) { if(_fieldList.find(QString(TIME_FIELD)) == _fieldList.end()) { _bHasTime = true; _fieldList.append(TIME_FIELD); } } } if(lNumFrames * lMaxRepeat != _numFrames) { _numCols = iNumCols; newNF = lNumFrames * lMaxRepeat; updateType = Kst::Object::UPDATE; } } } iStatus = 0; fits_close_file( ffits, &iStatus ); } } bool isnew = newNF != _numFrames; _numFrames = newNF; return (isnew ? Kst::Object::UPDATE : Kst::Object::NO_CHANGE); }
str FITSattach(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci) { mvc *m = NULL; sql_trans *tr; sql_schema *sch; sql_table *fits_tp, *fits_fl, *fits_tbl, *fits_col, *tbl = NULL; sql_column *col; str msg = MAL_SUCCEED; str fname = *(str*)getArgReference(stk, pci, 1); fitsfile *fptr; /* pointer to the FITS file */ int status = 0, i, j, hdutype, hdunum = 1, cnum = 0, bitpixnumber = 0; oid fid, tid, cid, rid = oid_nil; char tname[BUFSIZ], *tname_low = NULL, *s, bname[BUFSIZ], stmt[BUFSIZ]; long tbcol; char cname[BUFSIZ], tform[BUFSIZ], tunit[BUFSIZ], tnull[BUFSIZ], tdisp[BUFSIZ]; double tscal, tzero; char xtensionname[BUFSIZ] = "", stilversion[BUFSIZ] = ""; char stilclass[BUFSIZ] = "", tdate[BUFSIZ] = "", orig[BUFSIZ] = "", comm[BUFSIZ] = ""; msg = getSQLContext(cntxt, mb, &m, NULL); if (msg) return msg; if (fits_open_file(&fptr, fname, READONLY, &status)) { msg = createException(MAL, "fits.attach", "Missing FITS file %s.\n", fname); return msg; } tr = m->session->tr; sch = mvc_bind_schema(m, "sys"); fits_fl = mvc_bind_table(m, sch, "fits_files"); if (fits_fl == NULL) FITSinitCatalog(m); fits_fl = mvc_bind_table(m, sch, "fits_files"); fits_tbl = mvc_bind_table(m, sch, "fits_tables"); fits_col = mvc_bind_table(m, sch, "fits_columns"); fits_tp = mvc_bind_table(m, sch, "fits_table_properties"); /* check if the file is already attached */ col = mvc_bind_column(m, fits_fl, "name"); rid = table_funcs.column_find_row(m->session->tr, col, fname, NULL); if (rid != oid_nil) { fits_close_file(fptr, &status); msg = createException(SQL, "fits.attach", "File %s already attached\n", fname); return msg; } /* add row in the fits_files catalog table */ col = mvc_bind_column(m, fits_fl, "id"); fid = store_funcs.count_col(tr, col, 1) + 1; store_funcs.append_col(m->session->tr, mvc_bind_column(m, fits_fl, "id"), &fid, TYPE_int); store_funcs.append_col(m->session->tr, mvc_bind_column(m, fits_fl, "name"), fname, TYPE_str); col = mvc_bind_column(m, fits_tbl, "id"); tid = store_funcs.count_col(tr, col, 1) + 1; if ((s = strrchr(fname, DIR_SEP)) == NULL) s = fname; else s++; strcpy(bname, s); s = strrchr(bname, '.'); if (s) *s = 0; fits_get_num_hdus(fptr, &hdunum, &status); for (i = 1; i <= hdunum; i++) { fits_movabs_hdu(fptr, i, &hdutype, &status); if (hdutype != ASCII_TBL && hdutype != BINARY_TBL) continue; /* SQL table name - the name of FITS extention */ fits_read_key(fptr, TSTRING, "EXTNAME", tname, NULL, &status); if (status) { snprintf(tname, BUFSIZ, "%s_%d", bname, i); tname_low = toLower(tname); status = 0; }else { /* check table name for existence in the fits catalog */ tname_low = toLower(tname); col = mvc_bind_column(m, fits_tbl, "name"); rid = table_funcs.column_find_row(m->session->tr, col, tname_low, NULL); /* or as regular SQL table */ tbl = mvc_bind_table(m, sch, tname_low); if (rid != oid_nil || tbl) { snprintf(tname, BUFSIZ, "%s_%d", bname, i); tname_low = toLower(tname); } } fits_read_key(fptr, TSTRING, "BITPIX", &bitpixnumber, NULL, &status); if (status) { status = 0; } fits_read_key(fptr, TSTRING, "DATE-HDU", tdate, NULL, &status); if (status) { status = 0; } fits_read_key(fptr, TSTRING, "XTENSION", xtensionname, NULL, &status); if (status) { status = 0; } fits_read_key(fptr, TSTRING, "STILVERS", stilversion, NULL, &status); if (status) { status = 0; } fits_read_key(fptr, TSTRING, "STILCLAS", stilclass, NULL, &status); if (status) { status = 0; } fits_read_key(fptr, TSTRING, "ORIGIN", orig, NULL, &status); if (status) { status = 0; } fits_read_key(fptr, TSTRING, "COMMENT", comm, NULL, &status); if (status) { status = 0; } fits_get_num_cols(fptr, &cnum, &status); store_funcs.append_col(m->session->tr, mvc_bind_column(m, fits_tbl, "id"), &tid, TYPE_int); store_funcs.append_col(m->session->tr, mvc_bind_column(m, fits_tbl, "name"), tname_low, TYPE_str); store_funcs.append_col(m->session->tr, mvc_bind_column(m, fits_tbl, "columns"), &cnum, TYPE_int); store_funcs.append_col(m->session->tr, mvc_bind_column(m, fits_tbl, "file_id"), &fid, TYPE_int); store_funcs.append_col(m->session->tr, mvc_bind_column(m, fits_tbl, "hdu"), &i, TYPE_int); store_funcs.append_col(m->session->tr, mvc_bind_column(m, fits_tbl, "date"), tdate, TYPE_str); store_funcs.append_col(m->session->tr, mvc_bind_column(m, fits_tbl, "origin"), orig, TYPE_str); store_funcs.append_col(m->session->tr, mvc_bind_column(m, fits_tbl, "comment"), comm, TYPE_str); store_funcs.append_col(m->session->tr, mvc_bind_column(m, fits_tp, "table_id"), &tid, TYPE_int); store_funcs.append_col(m->session->tr, mvc_bind_column(m, fits_tp, "xtension"), xtensionname, TYPE_str); store_funcs.append_col(m->session->tr, mvc_bind_column(m, fits_tp, "bitpix"), &bitpixnumber, TYPE_int); store_funcs.append_col(m->session->tr, mvc_bind_column(m, fits_tp, "stilvers"), stilversion, TYPE_str); store_funcs.append_col(m->session->tr, mvc_bind_column(m, fits_tp, "stilclas"), stilclass, TYPE_str); /* read columns description */ s = stmt; col = mvc_bind_column(m, fits_col, "id"); cid = store_funcs.count_col(tr, col, 1) + 1; for (j = 1; j <= cnum; j++, cid++) { fits_get_acolparms(fptr, j, cname, &tbcol, tunit, tform, &tscal, &tzero, tnull, tdisp, &status); snprintf(stmt, BUFSIZ, FITS_INS_COL, (int)cid, cname, tform, tunit, j, (int)tid); msg = SQLstatementIntern(cntxt, &s, "fits.attach", TRUE, FALSE); if (msg != MAL_SUCCEED) { fits_close_file(fptr, &status); return msg; } } tid++; } fits_close_file(fptr, &status); return MAL_SUCCEED; }
str FITSloadTable(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci) { mvc *m = NULL; sql_schema *sch; sql_table *fits_fl, *fits_tbl, *tbl = NULL; sql_column *col; sql_subtype tpe; fitsfile *fptr; str tname = *(str*)getArgReference(stk, pci, 1); str fname; str msg = MAL_SUCCEED; oid rid = oid_nil, frid = oid_nil; int status = 0, cnum = 0, fid, hdu, hdutype, i, j, anynull = 0, mtype; int *tpcode = NULL; long *rep = NULL, *wid = NULL, rows; char keywrd[80], **cname, nm[FLEN_VALUE]; ptr nilptr; msg = getSQLContext(cntxt, mb, &m, NULL); if (msg) return msg; sch = mvc_bind_schema(m, "sys"); fits_tbl = mvc_bind_table(m, sch, "fits_tables"); if (fits_tbl == NULL) { msg = createException(MAL, "fits.loadtable", "FITS catalog is missing.\n"); return msg; } tbl = mvc_bind_table(m, sch, tname); if (tbl) { msg = createException(MAL, "fits.loadtable", "Table %s is already created.\n", tname); return msg; } col = mvc_bind_column(m, fits_tbl, "name"); rid = table_funcs.column_find_row(m->session->tr, col, tname, NULL); if (rid == oid_nil) { msg = createException(MAL, "fits.loadtable", "Table %s is unknown in FITS catalog. Attach first the containing file\n", tname); return msg; } /* Open FITS file and move to the table HDU */ col = mvc_bind_column(m, fits_tbl, "file_id"); fid = *(int*)table_funcs.column_find_value(m->session->tr, col, rid); fits_fl = mvc_bind_table(m, sch, "fits_files"); col = mvc_bind_column(m, fits_fl, "id"); frid = table_funcs.column_find_row(m->session->tr, col, (void *)&fid, NULL); col = mvc_bind_column(m, fits_fl, "name"); fname = (char *)table_funcs.column_find_value(m->session->tr, col, frid); if (fits_open_file(&fptr, fname, READONLY, &status)) { msg = createException(MAL, "fits.loadtable", "Missing FITS file %s.\n", fname); return msg; } col = mvc_bind_column(m, fits_tbl, "hdu"); hdu = *(int*)table_funcs.column_find_value(m->session->tr, col, rid); fits_movabs_hdu(fptr, hdu, &hdutype, &status); if (hdutype != ASCII_TBL && hdutype != BINARY_TBL) { msg = createException(MAL, "fits.loadtable", "HDU %d is not a table.\n", hdu); fits_close_file(fptr, &status); return msg; } /* create a SQL table to hold the FITS table */ /* col = mvc_bind_column(m, fits_tbl, "columns"); cnum = *(int*) table_funcs.column_find_value(m->session->tr, col, rid); */ fits_get_num_cols(fptr, &cnum, &status); tbl = mvc_create_table(m, sch, tname, tt_table, 0, SQL_PERSIST, 0, cnum); tpcode = (int *)GDKzalloc(sizeof(int) * cnum); rep = (long *)GDKzalloc(sizeof(long) * cnum); wid = (long *)GDKzalloc(sizeof(long) * cnum); cname = (char **)GDKzalloc(sizeof(char *) * cnum); for (j = 1; j <= cnum; j++) { /* fits_get_acolparms(fptr, j, cname, &tbcol, tunit, tform, &tscal, &tzero, tnull, tdisp, &status); */ snprintf(keywrd, 80, "TTYPE%d", j); fits_read_key(fptr, TSTRING, keywrd, nm, NULL, &status); if (status) { snprintf(nm, FLEN_VALUE, "column_%d", j); status = 0; } cname[j - 1] = GDKstrdup(toLower(nm)); fits_get_coltype(fptr, j, &tpcode[j - 1], &rep[j - 1], &wid[j - 1], &status); fits2subtype(&tpe, tpcode[j - 1], rep[j - 1], wid[j - 1]); /* mnstr_printf(cntxt->fdout,"#%d %ld %ld - M: %s\n", tpcode[j-1], rep[j-1], wid[j-1], tpe.type->sqlname); */ mvc_create_column(m, tbl, cname[j - 1], &tpe); } /* data load */ fits_get_num_rows(fptr, &rows, &status); mnstr_printf(cntxt->fdout,"#Loading %ld rows in table %s\n", rows, tname); for (j = 1; j <= cnum; j++) { BAT *tmp = NULL; int time0 = GDKms(); mtype = fits2mtype(tpcode[j - 1]); nilptr = ATOMnil(mtype); col = mvc_bind_column(m, tbl, cname[j - 1]); tmp = BATnew(TYPE_void, mtype, rows); if ( tmp == NULL){ GDKfree(tpcode); GDKfree(rep); GDKfree(wid); GDKfree(cname); throw(MAL,"fits.load", MAL_MALLOC_FAIL); } BATseqbase(tmp, 0); if (rows > (long)REMAP_PAGE_MAXSIZE) BATmmap(tmp, STORE_MMAP, STORE_MMAP, STORE_MMAP, STORE_MMAP, 0); if (mtype != TYPE_str) { fits_read_col(fptr, tpcode[j - 1], j, 1, 1, rows, nilptr, (void *)BUNtloc(bat_iterator(tmp), BUNfirst(tmp)), &anynull, &status); BATsetcount(tmp, rows); tmp->tsorted = 0; tmp->trevsorted = 0; } else { /* char *v = GDKzalloc(wid[j-1]);*/ int bsize = 50; int tm0, tloadtm = 0, tattachtm = 0; int batch = bsize, k; char **v = (char **) GDKzalloc(sizeof(char *) * bsize); for(i = 0; i < bsize; i++) v[i] = GDKzalloc(wid[j-1]); for(i = 0; i < rows; i += batch) { batch = rows - i < bsize ? rows - i: bsize; tm0 = GDKms(); fits_read_col(fptr, tpcode[j - 1], j, 1 + i, 1, batch, nilptr, (void *)v, &anynull, &status); tloadtm += GDKms() - tm0; tm0 = GDKms(); for(k = 0; k < batch ; k++) BUNappend(tmp, v[k], TRUE); tattachtm += GDKms() - tm0; } for(i = 0; i < bsize ; i++) GDKfree(v[i]); GDKfree(v); mnstr_printf(cntxt->fdout,"#String column load %d ms, BUNappend %d ms\n", tloadtm, tattachtm); } if (status) { char buf[FLEN_ERRMSG + 1]; fits_read_errmsg(buf); msg = createException(MAL, "fits.loadtable", "Cannot load column %s of %s table: %s.\n", cname[j - 1], tname, buf); break; } mnstr_printf(cntxt->fdout,"#Column %s loaded for %d ms\t", cname[j-1], GDKms() - time0); store_funcs.append_col(m->session->tr, col, tmp, TYPE_bat); mnstr_printf(cntxt->fdout,"#Total %d ms\n", GDKms() - time0); BBPunfix(tmp->batCacheid); } GDKfree(tpcode); GDKfree(rep); GDKfree(wid); GDKfree(cname); fits_close_file(fptr, &status); return msg; }
/*--------------------------------------------------------------------------*/ int ffprwu(fitsfile *fptr, LONGLONG firstrow, LONGLONG nrows, int *status) /* * fits_write_nullrows / ffprwu - write TNULLs to all columns in one or more rows * * fitsfile *fptr - pointer to FITS HDU opened for read/write * long int firstrow - first table row to set to null. (firstrow >= 1) * long int nrows - total number or rows to set to null. (nrows >= 1) * int *status - upon return, *status contains CFITSIO status code * * RETURNS: CFITSIO status code * * written by Craig Markwardt, GSFC */ { LONGLONG ntotrows; int ncols, i; int typecode = 0; LONGLONG repeat = 0, width = 0; int nullstatus; if (*status > 0) return *status; if ((firstrow <= 0) || (nrows <= 0)) return (*status = BAD_ROW_NUM); fits_get_num_rowsll(fptr, &ntotrows, status); if (firstrow + nrows - 1 > ntotrows) return (*status = BAD_ROW_NUM); fits_get_num_cols(fptr, &ncols, status); if (*status) return *status; /* Loop through each column and write nulls */ for (i=1; i <= ncols; i++) { repeat = 0; typecode = 0; width = 0; fits_get_coltypell(fptr, i, &typecode, &repeat, &width, status); if (*status) break; /* NOTE: data of TSTRING type must not write the total repeat count, since the repeat count is the *character* count, not the nstring count. Divide by string width to get number of strings. */ if (typecode == TSTRING) repeat /= width; /* Write NULLs */ nullstatus = 0; fits_write_col_null(fptr, i, firstrow, 1, repeat*nrows, &nullstatus); /* ignore error if no null value is defined for the column */ if (nullstatus && nullstatus != NO_NULL) return (*status = nullstatus); } return *status; }
bool WMAPSource::initFile( ) { bool bRetVal = true; int iResult = 0; _numFrames = 0; if( !_filename.isNull( ) && !_filename.isEmpty( ) ) { QString str; fitsfile* ffits; int iStatus = 0; iResult = fits_open_file( &ffits, _filename.ascii( ), READONLY, &iStatus ); if( iResult == 0 ) { int iNumHeaderDataUnits; if( fits_get_num_hdus( ffits, &iNumHeaderDataUnits, &iStatus ) == 0 ) { long lNumBaseRows = 0; long lNumRows; int iHDUType; int i; // // determine the number of frames... // for( i=0; i<iNumHeaderDataUnits-1; i++ ) { if( iStatus == 0 ) { fits_get_hdu_type( ffits, &iHDUType, &iStatus ); if( iHDUType == BINARY_TBL || iHDUType == ASCII_TBL ) { iResult = fits_get_num_rows( ffits, &lNumRows, &iStatus ); if( iResult == 0 ) { if( lNumBaseRows == 0 ) { lNumBaseRows = lNumRows; } else if( lNumRows != 1 ) { if( lNumRows < lNumBaseRows ) { lNumBaseRows = lNumRows; } } } } fits_movrel_hdu( ffits, 1, &iHDUType, &iStatus ); } } fits_movabs_hdu( ffits, 1, &iHDUType, &iStatus); field *fld = new field; fld->table = 0; fld->column = 0; fld->entry = 0; fld->entries = 0; fld->numSamplesPerFrame = 1; fld->numFrames = lNumBaseRows; _fields.insert( "INDEX", fld ); _fieldList.append( "INDEX" ); // // add the fields and metadata... // for( i=0; i<iNumHeaderDataUnits-1; i++ ) { if( iStatus == 0 ) { addToMetadata( ffits, iStatus ); // // create the field entries... // fits_get_hdu_type( ffits, &iHDUType, &iStatus ); if( iStatus == 0 ) { if( iHDUType == BINARY_TBL || iHDUType == ASCII_TBL ) { int iNumCols; iResult = fits_get_num_cols( ffits, &iNumCols, &iStatus ); if( iResult == 0 ) { iResult = fits_get_num_rows( ffits, &lNumRows, &iStatus ); if( iResult == 0 ) { if( lNumRows > 1 ) { addToFieldList( ffits, iNumCols, lNumRows, lNumBaseRows, iStatus ); } else if( lNumRows == 1 ) { addToMetadata( ffits, iNumCols, iStatus ); } } } } } fits_movrel_hdu( ffits, 1, &iHDUType, &iStatus); } } } iStatus = 0; updateNumFramesScalar( ); fits_close_file( ffits, &iStatus ); } } return bRetVal; }
int get_num_of_columns(fitsfile * fptr) { int result; fits_get_num_cols(fptr, &result, &fitsio_status); return result; }
KstObject::UpdateType LFIIOSource::update( int u ) { Q_UNUSED( u ) KstObject::UpdateType updateType = KstObject::NO_CHANGE; QString strTemplate; QString strName; fitsfile* ffits; char charTemplate[ FLEN_CARD ]; char charName[ FLEN_CARD ]; long lNumFrames; long lMaxRepeat = 1; long lRepeat; long lWidth; int iColNumber; int iNumCols; int iStatus = 0; int iResult = 0; int iTypeCode; int i; _valid = false; if( !_filename.isNull( ) && !_filename.isEmpty( ) ) { iResult = fits_open_table( &ffits, _filename.ascii( ), READONLY, &iStatus ); if( iResult == 0 ) { // // determine size of data... // iResult = fits_get_num_cols( ffits, &iNumCols, &iStatus ); if( iResult == 0 ) { iResult = fits_get_num_rows( ffits, &lNumFrames, &iStatus ); if( iResult == 0 ) { _strListColNames.clear( ); _valid = true; // // need to multiply lNumFrames by the maximum value of the vector repeat value... // for( i=0; i<iNumCols; i++ ) { iStatus = 0; sprintf( charTemplate, "%d", i+1 ); iResult = fits_get_colname( ffits, CASEINSEN, charTemplate, charName, &iColNumber, &iStatus ); if( iResult == 0 ) { strName = charName; _strListColNames.append( strName ); } else { strName.setNum( i ); _strListColNames.append( strName ); } iStatus = 0; iResult = fits_get_coltype( ffits, i+1, &iTypeCode, &lRepeat, &lWidth, &iStatus ); if( iResult == 0 ) { if( lRepeat > lMaxRepeat ) { lMaxRepeat = lRepeat; } } } if( lNumFrames * lMaxRepeat != _numFrames ) { _numCols = iNumCols; _numFrames = lNumFrames * lMaxRepeat; updateType = KstObject::UPDATE; } } } iStatus = 0; fits_close_file( ffits, &iStatus ); } } return updateType; }
bool PLANCKIDEFSource::initFile( ) { bool bRetVal = true; int iResult = 0; _numFrames = 0; if( !_filename.isNull( ) && !_filename.isEmpty( ) ) { QString str; fitsfile* ffits; int iStatus = 0; iResult = fits_open_file( &ffits, _filename.ascii( ), READONLY, &iStatus ); if( iResult == 0 ) { int iNumHeaderDataUnits; if( fits_get_num_hdus( ffits, &iNumHeaderDataUnits, &iStatus ) == 0 ) { long lNumRows; int iHDUType; int i; // // determine the number of frames... // if( iNumHeaderDataUnits > 1 ) { if( fits_movabs_hdu( ffits, 2, &iHDUType, &iStatus ) == 0 ) { if( fits_get_hdu_type( ffits, &iHDUType, &iStatus ) == 0 ) { if( iHDUType == BINARY_TBL ) { iResult = fits_get_num_rows( ffits, &lNumRows, &iStatus ); if( iResult == 0 ) { _numFrames = lNumRows; } } } } } if( _numFrames > 0 ) { fits_movabs_hdu( ffits, 1, &iHDUType, &iStatus ); field *fld = new field; fld->table = 0; fld->column = 0; _fields.insert( "INDEX", fld ); _fieldList.append( "INDEX" ); // // add the fields and metadata... // for( i=0; i<iNumHeaderDataUnits-1; i++ ) { if( iStatus == 0 ) { addToMetadata( ffits, iStatus ); // // the first table never contains data... // if( i > 0 ) { // // create the time entries if necessary... // if( _fields.find( "TIME_ABSOLUTE" ) == 0L && _fields.find( "TIME_RELATIVE" ) == 0L ) { char comment[FLEN_COMMENT]; int iStatusDelta = 0; int iStatusZero = 0; fits_read_key( ffits, TDOUBLE, "DELTA_T", &_dTimeDelta, comment, &iStatusDelta ); fits_read_key( ffits, TDOUBLE, "TIMEZERO", &_dTimeZero, comment, &iStatusZero ); if( iStatusDelta == 0 ) { if( iStatusZero == 0 ) { field *fld = new field; fld->table = 0; fld->column = 0; _fields.insert( "TIME_ABSOLUTE", fld ); _fieldList.append( "TIME_ABSOLUTE" ); } else { field *fld = new field; fld->table = 0; fld->column = 0; _fields.insert( "TIME_RELATIVE", fld ); _fieldList.append( "TIME_RELATIVE" ); } } } // // create the field entries... // fits_get_hdu_type( ffits, &iHDUType, &iStatus ); if( iStatus == 0 ) { if( iHDUType == BINARY_TBL || iHDUType == ASCII_TBL ) { int iNumCols; iResult = fits_get_num_cols( ffits, &iNumCols, &iStatus ); if( iResult == 0 ) { iResult = fits_get_num_rows( ffits, &lNumRows, &iStatus ); if( iResult == 0 ) { addToFieldList( ffits, iNumCols, iStatus ); } } } } } fits_movrel_hdu( ffits, 1, &iHDUType, &iStatus); } } } } iStatus = 0; updateNumFramesScalar( ); fits_close_file( ffits, &iStatus ); } } return bRetVal; }
int main(int argc, char *argv[]) { fitsfile *fptr = 0; /* FITS file pointer, defined in fitsio.h */ char keyname[FLEN_KEYWORD], colname[FLEN_VALUE], coltype[FLEN_VALUE]; int status = 0; /* CFITSIO status value MUST be initialized to zero! */ int single = 0, hdupos = 0, hdutype = 0, bitpix = 0, naxis = 0, ncols = 0, ii = 0; long naxes[10], nrows = 0; int printhelp = (argc == 2 && (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help") == 0)); if (printhelp || argc != 2) { fprintf(stderr, "Usage: %s filename[ext] \n", argv[0]); fprintf(stderr, "\n"); fprintf(stderr, "List the structure of a single extension, or, if ext is \n"); fprintf(stderr, "not given, list the structure of the entire FITS file. \n"); fprintf(stderr, "\n"); fprintf(stderr, "Note that it may be necessary to enclose the input file\n"); fprintf(stderr, "name in single quote characters on the Unix command line.\n"); return (0); } FILE *fout = popen(PAGER, "w"); if (fout == NULL) { fprintf(stderr, "Could not execute '%s'\n", PAGER); return (1); } if (!fits_open_file(&fptr, argv[1], READONLY, &status)) { fits_get_hdu_num(fptr, &hdupos); /* Get the current HDU position */ /* List only a single structure if a specific extension was given */ if (strchr(argv[1], '[') || strchr(argv[1], '+')) { single++; } for (; !status; hdupos++) { /* Main loop for each HDU */ fits_get_hdu_type(fptr, &hdutype, &status); /* Get the HDU type */ fprintf(fout, "\nHDU #%d ", hdupos); if (hdutype == IMAGE_HDU) { /* primary array or image HDU */ fits_get_img_param(fptr, 10, &bitpix, &naxis, naxes, &status); fprintf(fout, "Array: NAXIS = %d, BITPIX = %d\n", naxis, bitpix); for (ii = 0; ii < naxis; ii++) { fprintf(fout, " NAXIS%d = %ld\n",ii+1, naxes[ii]); } } else { /* a table HDU */ fits_get_num_rows(fptr, &nrows, &status); fits_get_num_cols(fptr, &ncols, &status); if (hdutype == ASCII_TBL) { fprintf(fout, "ASCII Table: "); } else { fprintf(fout, "Binary Table: "); } fprintf(fout, "%d columns x %ld rows\n", ncols, nrows); fprintf(fout, " COL NAME FORMAT\n"); for (ii = 1; ii <= ncols; ii++) { fits_make_keyn("TTYPE", ii, keyname, &status); /* make keyword */ fits_read_key(fptr, TSTRING, keyname, colname, NULL, &status); fits_make_keyn("TFORM", ii, keyname, &status); /* make keyword */ fits_read_key(fptr, TSTRING, keyname, coltype, NULL, &status); fprintf(fout, " %3d %-16s %-16s\n", ii, colname, coltype); } } if (single) { break; /* quit if only listing a single HDU */ } fits_movrel_hdu(fptr, 1, NULL, &status); /* try move to next ext */ } if (status == END_OF_FILE) { status = 0; /* Reset normal error */ } fits_close_file(fptr, &status); } pclose(fout); if (status) { fits_report_error(stderr, status); /* print any error message */ } return (status); }
int ReadASPData(struct ASPHdr *hdr, struct SubHdr *subhdr, struct RunVars *RunMode, fitsfile *Fin, int nscan, int NPtsProf, double **ASquared, double **BSquared, double **ReAconjB, double **ImAconjB, long **SampleCount) { int i, j, status=0, datacolnum, refcolnum, anynull; int NColumns; char Rawfile[100]; FILE *s; fits_get_num_cols(Fin, &NColumns, &status);status=0; /* malloc array for each channel based on profile size */ for(i=0;i<hdr->obs.NChan;i++){ ASquared[i] = (double *)malloc(NPtsProf*sizeof(double)); DZero(&ASquared[i][0], NPtsProf); BSquared[i] = (double *)malloc(NPtsProf*sizeof(double)); DZero(&BSquared[i][0], NPtsProf); ReAconjB[i] = (double *)malloc(NPtsProf*sizeof(double)); DZero(&ReAconjB[i][0], NPtsProf); ImAconjB[i] = (double *)malloc(NPtsProf*sizeof(double)); DZero(&ImAconjB[i][0], NPtsProf); SampleCount[i] = (long *)malloc(NPtsProf*sizeof(long)); LZero(&SampleCount[i][0], NPtsProf); } /* get middle dump time */ if(!strcmp(hdr->gen.HdrVer,"Ver1.0")) fits_read_key(Fin, TDOUBLE, "DUMPMIDSECS", &(subhdr->DumpMiddleSecs), NULL, &status); status = 0; /* move to ref table for newer version */ if(!strcmp(hdr->gen.HdrVer,"Ver1.0.1")) { fits_read_key(Fin, TDOUBLE, "MIDSECS", &(subhdr->DumpMiddleSecs), NULL, &status); status = 0; refcolnum = 0; fits_read_col(Fin, TDOUBLE, ++refcolnum, 1, 1, (long)hdr->obs.NChan, NULL, subhdr->DumpRefPhase, &anynull, &status); fits_read_col(Fin, TDOUBLE, ++refcolnum, 1, 1, (long)hdr->obs.NChan, NULL, subhdr->DumpRefPeriod, &anynull, &status); if(RunMode->Verbose){ printf("Dump %d: TIME OF DUMP = %lf\n",nscan,subhdr->DumpMiddleSecs); printf(" CHANNEL (MHz) REF. PHASE REF. PERIOD (s)\n"); printf(" ------------- ---------- ---------------\n"); for(i=0;i<hdr->obs.NChan;i++) printf(" %13.1lf%13.8lf%18.11lf\n", hdr->obs.ChanFreq[i], subhdr->DumpRefPhase[i], subhdr->DumpRefPeriod[i]); fflush(stdout); } /* Move to next HDU, i.e. data table for this dump */ fits_movrel_hdu(Fin, 1, NULL, &status); } datacolnum = 0; for(i=0;i<hdr->obs.NChan;i++) { fits_read_col(Fin, TDOUBLE, ++datacolnum, 1, 1, NPtsProf, NULL, &ASquared[i][0], &anynull, &status); fits_read_col(Fin, TDOUBLE, ++datacolnum, 1, 1, NPtsProf, NULL, &BSquared[i][0], &anynull, &status); fits_read_col(Fin, TDOUBLE, ++datacolnum, 1, 1, NPtsProf, NULL, &ReAconjB[i][0], &anynull, &status); fits_read_col(Fin, TDOUBLE, ++datacolnum, 1, 1, NPtsProf, NULL, &ImAconjB[i][0], &anynull, &status); if(!RunMode->OldFits) fits_read_col(Fin, TLONG, ++datacolnum, 1, 1, NPtsProf, NULL, &SampleCount[i][0], &anynull, &status); /* Normalize */ if(!RunMode->OldFits){ for(j=0;j<NPtsProf;j++){ ASquared[i][j] /= (double)SampleCount[i][j]; BSquared[i][j] /= (double)SampleCount[i][j]; ReAconjB[i][j] /= (double)SampleCount[i][j]; ImAconjB[i][j] /= (double)SampleCount[i][j]; } } } for(i=0;i<hdr->obs.NChan;i++) { /* read in reference phase, period if old version */ if(!strcmp(hdr->gen.HdrVer,"Ver1.0")){ fits_read_key(Fin, TDOUBLE, "DUMPREFPER", &(subhdr->DumpRefPeriod[i]), NULL, &status); status = 0; fits_read_key(Fin, TDOUBLE, "DUMPREFPHASE", &(subhdr->DumpRefPhase[i]), NULL, &status); status = 0; if(RunMode->Verbose && i==0){ printf("Dump %d: TIME OF DUMP = %lf\n",nscan, subhdr->DumpMiddleSecs); printf(" REF. PHASE = %lf, REF. PERIOD = %lf s\n", subhdr->DumpRefPhase[i], subhdr->DumpRefPeriod[i]);fflush(stdout); } } /* sprintf(HeadLine[i],"# %.1f %.7f %.10f %ld %.3f %.3f %d %s %d %s %.10f", (double)hdr->obs.IMJDStart, subhdr->DumpMiddleSecs, subhdr->DumpRefPeriod[i], (long)1,hdr->obs.ChanFreq[i], hdr->obs.DM, RunMode->NBinsOut, hdr->obs.ObsvtyCode, 1, hdr->target.PSRName, subhdr->DumpRefPhase[i]); if (RunMode->MakeRaw){ sprintf(Rawfile,"%s.%4.4d.%4.4d.prof.raw.asc",RunMode->OutfileRoot, (int)(hdr->obs.ChanFreq[i]),nscan); s = fopen(Rawfile, "w"); if (s==NULL) { printf("ASPFitsReader: Can't open file %s\n",Rawfile); fflush(stdout); exit(1); } fprintf(s,"%s\n",HeadLine[i]); for (j=0; j<NPtsProf; j++) { fprintf(s,"%d %lf %lf %lf %lf %d\n",j, ASquared[i][j], BSquared[i][j], ReAconjB[i][j], ImAconjB[i][j], SampleCount[i][j]); } fclose(s); } */ } return 1; }