forked from gmcgarragh/seviri_util
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathread_write_hrit.c
385 lines (325 loc) · 15.1 KB
/
read_write_hrit.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/*******************************************************************************
*
* Copyright (C) 2015-2018 Simon Proud <[email protected]>
*
* This source code is licensed under the GNU General Public License (GPL),
* Version 3. See the file COPYING for more details.
*
******************************************************************************/
#include "external.h"
#include "hrit_anc_funcs.h"
#include "internal.h"
#include "read_write.h"
#include "read_write_hrit.h"
/*******************************************************************************
* Read the EPI file associated with a SEVIRI timeslot. This file contains
* information on the actual image scan
*
* fname: Name of the EPI file to be read (including full directory)
* d: Main SEVIRI data structure
* aux: Auxiliary data structure
*
* returns: Zero if successful, nonzero if error
******************************************************************************/
static int read_hrit_epilogue(const char *fname, struct seviri_data *d,
struct seviri_auxillary_io_data *aux)
{
FILE *fp;
int out;
const unsigned int probeSize = 4;
long hdrlen = 10;
unsigned char probeBuf[probeSize];
/* Open epilogue */
if ((fp = fopen(fname, "rb")) == NULL) {
fprintf(stderr, "ERROR: Problem opening file for reading: %s ... %s\n",
fname, strerror(errno));
return -1;
}
fseek(fp,0,SEEK_SET);
/*
if (seviri_15HEADER_SatelliteStatus_read(fp, &d->header.SatelliteStatus, aux)) E_L_R();
*/
/* Read first part of prologue and check contents to see if correct filetype. */
out = fread((char *) probeBuf, probeSize,1,fp);
out = out;
if (probeBuf[0] == 0 && probeBuf[1] == 0 && probeBuf[2] == 16) {
if (fxxxx_swap(&hdrlen, 4, 1, fp, aux) < 0) {E_L_R();}
}
fseek(fp,hdrlen+1,SEEK_SET);
if (fxxxx_swap(&d->trailer.ImageProductionStats.SatelliteID, sizeof(short), 1, fp, aux) < 0) {E_L_R();}
if (fxxxx_swap(&d->trailer.ImageProductionStats.NominalImageScanning, sizeof(uchar), 1, fp, aux) < 0) {E_L_R();}
if (fxxxx_swap(&d->trailer.ImageProductionStats.ReducedScan, sizeof(uchar), 1, fp, aux) < 0) {E_L_R();}
if (seviri_TIME_CDS_SHORT_read(fp, &d->trailer.ImageProductionStats.ActScanForwardStart, aux)) {E_L_R();}
if (seviri_TIME_CDS_SHORT_read(fp, &d->trailer.ImageProductionStats.ActScanForwardEnd, aux)) {E_L_R();}
fclose(fp);
return 0;
}
/*******************************************************************************
* Read the PRO file associated with a SEVIRI timeslot. This file contains
* information on the planned image scan and calibration
*
* fname: Name of the PRO file to be read (including full directory)
* d: Main SEVIRI data structure
* aux: Auxiliary data structure
*
* returns: Zero if successful, nonzero if error
******************************************************************************/
static int read_hrit_prologue(const char *fname, struct seviri_data *d,
struct seviri_auxillary_io_data *aux)
{
FILE *fp;
int out;
const unsigned int probeSize = 4;
long hdrlen = 10;
unsigned char probeBuf[probeSize];
/* Open prologue*/
if ((fp = fopen(fname, "rb")) == NULL) {
fprintf(stderr, "ERROR: Problem opening file for reading: %s ... %s\n",
fname, strerror(errno));
return -1;
}
fseek(fp,0,SEEK_SET);
/* Read first part of prologue and check contents to see if correct filetype*/
out = fread((char *)probeBuf, probeSize,1,fp);
out = out;
if (probeBuf[0] == 0 && probeBuf[1] == 0 && probeBuf[2] == 16)
if (fxxxx_swap(&hdrlen, 4, 1, fp, aux) < 0) {E_L_R();}
fseek(fp, hdrlen, SEEK_SET);
seviri_15HEADER_SatelliteStatus_read(fp,&d->header.SatelliteStatus, aux);
/* Seek to the beginning of the useful part of the prologue (only care
about image desc and radiometric calibration. This seek value should be
constant, has not changed in 11 years of MSG data. */
fseek(fp, 386982, SEEK_SET);
/* Read the image description data */
if (seviri_15HEADER_ImageDescription_read (fp, &d->header.ImageDescription, aux)) {E_L_R();}
/* Read the per-channel calibration data */
if (seviri_15HEADER_RadiometricProcessing_read(fp, &d->header.RadiometricProcessing, aux)) {E_L_R();}
/* Read geometric processing data */
if (seviri_15HEADER_GeometricProcessing_read(fp, &d->header.GeometricProcessing, aux)) {E_L_R();}
fclose(fp);
return 0;
}
/*******************************************************************************
* Allocate the space in memory to be used for the SEVIRI image. Space is
* allocated for the HRV data is required, although it is unsupported.
*
* nbands: Number of bands to be read (NOT number of bands in file)
* band_ids: Array of band ids (channel numbers)
* d: Main SEVIRI data structure
*
* returns: Zero if successful, nonzero if error
******************************************************************************/
static int alloc_imagearr(uint nbands, const uint *band_ids, struct seviri_data *d)
{
int i,proc_hrv = 0,virb;
long length_vir,length_hrv;
virb = nbands;
for (i = 0; i < nbands; ++i) {
d->image.band_ids[i]=band_ids[i];
if (band_ids[i]==12) {
virb -= 1;
proc_hrv = 1;
}
}
d->image.n_bands = nbands;
d->image.fill_value = FILL_VALUE_US;
length_vir = d->header.ImageDescription.ReferenceGridVIS_IR.NumberOfLines *
d->header.ImageDescription.ReferenceGridVIS_IR.NumberOfColumns;
length_hrv = d->header.ImageDescription.ReferenceGridHRV.NumberOfLines *
d->header.ImageDescription.ReferenceGridHRV.NumberOfColumns;
if (virb<=0) return -1;
d->image.data_vir = malloc(virb * sizeof(ushort *));
for (i = 0; i < virb; ++i) {
d->image.data_vir[i] = malloc(length_vir * sizeof(float));
su_init_array_us(d->image.data_vir[i], length_vir, d->image.fill_value);
}
if (proc_hrv==1) d->image.data_hrv = malloc(length_hrv * sizeof(float));
return 0;
}
/*******************************************************************************
* Convenience function that returns the number of lines and columns in the
* image to be read with the given choice of offset and dimension parameters.
*
* indir: Directory containing the HRIT data
* timeslot: Timeslot to be read. Format: YYYYMMDDHHMM
* sat: Satellite number - can be 1, 2, 3 or 4
* i_line Output line offset to the beginning of the actual image
* within the full disk
* i_column Output column offset to the beginning of the actual image
* within the full disk
* n_lines Output number of lines of the actual image
* n_columns Output number of columns of the actual image
* bounds: Boundaries of the data to be read
* line0: First line to read
* line1: Last line to read
* column0: First column to read
* column1: Last column to read
* lat0: Initial latitude
* lat1: Final latitude
* lon0: Initial longitude
* lon1: Final longitude
*
* returns Non-zero on error
******************************************************************************/
int seviri_get_dimens_hrit(const char *indir, const char *timeslot, int sat,
uint *i_line, uint *i_column, uint *n_lines, uint *n_columns,
enum seviri_bounds bounds, uint line0, uint line1, uint column0,
uint column1, double lat0, double lat1, double lon0, double lon1, int rss)
{
struct seviri_dimension_data dimens;
struct seviri_marf_header_data marf_header;
/* Put image info in the correct place. This is done for consistency with
the .nat reader. HRIT files contain different data, so need to move
things around. */
sprintf(marf_header.secondary.NumberLinesVISIR.Value, "%i", IMAGE_SIZE_VIR_LINES);
sprintf(marf_header.secondary.NumberColumnsVISIR.Value, "%i", IMAGE_SIZE_VIR_COLUMNS);
sprintf(marf_header.secondary.NumberLinesHRV.Value, "%i", IMAGE_SIZE_HRV_LINES);
sprintf(marf_header.secondary.NumberColumnsHRV.Value, "%i", IMAGE_SIZE_HRV_COLUMNS);
sprintf(marf_header.secondary.SouthLineSelectedRectangle.Value, "%i",1);
sprintf(marf_header.secondary.NorthLineSelectedRectangle.Value, "%i",IMAGE_SIZE_VIR_LINES);
sprintf(marf_header.secondary.EastColumnSelectedRectangle.Value, "%i",1);
sprintf(marf_header.secondary.WestColumnSelectedRectangle.Value, "%i",IMAGE_SIZE_VIR_COLUMNS);
/* Allocate and fill in the seviri_dimension_data struct. */
if (seviri_get_dimension_data(&dimens, &marf_header, bounds, line0, line1,
column0, column1, lat0, lat1, lon0, lon1, rss)) {
fprintf(stderr, "ERROR: seviri_get_dimension_data()\n");
return -1;
}
*i_line = dimens.i_line_requested_VIR;
*i_column = dimens.i_column_requested_VIR;
*n_lines = dimens.n_lines_requested_VIR;
*n_columns = dimens.n_columns_requested_VIR;
return 0;
}
/*******************************************************************************
* Main function for reading the HRIT data
* Assembles the filenames, reads ancillary data, reads bands, tidies up.
*
* indir: Directory containing the HRIT data
* timeslot: Timeslot to be read. Format: YYYYMMDDHHMM
* sat: Satellite number - can be 1, 2, 3 or 4
* d: Main SEVIRI data structure
* nbands: Number of bands to be read (NOT number of bands in file)
* band_ids: Array of band ids (channel numbers)
* bounds: Boundaries of the data to be read
* line0: First line to read
* line1: Last line to read
* column0: First column to read
* column1: Last column to read
* lat0: Initial latitude
* lat1: Final latitude
* lon0: Initial longitude
* lon1: Final longitude
* rss: Flag to set rss processing (1=yes, 0=no)
* iodc: Flag to set IODC processing (1=yes, 0=no)
*
* returns: Zero if successful, nonzero if error
******************************************************************************/
int seviri_read_hrit(const char *indir, const char *timeslot, int sat,
struct seviri_data *d, uint n_bands, const uint *band_ids,
enum seviri_bounds bounds, uint line0, uint line1, uint column0,
uint column1, double lat0, double lat1, double lon0, double lon1, int rss,
int iodc)
{
long int out,i,j;
char *proname;
char *epiname;
char ***bnames;
struct seviri_auxillary_io_data aux;
struct seviri_dimension_data *dimens;
/* Get the names of the prologue, epilogue and data files. */
/* NOTE: Only supports full disk scanning. RSS unavailable! */
out = assemble_proname(&proname, indir, timeslot, sat, rss, iodc);
if (out != 0) {
fprintf(stderr, "ERROR: assemble_proname()\n");
return -1;
}
out = assemble_epiname(&epiname,indir,timeslot, sat, rss, iodc);
if (out != 0) {
fprintf(stderr, "ERROR: assemble_epiname()\n");
return -1;
}
out = assemble_fnames(&bnames, indir, timeslot, n_bands, band_ids,sat, rss,
iodc);
if (out != 0) {
fprintf(stderr, "ERROR: assemble_fnames()\n");
return -1;
}
/* Set up the aux data struct and check endianness */
seviri_auxillary_alloc(&aux);
aux.operation = 0;
aux.swap_bytes = su_is_little_endian();
/* Read the epilogue file */
if (read_hrit_epilogue(epiname,d,&aux)) {
fprintf(stderr, "ERROR: read_hrit_epilogue()\n");
return -1;
}
/* Read the prologue file */
if (read_hrit_prologue(proname,d,&aux)) {
fprintf(stderr, "ERROR: read_hrit_epilogue()\n");
return -1;
}
/* Put image info in the correct place. This is done for consistency with
the .nat reader. HRIT files contain different data, so need to move
things around. */
if (rss!=1) {
sprintf(d->marf_header.secondary.NumberLinesVISIR.Value, "%i", IMAGE_SIZE_VIR_LINES);
sprintf(d->marf_header.secondary.NumberColumnsVISIR.Value, "%i", IMAGE_SIZE_VIR_COLUMNS);
sprintf(d->marf_header.secondary.NumberLinesHRV.Value, "%i", IMAGE_SIZE_HRV_LINES);
sprintf(d->marf_header.secondary.NumberColumnsHRV.Value, "%i", IMAGE_SIZE_HRV_COLUMNS);
sprintf(d->marf_header.secondary.SouthLineSelectedRectangle.Value, "%i",1);
sprintf(d->marf_header.secondary.NorthLineSelectedRectangle.Value, "%i",IMAGE_SIZE_VIR_LINES);
sprintf(d->marf_header.secondary.EastColumnSelectedRectangle.Value, "%i",1);
sprintf(d->marf_header.secondary.WestColumnSelectedRectangle.Value, "%i",IMAGE_SIZE_VIR_COLUMNS);
}
else {
sprintf(d->marf_header.secondary.NumberLinesVISIR.Value, "%i", IMAGE_SIZE_VIR_RSS_LINES);
sprintf(d->marf_header.secondary.NumberColumnsVISIR.Value, "%i", IMAGE_SIZE_VIR_RSS_COLUMNS);
sprintf(d->marf_header.secondary.NumberLinesHRV.Value, "%i", IMAGE_SIZE_HRV_LINES);
sprintf(d->marf_header.secondary.NumberColumnsHRV.Value, "%i", IMAGE_SIZE_HRV_COLUMNS);
sprintf(d->marf_header.secondary.SouthLineSelectedRectangle.Value, "%i",1);
sprintf(d->marf_header.secondary.NorthLineSelectedRectangle.Value, "%i",IMAGE_SIZE_VIR_RSS_LINES);
sprintf(d->marf_header.secondary.EastColumnSelectedRectangle.Value, "%i",1);
sprintf(d->marf_header.secondary.WestColumnSelectedRectangle.Value, "%i",IMAGE_SIZE_VIR_RSS_COLUMNS);
}
/* Allocate and fill in the seviri_dimension_data struct. */
dimens = (struct seviri_dimension_data *) &d->image.dimens;
if (seviri_get_dimension_data(dimens, &d->marf_header, bounds, line0, line1,
column0, column1, lat0, lat1, lon0, lon1, rss)) {
fprintf(stderr, "ERROR: seviri_get_dimension_data()\n");
return -1;
}
/* Put image info in the correct place. This is done for consistency with
the .nat reader. HRIT files contain different data, so need to move
things around. */
d->image.i_line = dimens->i_line_requested_VIR;
d->image.i_column = dimens->i_column_requested_VIR;
d->image.n_lines = dimens->n_lines_requested_VIR;
d->image.n_columns = dimens->n_columns_requested_VIR;
d->image.packet_header = NULL;
d->image.LineSideInfo = NULL;
out = alloc_imagearr(n_bands, band_ids,d);
/* Loop over each band and each segment. Note: VIR only, no HRV */
for (i = 0; i < n_bands; i++) {
for (j = 0; j < 8; j++) {
if (rss == 1 && j < 5)
continue;
if (read_data_oneseg(bnames[i][j], j, i+1, d, rss)) {
fprintf(stderr, "ERROR: read_data_oneseg()\n");
return -1;
}
}
}
/* Tidy up */
seviri_auxillary_free(&aux);
free(proname);
free(epiname);
for (i = 0; i < n_bands; i++) {
for (j = 0; j < 8 ; j++) {
free(bnames[i][j]);
}
free(bnames[i]);
}
free(bnames);
return 0;
}