-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path4DEnVar.c
173 lines (131 loc) · 3.64 KB
/
4DEnVar.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
Requires my matrix i/o code and the GSL.
Tristan Quaife. 2021.
*/
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include <gsl_matrix.h>
#include<matrixio.h>
#include<4DEnVar_engine.h>
#ifndef TRUE
#define TRUE 1
#endif
#ifndef FALSE
#define FALSE 0
#endif
/*
============================
Some basic utility functions
============================
*/
void check_dims( gsl_matrix * gmat1, gsl_matrix * gmat2 )
/*Check the dimensions of two GSL matrices.
Exit with failure if they are not equal.*/
{
if( (gmat1->size1 != gmat2->size1) || (gmat1->size2 != gmat2->size2) ){
fprintf( stderr, "%s: matrix dimensions do not match\n", __FILE__ );
exit( EXIT_FAILURE );
}
return ;
}
gsl_matrix *load_ffMatrix_to_gsl_matrix( char *filename )
/*Wrapper function for fread_ascii_ffMatrix that handles
the file opening and exits with failure if open fails.
Returns the data as a GSL matrix.
*/
{
FILE * fp ;
double *ptr, tmp ;
int x, y ;
long xsize ;
long ysize ;
gsl_matrix *gmat ;
if( ( fp = fopen( filename, "r" ) ) == NULL ){
fprintf( stderr, "%s: unable to open file %s\n", __FILE__, filename );
exit( EXIT_FAILURE );
}
ptr=fread_ascii_ffMatrix( &xsize, &ysize, fp ) ;
gmat=(gsl_matrix *)gsl_matrix_alloc ( (size_t)ysize, (size_t)xsize);
for( y=0; y<ysize; y++ ){
for( x=0; x<xsize; x++ ){
tmp=*(ptr + y*xsize + x );
gsl_matrix_set (gmat, y, x, tmp);
}
}
fclose(fp);
free(ptr);
return(gmat);
}
gsl_vector *load_ffMatrix_to_gsl_vector( char *filename )
/*Wrapper function for fread_ascii_ffMatrix that handles
the file opening and exits with failure if open fails.
Returns the data as a GSL VECTOR.
*/
{
FILE * fp ;
double *ptr, tmp ;
int y ;
long xsize ;
long ysize ;
gsl_vector *gvec ;
if( ( fp = fopen( filename, "r" ) ) == NULL ){
fprintf( stderr, "%s: unable to open file %s\n", __FILE__, filename );
exit( EXIT_FAILURE );
}
ptr=fread_ascii_ffMatrix( &xsize, &ysize, fp ) ;
if( xsize>1 ){
fprintf( stderr, "%s: file %s should have a single column \n", __FILE__, filename );
exit( EXIT_FAILURE );
}
gvec=(gsl_vector *)gsl_vector_alloc ( (size_t)ysize );
for( y=0; y<ysize; y++ ){
tmp=*(ptr + y );
gsl_vector_set (gvec, y, tmp);
}
fclose(fp);
free(ptr);
return(gvec);
}
/*
===============================
========= main ================
===============================
*/
int main( int argc, char **argv )
{
/*
We need as input:
x - parameter ensemble matrix
hx - matrix of predicted observations
R - observation uncertainty matrix
y - observation vector
hx_bar - vector of predicted observations from mean parameters
*/
gsl_matrix *xb ;
gsl_matrix *Xa ;
gsl_matrix *hx ;
gsl_matrix *R ;
gsl_vector *y ;
gsl_vector *xa ;
gsl_vector *hx_bar ;
xb =load_ffMatrix_to_gsl_matrix(argv[1]);
hx =load_ffMatrix_to_gsl_matrix(argv[2]);
y =load_ffMatrix_to_gsl_vector(argv[3]);
R =load_ffMatrix_to_gsl_matrix(argv[4]);
hx_bar =load_ffMatrix_to_gsl_vector(argv[5]);
xa = fourDEnVar( xb, hx, y, R, hx_bar );
Xa = fourDEnVar_sample_posterior( xb, hx, R, hx_bar, xa );
print_gsl_vector(xa);
printf("==================\n");
print_gsl_matrix(Xa);
gsl_matrix_free (xb);
gsl_vector_free (xa);
gsl_matrix_free (Xa);
gsl_matrix_free (hx);
gsl_matrix_free (R);
gsl_vector_free (y);
gsl_vector_free (hx_bar);
return( EXIT_SUCCESS );
}