IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Mar 17, 2009, 12:08:50 PM (17 years ago)
Author:
beaumont
Message:

merged with head

Location:
branches/cnb_branches/cnb_branch_20090301
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/cnb_branches/cnb_branch_20090301

  • branches/cnb_branches/cnb_branch_20090301/ippTools/src/stacktool.c

    r21402 r23352  
    4444static bool donecleanupMode(pxConfig *config);
    4545static bool updatesumskyfileMode(pxConfig *config);
     46static bool exportrunMode(pxConfig *config);
     47static bool importrunMode(pxConfig *config);
    4648
    4749static bool setstackRunState(pxConfig *config, psS64 stack_id, const char *state);
     
    7880        MODECASE(STACKTOOL_MODE_DONECLEANUP,           donecleanupMode);
    7981        MODECASE(STACKTOOL_MODE_UPDATESUMSKYFILE,      updatesumskyfileMode);
     82        MODECASE(STACKTOOL_MODE_EXPORTRUN,             exportrunMode);
     83        MODECASE(STACKTOOL_MODE_IMPORTRUN,             importrunMode);
    8084        default:
    8185            psAbort("invalid option (this should not happen)");
     
    11801184}
    11811185
     1186bool exportrunMode(pxConfig *config)
     1187{
     1188  typedef struct ExportTable {
     1189    char tableName[80];
     1190    char sqlFilename[80];
     1191  } ExportTable;
     1192
     1193  int numExportTables = 3;
     1194 
     1195  PS_ASSERT_PTR_NON_NULL(config, NULL);
     1196
     1197  PXOPT_LOOKUP_S64(det_id, config->args, "-stack_id", true,  false);
     1198  PXOPT_LOOKUP_STR(outfile, config->args, "-outfile", true,  false);
     1199  PXOPT_LOOKUP_U64(limit,   config->args, "-limit",   false, false);
     1200
     1201  FILE *f = fopen (outfile, "w");
     1202  if (f == NULL) {
     1203      psError(PS_ERR_UNKNOWN, false, "failed to open output file");
     1204      return false;
     1205  }
     1206
     1207  psMetadata *where = psMetadataAlloc();
     1208  PXOPT_COPY_S64(config->args, where, "-stack_id", "stack_id", "==");
     1209
     1210  ExportTable tables [] = {
     1211    {"stackRun", "stacktool_export_run.sql"},
     1212    {"stackInputSkyfile", "stacktool_export_input_skyfile.sql"},
     1213    {"stackSumSkyfile", "stacktool_export_sum_skyfile.sql"},
     1214  };
     1215
     1216  for (int i=0; i < numExportTables; i++) {
     1217    psString query = pxDataGet(tables[i].sqlFilename);
     1218    if (!query) {
     1219      psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
     1220      return false;
     1221    }
     1222
     1223    if (where && psListLength(where->list)) {
     1224      psString whereClause = psDBGenerateWhereSQL(where, NULL);
     1225      psStringAppend(&query, " %s", whereClause);
     1226      psFree(whereClause);
     1227    }
     1228
     1229    // treat limit == 0 as "no limit"
     1230    if (limit) {
     1231      psString limitString = psDBGenerateLimitSQL(limit);
     1232      psStringAppend(&query, " %s", limitString);
     1233      psFree(limitString);
     1234    }
     1235
     1236    if (!p_psDBRunQuery(config->dbh, query)) {
     1237      psError(PS_ERR_UNKNOWN, false, "database error");
     1238      psFree(query);
     1239      return false;
     1240    }
     1241    psFree(query);
     1242
     1243    psArray *output = p_psDBFetchResult(config->dbh);
     1244    if (!output) {
     1245      psError(PS_ERR_UNKNOWN, false, "database error");
     1246      return false;
     1247    }
     1248    if (!psArrayLength(output)) {
     1249      psTrace("regtool", PS_LOG_INFO, "no rows found");
     1250      psFree(output);
     1251      return true;
     1252    }
     1253    psFree(output);
     1254  }
     1255
     1256    fclose (f);
     1257
     1258    return true;
     1259}
     1260
     1261bool importrunMode(pxConfig *config)
     1262{
     1263  unsigned int nFail;
     1264 
     1265  int numImportTables = 2;
     1266 
     1267  char tables[2] [80] = {"stackInputSkyfile", "stackSumSkyfile"};
     1268
     1269  PS_ASSERT_PTR_NON_NULL(config, NULL);
     1270 
     1271  PXOPT_LOOKUP_STR(infile, config->args, "-infile", true,  false);
     1272
     1273  psMetadata *input = psMetadataConfigRead (NULL, &nFail, infile, false);
     1274
     1275  fprintf (stdout, "---- input ----\n");
     1276  psMetadataPrint (stderr, input, 1);
     1277
     1278  psMetadataItem *item = psMetadataLookup (input, "stackRun");
     1279  psAssert (item, "entry not in input?");
     1280  psAssert (item->type == PS_DATA_METADATA_MULTI, "entry not multi?");
     1281
     1282  psMetadataItem *entry = psListGet (item->data.list, 0);
     1283  assert (entry);
     1284  assert (entry->type == PS_DATA_METADATA);
     1285  stackRunRow *stackRun = stackRunObjectFromMetadata (entry->data.md);
     1286  stackRunInsertObject (config->dbh, stackRun);
     1287
     1288  // fprintf (stdout, "---- stack run ----\n");
     1289  // psMetadataPrint (stderr, entry->data.md, 1);
     1290
     1291  for (int i = 0; i < numImportTables; i++) {
     1292    psMetadataItem *item = psMetadataLookup (input, tables[i]);
     1293    psAssert (item, "entry not in input?");
     1294    psAssert (item->type == PS_DATA_METADATA_MULTI, "entry not multi?");
     1295 
     1296    switch (i) {
     1297      case 0:
     1298        for (int i = 0; i < item->data.list->n; i++) {
     1299          entry = psListGet (item->data.list, i);
     1300          assert (entry);
     1301          assert (entry->type == PS_DATA_METADATA);
     1302          stackInputSkyfileRow *stackInputSkyfile = stackInputSkyfileObjectFromMetadata (entry->data.md);
     1303          stackInputSkyfileInsertObject (config->dbh, stackInputSkyfile);
     1304
     1305          // fprintf (stdout, "---- row %d ----\n", i);
     1306          // psMetadataPrint (stderr, entry->data.md, 1);
     1307        }
     1308        break;
     1309       
     1310      case 1:
     1311        for (int i = 0; i < item->data.list->n; i++) {
     1312          entry = psListGet (item->data.list, i);
     1313          assert (entry);
     1314          assert (entry->type == PS_DATA_METADATA);
     1315          stackSumSkyfileRow *stackSumSkyfile = stackSumSkyfileObjectFromMetadata (entry->data.md);
     1316          stackSumSkyfileInsertObject (config->dbh, stackSumSkyfile);
     1317
     1318          // fprintf (stdout, "---- row %d ----\n", i);
     1319          // psMetadataPrint (stderr, entry->data.md, 1);
     1320        }
     1321        break;
     1322    }
     1323  }
     1324 
     1325  return true;
     1326}
Note: See TracChangeset for help on using the changeset viewer.