OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
nrutil.c
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <stddef.h>
3 #include <stdlib.h>
4 #define NR_END 1
5 #define FREE_ARG char*
6 
7 void nrerror(char error_text[])
8 /* Numerical Recipes standard error handler */ {
9  fprintf(stderr, "Numerical Recipes run-time error...\n");
10  fprintf(stderr, "%s\n", error_text);
11  fprintf(stderr, "...now exiting to system...\n");
12  exit(1);
13 }
14 
15 float *vector(long nl, long nh)
16 /* allocate a float vector with subscript range v[nl..nh] */ {
17  float *v;
18 
19  v = (float *) malloc((size_t) ((nh - nl + 1 + NR_END) * sizeof (float)));
20  if (!v) nrerror("allocation failure in vector()");
21  return v - nl + NR_END;
22 }
23 
24 int *ivector(long nl, long nh)
25 /* allocate an int vector with subscript range v[nl..nh] */ {
26  int *v;
27 
28  v = (int *) malloc((size_t) ((nh - nl + 1 + NR_END) * sizeof (int)));
29  if (!v) nrerror("allocation failure in ivector()");
30  return v - nl + NR_END;
31 }
32 
33 unsigned char *cvector(long nl, long nh)
34 /* allocate an unsigned char vector with subscript range v[nl..nh] */ {
35  unsigned char *v;
36 
37  v = (unsigned char *) malloc((size_t) ((nh - nl + 1 + NR_END) * sizeof (unsigned char)));
38  if (!v) nrerror("allocation failure in cvector()");
39  return v - nl + NR_END;
40 }
41 
42 unsigned long *lvector(long nl, long nh)
43 /* allocate an unsigned long vector with subscript range v[nl..nh] */ {
44  unsigned long *v;
45 
46  v = (unsigned long *) malloc((size_t) ((nh - nl + 1 + NR_END) * sizeof (long)));
47  if (!v) nrerror("allocation failure in lvector()");
48  return v - nl + NR_END;
49 }
50 
51 double *dvector(long nl, long nh)
52 /* allocate a double vector with subscript range v[nl..nh] */ {
53  double *v;
54 
55  v = (double *) malloc((size_t) ((nh - nl + 1 + NR_END) * sizeof (double)));
56  if (!v) nrerror("allocation failure in dvector()");
57  return v - nl + NR_END;
58 }
59 
60 float **matrix(long nrl, long nrh, long ncl, long nch)
61 /* allocate a float matrix with subscript range m[nrl..nrh][ncl..nch] */ {
62  long i, nrow = nrh - nrl + 1, ncol = nch - ncl + 1;
63  float **m;
64 
65  /* allocate pointers to rows */
66  m = (float **) malloc((size_t) ((nrow + NR_END) * sizeof (float*)));
67  if (!m) nrerror("allocation failure 1 in matrix()");
68  m += NR_END;
69  m -= nrl;
70 
71  /* allocate rows and set pointers to them */
72  m[nrl] = (float *) malloc((size_t) ((nrow * ncol + NR_END) * sizeof (float)));
73  if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
74  m[nrl] += NR_END;
75  m[nrl] -= ncl;
76 
77  for (i = nrl + 1; i <= nrh; i++) m[i] = m[i - 1] + ncol;
78 
79  /* return pointer to array of pointers to rows */
80  return m;
81 }
82 
83 double **dmatrix(long nrl, long nrh, long ncl, long nch)
84 /* allocate a double matrix with subscript range m[nrl..nrh][ncl..nch] */ {
85  long i, nrow = nrh - nrl + 1, ncol = nch - ncl + 1;
86  double **m;
87 
88  /* allocate pointers to rows */
89  m = (double **) malloc((size_t) ((nrow + NR_END) * sizeof (double*)));
90  if (!m) nrerror("allocation failure 1 in matrix()");
91  m += NR_END;
92  m -= nrl;
93 
94  /* allocate rows and set pointers to them */
95  m[nrl] = (double *) malloc((size_t) ((nrow * ncol + NR_END) * sizeof (double)));
96  if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
97  m[nrl] += NR_END;
98  m[nrl] -= ncl;
99 
100  for (i = nrl + 1; i <= nrh; i++) m[i] = m[i - 1] + ncol;
101 
102  /* return pointer to array of pointers to rows */
103  return m;
104 }
105 
106 int **imatrix(long nrl, long nrh, long ncl, long nch)
107 /* allocate a int matrix with subscript range m[nrl..nrh][ncl..nch] */ {
108  long i, nrow = nrh - nrl + 1, ncol = nch - ncl + 1;
109  int **m;
110 
111  /* allocate pointers to rows */
112  m = (int **) malloc((size_t) ((nrow + NR_END) * sizeof (int*)));
113  if (!m) nrerror("allocation failure 1 in matrix()");
114  m += NR_END;
115  m -= nrl;
116 
117 
118  /* allocate rows and set pointers to them */
119  m[nrl] = (int *) malloc((size_t) ((nrow * ncol + NR_END) * sizeof (int)));
120  if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
121  m[nrl] += NR_END;
122  m[nrl] -= ncl;
123 
124  for (i = nrl + 1; i <= nrh; i++) m[i] = m[i - 1] + ncol;
125 
126  /* return pointer to array of pointers to rows */
127  return m;
128 }
129 
130 float **submatrix(float **a, long oldrl, long oldrh, long oldcl, long oldch,
131  long newrl, long newcl)
132 /* point a submatrix [newrl..][newcl..] to a[oldrl..oldrh][oldcl..oldch] */ {
133  long i, j, nrow = oldrh - oldrl + 1, ncol = oldcl - newcl;
134  float **m;
135 
136  /* allocate array of pointers to rows */
137  m = (float **) malloc((size_t) ((nrow + NR_END) * sizeof (float*)));
138  if (!m) nrerror("allocation failure in submatrix()");
139  m += NR_END;
140  m -= newrl;
141 
142  /* set pointers to rows */
143  for (i = oldrl, j = newrl; i <= oldrh; i++, j++) m[j] = a[i] + ncol;
144 
145  /* return pointer to array of pointers to rows */
146  return m;
147 }
148 
149 float **convert_matrix(float *a, long nrl, long nrh, long ncl, long nch)
150 /* allocate a float matrix m[nrl..nrh][ncl..nch] that points to the matrix
151 declared in the standard C manner as a[nrow][ncol], where nrow=nrh-nrl+1
152 and ncol=nch-ncl+1. The routine should be called with the address
153 &a[0][0] as the first argument. */ {
154  long i, j, nrow = nrh - nrl + 1, ncol = nch - ncl + 1;
155  float **m;
156 
157  /* allocate pointers to rows */
158  m = (float **) malloc((size_t) ((nrow + NR_END) * sizeof (float*)));
159  if (!m) nrerror("allocation failure in convert_matrix()");
160  m += NR_END;
161  m -= nrl;
162 
163  /* set pointers to rows */
164  m[nrl] = a - ncl;
165  for (i = 1, j = nrl + 1; i < nrow; i++, j++) m[j] = m[j - 1] + ncol;
166  /* return pointer to array of pointers to rows */
167  return m;
168 }
169 
170 float ***f3tensor(long nrl, long nrh, long ncl, long nch, long ndl, long ndh)
171 /* allocate a float 3tensor with range t[nrl..nrh][ncl..nch][ndl..ndh] */ {
172  long i, j, nrow = nrh - nrl + 1, ncol = nch - ncl + 1, ndep = ndh - ndl + 1;
173  float ***t;
174 
175  /* allocate pointers to pointers to rows */
176  t = (float ***) malloc((size_t) ((nrow + NR_END) * sizeof (float**)));
177  if (!t) nrerror("allocation failure 1 in f3tensor()");
178  t += NR_END;
179  t -= nrl;
180 
181  /* allocate pointers to rows and set pointers to them */
182  t[nrl] = (float **) malloc((size_t) ((nrow * ncol + NR_END) * sizeof (float*)));
183  if (!t[nrl]) nrerror("allocation failure 2 in f3tensor()");
184  t[nrl] += NR_END;
185  t[nrl] -= ncl;
186 
187  /* allocate rows and set pointers to them */
188  t[nrl][ncl] = (float *) malloc((size_t) ((nrow * ncol * ndep + NR_END) * sizeof (float)));
189  if (!t[nrl][ncl]) nrerror("allocation failure 3 in f3tensor()");
190  t[nrl][ncl] += NR_END;
191  t[nrl][ncl] -= ndl;
192 
193  for (j = ncl + 1; j <= nch; j++) t[nrl][j] = t[nrl][j - 1] + ndep;
194  for (i = nrl + 1; i <= nrh; i++) {
195  t[i] = t[i - 1] + ncol;
196  t[i][ncl] = t[i - 1][ncl] + ncol*ndep;
197  for (j = ncl + 1; j <= nch; j++) t[i][j] = t[i][j - 1] + ndep;
198  }
199 
200  /* return pointer to array of pointers to rows */
201  return t;
202 }
203 
204 void free_vector(float *v, long nl, long nh)
205 /* free a float vector allocated with vector() */ {
206  free((FREE_ARG) (v + nl - NR_END));
207 }
208 
209 void free_ivector(int *v, long nl, long nh)
210 /* free an int vector allocated with ivector() */ {
211  free((FREE_ARG) (v + nl - NR_END));
212 }
213 
214 void free_cvector(unsigned char *v, long nl, long nh)
215 /* free an unsigned char vector allocated with cvector() */ {
216  free((FREE_ARG) (v + nl - NR_END));
217 }
218 
219 void free_lvector(unsigned long *v, long nl, long nh)
220 /* free an unsigned long vector allocated with lvector() */ {
221  free((FREE_ARG) (v + nl - NR_END));
222 }
223 
224 void free_dvector(double *v, long nl, long nh)
225 /* free a double vector allocated with dvector() */ {
226  free((FREE_ARG) (v + nl - NR_END));
227 }
228 
229 void free_matrix(float **m, long nrl, long nrh, long ncl, long nch)
230 /* free a float matrix allocated by matrix() */ {
231  free((FREE_ARG) (m[nrl] + ncl - NR_END));
232  free((FREE_ARG) (m + nrl - NR_END));
233 }
234 
235 void free_dmatrix(double **m, long nrl, long nrh, long ncl, long nch)
236 /* free a double matrix allocated by dmatrix() */ {
237  free((FREE_ARG) (m[nrl] + ncl - NR_END));
238  free((FREE_ARG) (m + nrl - NR_END));
239 }
240 
241 void free_imatrix(int **m, long nrl, long nrh, long ncl, long nch)
242 /* free an int matrix allocated by imatrix() */ {
243  free((FREE_ARG) (m[nrl] + ncl - NR_END));
244  free((FREE_ARG) (m + nrl - NR_END));
245 }
246 
247 void free_submatrix(float **b, long nrl, long nrh, long ncl, long nch)
248 /* free a submatrix allocated by submatrix() */ {
249  free((FREE_ARG) (b + nrl - NR_END));
250 }
251 
252 void free_convert_matrix(float **b, long nrl, long nrh, long ncl, long nch)
253 /* free a matrix allocated by convert_matrix() */ {
254  free((FREE_ARG) (b + nrl - NR_END));
255 }
256 
257 void free_f3tensor(float ***t, long nrl, long nrh, long ncl, long nch,
258  long ndl, long ndh)
259 /* free a float f3tensor allocated by f3tensor() */ {
260  free((FREE_ARG) (t[nrl][ncl] + ndl - NR_END));
261  free((FREE_ARG) (t[nrl] + ncl - NR_END));
262  free((FREE_ARG) (t + nrl - NR_END));
263 }
void free_convert_matrix(float **b, long nrl, long nrh, long ncl, long nch)
Definition: nrutil.c:252
data_t t[NROOTS+1]
Definition: decode_rs.h:77
double ** dmatrix(long nrl, long nrh, long ncl, long nch)
Definition: nrutil.c:83
int j
Definition: decode_rs.h:73
float * vector(long nl, long nh)
Definition: nrutil.c:15
int * ivector(long nl, long nh)
Definition: nrutil.c:24
double * dvector(long nl, long nh)
Definition: nrutil.c:51
int32_t nl
Definition: atrem_corl1.h:132
unsigned char * cvector(long nl, long nh)
Definition: nrutil.c:33
void nrerror(char error_text[])
Definition: nrutil.c:7
void free_matrix(float **m, long nrl, long nrh, long ncl, long nch)
Definition: nrutil.c:229
float ** matrix(long nrl, long nrh, long ncl, long nch)
Definition: nrutil.c:60
void free_submatrix(float **b, long nrl, long nrh, long ncl, long nch)
Definition: nrutil.c:247
void free_dvector(double *v, long nl, long nh)
Definition: nrutil.c:224
void free_f3tensor(float ***t, long nrl, long nrh, long ncl, long nch, long ndl, long ndh)
Definition: nrutil.c:257
void free_vector(float *v, long nl, long nh)
Definition: nrutil.c:204
#define NR_END
Definition: nrutil.c:4
float ** convert_matrix(float *a, long nrl, long nrh, long ncl, long nch)
Definition: nrutil.c:149
void free_lvector(unsigned long *v, long nl, long nh)
Definition: nrutil.c:219
float *** f3tensor(long nrl, long nrh, long ncl, long nch, long ndl, long ndh)
Definition: nrutil.c:170
int ** imatrix(long nrl, long nrh, long ncl, long nch)
Definition: nrutil.c:106
data_t b[NROOTS+1]
Definition: decode_rs.h:77
void free_imatrix(int **m, long nrl, long nrh, long ncl, long nch)
Definition: nrutil.c:241
void free_cvector(unsigned char *v, long nl, long nh)
Definition: nrutil.c:214
void free_ivector(int *v, long nl, long nh)
Definition: nrutil.c:209
unsigned long * lvector(long nl, long nh)
Definition: nrutil.c:42
int i
Definition: decode_rs.h:71
PGE01 indicating that PGE02 PGE01 V6 for and PGE01 V2 for MOD03 were used to produce the granule By convention adopted in all MODIS Terra PGE02 code versions are The fourth digit of the PGE02 version denotes the LUT version used to produce the granule The source of the metadata environment variable ProcessingCenter was changed from a QA LUT value to the Process Configuration A sign used in error in the second order term was changed to a
Definition: HISTORY.txt:424
float ** submatrix(float **a, long oldrl, long oldrh, long oldcl, long oldch, long newrl, long newcl)
Definition: nrutil.c:130
#define FREE_ARG
Definition: nrutil.c:5
void free_dmatrix(double **m, long nrl, long nrh, long ncl, long nch)
Definition: nrutil.c:235