NASA Logo
Ocean Color Science Software

ocssw V2022
wavelength_3d.cpp
Go to the documentation of this file.
1 #include <iostream>
2 #include <sstream>
3 #include <string>
4 #include <vector>
5 #include <iterator>
6 #include <algorithm>
7 #include "wavelength_3d.h"
8 
9 using namespace std;
10 
11 extern "C" void get_wavelength3d(const filehandle *l1file, instr *input) {
12 
13  // Convert user wavelength_3d_str into string
14  string wavelength_3d_str = input->wavelength_3d_str;
15 
16  //--------------------------------------------------------------------------------------------------------
17  //If user input for wavelength_3d_str is empty, populate wavelength_3d_str indexes and size with all wavelengths
18  if (wavelength_3d_str.empty()) {
19  input->wavelength_3d = l1file->iwave;
20  input->nwavelengths_3d = l1file->nbands;
21  input->wavelength_3d_index = (int *)malloc(l1file->nbands * sizeof(int *));
22  for (int i = 0; i < l1file->nbands; i++) {
23  input->wavelength_3d_index[i] = i;
24  }
25  }
26  else { // If wavelength_3d user input array not empty
27  //--------------------------------------------------------------------------------------------------------
28  // Check if user wavelength input is in ascending order:
29  int size = 0;
30  for (int i = 0; input->wavelength_3d_str[i] != 0; i++) {
31  size++;
32  }
33  // Replace : with , for input wavelength_3d array
34  char *temp_wave = (char*)malloc(size * sizeof(char*));
35  for (int i = 0; i < size; i++) {
36  temp_wave[i] = input->wavelength_3d_str[i];
37  if (temp_wave[i] == ':') {
38  temp_wave[i] = ',';
39  }
40  }
41  temp_wave[size] = '\0';
42 
43  string temp_wave_str = temp_wave;
44  int size_wave_ptr = 1;
45  char **array = (char **)malloc(size * sizeof(char **));
46  array[0] = &temp_wave[0];
47 
48  // Save comma separated inputs
49  for (int i = 1; temp_wave[i] != 0; i++) {
50  if (temp_wave[i] == ',') {
51  size_wave_ptr++;
52  array = (char **)realloc(array, size_wave_ptr * sizeof(char **));
53  array[(size_wave_ptr) - 1] = &temp_wave[i + 1];
54  temp_wave[i] = '\0';
55  }
56  }
57 
58  // if wavelengths are not in ascending order, exit
59  for (int i = 0; i < size_wave_ptr - 1; i++) {
60  if (atoi(array[i + 1]) <= atoi(array[i])) {
61  cout << "Error: Input wavelengths in \"wavelength_3d\" not in ascending order at " << array[i+1] << endl;
62  exit(1);
63  }
64  }
65  //--------------------------------------------------------------------------------------------------------
66  // If input wavelengths are in ascending order, check if they are all found in input->iwave:
67  // If not all wavelengths found, then prompt error
68  for (int i = 0; i < size_wave_ptr; i++) {
69  bool found = false;
70  for (int j = 0; j < (int)l1file->nbands; j++) {
71  if(atoi(array[i]) == l1file->iwave[j]) {
72  found = true;
73  break;
74  }
75  }
76  if (!found) {
77  cout << "Error: Input wavelength " << array[i] << " in \"wavelength_3d\" not found." << endl;
78  exit(1);
79  }
80  }
81  //--------------------------------------------------------------------------------------------------------
82  // If input wavelengths are in ascending order and all found in input->iwave:
83  // Convert l1file->iwave to int vector
84  vector<int> iwave(l1file->iwave, l1file->iwave + l1file->nbands);
85 
86  // out_iwave array
87  vector<int> out_iwave;
88 
89  // Position of current divider
90  int pos = 0;
91  int num1 = 0;
92  int num2 = 0;
93 
94  // Iterate through wavelength_3d_str string
95  while (pos < (int)wavelength_3d_str.length()) {
96  // Find next divider
97  int div = wavelength_3d_str.find(",", pos);
98  // Check if end of string
99  if (div == (int)string::npos) {
100  div = wavelength_3d_str.length();
101  }
102 
103  // Check if range (contains ':')
104  int range = wavelength_3d_str.find(":", pos);
105 
106  if (range != (int)string::npos && range < div) {
107  // Split range into start and end
108  int start = stoi(wavelength_3d_str.substr(pos, range - pos));
109  int end = stoi(wavelength_3d_str.substr(range + 1, div - range - 1));
110 
111  // Add numbers in range to out_iwave
112  for (int i = start; i <= end; i++) {
113  out_iwave.push_back(i);
114  num1++;
115  }
116  }
117 
118  else {
119  // Is single number, add it to out_iwave
120  int num = stoi(wavelength_3d_str.substr(pos, div - pos));
121  out_iwave.push_back(num);
122  num2++;
123  }
124 
125  // Update position
126  pos = div + 1;
127  }
128 
129  int n = 0;
130  // Allocate indexes array memory
131  input->wavelength_3d_index = (int *)malloc(n * sizeof(int *));
132  input->nwavelengths_3d = 0;
133 
134  // Iterate through iwave
135  int size_check = 0;
136  for (int i = 0; i < (int)iwave.size(); i++) {
137  // check if iwave[i] is in out_iwave
138  bool found = false;
139  for(int j = 0; j < (int)out_iwave.size(); j++){
140  if(iwave[i] == out_iwave[j]){
141  found = true;
142  // Update num of wavelength_3d
143  (input->nwavelengths_3d)++;
144  size_check++;
145  break;
146  }
147  }
148  if(found) {
149  // Update wavelength_3d indexes
150  input->wavelength_3d_index = (int *)realloc(input->wavelength_3d_index, (n + 1) * sizeof(int*));
151  input->wavelength_3d_index[n] = i;
152  n++;
153  }
154  }
155  //--------------------------------------------------------------------------------------------------------
156  // Populate expanded user wavelength input into wavelength_3d array
157  input->wavelength_3d = (int *)malloc((input->nwavelengths_3d) * sizeof(int *));
158  for (int i = 0; i < input->nwavelengths_3d; i ++) {
159  input->wavelength_3d[i] = l1file->iwave[input->wavelength_3d_index[i]];
160  }
161  //--------------------------------------------------------------------------------------------------------
162  }
163 }
164 
int32 l1file(int32 sdfid, int32 *nsamp, int32 *nscans, int16 *dtynum)
Definition: l1stat_chk.c:586
data_t num2
Definition: decode_rs.h:74
int j
Definition: decode_rs.h:73
float32 * pos
Definition: l1_czcs_hdf.c:35
instr * input
void get_wavelength3d(const filehandle *l1file, instr *input)
data_t num1
Definition: decode_rs.h:74
int i
Definition: decode_rs.h:71