forked from gmcgarragh/seviri_util
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hrit_anc_funcs.c
357 lines (308 loc) · 12 KB
/
hrit_anc_funcs.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
/*******************************************************************************
*
* 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"
/*******************************************************************************
* Check if input band is HRV. If yes, 24 segments for HRIT image. Otherwise 8.
*
* band: Band number (1 -> 12)
*
* returns: Number of segments for the given band
******************************************************************************/
int is_hrv(int band)
{
int segs=8;
if (band==12) segs=24;
return segs;
}
/*******************************************************************************
* Helper function to build file names based upon the band.
*
* cnum: Channel number (1 -> 12). Note: 0 and 13 can be used for
* EPI+PRO.
*
* returns: Character array (len = 6) containing channel name string
******************************************************************************/
const char *chan_name(int cnum)
{
switch (cnum) {
case 0: return "______"; break;
case 1: return "VIS006"; break;
case 2: return "VIS008"; break;
case 3: return "IR_016"; break;
case 4: return "IR_039"; break;
case 5: return "WV_062"; break;
case 6: return "WV_073"; break;
case 7: return "IR_087"; break;
case 8: return "IR_097"; break;
case 9: return "IR_108"; break;
case 10: return "IR_120"; break;
case 11: return "IR_134"; break;
case 12: return "HRV___"; break;
case 13: return "______"; break;
default: return "______"; break;
}
}
/*******************************************************************************
*
******************************************************************************/
char *extract_path_sat_id_timeslot(const char *filename, int *sat_id,
char *timeslot, int *rss, int *iodc)
{
int startfnam;
int gopos;
char *ptr;
char *indir;
ptr=strstr(filename,"H-000-MSG");
if (ptr==NULL) {
printf("ERROR: Incorrectly formatted HRIT! Quitting\n");
return NULL;
}
startfnam=ptr-filename;
indir=(char *)malloc(sizeof(char)*(startfnam+1));
strncpy(indir,filename,startfnam);
indir[startfnam] = '\0';
gopos=startfnam+46;
strncpy(timeslot,filename+gopos,12);
timeslot[12]='\0';
*sat_id=atoi(&filename[startfnam+9]);
ptr=strstr(filename,"RSS");
if (ptr!=NULL)
*rss=1;
else
*rss=0;
ptr=strstr(filename,"IODC");
if (ptr!=NULL)
*iodc=1;
else
*iodc=0;
return indir;
}
/*******************************************************************************
* Builds the filenames for a given channel
*
* NOTE: Only supports full disk scanning. RSS and SRSS not supported.
*
* fnam: Output char array for the filenames. Format:
* Character array ([nbands][nsegs][fnames])
* indir: Directory that holds the HRIT file segments
* timeslot: Timeslot to be read. Format: YYYYMMDDHHMM
* nbands: Number of bands to be read (NOT number of bands in file)
* bids: Array of band ids (channel numbers)
* sat: Satellite number, can be 1, 2, 3 or 4
*
* returns: Zero if successful
******************************************************************************/
int assemble_fnames(char ****fnam, const char *indir, const char *timeslot,
int nbands, const uint *bids, int sat, int rss, int iodc)
{
int c,totsegs=0, nsegs[nbands];
char ***arr = malloc(sizeof(char **) * nbands);
for (c=0;c<nbands;c++) {
if (bids[c]==12)
nsegs[c]=24;
else nsegs[c]=8;
totsegs+=nsegs[c];
}
for (c=0;c<nbands;c++) {
int i;
const char *band;
uint cnum=bids[c];
arr[c]=malloc(sizeof(char *) * nsegs[c]);
band=chan_name(cnum);
if (cnum>0 && cnum<=12) {
/* Build the actual filenames */
for (i = 0; i < nsegs[c]; ++i) {
/* HRIT filename is 61 so use that plus indir len */
arr[c][i] = malloc(61+strlen(indir)+1);
if (rss==1)
sprintf(arr[c][i],
"%sH-000-MSG%d__-MSG%d_RSS____-%s___-%.6d___-%s-__",
indir, sat, sat, band, i+1, timeslot);
else if (iodc==1)
sprintf(arr[c][i],
"%sH-000-MSG%d__-MSG%d_IODC___-%s___-%.6d___-%s-__",
indir, sat, sat, band, i+1, timeslot);
else
sprintf(arr[c][i],
"%sH-000-MSG%d__-MSG%d________-%s___-%.6d___-%s-__",
indir, sat, sat, band, i+1, timeslot);
}
}
}
*fnam=arr;
return 0;
}
/*******************************************************************************
* Builds the epilogue filename for a timeslot
*
* NOTE: Only supports full disk scanning. RSS and SRSS are not supported.
*
* enam: Output char array for the epilogue filename.
* indir: Directory that holds the HRIT file segments
* timeslot: Timeslot to be read. Format: YYYYMMDDHHMM
* sat: Satellite number, can be 1, 2, 3 or 4
*
* returns: Zero if successful
******************************************************************************/
int assemble_epiname(char **enam, const char *indir, const char *timeslot,
int sat, int rss, int iodc)
{
/* HRIT filename is 61 so use that plus indir len. */
char *arr = malloc(61+strlen(indir)+1);
const char *band;
band=chan_name(0);
if (rss==1)
sprintf(arr, "%sH-000-MSG%d__-MSG%d_RSS____-%s___-EPI______-%s-__",
indir, sat, sat, band, timeslot);
else if (iodc==1)
sprintf(arr, "%sH-000-MSG%d__-MSG%d_IODC___-%s___-EPI______-%s-__",
indir, sat, sat, band, timeslot);
else
sprintf(arr, "%sH-000-MSG%d__-MSG%d________-%s___-EPI______-%s-__",
indir, sat, sat, band, timeslot);
*enam=arr;
return 0;
}
/*******************************************************************************
* Builds the prologue filename for a timeslot
*
* NOTE: Only supports full disk scanning. RSS and SRSS are not supported.
*
* pnam: Output char array for the epilogue filename.
* indir: Directory that holds the HRIT file segments
* timeslot: Timeslot to be read. Format: YYYYMMDDHHMM
* sat: Satellite number, can be 1, 2, 3 or 4
*
* returns: Zero if successful
******************************************************************************/
int assemble_proname(char **pnam, const char *indir, const char *timeslot,
int sat, int rss, int iodc)
{
/* HRIT filename is 61 so use that plus indir len. */
char *arr = malloc(61+strlen(indir)+1);
const char *band;
band=chan_name(0);
if (rss==1)
sprintf(arr, "%sH-000-MSG%d__-MSG%d_RSS____-%s___-PRO______-%s-__",
indir, sat, sat, band, timeslot);
else if (iodc==1)
sprintf(arr, "%sH-000-MSG%d__-MSG%d_IODC___-%s___-PRO______-%s-__",
indir, sat, sat, band, timeslot);
else
sprintf(arr, "%sH-000-MSG%d__-MSG%d________-%s___-PRO______-%s-__",
indir, sat, sat, band, timeslot);
*pnam=arr;
return 0;
}
/*******************************************************************************
* Reads one HRIT segment into the image memory space
*
* NOTE: Only supports full disk scanning. RSS and SRSS are not supported.
* NOTE: Does support HRV reading, but this is untested.
*
* fname: The name of the file to be read
* segnum: The segment number to be read (1->8 / 1->24 for VIR / HRV)
* cnum: The channel number (1 -> 11 for VIR, 12 for HRV)
* d: Main SEVIRI data structure
* dims: Container for the image boundaries.
*
* returns: Zero if successful
******************************************************************************/
int read_data_oneseg(char *fname, int segnum, int cnum, struct seviri_data *d,
int rss)
{
if (rss==1 && segnum<5) return 0;
/* Set up the various data that is required*/
uchar *data10;
const uchar shifts[] = {6, 4, 2, 0};
ushort temp;
const ushort masks[] = {0xFFC0, 0x3FF0, 0x0FFC, 0x03FF};
int x,out,j,jj,k,offset;
/* Required to align with NAT format reader. 4 must be subtracted form the
column as we read 4 pixels simultaneously*/
int first_line = d->image.dimens.i_line_requested_VIR;
int first_col = d->image.dimens.i_column_requested_VIR;
int last_line = first_line + d->image.dimens.n_lines_requested_VIR-1;
int last_col = first_col + d->image.dimens.n_columns_requested_VIR-1;
long int out_d_col,out_d_line;
FILE *fp;
if ((fp = fopen(fname, "rb")) == NULL) {
fprintf(stderr, "ERROR: Problem opening file for reading: %s ... %s\n",
fname, strerror(errno));
return -1;
}
/* Skip header section, don't bother to read */
fseek(fp,6198,SEEK_SET);
if (cnum>0 && cnum<12) {
int ncols = d->image.dimens.n_columns_selected_VIR;
/* Each segment if 464 lines, so skip to correct part of image based
on segnum. */
if (rss==1)
offset=(segnum-5)*464;
else
offset=segnum*464;
ushort tmpline[ncols];
data10 = malloc(ncols / 4 * 5 * sizeof(uchar));
/* Loop over all lines in segment */
for (x=offset;x<offset+464;x++) {
out_d_line=(x-first_line)*(last_col-first_col+1);
/* If we're outside the requested image boundary: move file
pointed and skip. */
if (x<first_line || x>last_line) {
fseek(fp,ncols/4*5,SEEK_CUR);
continue;
}
/* Otherwise read the data and store in the image memory space. */
out=fread(data10, sizeof(char), ncols / 4 * 5, fp);
out=out;
for (j = 0, jj = 0; j < ncols; ) {
for (k = 0; k < 4; ++k) {
temp = *((ushort *) (data10 + jj));
SWAP_2(temp, temp);
tmpline[j] = (temp & masks[k]) >> shifts[k];
if (j>=first_col && j<=last_col) {
out_d_col=j-first_col;
d->image.data_vir[cnum-1][out_d_line+out_d_col] =
tmpline[j];
}
j++, jj++;
}
jj++;
}
}
free(data10);
}
/* Same as above, but for HRV */
if (cnum==12) {
/* Fudge the HRV line numbers */
int nlines=11136;
int ncols=11136;
int offset=nlines-(segnum*464)-464;
for (x=offset+464;x>offset;x--) {
data10 = malloc(ncols / 4 * 5 * sizeof(uchar));
out=fread(data10, sizeof(char), ncols / 4 * 5, fp);
for (j = ncols, jj = 0; j > 0;) {
for (k = 0; k < 4; ++k) {
temp = *((ushort *) (data10 + jj));
SWAP_2(temp, temp);
d->image.data_hrv[(x*ncols*2)+j] =
(temp & masks[k]) >> shifts[k];
j--, jj++;
}
jj++;
}
}
free(data10);
}
fclose(fp);
if (cnum<=0 || cnum>12) return -1;
return 0;
}