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
00070
00071
00072
00073
00074
00075 #define MAX_KEYMAPS 10
00076 #define MAX_KEYS 256
00077
00078 #define FSN_CAM_ID_FIXED 50630
00079 #define FSN_TAI_FIXED 1000000
00080
00081
00082 #define TABLE_PATH "proj/lev0/apps/data/"
00083
00084
00085 #define UPDATE_CONFIG_PROG "proj/lev0/scripts/aia_ground/set_config_by_time.csh"
00086
00087
00088 #define CONFIG_SERIES "aia_ground.lev0_config"
00089
00090
00091 #include "jsoc_main.h"
00092 #include "drms.h"
00093 #include "drms_names.h"
00094
00095
00096
00097 #include "dr.h"
00098
00099
00100 #include <string.h>
00101 #include <stdlib.h>
00102
00103 #define NOT_SPECIFIED "***Not Specified***"
00104 #define DIE(msg) {fprintf(stderr,"$$$$ %s: %s\n",module_name,msg); return 1;}
00105
00106
00107
00108 ModuleArgs_t module_args[] =
00109 {
00110 {ARG_STRING, "in", NOT_SPECIFIED, "Path to DSDS aia_ground lev0 file"},
00111 {ARG_STRING, "out", NOT_SPECIFIED, "drms series for lev0 data"},
00112 {ARG_STRING, "fsn_key", "HSQFGSN", "Filtergram number keyword name"},
00113 {ARG_STRING, "time_key", "HOBITSEC", "Filtergram time keyword name"},
00114 {ARG_STRING, "keymap", NOT_SPECIFIED, "Keyword mapping table"},
00115 {ARG_STRING, "dsds", NOT_SPECIFIED, "DSDS source dataset name"},
00116 {ARG_FLAG, "r", "0", "RAL EGSE data expected"},
00117 {ARG_FLAG, "h", "0", "Print usage message and quit"},
00118 {ARG_FLAG, "v", "0", "verbose flag"},
00119 {ARG_END}
00120 };
00121
00122 char *module_name = "aia_import_egse_lev0";
00123
00124
00125
00126 int verbose = 0;
00127 int nice_intro(int help)
00128 {
00129 int usage = cmdparams_get_int(&cmdparams, "h", NULL) != 0;
00130 verbose = cmdparams_get_int(&cmdparams, "v", NULL) != 0;
00131 if (usage || help)
00132 {
00133 printf("import_egse_lev0 in='fits file' out='jsoc series' {keymap='keymap file'} {dsds='dsds dataset'} {-h} {-r} {-v} "
00134 " -h: print this message\n"
00135 " -r: RAL data input, no external config info needed\n"
00136 " -v: verbose\n"
00137 "keymap=<key map file> - optional\n"
00138 "dsds=<dsds dataset name for source file\n"
00139 "in=<lev0 fits file> - required\n"
00140 "out=<drms lev0 series> - required\n");
00141 return(1);
00142 }
00143 return(0);
00144 }
00145
00146
00147
00148 TIME SDO_to_DRMS_time(int sdo_s, int sdo_ss);
00149 int set_mech_values(DRMS_Record_t *rec, DR *dr, int fsn);
00150 int init_keymaps(char *keymaps[MAX_KEYS][MAX_KEYMAPS], int *nkeys, char *key_map_file);
00151 void sprint_time_ISO (char *tstring, TIME t);
00152
00153
00154
00155 int DoIt(void)
00156 {
00157 int status = 0;
00158 int RALmode = 0;
00159 int fsn;
00160 TIME t_obs;
00161 char *in, *out;
00162 char *in_filename;
00163 char tmpfile[1024];
00164 char tmpdir[1024];
00165 char *dsdsname;
00166 char *keymap;
00167 DRMS_Record_t *rec;
00168 DRMS_RecordSet_t *rs;
00169 HIterator_t key_hit;
00170 DRMS_Keyword_t *key;
00171 DR *lev0, *tmpdr;
00172 char *keymaps[MAX_KEYS][MAX_KEYMAPS];
00173 int keytarg, keytargs, newkey;
00174 ATTRIBUTES *fits_attr;
00175 TIME TIME2006Jan = sscan_time("2006.01.01_00:00:00");
00176
00177 if (nice_intro(0))
00178 return(0);
00179
00180 printf("Import ground test lev0 data:\n");
00181
00182
00183 in = strdup(cmdparams_get_str(&cmdparams, "in", NULL));
00184 if (strcmp(in,NOT_SPECIFIED)==0)
00185 DIE("in argument is required");
00186 printf(" in=%s\n", in);
00187
00188 out = strdup(cmdparams_get_str(&cmdparams, "out", NULL));
00189 if (strcmp(out,NOT_SPECIFIED)==0)
00190 DIE("out argument is required");
00191 printf(" out=%s\n", out);
00192
00193 dsdsname = cmdparams_get_str(&cmdparams, "dsds", NULL);
00194 if (strcmp(dsdsname,NOT_SPECIFIED)==0)
00195 dsdsname = "n.a.";
00196 printf(" dsds=%s\n", dsdsname);
00197
00198 keymap = cmdparams_get_str(&cmdparams, "keymap", NULL);
00199 if (strcmp(keymap,NOT_SPECIFIED)==0)
00200 printf(" keymap not used\n");
00201 else
00202 printf(" keymap=%s\n", keymap);
00203 if (init_keymaps(keymaps, &keytargs, keymap))
00204 DIE("Key Mapping File not found");
00205 printf(" keymap count=%d\n",keytargs);
00206
00207 RALmode = cmdparams_get_int(&cmdparams, "r", NULL);
00208 printf(" %s\n", (RALmode ? "RAL mode" : "DCHRI mode"));
00209
00210
00211
00212 rs = drms_create_records(drms_env, 1, out, DRMS_PERMANENT, &status);
00213 if (status)
00214 DIE("cant create records in output series");
00215 rec = rs->records[0];
00216
00217
00218
00219 lev0 = dr_get_fits(in);
00220 if (!lev0)
00221 DIE("Failed to read fits header");
00222 tmpdr = lev0;
00223
00224
00225
00226 drms_setkey_string(rec, "DSDS_SRC", dsdsname);
00227 dr_setkey_str(tmpdr, "DSDS_SRC", dsdsname);
00228 drms_setkey_string(rec, "CONFIG", (RALmode ? "RAL" : "CIF"));
00229 dr_setkey_str(tmpdr, "CONFIG", (RALmode ? "RAL" : "CIF"));
00230
00231
00232
00233 hiter_new(&key_hit, &rec->keywords);
00234 while( (key = (DRMS_Keyword_t *)hiter_getnext(&key_hit)) )
00235 {
00236 char describe[DRMS_MAXCOMMENTLEN];
00237 char *shortname;
00238 char *usename;
00239 char *keyname = key->info->name;
00240 strcpy(describe, key->info->description);
00241 shortname = strtok(describe," \t,:");
00242 if (!shortname)
00243 shortname = keyname;
00244 if (strlen(shortname) > 8)
00245 DIE("shortname too long\n");
00246
00247 if (dr_search_attr(lev0, shortname) != NULL)
00248 {
00249 usename = shortname;
00250
00251 }
00252 else
00253 {
00254 usename = shortname;
00255
00256
00257 for (keytarg = 0; keytarg < keytargs; keytarg++)
00258 {
00259 if (strcmp(shortname, keymaps[keytarg][0]) == 0)
00260 {
00261 int trykey;
00262
00263 for (trykey=1; trykey < MAX_KEYMAPS && keymaps[keytarg][trykey] != NULL; trykey++)
00264 if (fits_attr = dr_search_attr(lev0, keymaps[keytarg][trykey]))
00265 {
00266 usename = keymaps[keytarg][trykey];
00267
00268 break;
00269 }
00270 break;
00271 }
00272 }
00273
00274 }
00275
00276 if (strcmp(keyname, "T_OBS") == 0)
00277 {
00278 char t_obs_str[40];
00279 int t_obs_s;
00280 char *time_key = strdup(cmdparams_get_str(&cmdparams, "time_key", NULL));
00281 if (RALmode)
00282 {
00283 char *filename = DR_getkey_str(lev0, "FILENAME");
00284 int y,M,d,h,m,s;
00285 sscanf(filename, "i_%2d%2d%2d_%2d%2d%2d", &y, &M, &d, &h, &m, &s);
00286 sprintf(t_obs_str, "20%02d.%02d.%02d_%02d:%02d:%02d_UTC", y, M, d, h, m, s);
00287 t_obs = sscan_time(t_obs_str);
00288 }
00289 else
00290 {
00291 t_obs_s = dr_getkey_int(lev0, time_key);
00292 if (is_I_MISSING(t_obs_s) || t_obs_s < TIME2006Jan )
00293 {
00294 t_obs_s = dr_getkey_int(lev0, "SHS");
00295 if (is_I_MISSING(t_obs_s))
00296 DIE("Failed to find either time keyword");
00297 }
00298 t_obs = SDO_to_DRMS_time(t_obs_s, 0);
00299 }
00300 drms_setkey_double(rec, "T_OBS", t_obs);
00301 dr_setkey_time(tmpdr, "T_OBS", t_obs);
00302 sprint_ut(t_obs_str, t_obs);
00303 printf(" T_OBS=%s\n", t_obs_str);
00304 }
00305 else if (strcmp(keyname, "FSN")==0)
00306 {
00307 char *fsn_key = strdup(cmdparams_get_str(&cmdparams, "fsn_key", NULL));
00308 fsn = dr_getkey_int(lev0, fsn_key);
00309 if (is_I_MISSING(fsn))
00310 DIE("Failed to find FSN keyword");
00311 drms_setkey_int(rec, "FSN", fsn);
00312 dr_setkey_int(tmpdr, "FSN", fsn);
00313 printf(" FSN=%d\n", fsn);
00314 }
00315 if (strcmp(keyname, "TELEM_T") == 0)
00316 {
00317 int t = dr_getkey_int(lev0, "SHS");
00318 int tf = dr_getkey_int(lev0, "SHSS");
00319 if (!is_I_MISSING(t) && !is_I_MISSING(tf))
00320 {
00321 TIME telem_t = SDO_to_DRMS_time(t, tf);
00322 drms_setkey_double(rec, keyname, telem_t);
00323 dr_setkey_time(tmpdr, shortname, telem_t);
00324 }
00325 }
00326 else if (usename)
00327 {
00328 if (fits_attr = dr_search_attr(lev0, usename))
00329 {
00330 char *fits_val_str = dr_attrvalue_str(fits_attr);
00331
00332 drms_setkey_string(rec, keyname, fits_val_str);
00333 if (strcmp(usename, shortname) != 0)
00334 {
00335 free(fits_attr->name);
00336 fits_attr->name = strdup(shortname);
00337 }
00338 if (RALmode)
00339 {
00340 if (!strcmp(shortname,"HPCUPOLR") ||
00341 !strcmp(shortname,"HPCUPOLM") ||
00342 !strcmp(shortname,"HPCURETR") ||
00343 !strcmp(shortname,"HPCURETM") )
00344 dr_setkey_double(tmpdr, shortname, strtod(fits_val_str, NULL));
00345 }
00346 free(fits_val_str);
00347 }
00348 else
00349 {
00350
00351 dr_setkey_drmstype(tmpdr, shortname, key);
00352 }
00353 }
00354 }
00355
00356
00357 if (!RALmode)
00358 {
00359 char cmdline[1024], t_obs_str[1024];
00360 int status;
00361 char config_ds[1024];
00362 DRMS_Record_t *config_rec;
00363 DRMS_RecordSet_t *config_rs;
00364 HIterator_t config_key_hit;
00365 DRMS_Keyword_t *config_key;
00366
00367 if (fsn < FSN_TAI_FIXED)
00368 {
00369 t_obs += 33.0;
00370 drms_setkey_double(rec, "T_OBS", t_obs);
00371 dr_setkey_time(tmpdr, "T_OBS", t_obs);
00372 sprint_ut(t_obs_str, t_obs);
00373 printf(" CORRECTED T_OBS=%s\n", t_obs_str);
00374 }
00375
00376 sprint_time(t_obs_str, t_obs, "UTC", 0);
00377 sprintf(cmdline, "%s/%s '%s' %d %s", cmdparams_get_str(&cmdparams, "JSOCROOT", NULL),
00378 UPDATE_CONFIG_PROG, t_obs_str, fsn, cmdparams_get_str(&cmdparams, "DRMSSESSION", NULL));
00379 status = system(cmdline);
00380
00381 sprintf(config_ds, "%s[%d]", CONFIG_SERIES, fsn);
00382 config_rs = drms_open_records(drms_env, config_ds, &status);
00383 if (status == 0)
00384 {
00385 config_rec = config_rs->records[0];
00386 hiter_new(&config_key_hit, &config_rec->keywords);
00387 while( (config_key = (DRMS_Keyword_t *)hiter_getnext(&config_key_hit)) )
00388 {
00389 int stat = drms_setkey(rec, config_key->info->name, config_key->info->type, &config_key->value);
00390
00391 dr_setkey_drmstype(tmpdr, config_key->info->name, config_key);
00392 }
00393 drms_close_records(config_rs, DRMS_FREE_RECORD);
00394 }
00395 else
00396 fprintf(stderr,"cant open config records for %s\n",config_ds);
00397
00398 set_mech_values(rec, tmpdr, fsn);
00399 }
00400
00401
00402 {
00403 char tmpstr[100];
00404 sprint_time_ISO(tmpstr,CURRENT_SYSTEM_TIME);
00405 drms_setkey_string(rec, "DATE", tmpstr);
00406 dr_setkey_str(tmpdr, "DATE", tmpstr);
00407 }
00408
00409
00410 if (in_filename = strrchr(in,'/'))
00411 in_filename++;
00412 else
00413 in_filename = in;
00414 status = dr_write_fits_to_drms_segment(tmpdr, in_filename, rec, 0);
00415 if (status)
00416 DIE("FITS save in segment failure");
00417 dr_free(&lev0);
00418 status = drms_close_records(rs, DRMS_INSERT_RECORD);
00419 if (status)
00420 DIE("close failure");
00421 return 0;
00422 }
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435 TIME SDO_to_DRMS_time(int sdo_s, int sdo_ss)
00436 {
00437 static int firstcall = 1;
00438 static TIME sdo_epoch;
00439 if (firstcall)
00440 {
00441 firstcall = 0;
00442 sdo_epoch = sscan_time("1958.01.01_00:00:00_TAI");
00443 }
00444 return(sdo_epoch + (TIME)sdo_s + (TIME)sdo_ss/65536.0);
00445 }
00446
00447
00448
00449
00450 int set_mech_values(DRMS_Record_t *rec, DR *lev0, int fsn)
00451 {
00452 static int called = 0;
00453
00454 #define MAXROWS 10000
00455 static int pol[MAXROWS*4];
00456 static int tuning[MAXROWS*5];
00457 static int focus[MAXROWS*3];
00458 static int expose[MAXROWS*3];
00459
00460 static char *pol_keys[] = {"HPL1POS", "HPL2POS", "HPL3POS"};
00461 static char *pol_longkeys[] = {"HPL1POS", "HPL2POS", "HPL3POS"};
00462 static char *tuning_keys[] = {"HWL1POS", "HWL2POS", "HWL3POS", "HWL4POS"};
00463 static char *tuning_longkeys[] = {"HWL1POS", "HWL2POS", "HWL3POS", "HWL4POS"};
00464 static char *focus_keys[] = {"HCF1POS", "HCF2POS"};
00465 static char *focus_longkeys[] = {"HCF1POS", "HCF2POS"};
00466 static char *expose_keys[] = {"HSHIEXP", "HSHIEXP"};
00467 static char *expose_longkeys[] = {"HMI_FSW_IMG_CMDED_EXPOSURE", "HMI_FSW_IMG_CMDED_EXPOSURE"};
00468
00469 static char *camkey = "HCAMID";
00470 static char *camkey_longname = "HMI_SEQ_ID_EXP_PATH";
00471
00472 typedef struct tabinf
00473 {
00474 char *filename;
00475 char *index;
00476 char **keys;
00477 char **longkeys;
00478 int *table;
00479 int cols;
00480 char *longname;
00481 } TABINFO;
00482 static TABINFO tabinfo[] = {"in_air_cal3.p", "HPLTID", pol_keys, pol_longkeys, pol, 3,"HMI_SEQ_ID_PST",
00483 "in_air_cal3.w", "HWLTID", tuning_keys, tuning_longkeys, tuning, 4,"HMI_SEQ_ID_WLT",
00484 "in_air_cal.c", "HCFTID", focus_keys, focus_longkeys, focus, 2,"HMI_SEQ_ID_FOCUS",
00485 "in_air_cal.e", "HSQEIDX", expose_keys, expose_longkeys, expose, 2,"HMI_SEQ_EXPOSURE_INDX"};
00486 int tab;
00487 int status;
00488 int camera = dr_getkey_int(lev0, camkey);
00489 if (camera < 0 || camera > 3)
00490 {
00491 fprintf(stderr,"XX camera=%d outside range, use camera 1 exposures\n",camera);
00492 camera=1;
00493 }
00494
00495 if (!called)
00496 {
00497 called = 1;
00498 for (tab=0; tab<4; tab++)
00499 {
00500 char tablepath[1024];
00501 int idx, *res, val, vals;
00502 FILE *fp;
00503 char line[1024];
00504 strcpy(tablepath, cmdparams_get_str(&cmdparams, "JSOCROOT", NULL));
00505 strcat(tablepath, "/");
00506 strcat(tablepath, TABLE_PATH);
00507 strcat(tablepath, tabinfo[tab].filename);
00508 fp = fopen(tablepath, "r");
00509 if (!fp)
00510 {
00511 fprintf(stderr,"Failed to open mech table %s, die.\n",tablepath);
00512 return(1);
00513 }
00514 res = tabinfo[tab].table;
00515 vals = tabinfo[tab].cols;
00516 for (idx=0; idx<MAXROWS; idx++)
00517 for (val=0; val<vals+1; val++)
00518 res[val + (vals+1)*idx] = -1;
00519 for (idx=0; fgets(line,1024,fp); )
00520 {
00521 if (*line != '#')
00522 {
00523 char *e, *p=line;
00524 int d;
00525 for (val=0; val<vals+1; val++)
00526 {
00527 d = strtod(p, &e);
00528 if (e == p)
00529 break;
00530 else
00531 {
00532 p = e;
00533 res[val + (vals+1)*idx] = d;
00534 }
00535 }
00536 if (res[(vals+1)*idx] >= 0)
00537 idx++;
00538 }
00539 }
00540 fclose(fp);
00541 }
00542 }
00543
00544 for (tab=0; tab<4; tab++)
00545 {
00546 int row, index;
00547 int status;
00548 int val, vals = tabinfo[tab].cols;
00549 int *res = tabinfo[tab].table;
00550 char **keys = tabinfo[tab].keys;
00551 char **longkeys = tabinfo[tab].longkeys;
00552 index = dr_getkey_int(lev0, tabinfo[tab].index);
00553 if (tab == 3)
00554 {
00555 if (fsn < FSN_CAM_ID_FIXED)
00556 index += 1;
00557 dr_setkey_int(lev0, tabinfo[tab].index, index);
00558 drms_setkey_int(rec, tabinfo[tab].longname, index);
00559 }
00560 if (is_I_MISSING(index))
00561 {
00562 fprintf(stderr,"Mech Index %s not found.\n",tabinfo[tab].index);
00563 continue;
00564 }
00565 for (row=0; row<MAXROWS; row++)
00566 if (index == res[(vals+1)*row])
00567 {
00568 if (tab<3)
00569 {
00570 for (val=0; val<vals; val++)
00571 {
00572 drms_setkey_int(rec, longkeys[val], res[val+1+(vals+1)*row]);
00573 dr_setkey_int(lev0, keys[val], res[val+1+(vals+1)*row]);
00574 }
00575 break;
00576 }
00577 else
00578 {
00579 int exposure;
00580 if (camera <= 1)
00581 exposure = 0;
00582 else
00583 {
00584 camera -= 2;
00585 exposure = res[camera+1+(vals+1)*row];
00586 }
00587 drms_setkey_int(rec, longkeys[0], exposure);
00588 dr_setkey_int(lev0, keys[0], exposure);
00589 drms_setkey_int(rec, camkey_longname, camera);
00590 dr_setkey_int(lev0, camkey, camera);
00591 break;
00592 }
00593 }
00594 }
00595 return(0);
00596 }
00597
00598
00599
00600 int init_keymaps(char *keymaps[MAX_KEYS][MAX_KEYMAPS], int *nkeys, char *key_map_file)
00601 {
00602 int key, map;
00603 for (key=0; key<MAX_KEYS; key++)
00604 for (map=0; map<MAX_KEYMAPS; map++)
00605 keymaps[key][map] = NULL;
00606 key = 0;
00607 if (strcmp(key_map_file, NOT_SPECIFIED) != 0)
00608 {
00609 FILE *km = fopen(key_map_file,"r");
00610 char line[1024];
00611 if (!km)
00612 return(1);
00613 key=0;
00614 while (fgets(line, 1024, km))
00615 {
00616 char *tok;
00617 for (map=0, tok = strtok(line, " \t,:\n"); map<MAX_KEYMAPS && tok; map++, tok=strtok(NULL, " \t,\n"))
00618 {
00619 keymaps[key][map] = strdup(tok);
00620 }
00621 key++;
00622 }
00623 }
00624 *nkeys = key;
00625 return(0);
00626 }
00627
00628 void sprint_time_ISO (char *tstring, TIME t)
00629 {
00630 sprint_at(tstring,t);
00631 tstring[4] = tstring[7] = '-';
00632 tstring[10] = 'T';
00633 tstring[19] = '\0';
00634 }