field.h 7.77 KB
Newer Older
1
/*
Uwe Schulzweida's avatar
Uwe Schulzweida committed
2
3
4
  This file is part of CDO. CDO is a collection of Operators to
  manipulate and analyse Climate model Data.

Uwe Schulzweida's avatar
Uwe Schulzweida committed
5
  Copyright (C) 2003-2017 Uwe Schulzweida, <uwe.schulzweida AT mpimet.mpg.de>
Uwe Schulzweida's avatar
Uwe Schulzweida committed
6
7
8
9
10
11
12
13
14
15
16
17
  See COPYING file for copying and redistribution conditions.

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; version 2 of the License.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
*/

Uwe Schulzweida's avatar
Uwe Schulzweida committed
18
19
#ifndef _FIELD_H
#define _FIELD_H
Uwe Schulzweida's avatar
Uwe Schulzweida committed
20

21
22
23
#include <math.h>
#include "compare.h"

24
25
double var_to_std(double rvar, double missval);

Uwe Schulzweida's avatar
Uwe Schulzweida committed
26
27
28
29
30
enum field_flag {
  FIELD_NONE  =  1,
  FIELD_PTR   =  2,
  FIELD_WGT   =  4,
  FIELD_PTR2  =  8,
31
32
  FIELD_FLT   = 16,
  FIELD_ALL   = FIELD_PTR | FIELD_WGT
Uwe Schulzweida's avatar
Uwe Schulzweida committed
33
};
Uwe Schulzweida's avatar
Uwe Schulzweida committed
34

35

Uwe Schulzweida's avatar
cleanup    
Uwe Schulzweida committed
36
37
#define  MADDMN(x,y)  (DBL_IS_EQUAL((x),missval1) || DBL_IS_EQUAL((y),missval2) ? missval1 : (x)+(y))
#define  MSUBMN(x,y)  (DBL_IS_EQUAL((x),missval1) || DBL_IS_EQUAL((y),missval2) ? missval1 : (x)-(y))
38
#define  MMULMN(x,y)  (DBL_IS_EQUAL((x),0.)||DBL_IS_EQUAL((y),0.) ? 0 : DBL_IS_EQUAL((x),missval1) || DBL_IS_EQUAL((y),missval2) ? missval1 : (x)*(y))
Uwe Schulzweida's avatar
cleanup    
Uwe Schulzweida committed
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#define  MDIVMN(x,y)  (DBL_IS_EQUAL((x),missval1) || DBL_IS_EQUAL((y),missval2) || DBL_IS_EQUAL((y),0.) ? missval1 : (x)/(y))
#define  MPOWMN(x,y)  (DBL_IS_EQUAL((x),missval1) || DBL_IS_EQUAL((y),missval2) ? missval1 : pow((x),(y)))
#define  MSQRTMN(x)   (DBL_IS_EQUAL((x),missval1) || (x)<0 ? missval1 : sqrt(x))


#define  ADD(x,y)  ((x)+(y))
#define  SUB(x,y)  ((x)-(y))
#define  MUL(x,y)  ((x)*(y))
#define  DIV(x,y)  (IS_EQUAL((y),0.) ? missval1 : (x)/(y))
#define  POW(x,y)  pow((x),(y))
#define  SQRT(x)   sqrt(x)


#define  ADDM(x,y)  (IS_EQUAL((x),missval1) || IS_EQUAL((y),missval2) ? missval1 : (x)+(y))
#define  SUBM(x,y)  (IS_EQUAL((x),missval1) || IS_EQUAL((y),missval2) ? missval1 : (x)-(y))
54
#define  MULM(x,y)  (IS_EQUAL((x),0.)||IS_EQUAL((y),0.) ? 0 : IS_EQUAL((x),missval1) || IS_EQUAL((y),missval2) ? missval1 : (x)*(y))
Uwe Schulzweida's avatar
cleanup    
Uwe Schulzweida committed
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#define  DIVM(x,y)  (IS_EQUAL((x),missval1) || IS_EQUAL((y),missval2) || IS_EQUAL((y),0.) ? missval1 : (x)/(y))
#define  POWM(x,y)  (IS_EQUAL((x),missval1) || IS_EQUAL((y),missval2) ? missval1 : pow((x),(y)))
#define  SQRTM(x)   (IS_EQUAL((x),missval1) || (x)<0 ? missval1 : sqrt(x))


#define  ADDMN(x,y)  FADDMN(x, y, missval1, missval2)
#define  SUBMN(x,y)  FSUBMN(x, y, missval1, missval2)
#define  MULMN(x,y)  FMULMN(x, y, missval1, missval2)
#define  DIVMN(x,y)  FDIVMN(x, y, missval1, missval2)
#define  POWMN(x,y)  FPOWMN(x, y, missval1, missval2)
#define  SQRTMN(x)   FSQRTMN(x, missval1)


static inline
double FADDMN(double x, double y, double missval1, double missval2) { return MADDMN(x,y);}
static inline
double FSUBMN(double x, double y, double missval1, double missval2) { return MSUBMN(x, y);}
static inline
double FMULMN(double x, double y, double missval1, double missval2) { return MMULMN(x, y);}
static inline
double FDIVMN(double x, double y, double missval1, double missval2) { return MDIVMN(x, y);}
static inline
double FPOWMN(double x, double y, double missval1, double missval2) { return MPOWMN(x, y);}
static inline
double FSQRTMN(double x, double missval1) { return MSQRTMN(x);}
Uwe Schulzweida's avatar
Uwe Schulzweida committed
80
81

typedef struct {
82
  int      fpeRaised;
83
  int      nwpv; // number of words per value; real:1  complex:2
84
  int      memtype;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
85
86
  int      grid;
  int      zaxis;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
87
  size_t   size;
88
89
90
  size_t   nsamp;
  size_t   nmiss;
  size_t   nmiss2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
91
92
93
  double   missval;
  double  *weight;
  double  *ptr;
94
95
  float   *ptrf;
  void    *ptr2;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
96
}
97
field_type;
Uwe Schulzweida's avatar
Uwe Schulzweida committed
98

99

100
101
/* fieldmem.c */

102
103
104
105
void      field_init(field_type *field);
field_type **field_malloc(const int vlistID, const int ptype);
field_type **field_calloc(const int vlistID, const int ptype);
void      field_free(field_type **field, const int vlistID);
106

Uwe Schulzweida's avatar
Uwe Schulzweida committed
107
108
/* field.c */

109
110
111
112
113
114
double fldfun(field_type field, int function);
double fldrange(field_type field);
double fldmin(field_type field);
double fldmax(field_type field);
double fldsum(field_type field);
double fldavg(field_type field);
115
double fldavgw(field_type field);
116
double fldmean(field_type field);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
117
double fldmeanw(field_type field);
118
119
120
121
double fldstdw(field_type field);
double fldstd1w(field_type field);
double fldvarw(field_type field);
double fldvar1w(field_type field);
122
123
124
double fldpctl(field_type field, const double pn);
void   fldunm(field_type *field);
int    fldhvs(field_type *field, const size_t nlevels);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
125

126
/* ENS VALIDATION */
127
128
129
130
double fldcrps(field_type field);
double fldbrs(field_type field);
double fldrank(field_type field);
double fldroc(field_type field);
131

Uwe Schulzweida's avatar
Uwe Schulzweida committed
132
133
/* fieldzon.c */

134
135
136
137
138
139
140
141
142
143
144
145
void zonfun(field_type field1, field_type *field2, const int function);
void zonmin(field_type field1, field_type *field2);
void zonmax(field_type field1, field_type *field2);
void zonrange(field_type field1, field_type *field2);
void zonsum(field_type field1, field_type *field2);
void zonavg(field_type field1, field_type *field2);
void zonmean(field_type field1, field_type *field2);
void zonstd(field_type field1, field_type *field2);
void zonstd1(field_type field1, field_type *field2);
void zonvar(field_type field1, field_type *field2);
void zonvar1(field_type field1, field_type *field2);
void zonpctl(field_type field1, field_type *field2, const int k);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
146
147
148

/* fieldmer.c */

149
150
151
152
void merfun(field_type field1, field_type *field2, const int function);
void mermin(field_type field1, field_type *field2);
void mermax(field_type field1, field_type *field2);
void mersum(field_type field1, field_type *field2);
153
154
void meravgw(field_type field1, field_type *field2);
void mermeanw(field_type field1, field_type *field2);
155
156
157
158
void merstdw(field_type field1, field_type *field2);
void merstd1w(field_type field1, field_type *field2);
void mervarw(field_type field1, field_type *field2);
void mervar1w(field_type field1, field_type *field2);
159
void merpctl(field_type field1, field_type *field2, const int k);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
160

161
void fldrms(field_type field1, field_type field2, field_type *field3);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
162

163
void varrms(field_type field1, field_type field2, field_type *field3);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
164
165
166

/* fieldc.c */

167
void farcfun(field_type *field, const double rconst, const int function);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
168

169
170
171
172
void farcmul(field_type *field, const double rconst);
void farcdiv(field_type *field, const double rconst);
void farcadd(field_type *field, const double rconst);
void farcsub(field_type *field, const double rconst);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
173

174
void farmod(field_type *field, const double divisor);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
175

176
177
void farinv(field_type *field);
void farround(field_type *field);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
178
179
180

/* field2.c */

181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
void farfun(field_type *field1, field_type field2, int function);

void farcpy(field_type *field1, field_type field2);
void faradd(field_type *field1, field_type field2);
void farsum(field_type *field1, field_type field2);
void farsumw(field_type *field1, field_type field2, double w);
void farsumq(field_type *field1, field_type field2);
void farsumqw(field_type *field1, field_type field2, double w);
void farsumtr(field_type *field1, field_type field2, const double refval);
void farsub(field_type *field1, field_type field2);
void farmul(field_type *field1, field_type field2);
void fardiv(field_type *field1, field_type field2);
void farmin(field_type *field1, field_type field2);
void farmax(field_type *field1, field_type field2);
void farvar(field_type *field1, field_type field2, field_type field3, int divisor);
void farstd(field_type *field1, field_type field2, field_type field3, int divisor);
void farcvar(field_type *field1, field_type field2, int nsets, int divisor);
void farcstd(field_type *field1, field_type field2, int nsets, int divisor);
void farmoq(field_type *field1, field_type field2);
void farmoqw(field_type *field1, field_type field2, double w);
void faratan2(field_type *field1, field_type field2);

void farcount(field_type *field1, field_type field2);
Uwe Schulzweida's avatar
Uwe Schulzweida committed
204

Uwe Schulzweida's avatar
Uwe Schulzweida committed
205
#endif  /* _FIELD_H */