irplib_cat.c

00001 /* $Id: irplib_cat.c,v 1.5 2009/03/06 10:30:05 llundin Exp $
00002  *
00003  * This file is part of the irplib package
00004  * Copyright (C) 2002,2003 European Southern Observatory
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02111-1307  USA
00019  */
00020 
00021 /*
00022  * $Author: llundin $
00023  * $Date: 2009/03/06 10:30:05 $
00024  * $Revision: 1.5 $
00025  * $Name: visir-3_2_2 $
00026  */
00027 
00028 #ifdef HAVE_CONFIG_H
00029 #include <config.h>
00030 #endif
00031 
00032 /*-----------------------------------------------------------------------------
00033                                    Includes
00034  -----------------------------------------------------------------------------*/
00035 
00036 #include <libgen.h>
00037 #include <math.h>
00038 #include <cpl.h>
00039 
00040 #include "irplib_cat.h"
00041 #include "irplib_wcs.h"
00042 
00043 #define FILENAME_SZBUF 1024
00044 
00045 #if defined CPL_VERSION_CODE && CPL_VERSION_CODE >= CPL_VERSION(4, 8, 0)
00046 /*----------------------------------------------------------------------------*/
00050 /*----------------------------------------------------------------------------*/
00051 
00054 /*---------------------------------------------------------------------------*/
00073 /*---------------------------------------------------------------------------*/
00074 
00075 int irplib_2mass_get_catpars
00076 (cpl_frame *  master_index,
00077  char      ** catpath,
00078  char      ** catname)
00079 {
00080     cpl_propertylist * p;
00081     const char       * unk = "unknown";
00082     char             * fname;
00083     int                status;
00084 
00085     /* Initialise a few things */
00086     *catpath = NULL;
00087     *catname = NULL;
00088 
00089     /* First get the full path to the index file and make sure it exists */
00090     fname = cpl_strdup(cpl_frame_get_filename(master_index));
00091     if (access((const char *)fname,R_OK) != 0)
00092     {
00093          cpl_msg_error(__func__,"Can't access index file %s",fname);
00094          cpl_free(fname);
00095          return CPL_ERROR_FILE_IO;
00096     }
00097     *catpath = cpl_strdup(dirname(fname));
00098 
00099     /* Try to load the propertylist. If it is not possible signal a fatal
00100        error since this probably means the whole file is messed up */
00101     if ((p = cpl_propertylist_load(cpl_frame_get_filename(master_index),0)) == NULL)
00102     {
00103         cpl_msg_error(__func__,"Can't load index file header %s",fname);
00104         cpl_free(*catpath);
00105         cpl_free(fname);
00106         return CPL_ERROR_FILE_IO;
00107     }
00108     
00109     /* If there is a catalogue name in the header then send it back. If there
00110        isn't then give a default name and send a warning */
00111     if (cpl_propertylist_has(p,"CATNAME")) 
00112     {
00113         *catname = cpl_strdup(cpl_propertylist_get_string(p,"CATNAME"));
00114         status = CPL_ERROR_NONE;
00115     } else {
00116         *catname = cpl_strdup(unk);
00117         cpl_msg_warning(__func__,"Property CATNAME not in index file header %s",
00118                         fname);
00119     }
00120 
00121     /* Free and return */
00122     cpl_free(fname);
00123     cpl_propertylist_delete(p);
00124     return(status);
00125 }
00126 
00127 
00128 
00129 /*---------------------------------------------------------------------------*/
00152 /*---------------------------------------------------------------------------*/
00153 
00154 cpl_error_code irplib_cat_get_image_limits
00155 (cpl_wcs          * wcs,
00156  float              ext_search,
00157  double           * ra1,
00158  double           * ra2,
00159  double           * dec1,
00160  double           * dec2)
00161 {
00162     double            ra;
00163     double            dec;
00164     double            x;
00165     double            y;
00166     double            dra;
00167     double            ddec;
00168     double            min_4q;
00169     double            max_1q;
00170     int               first_quad;
00171     int               fourth_quad;
00172     const int       * naxes;
00173     long              i;
00174     long              j;
00175     const cpl_array * a;
00176 
00177     /* Initialise these in case of failure later*/
00178     *ra1 = 0.0;
00179     *ra2 = 0.0;
00180     *dec1 = 0.0;
00181     *dec2 = 0.0;
00182 
00183     /* Grab the WCS info from the property list */
00184     if (wcs == NULL) 
00185         return CPL_ERROR_DATA_NOT_FOUND;
00186     
00187     /* Get the size of the data array */
00188 
00189     a = cpl_wcs_get_image_dims(wcs);
00190     if(a == NULL)
00191         return CPL_ERROR_ILLEGAL_INPUT;
00192     naxes = cpl_array_get_data_int_const(a);
00193 
00194     /* Find the RA and Dec limits of the image */
00195 
00196     *ra1 = 370.0;
00197     *ra2 = -370.0;
00198     *dec1 = 95.0;
00199     *dec2 = -95.0;
00200     first_quad = 0;
00201     fourth_quad = 0;
00202     min_4q = 370.0;
00203     max_1q = 0.0;
00204     for (j = 1; j < naxes[1]; j += 10) {
00205         j = fmin(j,naxes[1]);
00206         y = (double)j;
00207         for (i = 1; i < naxes[0]; i += 10) {
00208             i = fmin(i,naxes[0]);
00209             x = (double)i;
00210             irplib_wcs_xytoradec(wcs,x,y,&ra,&dec);
00211             if (ra >= 0.0 && ra <= 90.0) {
00212                 first_quad = 1;
00213                 max_1q = fmax(ra,max_1q);
00214             } else if (ra >= 270.0 && ra <= 360.0) {
00215                 fourth_quad = 1;
00216                 min_4q = fmin((ra-360.0),min_4q);
00217             }
00218             *ra1 = fmin(*ra1,ra);
00219             *ra2 = fmax(*ra2,ra);
00220             *dec1 = fmin(*dec1,dec);
00221             *dec2 = fmax(*dec2,dec);
00222         }
00223     }
00224 
00225     /* Now have a look to see if you had RA values in both the first and
00226        fourth quadrants.  If you have, then make the minimum RA a negative
00227        value.  This will be the signal to the caller that you have the
00228        wraparound... */
00229 
00230     if (first_quad && fourth_quad) {
00231         *ra1 = min_4q;
00232         *ra2 = max_1q;
00233     }
00234 
00235     /* Pad out search a bit */
00236     if (ext_search) 
00237     {
00238         dra = 0.5*ext_search*(*ra2 - *ra1);
00239         *ra1 -= dra;
00240         *ra2 += dra;
00241         ddec = 0.5*ext_search*(*dec2 - *dec1);
00242         *dec1 -= ddec;
00243         *dec2 += ddec;
00244     }
00245 
00246     /* Exit */
00247     return CPL_ERROR_NONE;
00248 }
00249 
00250 /*---------------------------------------------------------------------------*/
00274 /*---------------------------------------------------------------------------*/
00275 
00276 cpl_table *  irplib_2mass_extract
00277 (char *path,
00278  float ramin,
00279  float ramax,
00280  float decmin,
00281  float decmax) 
00282 {
00283     cpl_table *t,*s;
00284     cpl_table *out;
00285     int i,nrows,start,finish,first_index,last_index,irow,init,j;
00286     int first_index_ra,last_index_ra,wrap,iwrap;
00287     float dectest,ratest,ramin_wrap,ramax_wrap;
00288     char fullname[FILENAME_SZBUF];
00289     cpl_array *a;
00290     const char *deccol[] = {"Dec"};
00291     cpl_propertylist *p;
00292 
00293     /* Create an output table */
00294 
00295     out = cpl_table_new(0);
00296     init = 1;
00297 
00298     /* Create a cpl array */
00299 
00300     /* deccol will NOT be modified */
00301     a = cpl_array_wrap_string((char **)deccol,1);
00302 
00303     /* Is there a wrap around problem? */
00304 
00305     wrap = (ramin < 0.0 && ramax > 0.0) ? 2 : 1;
00306 
00307     /* Loop for each query. If there is a wrap around problem then we need 2
00308        queries. If not, then we only need 1 */
00309 
00310     for (iwrap = 0; iwrap < wrap; iwrap++) {
00311         if (wrap == 2) {
00312             if (iwrap == 0) {
00313                 ramin_wrap = ramin + 360.0;
00314                 ramax_wrap = 360.0;
00315             } else {
00316                 ramin_wrap = 0.000001;
00317                 ramax_wrap = ramax;
00318             }
00319         } else {
00320             ramin_wrap = ramin;
00321             ramax_wrap = ramax;
00322         }
00323 
00324         /* Find out where in the index to look */
00325 
00326         first_index_ra = (int)ramin_wrap;
00327         last_index_ra = fmin((int)ramax_wrap,359);
00328     
00329         /* Look at the min and max RA and decide which files need to be 
00330            opened. */
00331 
00332         for (i = first_index_ra; i <= last_index_ra; i++) 
00333         {
00334 
00335             /* Ok, we've found one that needs opening. Read the file with 
00336                the relevant CPL call */
00337 
00338             (void)snprintf(fullname,FILENAME_SZBUF,"%s/npsc%03d.fits",path,i);
00339 
00340             /* Read the propertylist so that you know how many rows there
00341                are in the table */
00342 
00343             p = cpl_propertylist_load(fullname,1);
00344             if (p == NULL) 
00345             {
00346                 cpl_table_delete(out);
00347                 cpl_array_unwrap(a);
00348                 return(NULL);
00349             }
00350             nrows = cpl_propertylist_get_int(p, "NAXIS2");
00351             cpl_propertylist_delete(p);
00352 
00353             /* Load various rows until you find the Dec range that you 
00354              have specified. First the minimum Dec */
00355 
00356             start = 0;
00357             finish = nrows;
00358             first_index = nrows/2;
00359             while (finish - start >= 2)
00360             {
00361                 t = cpl_table_load_window(fullname, 1, 0, a, first_index, 1);
00362                 dectest = cpl_table_get_float(t, "Dec", 0, NULL);
00363                 cpl_table_delete(t);
00364                 if (dectest < decmin)
00365                 {
00366                     start = first_index;
00367                     first_index = (first_index + finish)/2;
00368                 }
00369                 else
00370                 {
00371                     finish = first_index;
00372                     first_index = (first_index + start)/2;
00373                 }
00374             }
00375 
00376             /* Load various rows until you find the Dec range that you 
00377              have specified. Now the maximum Dec */
00378 
00379             start = first_index;
00380             finish = nrows;
00381             last_index = start + (finish - start)/2;
00382             while (finish - start >= 2)
00383             {
00384                 t = cpl_table_load_window(fullname, 1, 0, a, last_index, 1);
00385                 dectest = cpl_table_get_float(t, "Dec", 0, NULL);
00386                 cpl_table_delete(t);
00387                 if (dectest < decmax)
00388                 {
00389                     start = last_index;
00390                     last_index = (last_index + finish)/2;
00391                 }
00392                 else
00393                 {
00394                     finish = last_index;
00395                     last_index = (last_index + start)/2;
00396                 }
00397             }
00398             if (last_index < first_index)
00399                 last_index = first_index;
00400 
00401             /* Ok now now load all the rows in the relevant dec limits */
00402 
00403             nrows = last_index - first_index + 1;
00404             if ((t = cpl_table_load_window(fullname, 1, 0, NULL, first_index,
00405                                            nrows)) == NULL)
00406             {
00407                 cpl_table_delete(out);
00408                 cpl_array_unwrap(a);
00409                 return (NULL);
00410             }
00411             cpl_table_unselect_all(t);
00412 
00413             /* Right, we now know what range of rows to search. Go through 
00414              these and pick the ones that are in the correct range of RA.
00415              If a row qualifies, then 'select' it. */
00416 
00417             for (j = 0; j < nrows; j++)
00418             {
00419                 ratest = cpl_table_get_float(t, "RA", j, NULL);
00420                 if (cpl_error_get_code() != CPL_ERROR_NONE)
00421                 {
00422                     cpl_table_delete(t);
00423                     cpl_array_unwrap(a);
00424                     cpl_table_delete(out);
00425                     return (NULL);
00426                 }
00427                 if (ratest >= ramin_wrap && ratest <= ramax_wrap)
00428                     cpl_table_select_row(t, j);
00429             }
00430 
00431             /* Extract the rows that have been selected now and append them
00432              onto the output table */
00433 
00434             s = cpl_table_extract_selected(t);
00435             if (init == 1)
00436             {
00437                 cpl_table_copy_structure(out, t);
00438                 init = 0;
00439             }
00440             irow = cpl_table_get_nrow(out) + 1;
00441             cpl_table_insert(out, s, irow);
00442 
00443             /* Tidy up */
00444 
00445             cpl_table_delete(t);
00446             cpl_table_delete(s);
00447         }
00448     }
00449 
00450     /* Ok, now just return the table and get out of here */
00451 
00452     cpl_array_unwrap(a);
00453     return(out);
00454 }
00455 #endif
00456 

Generated on Fri Jul 3 11:15:22 2009 for VISIR Pipeline Reference Manual by  doxygen 1.5.8