Main Page | Modules | Data Structures | File List | Data Fields | Globals | Related Pages

lib/external.c

Go to the documentation of this file.
00001 /* === Doxygen Comment ======================================= */
00012 /* =========================================================== */
00013 
00014 #include <IFU_io.h>
00015 #include <IFU_math.h>
00016 #include <snifs.h>
00017 
00083 /* === Doxygen Comment ======================================= */
00098 /* =========================================================== */
00099 
00100 long RD_2col(TABLE *table, int col1, double *data1, int col2, double *data2)
00101 {
00102   int l, n, type[2];
00103   float fjunk;
00104 
00105   /* ================================================================== */
00106   /* Check if the columns are in float or double                        */
00107   /* ================================================================== */
00108 
00109   get_col_info(table, col1 ,&(type[0]), NULL, NULL);
00110 
00111   if (type[0] != FLOAT && type[0] != DOUBLE) {
00112     print_error("Incorrect type for column 1 (expecting FLOAT or DOUBLE)");
00113     return(ERR_BAD_COL);
00114   }
00115 
00116   if (col2) {
00117     get_col_info(table, col2 ,&(type[1]), NULL, NULL);
00118 
00119     if (type[1] != FLOAT && type[1] != DOUBLE) {
00120       print_error("Incorrect type for column 2 (expecting FLOAT or DOUBLE)");
00121       return(ERR_BAD_COL);
00122     }
00123   }
00124   
00125   /* ================================================================== */
00126   /* Loop on the rows of the table                                      */
00127   /* ================================================================== */
00128 
00129   for (l=0, n=0; l<table->row; l++) {
00130     if (type[0] == FLOAT) {
00131       if (RD_tbl(table, l, col1, &fjunk) == ERR_NODATA) continue;
00132       data1[n] = (double)fjunk;
00133     }
00134     else if (RD_tbl(table, l, col1, data1+n) == ERR_NODATA) continue;
00135 
00136     if (col2) {
00137       if (type[1] == FLOAT) {
00138         if (RD_tbl(table, l, col2, &fjunk) == ERR_NODATA) continue;
00139         data2[n] = (double)fjunk;
00140       }
00141       else if (RD_tbl(table, l, col2, data2+n) == ERR_NODATA) continue;
00142     }
00143     n++;
00144   }
00145 
00146   return(n);
00147 }
00148 
00149 /* === Doxygen Comment ======================================= */
00167 /* =========================================================== */
00168 
00169 int read_2columns(char *tablename, char *col1name, char *col2name, int fclass,
00170                   double **array1, double **array2)
00171 {
00172   TABLE table;
00173 
00174   int col1, col2, npts;
00175 
00176   /* Open input table */
00177   if (open_table(&table, tablename, "I") < 0) {
00178     print_error("Cannot open table %s", tablename);
00179     return(ERR_OPEN);
00180   }
00181 
00182   /* Check fclass if needed (>0) */
00183   if (fclass && read_file_class(&table) != fclass) {
00184     print_error("Wrong fclass (%d) for table %s (should be %d)",
00185                 read_file_class(&table), tablename, fclass);
00186     close_table(&table);
00187     return(ERR_BAD_COL);
00188   }
00189 
00190   /* Get the column IDs */
00191   if ((col1 = get_col_ref(&table, col1name)) < 0 || 
00192       (col2 = get_col_ref(&table, col2name)) < 0) {
00193     print_error("Cannot find column %s or %s in table %s", 
00194                 col1name, col2name, tablename);
00195     close_table(&table);
00196     return(ERR_NOCOL);
00197   }
00198 
00199   /* Allocating memory and reading the table */
00200   *array1 = (double *)malloc(table.row*sizeof(double));
00201   *array2 = (double *)malloc(table.row*sizeof(double));
00202 
00203   npts = RD_2col(&table, col1, *array1, col2, *array2);
00204 
00205   close_table(&table);
00206 
00207   return(npts);
00208 }
00209 
00210 /* === Doxygen Comment ======================================= */
00235 /* =========================================================== */
00236 
00237 int read_fluxref_table(char *tablename, char *lbdacolname, char *fluxcolname,
00238                        double **lbda, double **flux, char *flxunits,
00239                        char *rescolname, double **specres)
00240 {
00241   TABLE table;
00242   int lbdacol, fluxcol, rescol, npts, type;
00243 
00244   /* Open input table */
00245   if (open_table(&table, tablename, "I") < 0) {
00246     print_error("Cannot open reference flux table %s", tablename);
00247     return(ERR_OPEN);
00248   }
00249   handle_select_flag(&table,'W',NULL);
00250 
00251   /* Check fclass = TBL_FLUX_REF */
00252   if (read_file_class(&table) != TBL_FLUX_REF) {
00253     print_error("Table %s should be a 'Reference flux table' "
00254                 "(fclass=%d, got %d)",
00255                 tablename, TBL_FLUX_REF, read_file_class(&table));
00256     close_table(&table);
00257     return(ERR_BAD_TYPE);
00258   }
00259 
00260   /* Get column IDs */
00261   if ((lbdacol = get_col_ref(&table, lbdacolname)) < 0 || 
00262       (fluxcol = get_col_ref(&table, fluxcolname)) < 0) {
00263     print_error("Cannot find column %s or %s in ref. flux table %s", 
00264                 lbdacolname, fluxcolname, tablename);
00265     close_table(&table);
00266     return(ERR_NOCOL);
00267   }
00268 
00269   /* Allocating memory and reading the table */
00270   *lbda = (double *)malloc(table.row*sizeof(double));
00271   *flux = (double *)malloc(table.row*sizeof(double));
00272 
00273   npts = RD_2col(&table, lbdacol, *lbda, fluxcol, *flux);
00274 
00275   flxunits[0] = '\0';                        /* Default: empty string */
00276   /* Try to read flux units from column units */
00277   get_col_info(&table, fluxcol ,&type, NULL, flxunits);
00278   if (strlen(flxunits)) {
00279     if (DEBUG) print_msg("Table %s col %s: units = '%s'",
00280                          tablename, fluxcolname, flxunits);
00281   }
00282   else {
00283     /* If empty, read units from FLXUNITS keyword */
00284     if (DEBUG) 
00285       print_warning("Reading flux units from keyword %s in table %s", 
00286                     FLXUNITS, tablename);
00287     disable_user_warnings();
00288     if (RD_desc(&table, FLXUNITS, CHAR, lg_unit+1, flxunits) < 0) 
00289       print_warning("Cannot read flux units keyword %s in table %s", 
00290                     FLXUNITS, tablename);
00291     else if (DEBUG) print_msg("Table %s key %s: units = '%s'", 
00292                               tablename, FLXUNITS, flxunits);
00293     restore_user_warnings();
00294   }
00295 
00296   /* Spectral resolution (if non-NULL) */
00297 
00298   if (rescolname != NULL) {
00299   
00300     if ((rescol = get_col_ref(&table, rescolname)) < 0) {
00301       print_error("Cannot find column %s in ref. flux table %s", 
00302                   rescolname, tablename);
00303       close_table(&table);
00304       return(ERR_NOCOL);
00305     }
00306 
00307     *specres = (double *)malloc(table.row*sizeof(double));
00308     if (RD_2col(&table, rescol, *specres, 0, NULL) != npts) {
00309       /* Paranoiac check */
00310       print_error("Cannot read enough spectral resolutions.");
00311       close_table(&table);
00312       return(ERR_BAD_COL);
00313     }
00314   }
00315 
00316   close_table(&table);
00317 
00318   return(npts);
00319 }
00320 
00321 /* === Doxygen Comment ======================================= */
00340 /* =========================================================== */
00341 
00342 int read_lbdaref_table(char *tablename, char *lbdaname, char *flagname,
00343                        float **lbda, float lbda_inf, float lbda_sup)
00344 {
00345   TABLE table;
00346   int *iflag, collbda, colflag, dtype, nlbda, i;
00347   float *tmplbda;
00348 
00349   /* Open input table */
00350   if (open_table(&table,tablename,"I") < 0) {
00351     print_error("Cannot open reference wavelength table %s",tablename);
00352     return(ERR_OPEN);
00353   }
00354   handle_select_flag(&table,'W',NULL);
00355     
00356   /* Get column IDs */
00357   if ((collbda = get_col_ref(&table,lbdaname)) < 0 ||
00358       (colflag = get_col_ref(&table,flagname)) < 0) {
00359     print_error("Cannot find column %s or %s in ref. wavelength table %s",
00360                 lbdaname, flagname, table.name);
00361     close_table(&table);
00362     return(ERR_NOCOL);
00363   }
00364 
00365   /* Check column types */
00366   get_col_info(&table, collbda, &dtype, NULL, NULL);
00367   if (dtype != FLOAT) {
00368     print_error("Column %s in table %s should be a FLOAT column (%d)",
00369                 lbdaname, table.name, dtype);
00370     close_table(&table);
00371     return(ERR_BAD_TYPE);
00372   }
00373   get_col_info(&table, colflag, &dtype, NULL, NULL);
00374   if (dtype != INT && dtype != LONG) {
00375     print_error("Column %s in table %s should be a INT/LONG column (%d)",
00376                 flagname, table.name, dtype);
00377     close_table(&table);
00378     return(ERR_BAD_TYPE);
00379   }
00380 
00381   *lbda = (float *)malloc(table.row*sizeof(float));
00382 
00383   /* Read *all* the lambda */ 
00384   tmplbda = (float *)malloc(table.row*sizeof(float));
00385   iflag   =   (int *)malloc(table.row*sizeof(int));
00386   RD_col(&table,collbda,tmplbda);
00387   RD_col(&table,colflag,iflag);
00388 
00389   /* Select lambda in the proper range */
00390   for (nlbda=0, i=0; i<table.row; i++) {
00391     if (iflag[i] && 
00392         tmplbda[i] >= lbda_inf && tmplbda[i] <= lbda_sup) {
00393       (*lbda)[nlbda++] = tmplbda[i];
00394       if (DEBUG) print_msg("   Line %2d: %.1f AA",nlbda,tmplbda[i]);
00395     }
00396     else if (DEBUG && !iflag[i]) {
00397       print_msg("   Line %.1f AA flagged-out",tmplbda[i]);
00398     }
00399   }
00400   free(tmplbda);
00401   free(iflag);
00402 
00403   print_msg("-> %d/%d lines selected in %.1f-%.1f AA",
00404             nlbda,table.row,lbda_inf,lbda_sup);
00405 
00406   close_table(&table);
00407 
00408   return(nlbda);
00409 }

Generated on Wed Oct 26 23:59:39 2005 for Snifs by doxygen 1.3.5