00001 #include <sys/types.h> 00002 #include <sys/stat.h> 00003 #include <unistd.h> 00004 #include <stdlib.h> 00005 #include <string.h> 00006 #include <time.h> 00007 #include "jsoc_main.h" 00008 #include "drms.h" 00009 #include "fitsexport.h" 00010 #include "drms_names.h" 00011 00012 #define NOT_SPECIFIED "***Not Specified***" 00013 #define DIE(msg) {fprintf(stderr,"$$$$ %s: %s\n", module_name, msg); return 1;} 00014 00015 ModuleArgs_t module_args[] = 00016 { 00017 {ARG_STRING, "dsinp", NOT_SPECIFIED, "Series name w/optional record spec"}, 00018 {ARG_STRING, "pathout", "/cache/sdo/test_jps", "out base path"}, 00019 {ARG_STRING, "prefix", "AIA", "output filename prefix"}, 00020 {ARG_STRING, "segment", "0", "segment number"}, 00021 {ARG_STRING, "suffix", NOT_SPECIFIED, "output filename suffix"}, 00022 {ARG_STRING, "skip", "0", "skip writing existing files"}, 00023 {ARG_FLAG, "h", "0", "Print usage message and quit"}, 00024 {ARG_FLAG, "c", "0", "Compress output image"}, 00025 {ARG_FLAG, "s", "0", "sleep flag"}, 00026 {ARG_FLAG, "u", "0", "Uncompress output image"}, 00027 {ARG_FLAG, "v", "0", "verbose flag"}, 00028 {ARG_FLAG, "o", "0", "first segment flag"}, 00029 {ARG_END} 00030 }; 00031 00032 char *module_name = "drms2hdir"; 00033 00034 int de_compress=0, do_compress=0, do_sleep = 0, verbose = 0, do_1segment = 0; 00035 int skip = 0, segment; 00036 00037 int nice_intro(int help) 00038 { 00039 int usage = cmdparams_get_int(&cmdparams, "h", NULL) != 0; 00040 verbose = cmdparams_get_int(&cmdparams, "v", NULL) != 0; 00041 if (usage || help) { 00042 printf("aia_lev0_fits {-h} {-v} dsinp=<recordset query> pathout=name\n" 00043 " -h: print this message\n" 00044 " -c: compress image\n" 00045 " -s: sleep between writes\n" 00046 " -u: uncompress image\n" 00047 " -v: verbose\n" 00048 " -o: export only first segment\n" 00049 "skip=<[0|1]>\n" 00050 "dsinp=<recordset query> as <series>{[record specifier]} - required\n" 00051 "prefix=<output filename prefix> default is AIA\n" 00052 "segment=<segment number> default is 0\n" 00053 "suffix=<output filename suffix> default is wavelength as a number\n" 00054 "pathout=<output directory path>\n"); 00055 return(1); 00056 } 00057 do_sleep = cmdparams_get_int(&cmdparams, "s", NULL) != 0; 00058 do_compress = cmdparams_get_int(&cmdparams, "c", NULL) != 0; 00059 de_compress = cmdparams_get_int(&cmdparams, "u", NULL) != 0; 00060 do_1segment = cmdparams_get_int(&cmdparams, "o", NULL) != 0; 00061 skip = cmdparams_get_int(&cmdparams, "skip", NULL); 00062 return(0); 00063 } 00064 00065 int check_outpath(int yr, int mo, int da, int hr, char *outpathbase) 00066 { 00067 struct stat sbuf; 00068 char outfilename[512]; 00069 00070 if (stat(outpathbase, &sbuf)) { 00071 fprintf(stderr, "Cant stat output base path\n"); return 1; 00072 } 00073 sprintf(outfilename, "%s/%4.4d", outpathbase, yr); 00074 if (stat(outfilename, &sbuf)) { 00075 if (mkdir(outfilename, 0777)) { 00076 fprintf(stderr, "Cant make year diectory '%s'\n", outfilename); 00077 return 1; 00078 } 00079 } 00080 sprintf(outfilename, "%s/%4.4d/%2.2d", outpathbase, yr, mo); 00081 if (stat(outfilename, &sbuf)) { 00082 if (mkdir(outfilename, 0777)) { 00083 fprintf(stderr, "Cant make month diectory '%s'\n", outfilename); 00084 return 1; 00085 } 00086 } 00087 sprintf(outfilename, "%s/%4.4d/%2.2d/%2.2d", outpathbase, yr, mo, da); 00088 if (stat(outfilename, &sbuf)) { 00089 if (mkdir(outfilename, 0777)) { 00090 fprintf(stderr, "Cant make day diectory '%s'\n", outfilename); 00091 return 1; 00092 } 00093 } 00094 sprintf(outfilename, "%s/%4.4d/%2.2d/%2.2d/H%2.2d00", 00095 outpathbase, yr, mo, da, hr); 00096 if (stat(outfilename, &sbuf)) { 00097 if (mkdir(outfilename, 0777)) { 00098 fprintf(stderr, "Cant make day diectory '%s'\n", outfilename); 00099 return 1; 00100 } 00101 } 00102 return 0; 00103 } 00104 00105 int DoIt () 00106 { 00107 int irec, iseg, length[2], nrecs, nsegs, status=0, iwvl, ni=0, fsn, good_rec; 00108 int yr, mo, da, hr, mn, sc, ss, iris=0; 00109 int wvls[10] = { 171, 193, 335, 304, 1700, 211, 131, 94, 1600, 4500 }; 00110 char *dsinp, *outpathbase, *date_obs, outfilename[512], *outcparms=NULL, *wu; 00111 char c1, c2, c3, *prefix, *suffix, *imtype, ds[3]; 00112 time_t t0, tnow; 00113 DRMS_Record_t *inprec; 00114 DRMS_RecordSet_t *inprs; 00115 DRMS_Segment_t *inpseg; 00116 struct stat sbuf; 00117 TIME t_obs; 00118 00119 if (nice_intro(0)) return(0); 00120 dsinp = strdup(cmdparams_get_str(&cmdparams, "dsinp", NULL)); 00121 outpathbase = strdup(cmdparams_get_str(&cmdparams, "pathout", NULL)); 00122 prefix = strdup(cmdparams_get_str(&cmdparams, "prefix", NULL)); 00123 segment = cmdparams_get_int(&cmdparams, "segment", NULL); 00124 suffix = strdup(cmdparams_get_str(&cmdparams, "suffix", NULL)); 00125 if (strcmp(dsinp, NOT_SPECIFIED)==0) DIE("in argument is required"); 00126 if (!strncasecmp(prefix, "iris", 4)) iris = 1; 00127 00128 inprs = drms_open_records(drms_env, dsinp, &status); 00129 if (status) DIE("cant open recordset query"); 00130 drms_stage_records(inprs, 1, 0); 00131 nrecs = inprs->n; 00132 printf("%d records\n", nrecs); 00133 t0 = time(NULL); 00134 for (irec=0; irec<nrecs; irec++) { 00135 inprec = inprs->records[irec]; 00136 nsegs = hcon_size(&inprec->segments); 00137 if (do_1segment) nsegs = 1; 00138 for (iseg=segment; iseg<segment + nsegs; iseg++) { 00139 char *filename, *inpsegname, outpath[512]; 00140 float wavl; 00141 inpseg = drms_segment_lookupnum(inprec, iseg); 00142 inpsegname = inpseg->info->name; 00143 filename = inpseg->filename; 00144 if(strlen(filename)) { 00145 if (verbose) printf("rec %d, seg %d, protocol %d, '%s', '%s'\n", 00146 irec, iseg, inpseg->info->protocol, inpsegname, filename); 00147 wu = drms_getkey_string(inprec, "WAVEUNIT", &status); 00148 if (status && strcmp(suffix, NOT_SPECIFIED)==0) suffix = ""; 00149 if (!strncasecmp(wu, "nm", 2)) { 00150 iwvl = (int)(drms_getkey_float(inprec, "WAVELNTH", &status)*10+.5); 00151 } else if (!strncasecmp(wu, "angstrom", 8)) { 00152 iwvl = drms_getkey_int(inprec, "WAVELNTH", &status); 00153 } 00154 fsn = drms_getkey_int(inprec, "FSN", &status); 00155 t_obs = drms_getkey_time(inprec, "T_OBS", &status); 00156 date_obs = drms_getkey_string(inprec, "T_OBS", &status); 00157 good_rec = 1; 00158 if ((t_obs<0.0) || (fsn == 0x1c001c00)) good_rec = 0; 00159 sscanf(date_obs, "%d%c%d%c%d%c%d:%d:%d.%2d", 00160 &yr, &c1, &mo, &c2, &da, &c3, &hr, &mn, &sc, &ss); 00161 if (check_outpath(yr, mo, da, hr, outpathbase))DIE("Cant write to out"); 00162 if (do_1segment && !iseg) inpsegname = ""; 00163 imtype = drms_getkey_string(inprec, "IMG_TYPE", &status); 00164 if (strncmp(imtype, "DARK", 4)) strcpy(ds, ""); 00165 else strcpy(ds, "d"); 00166 sprintf(outpath, "%s/%4.4d/%2.2d/%2.2d/H%2.2d00", 00167 outpathbase, yr, mo, da, hr); 00168 if (iris) { 00169 int i; 00170 suffix = drms_getkey_string(inprec, "INSTRUME", &status); 00171 for (i=0; i<strlen(suffix); i++) suffix[i] = tolower(suffix[i]); 00172 } 00173 if (strcmp(suffix, NOT_SPECIFIED)==0) { 00174 sprintf(outfilename, 00175 "%s/%s%4.4d%2.2d%2.2d_%2.2d%2.2d%2.2d_%4.4d%s%s.fits", 00176 outpath, prefix, yr, mo, da, hr, mn, sc, iwvl, inpsegname, ds); 00177 } else { 00178 if (iris) { 00179 sprintf(outfilename, 00180 "%s/%s%4.4d%2.2d%2.2d_%2.2d%2.2d%2.2d%2.2d_%s%s%s.fits", outpath, 00181 prefix, yr, mo, da, hr, mn, sc, ss, suffix, inpsegname, ds); 00182 } else { 00183 sprintf(outfilename, 00184 "%s/%s%4.4d%2.2d%2.2d_%2.2d%2.2d%2.2d_%s%s%s.fits", 00185 outpath, prefix, yr, mo, da, hr, mn, sc, suffix, inpsegname, ds); 00186 } 00187 } 00188 if ((stat(outfilename, &sbuf) || !skip) && good_rec) { 00189 if (do_compress) outcparms = "compress"; 00190 if (de_compress) { 00191 outcparms = ""; 00192 } 00193 if (DRMS_SUCCESS != fitsexport_export_tofile(inpseg, outcparms, outfilename, NULL, NULL)) { 00194 DIE("cant write FITS file"); 00195 } 00196 if (do_sleep) { 00197 ni++; 00198 tnow = time(NULL); 00199 if ((tnow - t0) < (1.25*ni)) sleep(1); 00200 } 00201 } 00202 } 00203 } 00204 } 00205 return status; 00206 }