00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00196 #include "jsoc_main.h"
00197
00198 char *module_name = "ingest_from_fits";
00199
00200 #define DIE(msg) {fflush(stdout);fprintf(stderr,"%s, status=%d\n",msg,status); return(status);}
00201
00202 ModuleArgs_t module_args[] =
00203 {
00204 {ARG_STRING, "in", "NOT_SPECIFIED", "Input FITS file."},
00205 {ARG_STRING, "ds", "NOT_SPECIFIED", "Target DRMS data series."},
00206 {ARG_STRING, "map", "NOT_SPECIFIED", "Map file for newname from oldname."},
00207 {ARG_STRING, "primekey", "NOT_SPECIFIED", "keyword name to use as a prime key in the JSD created"},
00208 {ARG_FLAG, "c", "0", "Use generated jsd to create a series. Requires both ds and primekey args."},
00209 {ARG_FLAG, "j", "0", "Print jsd."},
00210 {ARG_END}
00211 };
00212
00213 # define MAXJSDLEN 100000
00214 # define MAXMAPLEN 10000
00215
00216 int DoIt(void)
00217 {
00218 const char *in = params_get_str(&cmdparams, "in");
00219 const char *ds = params_get_str(&cmdparams, "ds");
00220 const char *map = params_get_str(&cmdparams, "map");
00221 const char *primekey = params_get_str(&cmdparams, "primekey");
00222 int printjsd = params_isflagset(&cmdparams, "j");
00223 int wantcreate = params_isflagset(&cmdparams, "c");
00224 int haveseriesname = strcmp(ds, "NOT_SPECIFIED") != 0;
00225 int haveprime = strcmp(primekey, "NOT_SPECIFIED") != 0;
00226 int havemap = strcmp(map, "NOT_SPECIFIED") != 0;
00227 int wantjsd = printjsd || wantcreate;
00228 int insertrec = haveseriesname && !printjsd;
00229 int status = DRMS_SUCCESS;
00230 DRMS_Array_t *data = NULL;
00231 DRMS_Keyword_t *key=NULL;
00232 HContainer_t *keywords = NULL;
00233 HIterator_t hit;
00234 int readraw = 1;
00235 char jsd[MAXJSDLEN];
00236 char *newnames[MAXMAPLEN];
00237 char *oldnames[MAXMAPLEN];
00238 char *actions[MAXMAPLEN];
00239 int imap, nmap = 0;
00240
00241 if (strcmp(in, "NOT_SPECIFIED") == 0)
00242 {
00243 if (cmdparams_numargs(&cmdparams) < 1 || !(in = cmdparams_getarg(&cmdparams, 1)))
00244 DIE("No input data found");
00245 }
00246
00247 data = drms_fitsrw_read(drms_env, in, readraw, &keywords, &status);
00248 if (status || !keywords)
00249 {
00250 DIE("No keywords found");
00251 }
00252
00253 if (wantjsd)
00254 {
00255 char *pjsd = jsd;
00256 char keyname[DRMS_MAXNAMELEN];
00257 char *colon;
00258 DRMS_Type_t datatype;
00259 int iaxis, naxis, dims[10];
00260 double bzero, bscale;
00261
00262
00263 pjsd += sprintf(pjsd, "#=====General Series Information=====\n");
00264 pjsd += sprintf(pjsd, "Seriesname: %s\n", (haveseriesname ? ds : "<NAME HERE>"));
00265 pjsd += sprintf(pjsd, "Author: %s\n", getenv("USER"));
00266 pjsd += sprintf(pjsd, "Owner: nobody_yet\n");
00267 pjsd += sprintf(pjsd, "Unitsize: 1\n");
00268 pjsd += sprintf(pjsd, "Archive: 0\n");
00269 pjsd += sprintf(pjsd, "Retention: 10\n");
00270 pjsd += sprintf(pjsd, "Tapegroup: 0\n");
00271 pjsd += sprintf(pjsd, "PrimeKeys: %s\n", (haveprime ? primekey : "<PRIME KEYS HERE OR DELETE LINE>"));
00272 pjsd += sprintf(pjsd, "DBIndex: %s\n", (haveprime ? primekey : "<PRIME KEYS HERE OR DELETE LINE>"));
00273 pjsd += sprintf(pjsd, "Description: \"From: %s\"\n", in);
00274
00275 pjsd += sprintf(pjsd, "#===== Keywords\n");
00276
00277
00278
00279
00280
00281
00282
00283 datatype = data->type;
00284 naxis = data->naxis;
00285 memcpy(dims, data->axis, sizeof(int) * naxis);
00286 bzero = data->bzero;
00287 bscale = data->bscale;
00288
00289 hiter_new (&hit, keywords);
00290 while ( key = (DRMS_Keyword_t *)hiter_getnext(&hit) )
00291 {
00292 strcpy(keyname, key->info->name);
00293
00294 colon = index(key->info->description, ':');
00295
00296
00297
00298 if (*(key->info->description) == '[')
00299 {
00300 char *c;
00301 char originalname[80];
00302 strcpy(originalname, key->info->description+1);
00303 c = index(originalname, ':');
00304 if (c)
00305 *c = '\0';
00306 c = index(originalname, ']');
00307 if (c)
00308 *c = '\0';
00309 newnames[nmap] = strdup(keyname);
00310 oldnames[nmap] = strdup(originalname);
00311 actions[nmap] = strdup("copy");
00312 nmap++;
00313 }
00314
00315 pjsd += sprintf(pjsd, "Keyword: %s, ", keyname);
00316
00317 switch (key->info->type)
00318 {
00319 case DRMS_TYPE_CHAR:
00320 if (colon)
00321 pjsd += sprintf(pjsd, "char, variable, record, DRMS_MISSING_VALUE, \"%%d\", \"none\", ");
00322 else
00323 pjsd += sprintf(pjsd, "int, variable, record, DRMS_MISSING_VALUE, \"%%d\", \"none\", ");
00324 break;
00325 case DRMS_TYPE_SHORT:
00326 case DRMS_TYPE_INT:
00327 pjsd += sprintf(pjsd, "int, variable, record, DRMS_MISSING_VALUE, \"%%d\", \"none\", ");
00328 break;
00329 case DRMS_TYPE_LONGLONG:
00330 pjsd += sprintf(pjsd, "longlong, variable, record, DRMS_MISSING_VALUE, \"%%lld\", \"none\", ");
00331 break;
00332 case DRMS_TYPE_FLOAT:
00333 case DRMS_TYPE_DOUBLE:
00334 pjsd += sprintf(pjsd, "double, variable, record, DRMS_MISSING_VALUE, \"%%f\", \"none\", ");
00335 break;
00336 case DRMS_TYPE_TIME:
00337 pjsd += sprintf(pjsd, "time, variable, record, DRMS_MISSING_VALUE, 0, \"UTC\", ");
00338 break;
00339 case DRMS_TYPE_STRING:
00340 pjsd += sprintf(pjsd, "string, variable, record, \"\", \"%%s\", \"none\", ");
00341 break;
00342 default:
00343 DIE("bad key type");
00344 }
00345 pjsd += sprintf(pjsd, "\"%s\"\n", key->info->description);
00346 }
00347
00348 hiter_free(&hit);
00349
00350 pjsd += sprintf(pjsd, "#======= Segments =======\n");
00351 pjsd += sprintf(pjsd, "Data: array, variable, %s, %d, ", drms_type2str(datatype), naxis);
00352
00353 for (iaxis = 0; iaxis < naxis; iaxis++)
00354 pjsd += sprintf(pjsd, "%d, ", dims[iaxis]);
00355 pjsd += sprintf(pjsd, "\"\", fits, \"%s\", %f, %f, \"%s\"\n",
00356 (datatype != DRMS_TYPE_FLOAT &&
00357 datatype != DRMS_TYPE_DOUBLE &&
00358 datatype != DRMS_TYPE_LONGLONG) ? "compress Rice" : "",
00359 bzero, bscale, in);
00360 pjsd += sprintf(pjsd, "#======= End JSD =======\n");
00361
00362 if (printjsd)
00363 {
00364 printf("%s\n", jsd);
00365
00366 printf("#====== BEGIN KEYNAME MAP =======\n");
00367 printf("# REMOVE these keyname map lines from the JSD\n");
00368 printf("# place the keyname map into a file for later use\n");
00369 printf("# mapfile has structure: wantedDRMSname namefromFITSfile action\n");
00370 printf("# but the second column needs to be the auto-converted name to provoke substitution\n");
00371 printf("# use \"copy\" for default action - without quotes\n");
00372 for (imap=0; imap<nmap; imap++)
00373 printf("# FITS name %s is converted to %s on input.\n%s\t%s\t%s\n",
00374 oldnames[imap], newnames[imap], newnames[imap], newnames[imap], actions[imap]);
00375 printf("#======END KEYNAME MAP =======\n");
00376 }
00377 }
00378
00379
00380 if (havemap)
00381 {
00382 FILE *mapfile = fopen(map, "r");
00383 char line[10000];
00384 char newname[DRMS_MAXNAMELEN];
00385 char oldname[DRMS_MAXNAMELEN];
00386 char action[100];
00387 while (fgets(line, 1000, mapfile))
00388 {
00389 if (*line == '#')
00390 continue;
00391 if (sscanf(line,"%s%s%s", newname, oldname, action) != 3)
00392 {
00393 DIE("A mapfile line does not contain 3 words\n");
00394 }
00395 for (imap=0; imap<nmap; imap++)
00396 if (strcmp(oldname, oldnames[imap]) == 0)
00397 {
00398 if (newnames[imap]) free(newnames[imap]);
00399 newnames[imap] = strdup(newname);
00400 if (actions[imap]) free(actions[imap]);
00401 actions[imap] = strdup(action);
00402 break;
00403 }
00404 if (imap == nmap)
00405 {
00406 if (nmap >= MAXMAPLEN)
00407 {
00408 DIE("Too many mapped keywords, increase MAXMAPLEN\n");
00409 }
00410 newnames[nmap] = strdup(newname);
00411 oldnames[nmap] = strdup(oldname);
00412 actions[nmap] = strdup(action);
00413 nmap++;
00414 }
00415 }
00416 fclose(mapfile);
00417 }
00418
00419 if (wantcreate)
00420 {
00421 DRMS_Record_t *template;
00422 if (!haveseriesname || !haveprime)
00423 {
00424 DIE("Cant create series without ds and primekey args.\n");
00425 }
00426 if (drms_series_exists(drms_env , ds, &status))
00427 {
00428 DIE("Cant create existing series\n");
00429 }
00430 template = drms_parse_description(drms_env, jsd);
00431 if (template==NULL)
00432 {
00433 DIE("Failed to parse\n");
00434 }
00435 if (drms_create_series(template, 0))
00436 {
00437 DIE("Failed to create series.\n");
00438 }
00439 drms_free_record_struct(template);
00440 free(template);
00441 fprintf(stderr,"Series %s created.\n", ds);
00442 }
00443
00444 if (insertrec)
00445 {
00446 char *usename;
00447 char *action;
00448 DRMS_RecordSet_t *rs;
00449 DRMS_Record_t *rec;
00450 if (!drms_series_exists(drms_env , ds, &status))
00451 {
00452 DIE("Series does not exist, cant insert record\n");
00453 }
00454 rs = drms_create_records(drms_env, 1, ds, DRMS_PERMANENT, &status);
00455 if (status)
00456 {
00457 DIE("Could not create new records in series");
00458 }
00459 rec = rs->records[0];
00460 hiter_new (&hit, keywords);
00461 while ( key = (DRMS_Keyword_t *)hiter_getnext(&hit) )
00462 {
00463 DRMS_Keyword_t *outkey;
00464 usename = key->info->name;
00465 action = "copy";
00466 for (imap=0; imap<nmap; imap++)
00467 if (strcmp(usename, oldnames[imap]) == 0)
00468 {
00469 usename = newnames[imap];
00470 action = actions[imap];
00471 break;
00472 }
00473 outkey = drms_keyword_lookup(rec, usename, 0);
00474 if (outkey)
00475 {
00476 if (strcmp(action, "copy") == 0)
00477 drms_setkey(rec, usename, key->info->type, &key->value);
00478
00479 else
00480 {
00481 fprintf(stderr, "old keyword %s has no action to make new key %s\n",
00482 key->info->name, usename);
00483 DIE("no action found for keyword\n");
00484 }
00485 }
00486 }
00487
00488 hiter_free(&hit);
00489 status = drms_segment_write(drms_segment_lookupnum(rec,0), data, 0);
00490
00491 if (status)
00492 {
00493 DIE("Could not write record");
00494 }
00495 drms_close_records(rs, DRMS_INSERT_RECORD);
00496 }
00497
00498 drms_free_array(data);
00499 hcon_destroy(&keywords);
00500 return (DRMS_SUCCESS);
00501 }