00001 #include "jsoc_main.h"
00002 #include "drms.h"
00003 #include "drms_names.h"
00004
00005 #define NOTSPECIFIED "***NOTSPECIFIED***"
00006 #define DIE(msg) {fprintf(stderr,"$$$$ %s: %s\n", module_name, msg); return 1;}
00007
00008 ModuleArgs_t module_args[] = {
00009 {ARG_STRING, "ds", "lm_jps.pfss", "output data series"},
00010 {ARG_STRING, "ser_beg", NOTSPECIFIED, "serial number to begin"},
00011 {ARG_STRING, "ser_end", NOTSPECIFIED, "serial number to end (not saved)"},
00012 {ARG_STRING, "date_beg", NOTSPECIFIED, "date to begin"},
00013 {ARG_STRING, "date_end", NOTSPECIFIED, "date to end (not saved)"},
00014 {ARG_STRING, "spath", "/archive/pfss/kitrun48/surffield-serial", ""},
00015 {ARG_STRING, "bpath", "/archive/pfss/kitrun48/Bfield-serial", ""},
00016 {ARG_STRING, "sbase", "%s/kitrun048_%5.5d.sav", "surface base file name"},
00017 {ARG_STRING, "bbase", "%s/Bfield_%5.5d.sav", "B field base file name"},
00018 {ARG_FLAG, "h", "0", "Print usage message and quit"},
00019 {ARG_FLAG, "v", "0", "verbose flag"},
00020 {ARG_END}
00021 };
00022
00023 char *module_name = "pfss_backup";
00024 int verbose = 0;
00025
00026 void sprint_time_ISO (char *tstring, TIME t)
00027 {
00028 sprint_at(tstring,t);
00029 tstring[4] = tstring[7] = '-';
00030 tstring[10] = 'T';
00031 tstring[19] = '\0';
00032 }
00033
00034 int nice_intro(int help)
00035 {
00036 int usage = cmdparams_get_int(&cmdparams, "h", NULL) != 0;
00037 verbose = cmdparams_get_int(&cmdparams, "v", NULL) != 0;
00038 if (usage || help) {
00039 printf("pfss_backup -h -v ds=output_series ser_beg=sn1 ser_end=sn_stop\n"
00040 " -h: print this message\n"
00041 " -v: verbose\n"
00042 "ds=<output series name> default is lm_jps.pfss\n"
00043 "ser_beg=<first serial number to backup>\n"
00044 "ser_end=<serial number to stop> (not backup)\n"
00045 "date_beg=<first date to backup>\n"
00046 "date_end=<date to stop> (not backup)\n"
00047 "spath=<base path to surface field files>\n"
00048 "bpath=<base path to B field files>\n"
00049 "sbase=<surface field base file name>\n"
00050 "bbase=<B field field base file name>\n");
00051 return(1);
00052 }
00053 return(0);
00054 }
00055
00056 int DoIt ()
00057 {
00058 char *date_beg, *date_end, *ds, *spath, *bpath, *sbase, *bbase;
00059 char sname[512], bname[512], now_str[64];
00060 int ser_beg, ser_end, ser_num, status=0;
00061 int s_ok, b_ok, sp_ok, bp_ok;
00062 time_t trec, tnow;
00063 DRMS_Record_t *outrec;
00064 DRMS_Segment_t *outseg;
00065 struct tm tm_rec;
00066 struct stat sbuf;
00067
00068 if (nice_intro(0)) return(0);
00069 ser_beg = cmdparams_get_int(&cmdparams, "ser_beg", NULL);
00070 ser_end = cmdparams_get_int(&cmdparams, "ser_end", NULL);
00071 ds = strdup(cmdparams_get_str(&cmdparams, "ds", NULL));
00072 spath = strdup(cmdparams_get_str(&cmdparams, "spath", NULL));
00073 bpath = strdup(cmdparams_get_str(&cmdparams, "bpath", NULL));
00074 sbase = strdup(cmdparams_get_str(&cmdparams, "sbase", NULL));
00075 bbase = strdup(cmdparams_get_str(&cmdparams, "bbase", NULL));
00076 if (verbose) fprintf(stdout, "'%s'\n'%s'\n", spath, bpath);
00077 if (verbose) fprintf(stdout, "'%s'\n'%s'\n", sbase, bbase);
00078 if (ser_beg < 0) {
00079 time_t tr_b, tr_e;
00080 date_beg = strdup(cmdparams_get_str(&cmdparams, "date_beg", NULL));
00081 tr_b = sscan_time(date_beg) - UNIX_EPOCH + 218;
00082 ser_beg = (tr_b - 836179440)/21600;
00083 date_end = strdup(cmdparams_get_str(&cmdparams, "date_end", NULL));
00084 tr_e = sscan_time(date_end) - UNIX_EPOCH + 218;
00085 ser_end = (tr_e - 836179440)/21600;
00086 if (verbose) {
00087 fprintf(stderr, "trec: %ld %ld,ser_beg, end: %d, %d\n",tr_b,tr_e,ser_beg,ser_end);
00088 }
00089 }
00090 if (stat(spath, &sbuf)) {
00091 sp_ok = 0;
00092 fprintf(stderr, "Can not stat %s\n", spath);
00093
00094 } else sp_ok = 1;
00095 if (stat(bpath, &sbuf)) {
00096 bp_ok = 0;
00097 fprintf(stderr, "Can not stat %s\n", bpath);
00098
00099 } else bp_ok = 1;
00100
00101
00102 for (ser_num = ser_beg; ser_num < ser_end; ser_num++) {
00103 trec = 21600*ser_num + 836179440 - 8;
00104 if (verbose) {
00105 fprintf(stderr, "trec: %ld,ser_beg, end: %d, %d\n", trec, ser_beg, ser_end);
00106 }
00107 gmtime_r(&trec, &tm_rec);
00108 snprintf(sname, 512, sbase, spath, ser_num);
00109 snprintf(bname, 512, bbase, bpath, ser_num);
00110 outrec = drms_create_record(drms_env, ds, DRMS_PERMANENT, &status);
00111 if (status) DIE("Can't create output record");
00112
00113 sprint_time_ISO(now_str, trec + UNIX_EPOCH);
00114 drms_setkey_string(outrec, "model_date", now_str);
00115 if (verbose) fprintf(stderr, "model_date: %s\n", now_str);
00116 drms_setkey_int(outrec, "serial", ser_num);
00117 if (stat(sname, &sbuf)) {
00118 s_ok = 0;
00119 if (s_ok) fprintf(stderr, "Can not stat %s\n", sname);
00120 } else {
00121 s_ok = 1;
00122
00123 sprint_time_ISO(now_str, sbuf.st_mtime + UNIX_EPOCH);
00124 drms_setkey_string(outrec, "s_calc_date", now_str);
00125 }
00126 if (stat(bname, &sbuf)) {
00127 b_ok = 0;
00128 if (b_ok) fprintf(stderr, "Can not stat %s\n", bname);
00129 } else {
00130 b_ok = 1;
00131
00132 sprint_time_ISO(now_str, sbuf.st_mtime + UNIX_EPOCH);
00133 drms_setkey_string(outrec, "b_calc_date", now_str);
00134 }
00135 tnow = CURRENT_SYSTEM_TIME;
00136 drms_setkey_time(outrec, "backup_date", tnow);
00137 sprint_time_ISO(now_str, tnow);
00138 if (s_ok) {
00139 outseg = drms_segment_lookup(outrec, "surffield");
00140 if(drms_segment_write_from_file(outseg, sname)) {
00141 fprintf(stderr, "Can not write surface field segment for %s.\n", sname);
00142 }
00143 }
00144 if (b_ok) {
00145 outseg = drms_segment_lookup(outrec, "Bfield");
00146 if(drms_segment_write_from_file(outseg, bname)) {
00147 fprintf(stderr, "Can not write B field segment for %s.\n", bname);
00148 }
00149 }
00150 if (s_ok || b_ok) status = drms_close_record(outrec, DRMS_INSERT_RECORD);
00151 else status = drms_close_record(outrec, DRMS_FREE_RECORD);
00152 if(status) fprintf(stderr, "Error closing rec for ser #: %d.\n", ser_num);
00153 }
00154 return status;
00155 }