OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
fuzzy_func_v3.c
Go to the documentation of this file.
1 /*
2  * fuzzy_member_func.c
3  *
4  *
5  * Created by Tim Moore on 9/1/09.
6  * Copyright 2009 UNH. All rights reserved.
7  *
8  * Input:
9  * rrs - a spectral rrs(0-) at specified wavelengths from a satellite
10  * urrs - the matrix of mean rrs(0-) spectra for the different optical water types
11  * y3inv - the inverted covariance matrix for the optical water types
12  * nclasses - the number of optical water types
13  * df - the number of rrs wavelengths
14  *
15  *
16  * outdata - pointer to the output fuzzy memberships to the optical water types for the rrs input
17  *
18  */
19 
20 #define NRANSI
21 #include "nr.h"
22 #include "nrutil.h"
23 #include <math.h>
24 #define DEBUG_FUZZY 0
25 #define ITMAX 100
26 #define EPS 3.0e-7
27 #define FPMIN 1.0e-30
28 #include <stdio.h>
29 #include <stddef.h>
30 #include <stdlib.h>
31 #define NR_END 1
32 #define FREE_ARG char*
33 
34 void fuzzy_func_v3(float *rrs, float **urrs, float ***y3inv, int nclasses, int nowts, int df, double *outdata)
35  {
36 
37  int nmax, i, j, k;
38  float *sa, *ax, *b, *y, *alphachi, **yinv, gamm, x, gln, chiz, z2;
39  unsigned long msize, *ija;
40 
41  nmax = 2 * df * df + 1;
42 
43  ija = lvector(1, nmax);
44  ax = vector(1, df);
45  b = vector(1, df);
46  sa = vector(1, nmax);
47 
48  yinv = matrix(1, df, 1, df);
49  y = vector(1, df);
50  alphachi = vector(1, nclasses);
51 
52 
53  if (DEBUG_FUZZY) {
54  printf("\ninside fuzzy_func...");
55  printf("\nnclasses = %d", nclasses);
56  printf("\ndf = %d", df);
57  }
58 
59  for (i = 1; i < nclasses + 1; i++) {
60  for (j = 1; j < df + 1; j++) {
61  y[j] = rrs[j - 1] - urrs[j][i];
62  if (DEBUG_FUZZY) printf("\n%d %d %f %f %f", i, j, y[j], rrs[j - 1], urrs[j][i]);
63  for (k = 1; k < df + 1; k++) {
64  yinv[j][k] = y3inv[i][j][k];
65  }
66 
67  }
68 
69  sprsin(yinv, df, 0.5, nmax, sa, ija);
70  msize = ija[1] - 2;
71  sprsax(sa, ija, y, b, msize);
72  z2 = 0;
73  for (j = 1; j < df + 1; j++) {
74  z2 = z2 + y[j] * b[j];
75  }
76  x = z2 / 2.0;
77  chiz = ((float) df) / 2.0;
78  if (DEBUG_FUZZY) printf("\nz2=%f", z2);
79 
80  /*
81  if (x < 0) { // baf
82  free_vector(y,1,df+1);
83  free_vector(alphachi,1,nclasses);
84  free_matrix(yinv,1,df,1,df);
85 
86  free_vector(sa,1,nmax);
87  free_vector(b,1,df);
88  free_vector(ax,1,df);
89  free_lvector(ija,1,nmax);
90  return(1);
91  }
92  */
93 
94  if (x <= (chiz + 1.0)) {
95  gser(&gamm, chiz, x, &gln);
96  alphachi[i] = 1.0 - gamm;
97  } else {
98  gcf(&gamm, chiz, x, &gln);
99  alphachi[i] = gamm;
100  }
101 
102  if (DEBUG_FUZZY) printf("\ngamm=%f", gamm);
103 
104  }
105 
106  if (DEBUG_FUZZY) {
107  for (i = 0; i < nclasses; i++) {
108  printf("\nalpha[%d]=%f", i, alphachi[i + 1]);
109  }
110  }
111 
112  for (i = 0; i < nowts - 1; i++) {
113  *outdata++ = (double) alphachi[i + 1];
114  }
115 
116  *outdata++ = alphachi[9] + alphachi[10] + alphachi[11] + alphachi[12] + alphachi[13] + alphachi[14] + alphachi[15] + alphachi[16];
117 
118  free_vector(y, 1, df + 1);
119  free_vector(alphachi, 1, nclasses);
120  free_matrix(yinv, 1, df, 1, df);
121 
122  free_vector(sa, 1, nmax);
123  free_vector(b, 1, df);
124  free_vector(ax, 1, df);
125  free_lvector(ija, 1, nmax);
126 
127  if (DEBUG_FUZZY) printf("\nleaving fuzzy_member_func...");
128 
129 
130 }
int j
Definition: decode_rs.h:73
float * vector(long nl, long nh)
Definition: nrutil.c:15
#define DEBUG_FUZZY
Definition: fuzzy_func_v3.c:24
subroutine gser(gamser, a, x, gln)
Definition: ocn.f:1210
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 fuzzy_func_v3(float *rrs, float **urrs, float ***y3inv, int nclasses, int nowts, int df, double *outdata)
Definition: fuzzy_func_v3.c:34
subroutine gcf(gammcf, a, x, gln)
Definition: ocn.f:1236
void sprsax(float sa[], unsigned long ija[], float x[], float b[], unsigned long n)
Definition: sprsax.c:1
void free_vector(float *v, long nl, long nh)
Definition: nrutil.c:204
void free_lvector(unsigned long *v, long nl, long nh)
Definition: nrutil.c:219
integer, parameter double
data_t b[NROOTS+1]
Definition: decode_rs.h:77
unsigned long * lvector(long nl, long nh)
Definition: nrutil.c:42
void sprsin(float **a, int n, float thresh, unsigned long nmax, float sa[], unsigned long ija[])
Definition: sprsin.c:3
int i
Definition: decode_rs.h:71
int k
Definition: decode_rs.h:73