OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
fileFormatUtils.c
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <genutils.h>
3 #include <string.h>
4 #include <strings.h> // strcasecmp
5 #include <stdlib.h>
6 
7 #define THE_STRING_MAX 128
8 #define FILE_FORMAT_FILE "$OCDATAROOT/common/file_formats.txt"
9 
10 static int isFormatsLoaded = 0;
11 static int numFormats = 0;
12 static char** formatIndex = NULL;
13 static char** formatName = NULL;
14 static char** formatExtension = NULL;
15 
16 /*
17 This is a sample format file. Actually the current one.
18 
19 
20 # The colon-separated columns are:
21 # 1) index number
22 # 2) format_name
23 # 3) default extension
24 #
25 1:HDF4:
26 2:netCDF4:nc
27 3:HDF5:h5
28 4:BIN:bin
29 5:PNG:png
30 6:TIFF:tiff
31 7:PPM:ppm
32 8:JPG:jpg
33 
34  */
35 
40  int index;
41  char line[THE_STRING_MAX];
42  char str1[THE_STRING_MAX];
43  char fileName[FILENAME_MAX];
44  int count;
45  FILE* fp;
46  int fileLineNum;
47 
48  isFormatsLoaded = 1;
49 
50  // open the file
52  fp = fopen(fileName, "r");
53  if (fp == NULL) {
54  printf("Error - Could not open file format definition file %s\n", fileName);
55  exit(1);
56  }
57 
58  // count lines in the file
59  fileLineNum = 0;
60  while (fgets(line, THE_STRING_MAX, fp)) {
61  fileLineNum++;
63  if (line[0] && line[0] != '#') {
64  count = sscanf(line, "%d:%s", &index, str1);
65  if (count != 2) {
66  printf("-E- %s Line %d: error reading file type record on line %d of file %s.\n",
67  __FILE__, __LINE__, fileLineNum, fileName);
68  exit(1);
69  }
70  numFormats++;
71  }
72  }
73 
74  // rewind file
75  fseek(fp, 0, SEEK_SET);
76 
77  // allocate storage
78  formatIndex = (char**) calloc(numFormats, sizeof (char*));
79  formatName = (char**) calloc(numFormats, sizeof (char*));
80  formatExtension = (char**) calloc(numFormats, sizeof (char*));
81  if (!formatIndex || !formatName || !formatExtension) {
82  printf("Error - Could not allocate space for the file format definition file %s\n", fileName);
83  exit(1);
84  }
85 
86  fileLineNum = 0;
87  index = 0;
88  while (fgets(line, THE_STRING_MAX, fp)) {
89  fileLineNum++;
91  if (line[0] && line[0] != '#') {
92  char *s1, *s2;
93  s1 = line;
94 
95  // get file type index
96  s2 = strchr(s1, ':');
97  if (s2 == NULL) {
98  printf("-E- %s Line %d: did not fine first ':' on line %d of file %s.\n",
99  __FILE__, __LINE__, fileLineNum, fileName);
100  exit(1);
101  }
102  *s2 = '\0';
103  formatIndex[index] = trimBlanksDup(s1);
104  s1 = s2 + 1;
105 
106  // get file type name
107  s2 = strchr(s1, ':');
108  if (s2 == NULL) {
109  printf("-E- %s Line %d: did not find second ':' on line %d of file %s.\n",
110  __FILE__, __LINE__, fileLineNum, fileName);
111  exit(1);
112  }
113  *s2 = '\0';
114  formatName[index] = trimBlanksDup(s1);
115  s1 = s2 + 1;
116 
117  // get file type extension
118  formatExtension[index] = trimBlanksDup(s1);
119 
120  index++;
121  if (index > numFormats) {
122  printf("-E- %s Line %d: Read more lines the second time through on line %d of file %s.\n",
123  __FILE__, __LINE__, fileLineNum, fileName);
124  exit(1);
125  }
126  }
127  }
128  fclose(fp);
129 }
130 
136 int getInternalIndex(const char* str) {
137  int i;
138  int result = -1;
139 
140  // bail if NULL is passed in
141  if (str == NULL)
142  return (result);
143 
144  char* inStr = trimBlanksDup(str);
145 
146  // bail if empty string
147  if (inStr[0] == 0)
148  return (result);
149 
150  if (!isFormatsLoaded)
151  readFileFormats();
152 
153  // loop through the arrays
154  for (i = 0; i < numFormats; i++) {
155 
156  // look in the index array
157  if (strcmp(inStr, formatIndex[i]) == 0) {
158  result = i;
159  break;
160  }
161 
162  // look in the format array
163  if (strcasecmp(inStr, formatName[i]) == 0) {
164  result = i;
165  break;
166  }
167 
168  // look in the extension array
169  if (strcasecmp(inStr, formatExtension[i]) == 0) {
170  result = i;
171  break;
172  }
173 
174  }
175 
176  free(inStr);
177  return result;
178 }
179 
188 int getFileFormatIndex(const char* str) {
189  int i = getInternalIndex(str);
190  if (i == -1) {
191  return -1;
192  }
193  return atoi(formatIndex[i]);
194 }
195 
205 const char* getFileFormatName(const char* str) {
206  int i = getInternalIndex(str);
207  if (i == -1) {
208  return NULL;
209  }
210  return formatName[i];
211 }
212 
221 const char* getFileFormatExtension(const char* str) {
222  int i = getInternalIndex(str);
223  if (i == -1) {
224  return NULL;
225  }
226  return formatExtension[i];
227 }
228 
an array had not been initialized Several spelling and grammar corrections were which is read from the appropriate MCF the above metadata values were hard coded A problem calculating the average background DN for SWIR bands when the moon is in the space view port was corrected The new algorithm used to calculate the average background DN for all reflective bands when the moon is in the space view port is now the same as the algorithm employed by the thermal bands For non SWIR changes in the averages are typically less than Also for non SWIR the black body DNs remain a backup in case the SV DNs are not available For SWIR the changes in computed averages were larger because the old which used the black body suffered from contamination by the micron leak As a consequence of the if SV DNs are not available for the SWIR the EV pixels will not be the granule time is used to identify the appropriate tables within the set given for one LUT the first two or last two tables respectively will be used for the interpolation If there is only one LUT in the set of it will be treated as a constant LUT The manner in which Earth View data is checked for saturation was changed Previously the raw Earth View DNs and Space View DNs were checked against the lookup table values contained in the table dn_sat The change made is to check the raw Earth and Space View DNs to be sure they are less than the maximum saturation value and to check the Space View subtracted Earth View dns against a set of values contained in the new lookup table dn_sat_ev The metadata configuration and ASSOCIATEDINSTRUMENTSHORTNAME from the MOD02HKM product The same metatdata with extensions and were removed from the MOD021KM and MOD02OBC products ASSOCIATEDSENSORSHORTNAME was set to MODIS in all products These changes are reflected in new File Specification which users may consult for exact the pow functions were eliminated in Emissive_Cal and Emissive bands replaced by more efficient code Other calculations throughout the code were also made more efficient Aside from a few round off there was no difference to the product The CPU time decreased by about for a day case and for a night case A minor bug in calculating the uncertainty index for emissive bands was corrected The frame index(0-based) was previously being used the frame number(1-based) should have been used. There were only a few minor changes to the uncertainty index(maximum of 1 digit). 3. Some inefficient arrays(Sigma_RVS_norm_sq) were eliminated and some code lines in Preprocess_L1A_Data were moved into Process_OBCEng_Emiss. There were no changes to the product. Required RAM was reduced by 20 MB. Now
#define NULL
Definition: decode_rs.h:63
void trimBlanks(char *str)
Definition: trimBlanks.c:10
const char * getFileFormatExtension(const char *str)
#define FILE_FORMAT_FILE
void readFileFormats()
const char * getFileFormatName(const char *str)
int getInternalIndex(const char *str)
void parse_file_name(const char *inpath, char *outpath)
#define THE_STRING_MAX
int getFileFormatIndex(const char *str)
const char * str
Definition: l1c_msi.cpp:35
char * trimBlanksDup(const char *str)
Definition: trimBlanks.c:58
int i
Definition: decode_rs.h:71
int count
Definition: decode_rs.h:79