OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
get_f0.c
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include "l1.h"
4 
5 int get_f0_neckel(int32_t wl, int32_t width, float *f0) {
6  static int firstCall = 1;
7  static int nf0table = 600;
8  static float f0_table[600];
9  static int min_wl = 330;
10  static int del_wl = 1;
11 
12  FILE *fp;
13  char *filedir;
14  char filename[FILENAME_MAX];
15  char line [80];
16  int32_t i, imin, imax;
17 
18  if (firstCall) {
19  float tmp_wl;
20  if ((filedir = getenv("OCDATAROOT")) == NULL) {
21  printf("-E- %s: OCDATAROOT env variable undefined.\n", __FILE__);
22  return (0);
23  }
24  strcpy(filename, filedir);
25  strcat(filename, "/common/f0_table_neckel.dat");
26 
27  if ((fp = fopen(filename, "r")) == NULL) {
28  fprintf(stderr,
29  "-E- %s line %d: unable to open %s for reading\n",
30  __FILE__, __LINE__, filename);
31  return (0);
32  }
33 
34  for (i = 0; i < nf0table; i++) {
35  if (fgets(line, 80, fp) == NULL) {
36  fprintf(stderr,
37  "-E- %s line %d: error reading %s at line %d\n",
38  __FILE__, __LINE__, filename, i);
39  return (0);
40  }
41  sscanf(line, "%f %f", &tmp_wl, &f0_table[i]);
42  }
43 
44  firstCall = 0;
45  }
46 
47  i = (wl - min_wl) / del_wl;
48  imin = MAX(i - width / 2 / del_wl, 0);
49  imax = MIN(i + width / 2 / del_wl, nf0table);
50 
51  if (i < 0 || i > nf0table) {
52  fprintf(stderr,
53  "-E- %s line %d: wavelength of %d outside F0 table range.\n",
54  __FILE__, __LINE__, wl);
55  return (0);
56  }
57 
58  *f0 = 0.0;
59  for (i = imin; i <= imax; i++)
60  *f0 += f0_table[i];
61 
62  *f0 /= (imax - imin + 1);
63 
64  return (1);
65 }
66 
67 int get_f0_neckel_(int32_t *wl, int32_t *width, float *f0) {
68  return (get_f0_neckel(*wl, *width, f0));
69 }
70 
71 int get_f0_thuillier(int32_t wl, int32_t width, float *f0) {
72  static int firstCall = 1;
73  static int nf0table = 771;
74  static float f0_table[771];
75  static int min_wl = 380;
76  static int del_wl = 1;
77 
78  FILE *fp;
79  char *filedir;
80  char filename[FILENAME_MAX];
81  char line [80];
82  int32_t i, imin, imax;
83 
84  if (firstCall) {
85  float tmp_wl;
86  if ((filedir = getenv("OCDATAROOT")) == NULL) {
87  printf("-E- %s: OCDATAROOT env variable undefined.\n", __FILE__);
88  return (0);
89  }
90  strcpy(filename, filedir);
91  strcat(filename, "/common/f0_table_thuillier.dat");
92 
93  if ((fp = fopen(filename, "r")) == NULL) {
94  fprintf(stderr,
95  "-E- %s line %d: unable to open %s for reading\n",
96  __FILE__, __LINE__, filename);
97  return (0);
98  }
99 
100  for (i = 0; i < nf0table; i++) {
101  if (fgets(line, 80, fp) == NULL) {
102  fprintf(stderr,
103  "-E- %s line %d: error reading %s at line %d\n",
104  __FILE__, __LINE__, filename, i);
105  return (0);
106  }
107  sscanf(line, "%f %f", &tmp_wl, &f0_table[i]);
108  }
109 
110  firstCall = 0;
111  }
112 
113  i = (wl - min_wl) / del_wl;
114  imin = MAX(i - width / 2 / del_wl, 0);
115  imax = MIN(i + width / 2 / del_wl, nf0table);
116 
117  if (i < 0 || i > nf0table) {
118  fprintf(stderr,
119  "-E- %s line %d: wavelength of %d outside F0 table range.\n",
120  __FILE__, __LINE__, wl);
121  return (0);
122  }
123 
124  *f0 = 0.0;
125  for (i = imin; i <= imax; i++)
126  *f0 += f0_table[i];
127 
128  *f0 /= (imax - imin + 1);
129 
130  return (1);
131 }
132 
133 int get_f0_thuillier_(int32_t *wl, int32_t *width, float *f0) {
134  return (get_f0_thuillier(*wl, *width, f0));
135 }
136 
137 void get_f0_thuillier_ext(int32_t wl, int32_t width, float *f0) {
138  static int firstCall = 1;
139  static int nheader = 15;
140  static int nf0table = 2198;
141  static float f0_table[2198];
142  static int min_wl = 200;
143  static int del_wl = 1;
144 
145  FILE *fp;
146  char *filedir;
147  char filename[FILENAME_MAX];
148  char line [80];
149  int32_t i, imin, imax;
150 
151  if (firstCall) {
152  float tmp_wl;
153  if ((filedir = getenv("OCDATAROOT")) == NULL) {
154  printf("-E- %s: OCDATAROOT env variable undefined.\n", __FILE__);
155  exit(EXIT_FAILURE);
156  }
157  strcpy(filename, filedir);
158  strcat(filename, "/common/Thuillier_F0.dat");
159 
160  if ((fp = fopen(filename, "r")) == NULL) {
161  fprintf(stderr,
162  "-E- %s line %d: unable to open %s for reading\n",
163  __FILE__, __LINE__, filename);
164  exit(EXIT_FAILURE);
165  }
166 
167  if (want_verbose)
168  printf("Reading Thuillier_F0.dat\n");
169  for (i = 0; i < nheader; i++) {
170  fgets(line, 80, fp);
171  /*
172  printf("%s",line);
173  */
174  }
175 
176  for (i = 0; i < nf0table; i++) {
177  if (fgets(line, 80, fp) == NULL) {
178  fprintf(stderr,
179  "-E- %s line %d: error reading %s at line %d\n",
180  __FILE__, __LINE__, filename, i);
181  exit(EXIT_FAILURE);
182  }
183  sscanf(line, "%f %f", &tmp_wl, &f0_table[i]);
184  }
185 
186  firstCall = 0;
187  }
188 
189  i = (wl - min_wl) / del_wl;
190  imin = MAX(i - width / 2 / del_wl, 0);
191  imax = MIN(i + width / 2 / del_wl, nf0table);
192 
193  if (i < 0 || i > nf0table) {
194  fprintf(stderr,
195  "-E- %s line %d: wavelength of %d outside F0 table range.\n",
196  __FILE__, __LINE__, wl);
197  exit(EXIT_FAILURE);
198  }
199 
200  *f0 = 0.0;
201  for (i = imin; i <= imax; i++)
202  *f0 += f0_table[i];
203 
204  *f0 /= (imax - imin + 1);
205 
206 }
207 
208 int get_f0_thuillier_ext_(int32_t *wl, int32_t *width, float *f0) {
209  return (get_f0_thuillier(*wl, *width, f0));
210 }
211 
#define MAX(A, B)
Definition: swl0_utils.h:26
#define MIN(x, y)
Definition: rice.h:169
#define NULL
Definition: decode_rs.h:63
int get_f0_thuillier_(int32_t *wl, int32_t *width, float *f0)
Definition: get_f0.c:133
int get_f0_neckel(int32_t wl, int32_t width, float *f0)
Definition: get_f0.c:5
int get_f0_thuillier_ext_(int32_t *wl, int32_t *width, float *f0)
Definition: get_f0.c:208
int get_f0_neckel_(int32_t *wl, int32_t *width, float *f0)
Definition: get_f0.c:67
int get_f0_thuillier(int32_t wl, int32_t width, float *f0)
Definition: get_f0.c:71
int want_verbose
char filename[FILENAME_MAX]
Definition: atrem_corl1.h:122
int i
Definition: decode_rs.h:71
void get_f0_thuillier_ext(int32_t wl, int32_t width, float *f0)
Definition: get_f0.c:137
How many dimensions is the output array Default is Not sure if anything above will work correctly strcpy(l2prod->title, "no title yet")