OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
getnavdata.c
Go to the documentation of this file.
1 /* -------------------------------------------------------------------- */
2 /* getnavdata() - loads telemetry structure with raw navigation inputs */
3 /* -------------------------------------------------------------------- */
4 
5 #include <stdio.h>
6 #include "swl0_proto.h"
7 
8 INT32 getnavdata(swl0scene *scene, input_sType navinp[]) {
9  BYTE mnf[L0LEN];
10  INT16 ttag[4];
11  INT16 scid[2];
12  INT16 mnftype;
13  INT16 mnfnum;
14  FILE *fp;
15  INT32 irec;
16  INT32 i;
17  int j;
18  int ninst; /* number of instrument telemetry block */
19  int oinst; /* byte offset to first inst tlm block */
20  int sinst = 88; /* byte length of one inst tlm block */
21  timeTab *trec; /* temporal anomaly table entry */
22  int endian = 0;
23 
24  if (endianess() == 1)
25  endian = 1;
26 
27  if (scene->mnftype == GACTYPE) {
28  ninst = 5;
29  oinst = O_INSTGAC;
30  } else {
31  ninst = 1;
32  oinst = O_INSTLAC;
33  }
34 
35  memset(navinp, 0, sizeof (input_sType) * scene->nrec);
36 
37  /* */
38  /* Open file for reading */
39  /* */
40  if ((fp = fopen(scene->l0file, "r")) == NULL) {
41  fprintf(stderr,
42  "-E- %s line %d: unable to open %s for reading\n",
43  __FILE__, __LINE__, scene->l0file);
44  return (1);
45  }
46 
47 
48  /* */
49  /* Loop through scene record index and load nav input structure */
50  /* */
51  for (i = 0; i < scene->nrec; i++) {
52 
53  irec = scene->indx[i];
54 
55  if (fseek(fp, sizeof (swl0hdr) + irec * (L0LEN), SEEK_SET) != 0) {
56  fprintf(stderr,
57  "-E- %s line %d: error reading %s\n",
58  __FILE__, __LINE__, scene->l0file);
59  return (1);
60  }
61  if (fread(mnf, L0LEN, 1, fp) != 1) {
62  fprintf(stderr,
63  "-E- %s line %d: error reading %s\n",
64  __FILE__, __LINE__, scene->l0file);
65  return (1);
66  }
67 
68  /* Set telemetry quality flag */
69  navinp[i].flag = scene->qual[i];
70 
71  /* Get basic frame attributes */
72  memcpy(scid, &mnf[O_SCID], sizeof (scid));
73  if (endian)
74  swapc_bytes((char *) scid, 2, 2);
75  mnfnum = scid2mnfnum(scid);
76  mnftype = scid2mnftype(scid);
77 
78  /* This should never happen if scene index is OK */
79  if (mnftype != scene->mnftype) {
80  fprintf(stderr,
81  "-E- %s line %d: frame type mismatch\n",
82  __FILE__, __LINE__);
83  return (1);
84  }
85 
86  memcpy(navinp[i].sc_id, &mnf[O_SCID], 4);
87  if (endian)
88  swapc_bytes((char *) navinp[i].sc_id, 2, 2);
89 
90  memcpy(ttag, &mnf[O_TIME], 8);
91  if (endian)
92  swapc_bytes((char *) ttag, 2, 4);
93  ttag2ydmsec(ttag,
94  &navinp[i].iyear, &navinp[i].iday, (INT32 *) & navinp[i].msec);
95 
96  /* Correct for time shifts */
97  trec = temporal_anomaly(mnftype, navinp[i].iyear, navinp[i].iday, navinp[i].msec);
98  if (trec != NULL) {
99  if (trec->action == 1) {
100  printf("Correcting frame %d of time %d %d %d by %d msecs\n",
101  irec, navinp[i].iyear, navinp[i].iday, navinp[i].msec, trec->delmsec);
102  navinp[i].msec -= trec->delmsec;
103  navinp[i].nflag[5] = 1;
104  if (i == 0)
105  scene->stime -= trec->delmsec / 1000.0;
106  else if (i == scene->nrec - 1)
107  scene->etime -= trec->delmsec / 1000.0;
108  } else if (trec->action == 2) {
109  printf("Failing frame %d of time %d %d %d for time correction ambiguity\n",
110  irec, navinp[i].iyear, navinp[i].iday, navinp[i].msec);
111  navinp[i].nflag[5] = 1;
112  navinp[i].nflag[0] = 1;
113  }
114  }
115 
116  /* Convert SOH telemetry */
117  conv_soh_(&mnf[O_SOH], navinp[i].sc_ana, navinp[i].sc_dis);
118 
119  /* Convert instrument tlm, if it is valid */
120  for (j = 0; j < ninst; j++)
121  if (valid_instlm(mnftype, mnfnum, j))
122  conv_ins_(&mnf[oinst + sinst * j ],
123  &(navinp[i].inst_ana[j][0]),
124  &(navinp[i].inst_dis[j][0]));
125  }
126 
127  fclose(fp);
128 
129  return (i);
130 
131 }
132 
133 
134 
135 
int j
Definition: decode_rs.h:73
int conv_soh_(BYTE *soh, FLOAT32 *scana, BYTE *scdis)
INT32 getnavdata(swl0scene *scene, input_sType navinp[])
Definition: getnavdata.c:8
#define NULL
Definition: decode_rs.h:63
#define O_TIME
Definition: swl0_parms.h:33
void ttag2ydmsec(INT16 ttag[], INT16 *year, INT16 *day, INT32 *msec)
Definition: swl0_utils.c:71
int32_t INT32
Definition: elements.h:6
int32 * msec
Definition: l1_czcs_hdf.c:31
INT16 scid2mnftype(INT16 scid[])
Definition: swl0_utils.c:20
int endianess(void)
determine endianess
Definition: endianess.c:10
int swapc_bytes(char *in, int nbyte, int ntime)
Definition: swapc_bytes.c:4
#define O_INSTGAC
Definition: swl0_parms.h:40
unsigned char BYTE
Definition: elements.h:4
short int INT16
Definition: elements.h:5
INT16 scid2mnfnum(INT16 scid[])
Definition: swl0_utils.c:29
#define GACTYPE
Definition: swl0_parms.h:19
#define L0LEN
Definition: swl0_parms.h:8
#define O_SCID
Definition: swl0_parms.h:32
#define O_SOH
Definition: swl0_parms.h:34
#define O_INSTLAC
Definition: swl0_parms.h:41
int i
Definition: decode_rs.h:71
int conv_ins_(BYTE *soh, FLOAT32 *insta, BYTE *instd)
INT16 valid_instlm(INT16 mnftype, INT16 mnfnum, INT16 scanNum)
Definition: instlm.c:15
timeTab * temporal_anomaly(INT16 mnftype, INT16 year, INT16 day, INT32 msec)