IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Oct 9, 2006, 3:02:25 PM (20 years ago)
Author:
magnier
Message:

fleshing out detselect support (still missing filters and exptimes, etc)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/psModules/src/detrend/pmDetrendDB.c

    r9290 r9433  
    88#include "pmFPA.h"
    99#include "pmDetrendDB.h"
     10#include "psPipe.h"
     11#include "psIOBuffer.h"
    1012
    1113// ************* detrend select functions **************
     
    2527
    2628// define basic options for a new detrend database query
    27 pmDetrendSelectOptions *pmDetrendSelectOptionsAlloc(char *camera, psTime time, pmDetrendType type)
     29pmDetrendSelectOptions *pmDetrendSelectOptionsAlloc(const char *camera, psTime time, pmDetrendType type)
    2830{
    2931
     
    3234
    3335    // basic options required by every query
    34     options->camera = psMemIncrRefCounter(camera);
     36    options->camera = psStringCopy (camera);
    3537    options->time   = time;
    3638    options->type   = type;
    3739
    3840    // these other options depend on the type of detrend data
    39     options->filter = NULL;
    40     options->exptime = 0.0;
    41     options->airmass = 0.0;
     41    options->filter = NULL;  //
     42    options->exptime = -1.0; // the undefined value (safe since exptime >= 0)
     43    options->airmass = -1.0; // the undefined value (safe since airmass >= 1)
    4244
    4345    return options;
     
    9092// detselect -camera (camera) -time (time) -type (type) [others]
    9193// returns: (type) (class) (exp_flag) DONE
    92 pmDetrendSelectResults *pmDetrendSelect (pmDetrendSelectOptions *options)
    93 {
    94     bool status;
     94pmDetrendSelectResults *pmDetrendSelect (const pmDetrendSelectOptions *options)
     95{
     96    int status, exit_status;
    9597    char *line = NULL;
    96 
    9798    char *time = psTimeToISO (&options->time);
    9899    char *type = pmDetrendTypeToString (options->type);
    99100
     101    psArray *array = NULL;
     102    pmDetrendSelectResults *results = pmDetrendSelectResultsAlloc();
     103
    100104    // generate the detselect command
    101     psStringAppend (&line, "detselect -camera %s -time %s -type %s", options->camera, time, type);
    102     psFree (time);
    103     psFree (type);
     105    // psStringAppend (&line, "detselect -inst %s -time %s -det_type %s", options->camera, time, type);
     106    // XXX we need to put in the filter and other restrictions based on the recipe
     107    // detselect -search -simple returns: DET_ID ITERATION CLASS TYPE
     108    psStringAppend (&line, "detselect -search -simple -inst %s -det_type %s", options->camera, type);
     109    if (options->filter) {
     110        psStringAppend (&line, " -filter %s", options->filter);
     111    }
     112    if (options->exptime > -1.0) {
     113        psStringAppend (&line, " -exptime %f", options->exptime);
     114    }
     115    if (options->airmass > -1.0) {
     116        psStringAppend (&line, " -airmass %f", options->airmass);
     117    }
     118
     119    psTrace("psModules.detrend", 5, "running %s", line);
    104120
    105121    // use psPipe to exec the command, wait for response
    106122    psIOBuffer *buffer = psIOBufferAlloc (512);
    107123    psPipe *pipe = psPipeOpen (line);
     124    if (!pipe) {
     125        psError (PS_ERR_IO, false, "error calling command %s", line);
     126        goto failure;
     127    }
     128
    108129    status = psIOBufferReadEmpty (buffer, 100, pipe->stdout);
    109130    if (!status) {
    110131        psError (PS_ERR_IO, false, "detselect is not responding");
    111         psFree (buffer);
    112         psFree (pipe);
    113         psFree (line);
    114         return NULL;
    115     }
    116     psPipeClose (pipe);
    117     psFree (pipe);
    118     psFree (line);
     132        goto failure;
     133    }
     134    exit_status = psPipeClose (pipe);
     135    if (exit_status) {
     136        psError (PS_ERR_IO, false, "error running detselect");
     137        goto failure;
     138    }
     139
     140    psTrace("psModules.detrend", 5, "got answer: %s\n", buffer->data);
    119141
    120142    // XXX need to parse the response more robustly
    121143    // XXX for now, assume a single line
    122     psArray *array = psStringSplitArray (buffer->data, " ", false);
    123     psFree (buffer);
    124 
    125     if (!array)
    126         return NULL;
    127     if (!array->n)
    128         return NULL;
    129     if (!strcasecmp(array->data[0], "ERROR"))
    130         return NULL;
    131 
    132     pmFPALevel level = pmFPALevelFromName (array->data[1]);
    133     if (level == PM_FPA_LEVEL_NONE)
    134         return NULL;
    135 
    136     pmDetrendSelectResults *results = pmDetrendSelectResultsAlloc();
     144    array = psStringSplitArray (buffer->data, " ", false);
     145
     146    if (!array) {
     147        psError(PS_ERR_IO, false, "failed to split detselect answer %s\n", buffer->data);
     148        goto failure;
     149    }
     150    if (array->n != 4) {
     151        psError(PS_ERR_IO, false, "invalid number of fields in detselect answer %s (%ld)\n", buffer->data, array->n);
     152        goto failure;
     153    }
     154
     155    pmFPALevel level = pmFPALevelFromName (array->data[2]);
     156    if (level == PM_FPA_LEVEL_NONE) {
     157        psError(PS_ERR_IO, false, "invalid file level (%s) from detselect\n", (char *)array->data[2]);
     158        goto failure;
     159    }
     160
    137161    results->level = level;
    138     results->detID = psStringCopy (array->data[2]);
    139 
    140     psFree (array);
     162    psStringAppend (&results->detID, " -det_id %s -iteration %s ", (char *)array->data[0], (char *)array->data[1]);
     163
     164    psTrace("psModules.detrend", 5, "generated detID %s\n", results->detID);
     165    psTrace("psModules.detrend", 5, "fileLevel is %d (%s)\n", results->level, (char *)array->data[2]);
     166    psTrace("psModules.detrend", 5, "selected type is %s\n", (char *)array->data[3]);
     167
     168    psFree (array);
     169    psFree (pipe);
     170    psFree (buffer);
     171    psFree (line);
     172    psFree (type);
     173    psFree (time);
    141174    return results;
     175
     176failure:
     177    psFree (array);
     178    psFree (results);
     179    psFree (pipe);
     180    psFree (buffer);
     181    psFree (line);
     182    psFree (type);
     183    psFree (time);
     184    return NULL;
    142185}
    143186
     
    146189// detselect -select -detID (detID) -classID (classID)
    147190// returns: (detID) (classID) (filename) DONE
    148 char *pmDetrendFile (char *detID, char *classID)
    149 {
     191char *pmDetrendFile (const char *detID, const char *classID)
     192{
     193    PS_ASSERT_PTR_NON_NULL(detID, NULL);
     194    PS_ASSERT_PTR_NON_NULL(classID, NULL);
     195
    150196    bool status;
    151197    char *line = NULL;
     198    psArray *array = NULL;
    152199
    153200    // generate the detselect command
    154     psStringAppend (&line, "detselect -select -detID %s -classID %s", detID, classID);
     201    psStringAppend (&line, "detselect -select -simple %s -class_id %s", detID, classID);
     202    psTrace("psModules.detrend", 5, "running %s", line);
    155203
    156204    // use psPipe to exec the command, wait for response
    157205    psIOBuffer *buffer = psIOBufferAlloc (512);
    158206    psPipe *pipe = psPipeOpen (line);
     207    if (!pipe) {
     208        psError (PS_ERR_IO, false, "error calling command %s", line);
     209        goto failure;
     210    }
    159211    status = psIOBufferReadEmpty (buffer, 100, pipe->stdout);
    160212    if (!status) {
    161213        psError (PS_ERR_IO, false, "detselect is not responding");
    162         psFree (buffer);
    163         psFree (pipe);
    164         psFree (line);
    165         return NULL;
    166     }
    167     psPipeClose (pipe);
    168     psFree (pipe);
    169     psFree (line);
     214        goto failure;
     215    }
     216    status = psPipeClose (pipe);
     217    if (status) {
     218        psError (PS_ERR_IO, false, "error running detselect");
     219        goto failure;
     220    }
     221
     222    psTrace("psModules.detrend", 5, "got answer: %s\n", buffer->data);
    170223
    171224    // XXX need to parse the response more robustly
    172225    // XXX for now, assume a single line
    173     psArray *array = psStringSplitArray (buffer->data, " ", false);
    174     psFree (buffer);
    175 
    176     if (!array)
    177         return NULL;
    178     if (!array->n)
    179         return NULL;
    180     if (!strcasecmp(array->data[0], "ERROR"))
    181         return NULL;
    182 
    183     char *result = psStringCopy (array->data[2]);
    184 
    185     psFree (array);
     226    array = psStringSplitArray (buffer->data, " ", false);
     227
     228    if (!array) {
     229        psError(PS_ERR_IO, false, "failed to split detselect answer %s\n", buffer->data);
     230        goto failure;
     231    }
     232    if (array->n == 0) {
     233        psError(PS_ERR_IO, true, "empty result set from detselect\n");
     234        goto failure;
     235    }
     236    if (array->n < 5) {
     237        psError(PS_ERR_IO, true, "invalid result set from detselect %s\n", buffer->data);
     238        goto failure;
     239    }
     240
     241    char *result = psStringCopy (array->data[4]);
     242    psTrace("psModules.detrend", 5, "detrend file: %s\n", result);
     243
     244    psFree (array);
     245    psFree (pipe);
     246    psFree (buffer);
     247    psFree (line);
    186248    return result;
    187 }
     249
     250failure:
     251    psFree (array);
     252    psFree (pipe);
     253    psFree (buffer);
     254    psFree (line);
     255    return NULL;
     256}
Note: See TracChangeset for help on using the changeset viewer.