midiFitsUtility.c

00001 /******************************************************************************
00002 *******************************************************************************
00003 *               European Southern Observatory
00004 *             VLTI MIDI Data Reduction Software
00005 *
00006 * Module name:  midiFitsUtility.c
00007 * Description:  Contains general routines for FITS handling
00008 *
00009 * History:
00010 * 25-Apr-03     (csabet) Created
00011 *******************************************************************************
00012 ******************************************************************************/
00013 
00014 /******************************************************************************
00015 *   Compiler directives
00016 ******************************************************************************/
00017 
00018 /******************************************************************************
00019 *   Include files
00020 ******************************************************************************/
00021 #include <sys/stat.h>
00022 #include <stdio.h>
00023 #include <cpl.h>
00024 #include "midiGlobal.h"
00025 #include "midiLib.h"
00026 #include "midiFitsUtility.h"
00027 #include "diagnostics.h"
00028 #include "memoryHandling.h"
00029 #include "errorHandling.h"
00030 #include "fileHandling.h"
00031 #include "qfits.h"
00032 #include "qfits_rw.h"
00033 
00034 /**********************************************************
00035 *   Global Variables
00036 **********************************************************/
00037 
00038 /**********************************************************
00039 *   Constant definitions
00040 **********************************************************/
00041 
00042 /*============================ C O D E    A R E A ===========================*/
00043 
00044 
00045 
00046 /******************************************************************************
00047 *               European Southern Observatory
00048 *            VLTI MIDI Data Reduction Software 
00049 *
00050 * Module name:  getImageFormat
00051 * Input/Output: See function arguments to avoid duplication
00052 * Description:  This routine obtains all the image formats for later processing
00053 *
00054 * History:      
00055 * 14-Aug-03     (csabet) Created
00056 ******************************************************************************/
00057 void getImageFormat (
00058     char        *inFitsFile,                // In: Name of the input FITS file
00059     int         extNumOfImagingDataFile,    // In: Extension number of the IMAGING_DATA in input file
00060     ImageFormat *format,                     // Ou: Pointer to format structure
00061     int            *error)                        // Ou: Error status
00062 {
00063 
00064     //    Local Declarations
00065     //    ------------------
00066     const char    routine[] = "getImageFormat";
00067     char        *sTemp, *cleanString, *stringQfits, *emptyString;
00068     int            numOfExtensions;
00069     struct stat buf;
00070 
00071     //    Algorithm
00072     //    ---------
00073     if (diagnostic > 4)cpl_msg_info(cpl_func,"Invoking      routine   '%s' \n", routine);
00074     if (diagnostic > 4) fprintf(midiReportPtr, "Invoking      routine   '%s' \n", routine);
00075 
00076     //    Reset status
00077     *error = 0;
00078     format->numOfPinholes = 0;
00079     
00080     //    Check if the file exist
00081     if (stat (inFitsFile, &buf) == -1)
00082     {
00083         
00084         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__,     "Cannot find requested FITS file");
00085         *error = 1;
00086         return;
00087     }
00088     else if ((qfits_is_fits (inFitsFile)) != 1)  // Check if this is a FITS file
00089     {
00090         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Non-FITS file");
00091         *error = 1;
00092         return;
00093     }
00094 
00095     //    Check if the file has data tables
00096     numOfExtensions = qfits_query_n_ext (inFitsFile);
00097     if (numOfExtensions < 1)
00098     {
00099         format->hasData = 0;
00100         return;
00101     }
00102     else format->hasData = 1;
00103 
00104     //    Allocate memory
00105     cleanString = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00106     emptyString = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00107     strcpy (emptyString, "UNKNOWN");
00108 
00109     //    Get data Observation Category
00110     stringQfits = qfits_query_hdr (inFitsFile, "HIERARCH ESO DPR CATG");
00111     if (stringQfits == NULL)
00112     {
00113         sprintf (format->obsCatg, "%s", emptyString);
00114         if (diagnostic) midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot get 'DPR CATG'");
00115     }
00116     else
00117     {
00118         cleanUpString (stringQfits, cleanString);
00119         sprintf (format->obsCatg, "%s", cleanString);
00120     }
00121 
00122     //    Get data Observation Technique
00123     stringQfits = qfits_query_hdr (inFitsFile, "HIERARCH ESO DPR TECH");
00124     if (stringQfits == NULL)
00125     {
00126         sprintf (format->obsTech, "%s", emptyString);
00127         if (diagnostic) midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot get 'DPR TECH'");
00128     }
00129     else
00130     {
00131         cleanUpString (stringQfits, cleanString);
00132         sprintf (format->obsTech, "%s", cleanString);
00133     }
00134 
00135     //    Get data Observation Type
00136     stringQfits = qfits_query_hdr (inFitsFile, "HIERARCH ESO DPR TYPE");
00137     if (stringQfits == NULL)
00138     {
00139         sprintf (format->obsType, "%s", emptyString);
00140         if (diagnostic) midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot get 'DPR TYPE'");
00141     }
00142     else
00143     {
00144         cleanUpString (stringQfits, cleanString);
00145         sprintf (format->obsType, "%s", cleanString);
00146     }
00147 
00148     //    Get Shutter ID
00149     stringQfits = qfits_query_ext (inFitsFile, "HIERARCH ESO INS SHUT ID", 0);
00150     if (stringQfits == NULL) 
00151     {
00152         if (diagnostic) midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot get 'SHUT ID'");
00153         sprintf (format->shutterId, "%s", emptyString);    
00154     }
00155     else
00156     {
00157         cleanUpString (stringQfits, cleanString);
00158         sprintf (format->shutterId, "%s", cleanString);
00159     }
00160 
00161     //    Get beam combiner
00162     stringQfits = qfits_query_hdr (inFitsFile, "HIERARCH ESO INS OPT1 ID");
00163     if (stringQfits == NULL) 
00164     {
00165         if (diagnostic) midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot get 'OPT1 ID'");
00166         sprintf (format->beamCombiner, "%s", emptyString);
00167     }
00168     else
00169     {
00170         cleanUpString (stringQfits, cleanString);
00171         sprintf (format->beamCombiner, "%s", cleanString);
00172     }
00173 
00174     //    Determine if data is dispesed using the beam combiner
00175     if (strcmp (format->beamCombiner, "HIGH_SENS") == 0)
00176         format->isDisp = 1;
00177     else if (strcmp (cleanString, "SCI_PHOT") == 0)
00178         format->isDisp = 1;
00179     else if (strcmp (cleanString, "OPEN") == 0)
00180         format->isDisp = 0;
00181     else
00182     {
00183         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Unknown Beam Combiner 'OPT1 ID'");
00184         sprintf (format->beamCombiner, "%s", emptyString);
00185     }
00186 
00187     //    Get target name
00188     stringQfits = qfits_query_hdr (inFitsFile, "HIERARCH ESO OBS TARG NAME");
00189     if (stringQfits == NULL)
00190     {
00191         if (diagnostic) midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot get 'TARG NAME'");
00192         sprintf (format->targetName, "%s", emptyString);
00193     }
00194     else
00195     {
00196         cleanUpString (stringQfits, cleanString);
00197         sprintf (format->targetName, "%s", cleanString);
00198     }
00199 
00200     //    Get Grism ID
00201     stringQfits = qfits_query_hdr (inFitsFile, "HIERARCH ESO INS GRIS NAME");
00202     if (stringQfits == NULL)
00203     {
00204         if (diagnostic) midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot get 'GRIS NAME'");
00205         sprintf (format->grismId, "%s", emptyString);
00206     }
00207     else
00208     {
00209         cleanUpString (stringQfits, cleanString);
00210         sprintf (format->grismId, "%s", cleanString);
00211     }
00212 
00213     //    Get Camera Name
00214     stringQfits = qfits_query_hdr (inFitsFile, "HIERARCH ESO INS CAM ID");
00215     if (stringQfits == NULL) 
00216     {
00217         if (diagnostic) midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot read Camera ID");
00218         sprintf (format->cameraId, "%s", emptyString);
00219     }
00220     else
00221     {
00222         cleanUpString (stringQfits, cleanString);
00223         sprintf (format->cameraId, "%s", cleanString);
00224     }
00225 
00226     //    Get Filter ID
00227     stringQfits = qfits_query_hdr (inFitsFile, "HIERARCH ESO INS FILT NAME");
00228     if (stringQfits == NULL) 
00229     {
00230         if (diagnostic) midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot read Filter Name");
00231         sprintf (format->filterName, "%s", emptyString);
00232     }
00233     else
00234     {
00235         cleanUpString (stringQfits, cleanString);
00236         sprintf (format->filterName, "%s", cleanString);
00237     }
00238 
00239     //    Get TPL Start
00240     stringQfits = qfits_query_hdr (inFitsFile, "HIERARCH ESO TPL START");
00241     if (stringQfits == NULL)
00242     {
00243         if (diagnostic) midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot get 'TPL START'");
00244         sprintf (format->tplStart, "%s", emptyString);
00245     }
00246     else
00247     {
00248         cleanUpString (stringQfits, cleanString);
00249         sprintf (format->tplStart, "%s", cleanString);
00250     }
00251 
00252     //    Get TPL Name
00253     stringQfits = qfits_query_hdr (inFitsFile, "HIERARCH ESO TPL NAME");
00254     if (stringQfits == NULL)
00255     {
00256         if (diagnostic) midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot get 'TPL NAME'");
00257         sprintf (format->tplName, "%s", emptyString);
00258     }
00259     else
00260     {
00261         cleanUpString (stringQfits, cleanString);
00262         sprintf (format->tplName, "%s", cleanString);
00263     }
00264 
00265     //    Get MIDI subwindow format from the IMAGING_DATA
00266     sTemp = qfits_query_ext (inFitsFile, "TDIM14", extNumOfImagingDataFile);
00267     if (sTemp == NULL) sTemp = qfits_query_ext (inFitsFile, "TDIM17", extNumOfImagingDataFile);
00268     if (sTemp == NULL) sTemp = qfits_query_ext (inFitsFile, "TDIM20", extNumOfImagingDataFile);
00269     if (sTemp == NULL)
00270     {
00271         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot get TDIM14, TDIM17 or TDIM20");
00272         *error = 1;
00273     }
00274     else sscanf (sTemp, "'(%d,%d) '", &(format->iXWidth), &(format->iYWidth));
00275 
00276     //    Get number of frames
00277     sTemp = qfits_query_ext (inFitsFile, "NAXIS2", extNumOfImagingDataFile);
00278     if ((sTemp == NULL) || (!sscanf (sTemp, "%d", &(format->numOfFrames))))
00279     {
00280         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot get NAXIS2");
00281         *error = 1;
00282     }
00283     
00284     //    Get frames per scan
00285     sTemp = qfits_query_ext (inFitsFile, "MAXSTEP", extNumOfImagingDataFile);
00286     if ((sTemp == NULL) || (!sscanf (sTemp, "%d", &(format->framesPerScan))))
00287     {
00288         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot get MAXSTEP");
00289         *error = 1;
00290     }
00291 
00292     //    Compute sub-image size
00293     format->subWindowSize = format->iXWidth * format->iYWidth;
00294 
00295 
00296     //    Get number of pinholes. Get SLIT Name
00297     stringQfits = qfits_query_hdr (inFitsFile, "HIERARCH ESO INS SLIT NAME");
00298     if (stringQfits == NULL)
00299     {
00300         if (diagnostic) midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot get 'SLIT NAME'");
00301         cleanUpString (stringQfits, emptyString);
00302     }
00303     else
00304     {
00305         cleanUpString (stringQfits, cleanString);
00306         sprintf (format->slitName, "%s", cleanString);
00307         if (strcmp (cleanString, "SLIT_0.05") == 0) format->numOfPinholes = 1;        // size = 0.05
00308         else if (strcmp (cleanString, "SLIT_0.1") == 0) format->numOfPinholes = 1;    // size = 0.1
00309         else if (strcmp (cleanString, "SLIT_0.2") == 0) format->numOfPinholes = 1;    // size = 0.2
00310         else if (strcmp (cleanString, "S_0.4C") == 0) format->numOfPinholes = 1;    // size = 0.4
00311         else if (strcmp (cleanString, "T_0.07C") == 0) format->numOfPinholes = 3;    // size = 0.07    centred
00312         else if (strcmp (cleanString, "T_0.11C") == 0) format->numOfPinholes = 3;    // size = 0.11    centred
00313         else if (strcmp (cleanString, "T_0.17C") == 0) format->numOfPinholes = 3;    // size = 0.17    centred
00314         else if (strcmp (cleanString, "OPEN") == 0) format->numOfPinholes = 0;
00315         else if (strcmp (cleanString, "FullField") == 0) format->numOfPinholes = 0;
00316         else
00317         {
00318             if (diagnostic) midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Unknown 'SLIT NAME'");
00319             cleanUpString (stringQfits, emptyString);
00320         }
00321     }
00322     
00323     //    Compute number of detector regions
00324     format->numOfRegionsToProcess = 2;
00325     if (strcmp (format->beamCombiner, "SCI_PHOT") == 0)
00326         format->numOfDetectorRegions = 4;
00327     else
00328         format->numOfDetectorRegions = 2;
00329 
00330     //    Log results
00331     if (diagnostic) 
00332     {
00333        cpl_msg_info(cpl_func,"FORMAT OF FILE %s \n", inFitsFile);
00334        cpl_msg_info(cpl_func,"-------------- \n");
00335        cpl_msg_info(cpl_func,"Batch Template Name          = %s \n", batchTemplate);
00336        cpl_msg_info(cpl_func,"FITS TPL NAME                = %s \n", format->tplName);
00337        cpl_msg_info(cpl_func,"Observation Category         = %s \n", format->obsCatg);
00338        cpl_msg_info(cpl_func,"Observation Technique        = %s \n", format->obsTech);
00339        cpl_msg_info(cpl_func,"Observation Type             = %s \n", format->obsType);
00340        cpl_msg_info(cpl_func,"Beam Combiner                = %s \n", format->beamCombiner);
00341        cpl_msg_info(cpl_func,"Filter Name                  = %s \n", format->filterName);
00342        cpl_msg_info(cpl_func,"Shutter ID                   = %s \n", format->shutterId);
00343        cpl_msg_info(cpl_func,"Grism ID                     = %s \n", format->grismId);
00344        cpl_msg_info(cpl_func,"Target Name                  = %s \n", format->targetName);
00345        cpl_msg_info(cpl_func,"Slit Name                    = %s \n", format->slitName);
00346        cpl_msg_info(cpl_func,"TPL Start                    = %s \n", format->tplStart);
00347        cpl_msg_info(cpl_func,"Number of Frames             = %d \n", format->numOfFrames);
00348         if (strcmp (format->shutterId, "ABOPEN") == 0)
00349            cpl_msg_info(cpl_func,"Frames per scan              = %d \n", format->framesPerScan);
00350        cpl_msg_info(cpl_func,"X, Column dimension          = %d \n", format->iXWidth);
00351        cpl_msg_info(cpl_func,"Y, Column dimension          = %d \n", format->iYWidth);
00352        cpl_msg_info(cpl_func,"Sub-window size              = %d \n", format->subWindowSize);
00353        cpl_msg_info(cpl_func,"Number of Pinholes           = %d \n", format->numOfPinholes);
00354        cpl_msg_info(cpl_func,"Number of Detector Regions   = %d \n", format->numOfDetectorRegions);
00355         if (strcmp(format->beamCombiner, "SCI_PHOT") == 0)
00356         {
00357            cpl_msg_info(cpl_func,"Number of Interf Regions     = %d \n", format->numOfRegionsToProcess);
00358            cpl_msg_info(cpl_func,"Number of Photom A Regions   = %d \n", format->numOfRegionsToProcess-1);
00359            cpl_msg_info(cpl_func,"Number of Photom B Regions   = %d \n", format->numOfRegionsToProcess-1);
00360         }
00361         else
00362         {
00363            cpl_msg_info(cpl_func,"Number of Regions to process = %d \n", format->numOfRegionsToProcess);
00364         }
00365        cpl_msg_info(cpl_func,"\n");
00366     }
00367 
00368     fprintf (midiReportPtr, "FORMAT OF FILE %s \n", inFitsFile);
00369     fprintf (midiReportPtr, "-------------- \n");
00370     fprintf (midiReportPtr, "Batch Template Name          = %s \n", batchTemplate);
00371     fprintf (midiReportPtr, "FITS TPL NAME                = %s \n", format->tplName);
00372     fprintf (midiReportPtr, "Observation Category         = %s \n", format->obsCatg);
00373     fprintf (midiReportPtr, "Observation Technique        = %s \n", format->obsTech);
00374     fprintf (midiReportPtr, "Observation Type             = %s \n", format->obsType);
00375     fprintf (midiReportPtr, "Beam Combiner                = %s \n", format->beamCombiner);
00376     fprintf (midiReportPtr, "Filter Name                  = %s \n", format->filterName);
00377     fprintf (midiReportPtr, "Shutter ID                   = %s \n", format->shutterId);
00378     fprintf (midiReportPtr, "Grism ID                     = %s \n", format->grismId);
00379     fprintf (midiReportPtr, "Target Name                  = %s \n", format->targetName);
00380     fprintf (midiReportPtr, "Slit Name                    = %s \n", format->slitName);
00381     fprintf (midiReportPtr, "TPL Start                    = %s \n", format->tplStart);
00382     fprintf (midiReportPtr, "Number of Frames             = %d \n", format->numOfFrames);
00383     if (strcmp (format->shutterId, "ABOPEN") == 0)
00384         fprintf (midiReportPtr, "Frames per scan              = %d \n", format->framesPerScan);
00385     fprintf (midiReportPtr, "X, Column dimension          = %d \n", format->iXWidth);
00386     fprintf (midiReportPtr, "Y, Column dimension          = %d \n", format->iYWidth);
00387     fprintf (midiReportPtr, "Sub-window size              = %d \n", format->subWindowSize);
00388     fprintf (midiReportPtr, "Number of Pinholes           = %d \n", format->numOfPinholes);
00389     fprintf (midiReportPtr, "Number of Detector Regions   = %d \n", format->numOfDetectorRegions);
00390     if (strcmp(format->beamCombiner, "SCI_PHOT") == 0)
00391     {
00392         fprintf (midiReportPtr, "Number of Interf Regions     = %d \n", format->numOfRegionsToProcess);
00393         fprintf (midiReportPtr, "Number of Photom A Regions   = %d \n", format->numOfRegionsToProcess-1);
00394         fprintf (midiReportPtr, "Number of Photom B Regions   = %d \n", format->numOfRegionsToProcess-1);
00395     }
00396     else
00397     {
00398         fprintf (midiReportPtr, "Number of Regions to process = %d \n", format->numOfRegionsToProcess);
00399     }
00400     fprintf (midiReportPtr, "\n");
00401 
00402     //    Check validity of the format
00403     if (format->iXWidth <= 0 || format->iYWidth <= 0 || format->numOfFrames <= 0 || format->framesPerScan <= 0) 
00404     {
00405         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Invalid file format");
00406         *error = 1;
00407     }
00408 
00409     /*    Release memory */
00410     free (cleanString);
00411     free (emptyString);
00412     
00413     return;
00414 }
00415 /*****************************************************************************/
00416 
00417 
00418 /******************************************************************************
00419 *               European Southern Observatory
00420 *            VLTI MIDI Data Reduction Software
00421 *
00422 * Module name:  getFilterData
00423 * Input/Output: See function arguments to avoid duplication
00424 * Description:
00425 *
00426 * History:
00427 * 21-Jul-03     (csabet) Created
00428 ******************************************************************************/
00429 void getFilterData (
00430     char        *fileName,    // In: Name of the input FITS file
00431     FilterData    *filterInfo,// IO: Pointer to the filter data structure
00432     int            *error)        // Ou: Error status
00433 {
00434 
00435     /*  Local Declarations
00436     --------------------*/
00437     const char  routine[] = "getFilterData";
00438     char        *tempStr, *cleanString;
00439 
00440     //    Algorithm
00441     //    ---------
00442     if (diagnostic > 4)cpl_msg_info(cpl_func,"Invoking      routine   '%s' \n", routine);
00443     if (diagnostic > 4) fprintf(midiReportPtr, "Invoking      routine   '%s' \n", routine);
00444 
00445     //    Reset status
00446     *error = 0;
00447 
00448     //    Allocate memory
00449     cleanString = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00450     
00451     //    Set the default values
00452     filterInfo->optFreqLo = N_BAND_FILT_LONG;    // micron
00453     filterInfo->optFreqHi = N_BAND_FILT_SHORT;    // micron
00454 
00455     if (diagnostic)cpl_msg_info(cpl_func,"Extracting filter data from    %s \n", fileName);
00456     if (diagnostic) fprintf (midiReportPtr, "Extracting filter data from    %s \n", fileName);
00457 
00458     //    Get filter name
00459     tempStr = qfits_query_hdr (fileName, "HIERARCH ESO INS FILT NAME");
00460     if (tempStr == NULL)
00461     {
00462         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Extracting filter name");
00463        cpl_msg_info(cpl_func,"Default Lower %f Higher and Lower %f micron are used \n", filterInfo->optFreqLo, filterInfo->optFreqHi);
00464         fprintf(midiReportPtr, "Default Higher %f micron and Lower %f micron are used \n", filterInfo->optFreqLo, filterInfo->optFreqHi);
00465         free (cleanString);
00466         return;
00467     }
00468     else
00469     {
00470         cleanUpString (tempStr, cleanString);
00471         sscanf (cleanString, "%s", filterInfo->filterName);
00472     }
00473 
00474     if (strcmp (filterInfo->filterName, "Nband") == 0)
00475     {
00476         filterInfo->optFreqLo = N_BAND_FILT_LONG;
00477         filterInfo->optFreqHi = N_BAND_FILT_SHORT;
00478     }
00479     else if (strcmp (filterInfo->filterName, "OPEN") == 0)
00480     {
00481         filterInfo->optFreqLo = N_BAND_FILT_LONG;
00482         filterInfo->optFreqHi = N_BAND_FILT_SHORT;
00483     }
00484     else if (strcmp (filterInfo->filterName, "SiC") == 0)
00485     {
00486         filterInfo->optFreqLo = SIC_FILT_LONG;
00487         filterInfo->optFreqHi = SIC_FILT_SHORT;
00488     }
00489     else if (strcmp (filterInfo->filterName, "N8.7") == 0)
00490     {
00491         filterInfo->optFreqLo = N8_7_FILT_LONG;
00492         filterInfo->optFreqHi = N8_7_FILT_SHORT;
00493     }
00494     else if (strcmp (filterInfo->filterName, "ArIII") == 0)
00495     {
00496         filterInfo->optFreqLo = ArIII_FILT_LONG;
00497         filterInfo->optFreqHi = ArIII_FILT_SHORT;
00498     }
00499     else if (strcmp (filterInfo->filterName, "SIV") == 0)
00500     {
00501         filterInfo->optFreqLo = SIV_FILT_LONG;
00502         filterInfo->optFreqHi = SIV_FILT_SHORT;
00503     }
00504     else if (strcmp (filterInfo->filterName, "N11.3") == 0)
00505     {
00506         filterInfo->optFreqLo = N11_3_FILT_LONG;
00507         filterInfo->optFreqHi = N11_3_FILT_SHORT;
00508     }
00509     else if (strcmp (filterInfo->filterName, "NeII") == 0)
00510     {
00511         filterInfo->optFreqLo = NEII_FILT_LONG;
00512         filterInfo->optFreqHi = NEII_FILT_SHORT;
00513     }
00514     else
00515     {
00516         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Unknown Filter ID");
00517        cpl_msg_info(cpl_func,"             Default Higher %f micron and Lower %f micron are used \n\n", filterInfo->optFreqLo, filterInfo->optFreqHi);
00518         fprintf(midiReportPtr, "             Default Higher %f micron and Lower %f micron are used \n\n", filterInfo->optFreqLo, filterInfo->optFreqHi);
00519         free (cleanString);
00520         return;
00521     }
00522 
00523     if (diagnostic)cpl_msg_info(cpl_func,"Filter ID                       = %s \n", filterInfo->filterName);
00524     if (diagnostic)cpl_msg_info(cpl_func,"Lower optical filter wavelength = %f micron  \n", filterInfo->optFreqHi);
00525     if (diagnostic)cpl_msg_info(cpl_func,"Upper optical filter wavelength = %f micron  \n\n", filterInfo->optFreqLo);
00526     fprintf (midiReportPtr, "Filter ID                       = %s \n", filterInfo->filterName);
00527     fprintf (midiReportPtr, "Lower optical filter wavelength = %f micron  \n", filterInfo->optFreqHi);
00528     fprintf (midiReportPtr, "Upper optical filter wavelength = %f micron  \n\n", filterInfo->optFreqLo);
00529 
00530     //    Convert to THz
00531     filterInfo->optFreqLo = SPEED_OF_LIGHT / filterInfo->optFreqLo;
00532     filterInfo->optFreqHi = SPEED_OF_LIGHT / filterInfo->optFreqHi;
00533     
00534     free (cleanString);
00535 
00536     return;
00537 }
00538 /*****************************************************************************/
00539 
00540 
00541 /******************************************************************************
00542 *               European Southern Observatory
00543 *             VLTI MIDI Data Reduction Software
00544 *
00545 * Module name:  getFitsExtensionNumber
00546 * Input/Output: See function arguments to avoid duplication
00547 * Description:  This routine returns the extension number of the given extension
00548 *
00549 * History:      
00550 * 04-Oct-04     (csabet) Created
00551 ******************************************************************************/
00552 int getFitsExtensionNumber (    // Ou: Extension number
00553     char    *fileName,            // In: Name of the input FITS file
00554     const char    *extName,            // In:    Given extension name
00555     int        *error)                // Ou:    Error status
00556 {
00557 
00558     /*  Local Declarations
00559     --------------------*/
00560     const char  routine[] = "getFitsExtensionNumber";
00561     int         i, numOfExtensions = 0, extNumber = -1, found = 0;
00562     char        *stringTemp, *cleanString;
00563 
00564     /*  Algorithm
00565     -----------*/
00566     if (diagnostic > 4)cpl_msg_info(cpl_func,"Invoking      routine   '%s' \n", routine);
00567     if (diagnostic > 4) fprintf(midiReportPtr, "Invoking      routine   '%s' \n", routine);
00568     
00569     //    Reset status
00570     *error = 0;
00571 
00572     //  Allocate memory
00573     cleanString = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00574 
00575     //  Get the total number of extensions
00576     numOfExtensions = qfits_query_n_ext (fileName);
00577 
00578     if (diagnostic)cpl_msg_info(cpl_func,"Number of extensions          = %d\n", numOfExtensions);
00579     if (diagnostic) fprintf(midiReportPtr, "Number of extensions          = %d\n", numOfExtensions);
00580 
00581     for (i = 0; i < numOfExtensions+1; i++)
00582     {
00583         stringTemp = qfits_query_ext (fileName, "EXTNAME", i);
00584         if (stringTemp != NULL)
00585         {
00586             cleanUpString (stringTemp, cleanString);
00587 
00588             if (strcmp (cleanString, extName) == 0) 
00589             {
00590                 if (diagnostic)cpl_msg_info(cpl_func,"%s extension number = %d\n", extName, i);
00591                 if (diagnostic) fprintf (midiReportPtr, "%s extension number = %d\n", extName, i);
00592                 extNumber = i;
00593                 found = 1;
00594                 break;
00595             }
00596         }
00597     }
00598 
00599     if (found == 0)
00600     {
00601         sprintf (midiMessage, "Cannot find FITS   '%s'  extension", extName);
00602         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
00603         *error = 1;
00604     }
00605 
00606     //    Release memory
00607     free (cleanString);
00608     
00609     return (extNumber);
00610 }
00611 /*****************************************************************************/
00612 
00613 
00614 
00615 
00616 /******************************************************************************
00617 *               European Southern Observatory
00618 *            VLTI MIDI Data Reduction Software
00619 *
00620 * Module name:  createMidiMaskFile
00621 * Input/Output: See function arguments to avoid duplication
00622 * Description:  Creates a mask file from an input photometry file
00623 *
00624 * History:
00625 * 26-Nov-03 (csabet) Created
00626 ******************************************************************************/
00627 void createMidiMaskFile (
00628     int            widthX,            // In: Column dimension
00629     int            widthY,            // In: Column dimension
00630     char        *inFileName,    // In: Name of the input data file
00631     short int    *inData1,        // In: Pointer to the input data
00632     short int    *inData2,        // In: Pointer to the input data
00633     int            numOfFrames,    // In: Number of frames
00634     int            subWindowSize,    // In: Sizeof the sub-window
00635     char        *maskFileName,    // In: Full path name of the mask file
00636     int            *error)            // Ou: Error status
00637 {
00638 
00639     /*  Local Declarations
00640     --------------------*/
00641     const char      routine[] = "createMidiMaskFile";
00642     float           *outData1, *outData2;
00643     qfits_header    *primHeadMask;
00644     FILE            *maskFitsPtr;
00645     char            *dateTime;
00646 
00647     /*  Algorithm
00648     -----------*/
00649     if (diagnostic > 4)cpl_msg_info(cpl_func,"Invoking      routine   '%s' \n", routine);
00650     if (diagnostic > 4) fprintf(midiReportPtr, "Invoking      routine   '%s' \n", routine);
00651 
00652     /*  Reset status */
00653     *error = 0;
00654 
00655     if (diagnostic)cpl_msg_info(cpl_func,"Creating mask FITS file   %s \n", maskFileName);
00656     fprintf (midiReportPtr, "Creating mask FITS file   %s \n", maskFileName);
00657 
00658     /*  Extract Primary Header from the input file */
00659     if (diagnostic)cpl_msg_info(cpl_func,"Extracting primary header from input FITS file   %s \n", inFileName);
00660     if (diagnostic) fprintf(midiReportPtr, "Extracting primary header from input FITS file   %s \n", inFileName);
00661     primHeadMask = qfits_header_read (inFileName);
00662     if (primHeadMask == NULL)
00663     {
00664         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot extract primary header from input FITS file");
00665        cpl_msg_info(cpl_func,"\nCannot Create mask FITS file for batch  %d \n", batchNumber);
00666         fprintf (midiReportPtr, "\nCannot Create mask FITS file for batch  %d \n", batchNumber);
00667         *error = 1;
00668         return;
00669     }
00670 
00671     /*  Update primary header for the Mask file */
00672     dateTime = qfits_get_datetime_iso8601 ();
00673     qfits_header_mod (primHeadMask, "DATE", dateTime, "Date and time this file was written");
00674     qfits_header_mod (primHeadMask, "INSTRUME", "MIDI", "MIDI Mask created by DRS pipeline" );
00675     qfits_header_add (primHeadMask, "EXTNAME", "Primary", "MIDI Mask primary header", "" );
00676 
00677     /*  Create the mask file and dump the primary header and close the file */
00678     maskFitsPtr = fopen (maskFileName, "w");
00679     qfits_header_dump (primHeadMask, maskFitsPtr);
00680     qfits_header_destroy (primHeadMask);
00681     fclose (maskFitsPtr);
00682 
00683     /*  Allocate memory */
00684     outData1 = (float *) calloc (subWindowSize, sizeof (float));
00685     outData2 = (float *) calloc (subWindowSize, sizeof (float));
00686 
00687     /*  Load data */
00688     loadMidiMaskData (numOfFrames, subWindowSize, inData1, inData2, outData1, outData2, error);
00689 
00690     /*  Create the tables and write the data */
00691     writeMidiMaskFile (widthX, widthY, maskFileName, subWindowSize, outData1, outData2, error);
00692     if (!(*error))
00693     {
00694        cpl_msg_info(cpl_func,"Created Mask FITS file: %s \n", maskFileName);
00695         fprintf (midiReportPtr, "Created Mask FITS file: %s \n", maskFileName);
00696     }
00697 
00698     /*  Release memory */
00699     free (outData1);
00700     free (outData2);
00701 
00702     return;
00703 }
00704 /*****************************************************************************/
00705 
00706 
00707 
00708 /******************************************************************************
00709 *               European Southern Observatory
00710 *            VLTI MIDI Data Reduction Software
00711 *
00712 * Module name:  loadMidiMaskData
00713 * Input/Output: See function arguments to avoid duplication
00714 * Description:  Uses the input data to compute masks and loads MIDI mask data
00715 *               into appropriate data structures
00716 *
00717 *
00718 * History:
00719 * 26-Nov-03     (csabet) Created
00720 ******************************************************************************/
00721 void loadMidiMaskData (
00722     int        numOfFrames,    // In: Number of frames in the data
00723     int        subWindowSize,    // In: Size of sub-window
00724     short    *inData1,        // In: Pointer to input data
00725     short    *inData2,        // In: Pointer to input data
00726     float    *outData1,        // Ou: Pointer to output data
00727     float    *outData2,        // Ou: Pointer to output data
00728     int        *error)            // Ou: Status
00729 {
00730     /*  Local Declarations
00731     --------------------*/
00732     const char      routine[] = "loadMidiMaskData";
00733     int             i, j, pixel;
00734     char            *tempFileName;
00735     FILE            *tempFilePtr;
00736 
00737     /*  Algorithm
00738     -----------*/
00739     if (diagnostic > 4)cpl_msg_info(cpl_func,"Invoking      routine   '%s' \n", routine);
00740     if (diagnostic > 4) fprintf(midiReportPtr, "Invoking      routine   '%s' \n", routine);
00741 
00742     /*  Reset status */
00743     *error = 0;
00744 
00745     /*  Create mask data */
00746     for (i = 0; i < subWindowSize; i++)
00747     {
00748         outData1[i] = 1.0;
00749         outData2[i] = 1.0;
00750     }
00751 
00752     if (plotFile && diagnostic)
00753     {
00754         /*  Allocate memory */
00755         tempFileName = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00756         
00757         sprintf (tempFileName, "%s%s.AvePhotomData1.plt", outFileDir, outRootName);
00758         tempFilePtr = fopen (tempFileName, "w");
00759         for (j = 0; j < numOfFrames; j++)
00760         {
00761             for (pixel = 0; pixel < subWindowSize; pixel++)
00762             {
00763                 i = j * subWindowSize + pixel;
00764                 fprintf (tempFilePtr, "%d ", inData1[i]);
00765             }
00766             fprintf (tempFilePtr, "\n");
00767         }
00768         fclose (tempFilePtr);
00769 
00770         sprintf (tempFileName, "%s%s.AvePhotomData2.plt", outFileDir, outRootName);
00771         tempFilePtr = fopen (tempFileName, "w");
00772         for (j = 0; j < numOfFrames; j++)
00773         {
00774             for (pixel = 0; pixel < subWindowSize; pixel++)
00775             {
00776                 i = j * subWindowSize + pixel;
00777                 fprintf (tempFilePtr, "%d ", inData2[i]);
00778             }
00779             fprintf (tempFilePtr, "\n");
00780         }
00781         fclose (tempFilePtr);
00782 
00783         sprintf (tempFileName, "%s%s.maskData1.plt", outFileDir, outRootName);
00784         tempFilePtr = fopen (tempFileName, "w");
00785         for (i = 0; i < subWindowSize; i++)
00786             fprintf (tempFilePtr, "%5.4f\n", outData1[i]);
00787         fclose (tempFilePtr);
00788 
00789         sprintf (tempFileName, "%s%s.maskData2.plt", outFileDir, outRootName);
00790         tempFilePtr = fopen (tempFileName, "w");
00791         for (i = 0; i < subWindowSize; i++)
00792             fprintf (tempFilePtr, "%5.4f\n", outData2[i]);
00793         fclose (tempFilePtr);
00794 
00795         /*  release memory */
00796         free (tempFileName);
00797     }
00798 
00799     return;
00800 
00801 }
00802 /*****************************************************************************/
00803 
00804 
00805 /******************************************************************************
00806 *               European Southern Observatory
00807 *            VLTI MIDI Data Reduction Software
00808 *
00809 * Module name:  writeMidiMaskFile
00810 * Input/Output: See function arguments to avoid duplication
00811 * Description:  Writes and creates appropriate tables into a new mask fits file
00812 *
00813 * History:
00814 * 26-Nov-03 (csabet) Created
00815 ******************************************************************************/
00816 void writeMidiMaskFile (
00817     int        widthX,            // In: Column dimension
00818     int        widthY,            // In: Column dimension
00819     char    *maskFitsName,    // In: Name of the FITS file
00820     int        numOfElements,    // In: Number of elements
00821     float    *data1,            // In: Pointer to the data1
00822     float    *data2,            // In: Pointer to the data2
00823     int        *error)            // Ou: Status
00824 {
00825     /*  Local Declarations
00826     --------------------*/
00827     const char  routine[] = "writeMidiMaskFile";
00828     int     numOfRowsImagData = 1, numOfElementsImagData;
00829 
00830     /*  Algorithm
00831     -----------*/
00832     if (diagnostic > 4)cpl_msg_info(cpl_func,"Invoking      routine   '%s' \n", routine);
00833     if (diagnostic > 4) fprintf(midiReportPtr, "Invoking      routine   '%s' \n", routine);
00834 
00835     /*  Reset status */
00836     *error = 0;
00837 
00838     if (diagnostic)cpl_msg_info(cpl_func,"Writing data into the mask FITS file \n\n");
00839     fprintf (midiReportPtr, "Writing data into the mask FITS file \n\n");
00840 
00841     /*  Write IMAGING_DATA extension */
00842     numOfElementsImagData = numOfElements;
00843     writeMidiMaskImagData (widthX, widthY, maskFitsName, numOfElementsImagData, numOfRowsImagData,
00844         data1, data2, error);
00845 
00846     /*  Add other extensions if needed */
00847     /*  TBD */
00848 
00849     return;
00850 
00851 }
00852 /*****************************************************************************/
00853 
00854 
00855 
00856 /******************************************************************************
00857 *               European Southern Observatory
00858 *            VLTI MIDI Data Reduction Software
00859 *
00860 * Module name:  writeMidiMaskImagData
00861 * Input/Output: See function arguments to avoid duplication
00862 * Description:  Writes IMAGING_DATA fits binary table in the MIDI mask file
00863 *       with an appropriate header.
00864 *
00865 *
00866 * History:
00867 * 27-Nov-03 (csabet) Created.
00868 ******************************************************************************/
00869 void writeMidiMaskImagData (
00870     int        iWidthX,        // In: Column dimension
00871     int        iWidthY,        // In: Column dimension
00872     char    *maskFitsName,    // In: Name of the FITS file
00873     int        numOfElements,    // In: Number of elements in data1 and data2
00874     int        numOfRows,        // In: Number of rows for data1 and data2
00875     float    *data1,            // In: Pointer to the data1
00876     float    *data2,            // In: Pointer to the data2
00877     int        *error)            // Ou: Status
00878 {
00879 
00880     /*  Local Declarations
00881     --------------------*/
00882     const char      routine[] = "writeMidiMaskImagData";
00883     qfits_table     *maskTable;
00884     int             tableWidth = -1, numOfColumns = 2;
00885     float           **array;
00886     qfits_col       *col;
00887     qfits_header    *imagDataHeader;
00888     FILE            *maskFitsPtr;
00889     char            *tdim2, *tdim1;
00890 
00891     /*  Algorithm
00892     -----------*/
00893     tableWidth = -1;
00894 
00895     /*  Reset status */
00896     *error = 0;
00897 
00898     /*  Allocate memory */
00899     tdim1 = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00900     tdim2 = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
00901 
00902     /*  Open the mask file for writing */
00903     maskFitsPtr = fopen (maskFitsName, "a");
00904     if (maskFitsPtr == NULL)
00905     {
00906         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot open mask file");
00907         *error = 1;
00908         free (tdim1);
00909         free (tdim2);
00910         return;
00911     }
00912 
00913     /*  Create a new table */
00914     maskTable = qfits_table_new (maskFitsName, QFITS_BINTABLE, tableWidth, numOfColumns, numOfRows);
00915     if (!maskTable)
00916     {
00917         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot create table");
00918         *error = 1;
00919         free (tdim1);
00920         free (tdim2);
00921         fclose (maskFitsPtr);
00922         return;
00923     }
00924 
00925     /*  Fill in the column information */
00926     col = maskTable->col;
00927     qfits_col_fill (col, numOfElements, 2, sizeof(float), TFITS_BIN_TYPE_E, "DATA1", " ", " ", " ",
00928         0, 0.0, 0, 1.0, 0);
00929     col++;
00930     qfits_col_fill (col, numOfElements, 2, sizeof(float), TFITS_BIN_TYPE_E, "DATA2", " ", " ", " ",
00931         0, 0.0, 0, 1.0, numOfElements* sizeof(float));
00932 
00933     /*  Create the imaging data header */
00934     imagDataHeader = qfits_table_ext_header_default(maskTable);
00935     qfits_header_add (imagDataHeader, "EXTNAME", "IMAGING_DATA", "MIDI Mask Extension name", "" );
00936 
00937 
00938     /*  Add dimensionality to the extension header */
00939     sprintf (tdim1, "'(%d,%d)'", iWidthX, iWidthY);
00940     sprintf (tdim2, "'(%d,%d)'", iWidthX, iWidthY);
00941     qfits_header_add (imagDataHeader, "TDIM1", tdim1, "Column dimensionality", "" );
00942     qfits_header_add (imagDataHeader, "TDIM2", tdim2, "Column dimensionality", "" );
00943 
00944 
00945 //  qfits_header_consoledump (imagDataHeader);
00946 
00947     /*  Allocate space for the array of data */
00948     array = (float **) calloc (numOfColumns, sizeof(float *));
00949     array[0] = data1;
00950     array[1] = data2;
00951 
00952     if (qfits_table_append_xtension_hdr (maskFitsPtr, maskTable, (const void **)array, imagDataHeader) == -1)
00953         *error = 1;
00954 
00955     qfits_header_destroy (imagDataHeader);
00956     qfits_table_close (maskTable);
00957     free (array);
00958     fclose (maskFitsPtr);
00959     free (tdim1);
00960     free (tdim2);
00961 
00962     return;
00963 }
00964 /*****************************************************************************/
00965 
00966 
00967 /******************************************************************************
00968 *               European Southern Observatory
00969 *             VLTI MIDI Data Reduction Software
00970 *
00971 * Module name:  findImagingDataExtension
00972 * Input/Output: See function arguments to avoid duplication
00973 * Description:  This routine returns the extension number of the IMAGING_DATA
00974 *
00975 * History:
00976 * 14-Aug-03     (csabet) Created. Derived from pballest July 2003
00977 ******************************************************************************/
00978 int findImagingDataExtension (        // Ou: Extension number
00979     char    *fileName,              // In: Name of the input FITS file
00980     int        defaultExtensionNumber, // In: efault extension number
00981     int        *error)                    // Ou: Error status
00982 {
00983     /*  Local Declarations
00984     --------------------*/
00985     const char  routine[] = "findImagingDataExtension";
00986     int         currentExtension    = 0;
00987     int         lastExtensionNumber = 0;
00988     char        *stringTemp, *cleanString;
00989 
00990     /*  Algorithm
00991     -----------*/
00992     if (diagnostic > 4)cpl_msg_info(cpl_func,"Invoking      routine   '%s' \n", routine);
00993     if (diagnostic > 4) fprintf(midiReportPtr, "Invoking      routine   '%s' \n", routine);
00994 
00995     /*  Reset status */
00996     *error = 0;
00997 
00998     /*  Allocate memory */
00999     cleanString = (char *) calloc (MAX_STRING_LENGTH, sizeof (char));
01000 
01001     if (diagnostic)
01002     {
01003         sprintf (midiMessage, "Searching for IMAGING_DATA extension in data file %s", fileName);
01004         midiReportInfo (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
01005     }
01006 
01007     /*  Get the total number of extensions */
01008     lastExtensionNumber = qfits_query_n_ext (fileName);
01009 
01010     if (diagnostic)
01011     {
01012         sprintf (midiMessage, "Number of extensions          = %d", lastExtensionNumber);
01013         midiReportInfo (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
01014     }
01015 
01016     if (defaultExtensionNumber <= lastExtensionNumber)
01017     {
01018         stringTemp = qfits_query_ext (fileName, "EXTNAME", defaultExtensionNumber);
01019         cleanUpString (stringTemp, cleanString);
01020 
01021         if (strcmp (cleanString, "IMAGING_DATA") == 0)
01022         {
01023             currentExtension = defaultExtensionNumber;
01024             if (diagnostic)
01025             {
01026                 sprintf (midiMessage, "IMAGING_DATA extension number = %d", currentExtension);
01027                 midiReportInfo (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
01028             }
01029 
01030             /*  Found. return extension number */
01031             free (cleanString);
01032             return (currentExtension);
01033         }
01034     }
01035 
01036     /*  In all other cases we search through all extensions for the proper one */
01037     for (currentExtension = 1; currentExtension <= lastExtensionNumber; ++currentExtension)
01038     {
01039         stringTemp = qfits_query_ext (fileName, "EXTNAME", currentExtension);
01040         cleanUpString (stringTemp, cleanString);
01041         if (strcmp (cleanString, "IMAGING_DATA") == 0) break;
01042     }
01043 
01044     if (currentExtension <= lastExtensionNumber)
01045     {
01046         if (diagnostic)
01047         {
01048             sprintf (midiMessage, "IMAGING_DATA extension number = %d", currentExtension);
01049             midiReportInfo (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
01050         }
01051         free (cleanString);
01052         return (currentExtension);
01053     }
01054     else
01055     {
01056         if (diagnostic)
01057         {
01058             sprintf (midiMessage, "No IMAGING_DATA extension in %s", fileName);
01059             midiReportInfo (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
01060         }
01061         free (cleanString);
01062         return (-1);    // Indicating no data
01063     }
01064 
01065     if (diagnostic) 
01066     {
01067         sprintf (midiMessage, "IMAGING_DATA extension number = %d", currentExtension);
01068         midiReportInfo (midiReportPtr, routine, __FILE__, __LINE__, midiMessage);
01069     }
01070     
01071     /* Release memory */
01072     free (cleanString);
01073 
01074     return (currentExtension);
01075 }
01076 /*****************************************************************************/
01077 
01078 
01079 
01080 /******************************************************************************
01081 *               European Southern Observatory
01082 *            VLTI MIDI Data Reduction Software
01083 *
01084 * Module name:  selectMask
01085 * Input/Output: See function arguments to avoid duplication
01086 * Description:  Selects a mask file from the information given in the input
01087 *               FITS file. If this Mask file does not exist or is not a FITS
01088 *               file the program terminates.
01089 *
01090 *
01091 * History:
01092 * 14-Aug-03     (csabet) Created. Derived from pballest July 2003
01093 ******************************************************************************/
01094 void selectMask (
01095     enum MaskMode    maskMode,    // In:  Method of choosing mask file
01096     MidiFiles        *fileNames,    // In:  Pointer to the file structure
01097     char            *maskFile,    // Ou:  Full name of the mask
01098     int                *error)        // Ou:  Error status
01099 {
01100 
01101     //    Local Declarations
01102     //    ------------------
01103     const char    routine[] = "selectMask";
01104     char          *qfitsTemp, *stringTemp;
01105     struct stat   buf;
01106     FILE          *temp1Ptr = NULL, *temp2Ptr = NULL;
01107     size_t        stringLength;
01108     unsigned int  i;
01109     char      nextChar, *cleanString;
01110 
01111     //    Algorithm
01112     //    ---------
01113     if (diagnostic > 4)cpl_msg_info(cpl_func,"Invoking      routine   '%s' \n", routine);
01114     if (diagnostic > 4) fprintf( midiReportPtr, "Invoking      routine   '%s' \n", routine);
01115 
01116     //    Reset status
01117     *error = 0;
01118 
01119     //    Allocate memory
01120     stringTemp = (char *) calloc (MAX_STRING_LENGTH, sizeof (char ));
01121     cleanString = (char *) calloc (MAX_STRING_LENGTH, sizeof (char ));
01122 
01123     //    Determine where to look for the mask
01124     switch (maskMode)
01125     {
01126        case SPECIFIED:
01127           //    Remove prefix
01128 /*             removePathName (fileNames->maskFileName, midiReportPtr); */
01129 /*             sprintf (maskFile, "%s%s", fileNames->maskFileDir, fileNames->maskFileName); */
01130           sprintf (maskFile, "%s", fileNames->maskFileName); 
01131           cpl_msg_info(cpl_func,"maskFile: %s",maskFile);
01132           break;
01133 
01134         case SELECTED:
01135             qfitsTemp = qfits_query_hdr (fileNames->inFitsName, "HIERARCH ESO DET FITSTPL");
01136             if (qfitsTemp != NULL)
01137             {
01138                 cleanUpString (qfitsTemp, cleanString);
01139 
01140                 /*  Replace the prefix */
01141                 stringLength = strlen (cleanString);
01142                 temp1Ptr = fopen("temp1.grb", "w");
01143                 fprintf (temp1Ptr, "%s\n", cleanString);
01144                 fclose (temp1Ptr);
01145                 temp1Ptr = fopen("temp1.grb", "r");
01146                 temp2Ptr = fopen("temp2.grb", "w");
01147                 for (i = 0; i < stringLength; i++)
01148                 {
01149                     fscanf (temp1Ptr, "%c", &nextChar);
01150                     if (i > 7) fprintf (temp2Ptr, "%c", nextChar);
01151                 }
01152                 fclose (temp1Ptr);
01153                 fclose (temp2Ptr);
01154                 temp2Ptr = fopen("temp2.grb", "r");
01155                 fscanf (temp2Ptr, "%s\n", stringTemp);
01156                 fclose (temp2Ptr);
01157 
01158                 remove ("temp1.grb");
01159                 remove ("temp2.grb");
01160                 sprintf (maskFile, "%s%s%s", fileNames->maskFileDir, "minrtsMask", stringTemp);
01161             }
01162             else
01163             {
01164                 midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot read mask name");
01165                 *error = 1;
01166             }
01167             break;
01168 
01169         case COMPUTED:
01170             sprintf (maskFile, "%s%s", fileNames->maskFileDir, fileNames->maskFileName);
01171             break;
01172 
01173         default:
01174             break;
01175     }
01176 
01177     /*  Rewrite the mask file for record */
01178     sprintf (fileNames->maskFileName, "%s", maskFile);
01179 
01180     /*  Check if the file exist */
01181     if (strcmp (maskFile, "") == 0)
01182     {
01183         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot find a suitable mask file");
01184         *error = 1;
01185     }
01186     else if (stat (maskFile, &buf) == -1)
01187     {
01188         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Cannot find mask file");
01189         *error = 1;
01190     }
01191     else if ((qfits_is_fits (maskFile)) != 1)    /*  Check if this is a FITS file */
01192     {
01193         midiReportWarning (midiReportPtr, routine, __FILE__, __LINE__, "Found Non-FITS file");
01194         *error = 1;
01195     }
01196 
01197     /*  Release memory */
01198     free (stringTemp);
01199     free (cleanString);
01200 
01201     return;
01202 }
01203 /*****************************************************************************/
01204 
01205  
01206 /******************************************************************************
01207 *               European Southern Observatory
01208 *          VLTI MIDI Maintenance Templates Software
01209 *
01210 * Module name:  midiInitFormat
01211 * Input/Output: See function arguments to avoid duplication
01212 * Description:  Initialises image format
01213 *
01214 * History:      
01215 * 10-Jul-05     (csabet) created
01216 ******************************************************************************/
01217 void midiInitFormat (
01218     ImageFormat *format)    // Ou: Pointer to the image format structure
01219 {
01220 
01221     //    Local Declarations
01222     //    ------------------
01223     const char    routine[] = "midiInitFormat";
01224         
01225     //    Algorithm
01226     //    ---------
01227     if (diagnostic > 4)cpl_msg_info(cpl_func,"Invoking      routine   '%s' \n", routine);
01228     if (diagnostic > 4) fprintf (midiReportPtr, "Invoking      routine   '%s' \n", routine);
01229 
01230     format->hasData = 0;
01231     format->numOfDetectorRegions = 0;
01232     format->numOfRegionsToProcess = 0;
01233     format->numOfPinholes = 0;
01234     format->numOfFrames = 0;
01235     format->framesPerScan = 0;
01236     format->numOfScans = 0;
01237     format->subWindowSize = 0;
01238     format->iXWidth = 0;
01239     format->iYWidth = 0;
01240      
01241     return;
01242 }
01243 /******************************************************************************/
01244 
01245 
01246 
01247 /******************************************************************************
01248 *               European Southern Observatory
01249 *          VLTI MIDI Maintenance Templates Software
01250 *
01251 * Module name:  midiCopyFormat
01252 * Input/Output: See function arguments to avoid duplication
01253 * Description:  Copies image format
01254 *
01255 * History:      
01256 * 10-Jul-05     (csabet) created
01257 ******************************************************************************/
01258 void midiCopyFormat (
01259     ImageFormat *formatIn,    // In: Pointer to the image format structure
01260     ImageFormat *formatOut)    // Ou: Pointer to the image format structure
01261 {
01262 
01263     //    Local Declarations
01264     //    ------------------
01265     const char    routine[] = "midiCopyFormat";
01266         
01267     //    Algorithm
01268     //    ---------
01269     if (diagnostic > 4)cpl_msg_info(cpl_func,"Invoking      routine   '%s' \n", routine);
01270     if (diagnostic > 4) fprintf (midiReportPtr, "Invoking      routine   '%s' \n", routine);
01271 
01272     sprintf (formatOut->obsCatg, "%s", formatIn->obsCatg);
01273     sprintf (formatOut->obsTech, "%s", formatIn->obsTech);
01274     sprintf (formatOut->obsType, "%s", formatIn->obsType);
01275     sprintf (formatOut->cameraId, "%s", formatIn->cameraId);
01276     sprintf (formatOut->beamCombiner, "%s", formatIn->beamCombiner);
01277     sprintf (formatOut->filterName, "%s", formatIn->filterName);
01278     sprintf (formatOut->shutterId, "%s", formatIn->shutterId);
01279     sprintf (formatOut->grismId, "%s", formatIn->grismId);
01280     sprintf (formatOut->targetName, "%s", formatIn->targetName);
01281     sprintf (formatOut->slitName, "%s", formatIn->slitName);
01282     sprintf (formatOut->tplStart, "%s", formatIn->tplStart);
01283     sprintf (formatOut->tplName, "%s", formatIn->tplName);
01284     formatOut->numOfDetectorRegions = formatIn->numOfDetectorRegions;
01285     formatOut->numOfRegionsToProcess = formatIn->numOfRegionsToProcess;
01286     formatOut->numOfPinholes = formatIn->numOfPinholes;
01287     formatOut->framesPerScan = formatIn->framesPerScan;
01288     formatOut->subWindowSize = formatIn->subWindowSize;
01289     formatOut->iXWidth = formatIn->iXWidth;
01290     formatOut->iYWidth = formatIn->iYWidth;
01291      
01292     return;
01293 }
01294 /******************************************************************************/
01295 
01296 
01297 
01298 /******************************************************************************
01299 *               European Southern Observatory
01300 *          VLTI MIDI Maintenance Templates Software
01301 *
01302 * Module name:  midiReportFormat
01303 * Input/Output: See function arguments to avoid duplication
01304 * Description:  Reports image format
01305 *
01306 * History:      
01307 * 05-Dec-05     (csabet) created
01308 ******************************************************************************/
01309 void midiReportFormat (
01310     const char        *type,        // In: Type of file
01311     int            isDisp,        // In: Whether DISPERSED or not
01312     ImageFormat *format)    // In: Pointer to the image format structure
01313 {
01314 
01315     //    Local Declarations
01316     //    ------------------
01317     const char    routine[] = "midiReportFormat";
01318         
01319     //    Algorithm
01320     //    ---------
01321     if (diagnostic > 4)cpl_msg_info(cpl_func,"Invoking      routine   '%s' \n", routine);
01322     if (diagnostic > 4) fprintf (midiReportPtr, "Invoking      routine   '%s' \n", routine);
01323 
01324    cpl_msg_info(cpl_func," \n");
01325    cpl_msg_info(cpl_func,"%s FORMAT \n", type);
01326    cpl_msg_info(cpl_func,"--------------------- \n");
01327    cpl_msg_info(cpl_func,"FITS Template Name           = %s \n", format->tplName);
01328    cpl_msg_info(cpl_func,"Batch Template Name          = %s \n", batchTemplate);
01329    cpl_msg_info(cpl_func,"Observation Category         = %s \n", format->obsCatg);
01330    cpl_msg_info(cpl_func,"Observation Technique        = %s \n", format->obsTech);
01331    cpl_msg_info(cpl_func,"Observation Type             = %s \n", format->obsType);
01332    cpl_msg_info(cpl_func,"Camera ID                    = %s \n", format->cameraId);
01333    cpl_msg_info(cpl_func,"Beam Combiner                = %s \n", format->beamCombiner);
01334    cpl_msg_info(cpl_func,"Filter ID                    = %s \n", format->filterName);
01335    cpl_msg_info(cpl_func,"Shutter ID                   = %s \n", format->shutterId);
01336    cpl_msg_info(cpl_func,"Grism ID                     = %s \n", format->grismId);
01337    cpl_msg_info(cpl_func,"Slit Name                    = %s \n", format->slitName);
01338    cpl_msg_info(cpl_func,"Target Name                  = %s \n", format->targetName);
01339    cpl_msg_info(cpl_func,"TPL Start                    = %s \n", format->tplStart);
01340     if (isDisp)
01341        cpl_msg_info(cpl_func,"Data is                      = DISPERSED \n");
01342     else
01343        cpl_msg_info(cpl_func,"Data is                      = UNDISPERSED \n");
01344    cpl_msg_info(cpl_func,"Number of Frames             = %d \n", format->numOfFrames);
01345     if ((strcmp(format->beamCombiner, "SCI_PHOT") == 0) || (strcmp (format->shutterId, "ABOPEN") == 0))
01346     {
01347        cpl_msg_info(cpl_func,"Frames per scan              = %d \n", format->framesPerScan);
01348        cpl_msg_info(cpl_func,"Number of Scans              = %d \n", format->numOfScans);
01349     }
01350    cpl_msg_info(cpl_func,"X, Column dimension          = %d \n", format->iXWidth);
01351    cpl_msg_info(cpl_func,"Y, Column dimension          = %d \n", format->iYWidth);
01352    cpl_msg_info(cpl_func,"Sub-window size              = %d \n", format->subWindowSize);
01353    cpl_msg_info(cpl_func,"Number of Pinholes           = %d \n", format->numOfPinholes);
01354    cpl_msg_info(cpl_func,"Number of Detector Regions   = %d \n", format->numOfDetectorRegions);
01355    cpl_msg_info(cpl_func,"Number of Regions to process = %d \n", format->numOfRegionsToProcess);
01356    cpl_msg_info(cpl_func,"\n");
01357 
01358     fprintf (midiReportPtr, " \n");
01359     fprintf (midiReportPtr, "%s FORMAT \n", type);
01360     fprintf (midiReportPtr, "--------------------- \n");
01361     fprintf (midiReportPtr, "FITS Template Name           = %s \n", format->tplName);
01362     fprintf (midiReportPtr, "Batch Template Name          = %s \n", batchTemplate);
01363     fprintf (midiReportPtr, "Observation Category         = %s \n", format->obsCatg);
01364     fprintf (midiReportPtr, "Observation Technique        = %s \n", format->obsTech);
01365     fprintf (midiReportPtr, "Observation Type             = %s \n", format->obsType);
01366     fprintf (midiReportPtr, "Camera ID                    = %s \n", format->cameraId);
01367     fprintf (midiReportPtr, "Beam Combiner                = %s \n", format->beamCombiner);
01368     fprintf (midiReportPtr, "Filter ID                    = %s \n", format->filterName);
01369     fprintf (midiReportPtr, "Shutter ID                   = %s \n", format->shutterId);
01370     fprintf (midiReportPtr, "Grism ID                     = %s \n", format->grismId);
01371     fprintf (midiReportPtr, "Slit Name                    = %s \n", format->slitName);
01372     fprintf (midiReportPtr, "Target Name                  = %s \n", format->targetName);
01373     fprintf (midiReportPtr, "TPL Start                    = %s \n", format->tplStart);
01374     if (isDisp)
01375         fprintf (midiReportPtr, "Data is                      = DISPERSED \n");
01376     else
01377         fprintf (midiReportPtr, "Data is                      = UNDISPERSED \n");
01378     fprintf (midiReportPtr, "Number of Frames             = %d \n", format->numOfFrames);
01379     if ((strcmp(format->beamCombiner, "SCI_PHOT") == 0) || (strcmp (format->shutterId, "ABOPEN") == 0))
01380     {
01381         fprintf (midiReportPtr, "Frames per scan              = %d \n", format->framesPerScan);
01382         fprintf (midiReportPtr, "Number of Scans              = %d \n", format->numOfScans);
01383     }
01384     fprintf (midiReportPtr, "X, Column dimension          = %d \n", format->iXWidth);
01385     fprintf (midiReportPtr, "Y, Column dimension          = %d \n", format->iYWidth);
01386     fprintf (midiReportPtr, "Sub-window size              = %d \n", format->subWindowSize);
01387     fprintf (midiReportPtr, "Number of Pinholes           = %d \n", format->numOfPinholes);
01388     fprintf (midiReportPtr, "Number of Detector Regions   = %d \n", format->numOfDetectorRegions);
01389     fprintf (midiReportPtr, "Number of Regions to process = %d \n", format->numOfRegionsToProcess);
01390     fprintf (midiReportPtr, "\n");
01391 
01392     return;
01393 }
01394 /******************************************************************************/
01395 
01396 
01397 /******************************************************************************
01398 *               European Southern Observatory
01399 *            VLTI MIDI Data Reduction Software
01400 *
01401 * Module name:  selectChannels
01402 * Input/Output: See function arguments to avoid duplication
01403 * Description:
01404 *
01405 *
01406 * History:
01407 * 21-Jul-03     (csabet) Created
01408 ******************************************************************************/
01409 void selectChannels (
01410     int            start,        // In: Start region
01411     int            end,        // In: End region
01412     ImageFormat    *format,    // In: File format
01413     float        **mask)        // In: Mask file
01414 {
01415     //    Local Declarations
01416     //    ------------------
01417     const char  routine[] = "selectChannels";
01418     int            i, x, y, r, masked;
01419     float        *array;
01420     
01421     //    Algorithm
01422     //    ---------
01423     if (diagnostic > 4)cpl_msg_info(cpl_func,"Invoking      routine   '%s' \n", routine);
01424     if (diagnostic > 4) fprintf(midiReportPtr, "Invoking      routine   '%s' \n", routine);
01425 
01426    cpl_msg_info(cpl_func,"\nSelecting active channels based on the given mask \n");
01427    cpl_msg_info(cpl_func,"------------------------------------------------- \n");
01428     fprintf(midiReportPtr, "\nSelecting active channels based on the given mask \n");
01429     fprintf(midiReportPtr, "------------------------------------------------- \n");
01430 
01431     //    Allocate memory
01432     array = (float *) calloc (format->iXWidth, sizeof(float));
01433     
01434     for (r = start; r < end; r++)
01435     {
01436         for (y = 0; y < format->iYWidth; y++)
01437         {
01438             for (x = 0; x < format->iXWidth; x++)
01439             {
01440                 i = x + y * format->iXWidth;
01441                 if (mask[r][i] > array[x]) array[x] = mask[r][i];
01442             }
01443         }
01444     }
01445 
01446     masked = 0;
01447     for (x = 0; x < format->iXWidth; x++)
01448     {
01449         if (array[x] == 0.0)
01450         {
01451             masked++;
01452             badChannelList[x] |= BSL_MASKED_CHANNEL;
01453         }
01454     }
01455 
01456    cpl_msg_info(cpl_func,"Number of active channels = %3d \n\n", (format->iXWidth - masked));
01457     fprintf(midiReportPtr, "Number of active channels = %3d QCLOG \n\n", (format->iXWidth - masked));
01458 
01459     //    Plot
01460     if (plotFile && diagnostic > 1)    midiCreatePlotFile2D ("SelectedChannels", "Selected Channels", 
01461         "Wavelength Channel", "Mask Profile", 0, array, 0, format->iXWidth, 1, 0);
01462 
01463     //    Release memory
01464     free (array);
01465     
01466     return;
01467 }
01468 /*****************************************************************************/
01469 

Generated on 5 Mar 2013 for MIDI Pipeline Reference Manual by  doxygen 1.6.1