/** @file  pmDetrendDB.h
*
*  @brief Tools to query the detrend database system
*
*  the functions in here do not perform the detrend database queries directly.  all interfaces
*  to the detrend database go through the external dettools functions.  this allows the modules
*  and directly dependent program to be sufficiently independent of the database schema that it
*  can be used with any properly defined detrend database tables.  
*
*  XXX place the specific name of the detrend database query command in the configuration data
*
*  @ingroup Detrend
*
*  @author EAM, IfA
*
*  @version $Revision: 1.2 $ $Name: not supported by cvs2svn $
*  @date $Date: 2006-07-01 00:00:11 $
*
*  Copyright 2004-2005 Institute for Astronomy, University of Hawaii
*/

#ifndef PM_DETREND_DB_H
#define PM_DETREND_DB_H

typedef enum {
    PM_DETREND_TYPE_MASK,
    PM_DETREND_TYPE_BIAS,
    PM_DETREND_TYPE_DARK,
    PM_DETREND_TYPE_FLAT,
    PM_DETREND_TYPE_FLAT_CORRECTION,
    PM_DETREND_TYPE_FRINGE_IMAGE,
    PM_DETREND_TYPE_FRINGE_TABLE,
    PM_DETREND_TYPE_BACKGROUND,
} pmDetrendType;

typedef struct
{
    char *camera;   // name of camera
    psTime time;   // time of input data
    pmDetrendType type;   // type of detrend data
    char *filter;   // name of filter
    float exptime;   // exposure time (for dark, maybe flat & fringe)
    float airmass;   // for fringe
}
pmDetrendSelectOptions;

typedef struct
{
    char *detID;   // identifier of detrend image
    pmFPALevel level;   // level in FPA hierarchy of individual file
}
pmDetrendSelectResults;

typedef struct
{
    char *detID;   // identifier of detrend image
    char *classID;   // level in FPA hierarchy of individual file
}
pmDetrendFileOptions;

psString pmDetrendTypeToString (pmDetrendType type);

pmDetrendSelectOptions *pmDetrendSelectOptionsAlloc(char *camera, psTime time, pmDetrendType type);
pmDetrendSelectResults *pmDetrendSelectResultsAlloc();
pmDetrendSelectResults *pmDetrendSelect (pmDetrendSelectOptions *options);
char *pmDetrendFile (char *detID, char *classID);

// move these to pslib??
typedef struct
{
    int stdin;
    int stdout;
    int stderr;
}
psPipe;

typedef struct
{
    char *data;
    int nAlloc;    // current size of allocated buffer
    int nReset;    // size to set buffer after flush
    int nBlock;    // number of bytes to try to read at a time
    int n;    // current size of filled data
}
psIOBuffer;

// psIOBuffer functions
psIOBuffer *psIOBufferAlloc (int nBuffer);
bool psIOBufferFlush (psIOBuffer *buffer);
int psIOBufferRead (psIOBuffer *buffer, int fd);
int psIOBufferReadEmpty (psIOBuffer *buffer, int maxRetries, int fd);

// psPipe functions
psPipe *psPipeAlloc ();
psPipe *psPipeOpen (char *command);
bool psPipeClose (psPipe *pipe);

# endif
