1 |
#include <sys/types.h> |
2 |
#include <sys/stat.h> |
3 |
#include <unistd.h> |
4 |
#include <stdlib.h> |
5 |
#include <string.h> |
6 |
#include <time.h> |
7 |
#include "jsoc_main.h" |
8 |
#include "drms.h" |
9 |
#include "fitsexport.h" |
10 |
#include "drms_names.h" |
11 |
|
12 |
#define NOT_SPECIFIED "***Not Specified***" |
13 |
#define DIE(msg) {fprintf(stderr,"$$$$ %s: %s\n", module_name, msg); return 1;} |
14 |
|
15 |
ModuleArgs_t module_args[] = |
16 |
{ |
17 |
{ARG_STRING, "dsinp", NOT_SPECIFIED, "Series name w/optional record spec"}, |
18 |
{ARG_STRING, "pathout", "/cache/sdo/test_jps", "out base path"}, |
19 |
{ARG_STRING, "prefix", "AIA", "output filename prefix"}, |
20 |
{ARG_STRING, "segment", "0", "segment number"}, |
21 |
{ARG_STRING, "suffix", NOT_SPECIFIED, "output filename suffix"}, |
22 |
{ARG_STRING, "skip", "0", "skip writing existing files"}, |
23 |
{ARG_FLAG, "h", "0", "Print usage message and quit"}, |
24 |
{ARG_FLAG, "c", "0", "Compress output image"}, |
25 |
{ARG_FLAG, "s", "0", "sleep flag"}, |
26 |
{ARG_FLAG, "u", "0", "Uncompress output image"}, |
27 |
{ARG_FLAG, "v", "0", "verbose flag"}, |
28 |
{ARG_FLAG, "o", "0", "first segment flag"}, |
29 |
{ARG_END} |
30 |
}; |
31 |
|
32 |
char *module_name = "aia_lev0_fits"; |
33 |
|
34 |
int de_compress=0, do_compress=0, do_sleep = 0, verbose = 0, do_1segment = 0; |
35 |
int skip = 0, segment; |
36 |
|
37 |
int nice_intro(int help) |
38 |
{ |
39 |
int usage = cmdparams_get_int(&cmdparams, "h", NULL) != 0; |
40 |
verbose = cmdparams_get_int(&cmdparams, "v", NULL) != 0; |
41 |
if (usage || help) { |
42 |
printf("aia_lev0_fits {-h} {-v} dsinp=<recordset query> pathout=name\n" |
43 |
" -h: print this message\n" |
44 |
" -c: compress image\n" |
45 |
" -s: sleep between writes\n" |
46 |
" -u: uncompress image\n" |
47 |
" -v: verbose\n" |
48 |
" -o: export only first segment\n" |
49 |
"skip=<[0|1]>\n" |
50 |
"dsinp=<recordset query> as <series>{[record specifier]} - required\n" |
51 |
"prefix=<output filename prefix> default is AIA\n" |
52 |
"segment=<segment number> default is 0\n" |
53 |
"suffix=<output filename suffix> default is wavelength as a number\n" |
54 |
"pathout=<output directory path>\n"); |
55 |
return(1); |
56 |
} |
57 |
do_sleep = cmdparams_get_int(&cmdparams, "s", NULL) != 0; |
58 |
do_compress = cmdparams_get_int(&cmdparams, "c", NULL) != 0; |
59 |
de_compress = cmdparams_get_int(&cmdparams, "u", NULL) != 0; |
60 |
do_1segment = cmdparams_get_int(&cmdparams, "o", NULL) != 0; |
61 |
skip = cmdparams_get_int(&cmdparams, "skip", NULL); |
62 |
return(0); |
63 |
} |
64 |
|
65 |
int check_outpath(int yr, int mo, int da, int hr, char *outpathbase) |
66 |
{ |
67 |
struct stat sbuf; |
68 |
char outfilename[512]; |
69 |
|
70 |
if (stat(outpathbase, &sbuf)) { |
71 |
fprintf(stderr, "Cant stat output base path\n"); return 1; |
72 |
} |
73 |
sprintf(outfilename, "%s/%4.4d", outpathbase, yr); |
74 |
if (stat(outfilename, &sbuf)) { |
75 |
if (mkdir(outfilename, 0777)) { |
76 |
fprintf(stderr, "Cant make year diectory '%s'\n", outfilename); |
77 |
return 1; |
78 |
} |
79 |
} |
80 |
sprintf(outfilename, "%s/%4.4d/%2.2d", outpathbase, yr, mo); |
81 |
if (stat(outfilename, &sbuf)) { |
82 |
if (mkdir(outfilename, 0777)) { |
83 |
fprintf(stderr, "Cant make month diectory '%s'\n", outfilename); |
84 |
return 1; |
85 |
} |
86 |
} |
87 |
sprintf(outfilename, "%s/%4.4d/%2.2d/%2.2d", outpathbase, yr, mo, da); |
88 |
if (stat(outfilename, &sbuf)) { |
89 |
if (mkdir(outfilename, 0777)) { |
90 |
fprintf(stderr, "Cant make day diectory '%s'\n", outfilename); |
91 |
return 1; |
92 |
} |
93 |
} |
94 |
sprintf(outfilename, "%s/%4.4d/%2.2d/%2.2d/H%2.2d00", |
95 |
outpathbase, yr, mo, da, hr); |
96 |
if (stat(outfilename, &sbuf)) { |
97 |
if (mkdir(outfilename, 0777)) { |
98 |
fprintf(stderr, "Cant make day diectory '%s'\n", outfilename); |
99 |
return 1; |
100 |
} |
101 |
} |
102 |
return 0; |
103 |
} |
104 |
|
105 |
int DoIt () |
106 |
{ |
107 |
int irec, iseg, length[2], nrecs, nsegs, status=0, iwvl, ni=0, fsn, good_rec; |
108 |
int yr, mo, da, hr, mn, sc; |
109 |
int wvls[10] = { 171, 193, 335, 304, 1700, 211, 131, 94, 1600, 4500 }; |
110 |
char *dsinp, *outpathbase, *date_obs, outfilename[512], *outcparms=NULL, *wu; |
111 |
char c1, c2, c3, *prefix, *suffix, *imtype, ds[3]; |
112 |
time_t t0, tnow; |
113 |
DRMS_Record_t *inprec; |
114 |
DRMS_RecordSet_t *inprs; |
115 |
DRMS_Segment_t *inpseg; |
116 |
struct stat sbuf; |
117 |
TIME t_obs; |
118 |
|
119 |
if (nice_intro(0)) return(0); |
120 |
dsinp = strdup(cmdparams_get_str(&cmdparams, "dsinp", NULL)); |
121 |
outpathbase = strdup(cmdparams_get_str(&cmdparams, "pathout", NULL)); |
122 |
prefix = strdup(cmdparams_get_str(&cmdparams, "prefix", NULL)); |
123 |
segment = cmdparams_get_int(&cmdparams, "segment", NULL); |
124 |
suffix = strdup(cmdparams_get_str(&cmdparams, "suffix", NULL)); |
125 |
if (strcmp(dsinp, NOT_SPECIFIED)==0) DIE("in argument is required"); |
126 |
|
127 |
inprs = drms_open_records(drms_env, dsinp, &status); |
128 |
if (status) DIE("cant open recordset query"); |
129 |
drms_stage_records(inprs, 1, 0); |
130 |
nrecs = inprs->n; |
131 |
printf("%d records\n", nrecs); |
132 |
t0 = time(NULL); |
133 |
for (irec=0; irec<nrecs; irec++) { |
134 |
inprec = inprs->records[irec]; |
135 |
nsegs = hcon_size(&inprec->segments); |
136 |
if (do_1segment) nsegs = 1; |
137 |
for (iseg=segment; iseg<segment + nsegs; iseg++) { |
138 |
char *filename, *inpsegname, outpath[512]; |
139 |
float wavl; |
140 |
inpseg = drms_segment_lookupnum(inprec, iseg); |
141 |
inpsegname = inpseg->info->name; |
142 |
filename = inpseg->filename; |
143 |
if(strlen(filename)) { |
144 |
if (verbose) printf("rec %d, seg %d, protocol %d, '%s', '%s'\n", |
145 |
irec, iseg, inpseg->info->protocol, inpsegname, filename); |
146 |
wu = drms_getkey_string(inprec, "WAVEUNIT", &status); |
147 |
if (status && strcmp(suffix, NOT_SPECIFIED)==0) suffix = ""; |
148 |
if (!strncasecmp(wu, "nm", 2)) { |
149 |
iwvl = (int)(drms_getkey_float(inprec, "WAVELNTH", &status)*10+.5); |
150 |
} else if (!strncasecmp(wu, "angstrom", 8)) { |
151 |
iwvl = drms_getkey_int(inprec, "WAVELNTH", &status); |
152 |
} |
153 |
fsn = drms_getkey_int(inprec, "FSN", &status); |
154 |
t_obs = drms_getkey_time(inprec, "T_OBS", &status); |
155 |
date_obs = drms_getkey_string(inprec, "T_OBS", &status); |
156 |
good_rec = 1; |
157 |
if ((t_obs<0.0) || (fsn == 0x1c001c00)) good_rec = 0; |
158 |
sscanf(date_obs, "%d%c%d%c%d%c%d:%d:%d", |
159 |
&yr, &c1, &mo, &c2, &da, &c3, &hr, &mn, &sc); |
160 |
if (check_outpath(yr, mo, da, hr, outpathbase))DIE("Cant write to out"); |
161 |
if (do_1segment && !iseg) inpsegname = ""; |
162 |
imtype = drms_getkey_string(inprec, "IMG_TYPE", &status); |
163 |
if (strncmp(imtype, "DARK", 4)) strcpy(ds, ""); |
164 |
else strcpy(ds, "d"); |
165 |
sprintf(outpath, "%s/%4.4d/%2.2d/%2.2d/H%2.2d00", |
166 |
outpathbase, yr, mo, da, hr); |
167 |
if (strcmp(suffix, NOT_SPECIFIED)==0) { |
168 |
sprintf(outfilename, |
169 |
"%s/%s%4.4d%2.2d%2.2d_%2.2d%2.2d%2.2d_%4.4d%s%s.fits", |
170 |
outpath, prefix, yr, mo, da, hr, mn, sc, iwvl, inpsegname, ds); |
171 |
} else { |
172 |
sprintf(outfilename, |
173 |
"%s/%s%4.4d%2.2d%2.2d_%2.2d%2.2d%2.2d_%s%s%s.fits", |
174 |
outpath, prefix, yr, mo, da, hr, mn, sc, suffix, inpsegname, ds); |
175 |
} |
176 |
if ((stat(outfilename, &sbuf) || !skip) && good_rec) { |
177 |
if (do_compress) outcparms = "compress"; |
178 |
if (de_compress) { |
179 |
outcparms = ""; |
180 |
} |
181 |
if (DRMS_SUCCESS != fitsexport_export_tofile(inpseg, outcparms, outfilename, NULL, NULL)) { |
182 |
DIE("cant write FITS file"); |
183 |
} |
184 |
if (do_sleep) { |
185 |
ni++; |
186 |
tnow = time(NULL); |
187 |
if ((tnow - t0) < (1.25*ni)) sleep(1); |
188 |
} |
189 |
} |
190 |
} |
191 |
} |
192 |
} |
193 |
return status; |
194 |
} |