forked from cacao-org/AOloopControl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAOloopControl_sig2Modecoeff.c
146 lines (95 loc) · 3.07 KB
/
AOloopControl_sig2Modecoeff.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
/**
* @file AOloopControl_aorun.c
* @brief AO loop Control compute functions
*
* REAL TIME COMPUTING ROUTINES
*
* @author O. Guyon
* @date 2018-01-04
*
*
* @bug No known bugs.
*
*/
#define _GNU_SOURCE
// uncomment for test print statements to stdout
//#define _PRINT_TEST
#include <time.h>
#include <string.h>
#include <sched.h>
#include <gsl/gsl_math.h>
#include <gsl/gsl_blas.h>
#include <pthread.h>
#include "info/info.h"
//libraries created by O. Guyon
#include "CommandLineInterface/CLIcore.h"
#include "AOloopControl/AOloopControl.h"
#include "00CORE/00CORE.h"
#include "COREMOD_memory/COREMOD_memory.h"
#include "COREMOD_iofits/COREMOD_iofits.h"
#include "AOloopControl_IOtools/AOloopControl_IOtools.h"
extern AOLOOPCONTROL_CONF *AOconf; // configuration - this can be an array
extern AOloopControl_var aoloopcontrol_var;
//
// assumes the WFS mode basis is already orthogonal
// removes reference from each frame
//
long AOloopControl_sig2Modecoeff(const char *WFSim_name, const char *IDwfsref_name, const char *WFSmodes_name, const char *outname)
{
long IDout;
long IDwfs, IDmodes, IDwfsref;
long wfsxsize, wfsysize, wfssize, NBmodes, NBframes;
double totref;
float coeff;
long ii, m, kk;
FILE *fp;
double *mcoeff_ave;
double *mcoeff_rms;
IDwfs = image_ID(WFSim_name);
wfsxsize = data.image[IDwfs].md[0].size[0];
wfsysize = data.image[IDwfs].md[0].size[1];
NBframes = data.image[IDwfs].md[0].size[2];
wfssize = wfsxsize*wfsysize;
IDwfsref = image_ID(IDwfsref_name);
IDmodes = image_ID(WFSmodes_name);
NBmodes = data.image[IDmodes].md[0].size[2];
mcoeff_ave = (double*) malloc(sizeof(double)*NBmodes);
mcoeff_rms = (double*) malloc(sizeof(double)*NBmodes);
IDout = create_2Dimage_ID(outname, NBframes, NBmodes);
totref = 0.0;
for(ii=0; ii<wfssize; ii++)
totref += data.image[IDwfsref].array.F[ii];
for(ii=0; ii<wfssize; ii++)
data.image[IDwfsref].array.F[ii] /= totref;
for(kk=0; kk<NBframes; kk++)
{
double totim = 0.0;
for(ii=0; ii<wfssize; ii++)
totim += data.image[IDwfs].array.F[kk*wfssize+ii];
for(ii=0; ii<wfssize; ii++)
{
data.image[IDwfs].array.F[kk*wfssize+ii] /= totim;
data.image[IDwfs].array.F[kk*wfssize+ii] -= data.image[IDwfsref].array.F[ii];
}
for(m=0; m<NBmodes; m++)
{
coeff = 0.0;
for(ii=0; ii<wfssize; ii++)
coeff += data.image[IDmodes].array.F[m*wfssize+ii] * data.image[IDwfs].array.F[kk*wfssize+ii];
data.image[IDout].array.F[m*NBframes+kk] = coeff;
mcoeff_ave[m] += coeff;
mcoeff_rms[m] += coeff*coeff;
}
}
fp = fopen("mode_stats.txt", "w");
for(m=0; m<NBmodes; m++)
{
mcoeff_rms[m] = sqrt( mcoeff_rms[m]/NBframes );
mcoeff_ave[m] /= NBframes;
fprintf(fp, "%4ld %12g %12g\n", m, mcoeff_ave[m], mcoeff_rms[m]);
}
fclose(fp);
free(mcoeff_ave);
free(mcoeff_rms);
return(IDout);
}