46 #define VERSION "0.76"
47 #define BOUNDS_ERROR 110
49 int main(
int argc,
char *argv[]) {
54 int32 spixl, epixl, npixl;
57 int32 nscan_day = 0, nscan_night = 0;
58 int32 spixl_old = 0, sscan_old = 0, nscan_old = 0;
66 int32 dims_r[H4_MAX_VAR_DIMS];
67 int32 start_r[H4_MAX_VAR_DIMS];
77 int32 dims_w[H4_MAX_VAR_DIMS];
78 int32 start_w[H4_MAX_VAR_DIMS];
84 static char attr_buf[2048];
87 static char sds_name[H4_MAX_NC_NAME];
96 static char vdata_name[VSNAMELENMAX];
100 char fieldlist[VSFIELDMAX * FIELDNAMELENMAX];
101 int32 vdata_size, n_records, interlace_mode;
108 char name[H4_MAX_NC_NAME];
115 sds_str sds_list[] = {
116 {
"Scan number", 0, 1, -1, -1},
117 {
"Frame count array", 0, 1, -1, -1},
118 {
"Scan Type", 0, 1, -1, -1},
119 {
"SD start time", 0, 1, -1, -1},
120 {
"SRCA start time", 0, 1, -1, -1},
121 {
"BB start time", 0, 1, -1, -1},
122 {
"SV start time", 0, 1, -1, -1},
123 {
"EV start time", 0, 1, -1, -1},
124 {
"SRCA calibration mode", 0, 1, -1, -1},
125 {
"Packet scan count", 0, 1, -1, -1},
126 {
"CCSDS Application Identifiers", 0, 1, -1, -1},
127 {
"Packet expedited data flag", 0, 1, -1, -1},
128 {
"Mirror side", 0, 1, -1, -1},
129 {
"Scan quality array", 0, 1, -1, -1},
130 {
"SD sector Pixel quality", 0, 1, -1, -1},
131 {
"SRCA sector Pixel quality", 0, 1, -1, -1},
132 {
"BB sector Pixel quality", 0, 1, -1, -1},
133 {
"SV sector Pixel quality", 0, 1, -1, -1},
134 {
"Earth sector Pixel quality", 0, 1, 1, 1},
135 {
"SD_250m", 0, 40, -1, -1},
136 {
"SD_500m", 0, 20, -1, -1},
137 {
"SD_1km_day", 0, 10, -1, -1},
138 {
"SD_1km_night", 0, 10, -1, -1},
139 {
"SRCA_250m", 0, 40, -1, -1},
140 {
"SRCA_500m", 0, 20, -1, -1},
141 {
"SRCA_1km_day", 0, 10, -1, -1},
142 {
"SRCA_1km_night", 0, 10, -1, -1},
143 {
"BB_250m", 0, 40, -1, -1},
144 {
"BB_500m", 0, 20, -1, -1},
145 {
"BB_1km_day", 0, 10, -1, -1},
146 {
"BB_1km_night", 0, 10, -1, -1},
147 {
"SV_250m", 0, 40, -1, -1},
148 {
"SV_500m", 0, 20, -1, -1},
149 {
"SV_1km_day", 0, 10, -1, -1},
150 {
"SV_1km_night", 0, 10, -1, -1},
151 {
"EV_250m", 0, 40, 2, 4},
152 {
"EV_500m", 0, 20, 2, 2},
153 {
"EV_1km_day", 0, 10, 2, 1},
154 {
"EV_1km_night", 0, 10, 2, 1},
155 {
"fpa_aem_config", 0, 1, -1, -1},
156 {
"science_state", 0, 1, -1, -1},
157 {
"science_abnormal", 0, 1, -1, -1},
158 {
"fpa_dcr_offset", 0, 1, -1, -1},
159 {
"raw_mir_enc", 0, 1, -1, -1},
160 {
"raw_vs_def", 0, 1, -1, -1},
161 {
"raw_vs_act", 0, 1, -1, -1},
162 {
"raw_sci_eng", 0, 1, -1, -1},
163 {
"raw_hk_telem", 0, 1, -1, -1},
164 {
"raw_sc_ancil", 0, 1, -1, -1},
165 {
"raw_param", 0, 1, -1, -1},
166 {
"raw_pv_gains", 0, 1, -1, -1},
168 n_datasets_w =
sizeof (sds_list) /
sizeof (sds_str);
173 printf(
"This is version %s of %s (compiled on %s %s)\n",
174 VERSION, argv[0], __DATE__, __TIME__);
178 printf(
"\nUsage: %s ", argv[0]);
179 printf(
"infile spixl epixl sline eline outfile"
181 "\n\tinfile - input MODIS L1A datafile"
182 "\n\tspixl - start pixel number (1-based)"
183 "\n\tepixl - end pixel number (1-based)"
184 "\n\tsline - start line (1-based)"
185 "\n\teline - end line (1-based)"
186 "\n\toutfile - output file name");
187 printf(
"\n\nNote: Enter line number NOT scan number!\n");
193 spixl = atoi(argv[2]);
194 epixl = atoi(argv[3]);
195 sscan = atoi(argv[4]);
196 escan = atoi(argv[5]);
199 printf(
"Input file: %s\n",
infile);
200 printf(
"Output file: %s\n", outfile);
203 HDFfid_r = Hopen(
infile, DFACC_READ, 0);
204 sd_id_r = SDstart(
infile, DFACC_RDONLY);
207 sscan = (sscan / 10) + (1 * ((sscan % 10) != 0));
208 escan = (escan / 10) + (1 * ((escan % 10) != 0));
210 if ((spixl > epixl) || (sscan > escan)) {
211 printf(
"\nInvalid range requested:\n");
212 npixl = epixl - spixl + 1;
213 nscan = escan - sscan + 1;
214 printf(
"spixl: %4d epixl: %4d npixl: %4d\n", spixl, epixl, npixl);
215 printf(
"sscan: %4d escan: %4d nscan: %4d\n", sscan, escan,
nscan);
220 if (SDfindattr(sd_id_r,
"Extract Pixel Offset") !=
FAIL)
221 SDreadattr(sd_id_r, SDfindattr(sd_id_r,
"Extract Pixel Offset"), &spixl_old);
222 if (SDfindattr(sd_id_r,
"Extract Line Offset") !=
FAIL)
223 SDreadattr(sd_id_r, SDfindattr(sd_id_r,
"Extract Line Offset"), &sscan_old);
226 sds_id_r = SDselect(sd_id_r, SDnametoindex(sd_id_r,
"EV_1km_day"));
227 status = SDgetinfo(sds_id_r, sds_name, &ndims, dims_r, &
dtype, &nattrs);
228 status = SDreadattr(sd_id_r, SDfindattr(sd_id_r,
"Number of Scans"), &nscan_old);
233 printf(
"Only %d scans in granule: not enough engineering data to continue processing.", nscan_old);
236 if ((sscan_old + escan) < 7) {
237 printf(
"Adjusting end scan to ensure inclusion of required engineering data.\n");
238 escan = 7 - sscan_old;
242 if (spixl > dims_r[2]) {
243 printf(
"spixl: %d greater than # of pixels per scan: %d\n", spixl, dims_r[2]);
246 if (epixl > dims_r[2]) {
247 printf(
"epixl: %d greater than # of pixels per scan: %d; adjusting.\n",
252 if (sscan > nscan_old) {
253 printf(
"sscan: %d greater than # of scan lines: %d\n", sscan, nscan_old);
256 if (escan > nscan_old) {
257 printf(
"escan: %d greater than # of scan lines: %d; adjusting.\n",
262 npixl = epixl - spixl + 1;
263 nscan = escan - sscan + 1;
264 printf(
"spixl: %4d epixl: %4d npixl: %4d\n", spixl, epixl, npixl);
265 printf(
"sscan: %4d escan: %4d nscan: %4d\n", sscan, escan,
nscan);
266 printf(
"Actual line start (0-based): %d\n", (sscan - 1) * 10);
269 HDFfid_w = Hopen(outfile, DFACC_CREATE, 0);
270 sd_id_w = SDstart(outfile, DFACC_RDWR);
275 printf(
"\nWriting Science Data Sets...\n");
278 SDfileinfo(sd_id_r, &n_datasets_r, &nglobal_attr);
279 for (sds_index = 0; sds_index < n_datasets_r; sds_index++) {
282 sds_id_r = SDselect(sd_id_r, sds_index);
283 status = SDgetinfo(sds_id_r, sds_name, &ndims, dims_r, &
dtype, &nattrs);
284 for (
j = 0;
j < n_datasets_w;
j++) {
285 if (strcmp(sds_list[
j].
name, sds_name) == 0)
break;
287 if (
j == n_datasets_w) {
288 printf(
"Unexpected input SDS: \"%s\"\n", sds_name);
294 for (
i = 0;
i < ndims;
i++) {
296 dims_w[
i] = dims_r[
i];
297 if (
i == sds_list[
j].scandim) {
298 start_r[
i] = sds_list[
j].scanfactor * (sscan - 1);
299 dims_w[
i] = sds_list[
j].scanfactor *
nscan;
301 if (
i == sds_list[
j].pixldim) {
302 start_r[
i] = sds_list[
j].pixlfactor * (spixl - 1);
303 dims_w[
i] = sds_list[
j].pixlfactor * npixl;
309 printf(
"%s\n rank %d, type %d, dims", sds_name, ndims,
dtype);
310 for (
i = 0;
i < ndims;
i++) printf(
"[%d]", dims_r[
i]);
311 printf(
"; extracting ");
312 for (
i = 0;
i < ndims;
i++) printf(
"[%d-%d]", start_r[
i], start_r[
i] + dims_w[
i] - 1);
316 sds_id_w = SDcreate(sd_id_w, sds_name,
dtype, ndims, dims_w);
317 if (sds_id_w == -1) {
318 printf(
"Field: %s cannot be created\n", sds_name);
323 for (
i = 0;
i < nattrs;
i++) {
324 status = SDreadattr(sds_id_r,
i, attr_buf);
332 status = SDcheckempty(sds_id_r, &emptySDS);
336 printf(
"write status: %d\n\n",
status);
341 if (strcmp(sds_name,
"Scan Type") == 0) {
343 scan_type = (
char *) (
data + 10 *
iscan);
345 if (strcmp(scan_type,
"Day") == 0) nscan_day++;
346 if (strcmp(scan_type,
"Night") == 0) nscan_night++;
351 SDendaccess(sds_id_w);
352 SDendaccess(sds_id_r);
358 printf(
"\nWriting VData...\n");
359 status = Vstart(HDFfid_r);
360 status = Vstart(HDFfid_w);
363 n_vdata = VSlone(HDFfid_r,
NULL, 0);
364 ref_array = calloc(n_vdata,
sizeof (int32));
365 n_vdata = VSlone(HDFfid_r, ref_array, n_vdata);
366 for (vd_index = 0; vd_index < n_vdata; vd_index++) {
369 vd_id_r = VSattach(HDFfid_r, ref_array[vd_index],
"r");
370 status = VSinquire(vd_id_r, &n_records, &interlace_mode,
371 fieldlist, &vdata_size, vdata_name);
374 vd_id_w = VSattach(HDFfid_w, -1,
"w");
375 VSsetname(vd_id_w, vdata_name);
376 VSsetinterlace(vd_id_w, interlace_mode);
379 n_fields = VFnfields(vd_id_r);
380 printf(
"%s\n n_records=%d, n_fields=%d\n", vdata_name, n_records, n_fields);
381 for (
i = 0;
i < n_fields;
i++) {
383 VFfieldname(vd_id_r,
i),
384 VFfieldtype(vd_id_r,
i),
385 VFfieldorder(vd_id_r,
i));
387 status = VSsetfields(vd_id_w, fieldlist);
391 status = VSsetfields(vd_id_r, fieldlist);
392 if ((n_records == nscan_old) &&
393 strcmp(vdata_name,
"Telemetry Major Cycle 0 of 63") != 0) {
394 VSseek(vd_id_r, sscan - 1);
399 data = calloc(n_records * VSsizeof(vd_id_r, fieldlist), 1);
400 nrec_r = VSread(vd_id_r, (
unsigned char *)
data, n_records, interlace_mode);
401 nrec_w = VSwrite(vd_id_w, (
unsigned char *)
data, n_records, interlace_mode);
402 if (nrec_r != nrec_w) {
403 printf(
"Write Error: %d %d %d\n",
i, nrec_r, nrec_w);
419 printf(
"\nWriting Global Attributes...\n");
420 for (
i = 0;
i < nglobal_attr;
i++) {
425 if (strcmp(attr_buf,
"Number of Scans") == 0) memcpy(
data, &
nscan, 4);
426 if (strcmp(attr_buf,
"Number of Day mode scans") == 0) memcpy(
data, &nscan_day, 4);
427 if (strcmp(attr_buf,
"Number of Night mode scans") == 0) memcpy(
data, &nscan_night, 4);
434 spixl = spixl_old + spixl - 1;
439 sscan = sscan_old + sscan - 1;