[Kst] extragear/graphics/kst/src/datasources/libfitstools
Ted Kisner
tskisner.public at gmail.com
Sun Jun 4 00:02:32 CEST 2006
SVN commit 547921 by tskisner:
More additions to libfitstools. Next step is to begin outline of fitsgeneral datasource so that I can test these functions.
M +245 -0 fitstools.cpp
M +12 -7 fitstools.h
--- trunk/extragear/graphics/kst/src/datasources/libfitstools/fitstools.cpp #547920:547921
@@ -43,6 +43,7 @@
return array;
}
+
int fitsSarrayFree(char **array, size_t nstring)
{
size_t i;
@@ -119,7 +120,251 @@
}
+int fitsNamesUnits( fitsfile *fp, int HDU, QStringList *names, QStringList *units ) {
+
+ int nString = 0;
+
+ // clear the lists
+ names->clear();
+ units->clear();
+
+ // move to desired HDU
+ int ret = 0;
+ int hduType;
+ if (fits_movabs_hdu(fp, HDU, &hduType, &ret)) {
+ return nString;
+ }
+
+ // for each type of HDU, find the column names or assign
+ // a generic image name (since images don't have names)
+
+ QString curName;
+ QString curUnits;
+ int maxDim;
+ long rowLen;
+ long nRows;
+ int nCols;
+ long tbCol;
+ char **fitsNames;
+ char **fitsUnits;
+ long *dimAxes = NULL;
+ int nAxis;
+ switch (hduType) {
+ case IMAGE_HDU:
+ // find the size of the image and include it in the
+ // generic name.
+ maxDim = 100; // no images should have > 100 dimensions...
+ dimAxes = (long*)calloc(maxDim, sizeof(long));
+
+ if (fits_read_imghdr(fp, maxDim, NULL, NULL, &nAxis, dimAxes, NULL, NULL, NULL, &ret)) {
+ return nString;
+ }
+ curName = "IMAGE (";
+ for (int i = 0; i < nAxis; i++) {
+ curUnits.sprintf("%ld", dimAxes[i]);
+ curName.append(curUnits);
+ if (i != nAxis-1) {
+ curName.append("x");
+ }
+ }
+ curName.append(")");
+ names->append(curName);
+ units->append(QString());
+ nString++;
+ free(dimAxes);
+ break;
+
+ case ASCII_TBL:
+ maxDim = 2147483646; // 2^32/2 - 2 (i.e. a big positive integer)
+
+ // find number of columns
+ if (fits_read_atblhdr(fp, maxDim, &rowLen, &nRows, &nCols, NULL, &tbCol, NULL, NULL, NULL, &ret)) {
+ return nString;
+ }
+
+ // allocate names and units
+ fitsNames = fitsSarrayAlloc(nCols);
+ fitsUnits = fitsSarrayAlloc(nCols);
+
+ // read names and units
+ if (fits_read_atblhdr(fp, maxDim, &rowLen, &nRows, &nCols, fitsNames, &tbCol, NULL, fitsUnits, NULL, &ret)) {
+ return nString;
+ }
+ for (int i = 0; i < nCols; i++) {
+ curName = fitsNames[i];
+ curUnits = fitsUnits[i];
+ names->append(curName);
+ units->append(curUnits);
+ nString++;
+ }
+ fitsSarrayFree(fitsNames, nCols);
+ fitsSarrayFree(fitsUnits, nCols);
+ break;
+
+ case BINARY_TBL:
+ maxDim = 2147483646; // 2^32/2 - 2 (i.e. a big positive integer)
+
+ // find number of columns
+ if (fits_read_btblhdr(fp, maxDim, &nRows, &nCols, NULL, NULL, NULL, NULL, NULL, &ret)) {
+ return nString;
+ }
+
+ // allocate names and units
+ fitsNames = fitsSarrayAlloc(nCols);
+ fitsUnits = fitsSarrayAlloc(nCols);
+
+ // read names and units
+ if (fits_read_btblhdr(fp, maxDim, &nRows, &nCols, fitsNames, NULL, fitsUnits, NULL, NULL, &ret)) {
+ return nString;
+ }
+ for (int i = 0; i < nCols; i++) {
+ curName = fitsNames[i];
+ curUnits = fitsUnits[i];
+ names->append(curName);
+ units->append(curUnits);
+ nString++;
+ }
+ fitsSarrayFree(fitsNames, nCols);
+ fitsSarrayFree(fitsUnits, nCols);
+ break;
+
+ default:
+ return nString;
+ break;
+ }
+
+ return nString;
+}
+QStringList fitsFields( fitsfile *fp, int HDU ) {
+
+ QStringList fields;
+ QStringList names;
+ QStringList units;
+
+ int nString = fitsNamesUnits(fp, HDU, &names, &units);
+ if (nString <= 0) {
+ return fields;
+ }
+
+ QString cur;
+ fields.append("INDEX");
+ for (int i = 0; i < nString; i++) {
+ if (names[i].contains("IMAGE")) {
+ cur.sprintf("%d - %s Data", i, names[i].latin1());
+ } else {
+ cur.sprintf("%d - %s", i, names[i].latin1());
+ }
+ if (units[i].isEmpty()) {
+ cur.sprintf("%s (Unknown Units)", cur.latin1());
+ } else {
+ cur.sprintf("%s (%s)", cur.latin1(), units[i].latin1());
+ }
+ fields.append(cur);
+ }
+
+ return fields;
+}
+
+QStringList fitsMatrices( fitsfile *fp, int HDU ) {
+
+ QStringList matrices;
+ QStringList names;
+ QStringList units;
+
+ int nString = fitsNamesUnits(fp, HDU, &names, &units);
+
+ if (nString <= 0) {
+ return matrices;
+ }
+
+ QString cur;
+ matrices.append("INDEX");
+
+ if (names[0].contains("IMAGE")) {
+ cur.sprintf("1 - %s", names[0].latin1());
+ } else {
+ cur.sprintf("1 - TABLE");
+ }
+ matrices.append(cur);
+
+ return matrices;
+}
+
+
+QValueList<int> fitsDim( fitsfile *fp, int HDU ) {
+
+ QValueList<int> dims;
+
+ // move to desired HDU
+ int ret = 0;
+ int hduType;
+ if (fits_movabs_hdu(fp, HDU, &hduType, &ret)) {
+ return dims;
+ }
+
+ // for each type of HDU, find the dimensions
+
+ int maxDim;
+ long rowLen;
+ long nRows;
+ int nCols;
+ long tbCol;
+ long *dimAxes = NULL;
+ int nAxis;
+ switch (hduType) {
+ case IMAGE_HDU:
+ // find the size of the image and include it in the
+ // generic name.
+ maxDim = 100; // no images should have > 100 dimensions...
+ dimAxes = (long*)calloc(maxDim, sizeof(long));
+
+ if (fits_read_imghdr(fp, maxDim, NULL, NULL, &nAxis, dimAxes, NULL, NULL, NULL, &ret)) {
+ return dims;
+ }
+ for (int i = 0; i < nAxis; i++) {
+ dims.append((int)dimAxes[i]);
+ }
+ free(dimAxes);
+ break;
+
+ case ASCII_TBL:
+ maxDim = 2147483646; // 2^32/2 - 2 (i.e. a big positive integer)
+
+ // find number of columns
+ if (fits_read_atblhdr(fp, maxDim, &rowLen, &nRows, &nCols, NULL, &tbCol, NULL, NULL, NULL, &ret)) {
+ return dims;
+ }
+
+ dims.append((int)nCols);
+ dims.append((int)nRows);
+
+ break;
+
+ case BINARY_TBL:
+ maxDim = 2147483646; // 2^32/2 - 2 (i.e. a big positive integer)
+
+ // find number of columns
+ if (fits_read_btblhdr(fp, maxDim, &nRows, &nCols, NULL, NULL, NULL, NULL, NULL, &ret)) {
+ return dims;
+ }
+
+ dims.append((int)nCols);
+ dims.append((int)nRows);
+
+ break;
+
+ default:
+ return dims;
+ break;
+ }
+
+ return dims;
+}
+
+
+// vim: ts=2 sw=2 et
+
--- trunk/extragear/graphics/kst/src/datasources/libfitstools/fitstools.h #547920:547921
@@ -20,7 +20,10 @@
#include <math.h>
#include <fitsio.h>
+#include <qpair.h>
#include <qstring.h>
+#include <qvaluelist.h>
+#include <qstringlist.h>
#include <qmap.h>
// convenience functions
@@ -33,20 +36,22 @@
int fitsNHDU( fitsfile *fp );
-//int fitsDim( fitsfile *fp, int HDU, int *rows, int *cols );
-
QMap<QString, QString> fitsKeys( fitsfile *fp, int HDU );
-//QStringList fitsNames( fitsfile *fp, int HDU );
+int fitsNamesUnits( fitsfile *fp, int HDU, QStringList *names, QStringList *units );
-//QStringList fitsUnits( fitsfile *fp, int HDU );
+QStringList fitsFields( fitsfile *fp, int HDU );
-//QStringList fitsFields( fitsfile *fp, int HDU );
-
-//QStringList fitsMatrices( fitsfile *fp, int HDU );
+QStringList fitsMatrices( fitsfile *fp, int HDU );
+QValueList<int> fitsDim( fitsfile *fp, int HDU );
+// read data from file
+
+
+
+
#endif
// vim: ts=2 sw=2 et
More information about the Kst
mailing list