-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcorrspec.c
178 lines (139 loc) · 4.82 KB
/
corrspec.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
170
171
172
173
174
175
176
177
// main.c
#include <unistd.h>
#include <sys/time.h>
#include <time.h>
#include <stdio.h>
#include <inttypes.h>
#include <stdlib.h>
#include <inttypes.h>
#include <string.h>
#include <arpa/inet.h>
#include "corrspec.h"
#include "callback.h"
#include "search_glob.h"
#include <glob.h>
#include <Python.h>
#include <stdbool.h>
#include <signal.h>
#include <errno.h>
#include <sys/mman.h>
#define handle_error(msg) \
do { perror(msg); exit(EXIT_FAILURE); } while (0)
struct Spectrum spec[4];
PyObject *pName, *pModule;
PyObject *pFunc1, *pFunc2;
struct RefDestination
{
int scanID1;
int scanID2;
int scanID3;
int scanID4;
};
struct RefDestination find_REF_destination(glob_t glob_result, int)
{
struct RefDestination destination;
destination.scanID1 = 1;
destination.scanID2 = 2;
destination.scanID3 = 3;
destination.scanID4 = 4;
return destination;
}
static void handler(int sig, siginfo_t *si, void *unused)
{
printf("Got SIGSEGV at address: 0x%lx\n", (long) si->si_addr);
printf("Gracefully exiting\n");
sleep(3);
// close wenv connection to avoid many spawned Python.exe processes
Py_DECREF(pFunc1);
Py_DECREF(pFunc2);
Py_DECREF(pModule);
Py_DECREF(pName);
Py_Finalize();
// Send email notification via perl script if run > 1hr AND completed succesfully
//
}
int main(int argc, char **argv) {
int isREFHOT = 0;
struct RefDestination destination;
glob_t glob_result;
// Set up SIGSEGV handler
struct sigaction sa;
sa.sa_flags = SA_SIGINFO;
sigemptyset(&sa.sa_mask);
sa.sa_sigaction = handler;
if (sigaction(SIGSEGV, &sa, NULL) == -1)
handle_error("sigaction");
// Set up the Python C Extensions here (Used in callback() function in callback.c
putenv("PYTHONPATH=./");
// Initialize the Python interpreter
Py_Initialize();
// Build the name object for both functions from callQC.py file
pName = PyUnicode_FromString("callQC");
// Load the module object
pModule = PyImport_Import(pName);
// Get the two functions from the module
if (pModule != NULL){
pFunc1 = PyObject_GetAttrString(pModule, "relpower");
pFunc2 = PyObject_GetAttrString(pModule, "qc");
} else {
PyErr_Print();
fprintf(stderr, "Failed to load the Python module in main()\n");
}
// Setup all possible FFTW array lengths
printf("readying fft\n");
for(int i=0; i<4; i++){
int N=(i+1)*128;
spec[i].in = (fftw_complex *) fftw_malloc((4*N) * sizeof(fftw_complex));
spec[i].out = (fftw_complex *) fftw_malloc((4*N) * sizeof(fftw_complex));
spec[i].p = fftw_plan_dft_1d((4*N-1), spec[i].in, spec[i].out, FFTW_FORWARD, FFTW_PATIENT|FFTW_PRESERVE_INPUT);
}
fftw_import_system_wisdom();
printf("ready to start\n");
int unit = atoi(argv[1]);
int start_scanID = atoi(argv[2]);
int stop_scanID = atoi(argv[3]);
// Get a list of ALL data files in a directory. Only use for yes/no process and isREFHOT?
char *glob_filename = malloc(55*sizeof(char));
sprintf(glob_filename, "/home/young/Desktop/GUSTO-DATA/ACS%d_*_*_*.dat", unit);
//if(glob("ACS*_*_*_*.dat", GLOB_ERR, NULL, &glob_result) != 0){
//if(glob("/home/young/Desktop/GUSTO-DATA/ACS*_*_*_*.dat", GLOB_ERR, NULL, &glob_result) != 0){
if(glob(glob_filename, GLOB_ERR, NULL, &glob_result) != 0){
perror("Error in glob\n");
return 1;
}
char *filename = malloc(55*sizeof(char));
const char *prefix_names[]={"HOT", "OTF", "REF"};
// Make a list of files to process
for (int scanid=start_scanID; scanid<=stop_scanID; scanid++){
for(int subscan=0; subscan<100; subscan++){
for(int type=0; type<3; type++){
//sprintf(filename, "ACS%d_%s_%05d_%04d.dat", unit, prefix_names[type], scanid, subscan);
sprintf(filename, "/home/young/Desktop/GUSTO-DATA/ACS%d_%s_%05d_%04d.dat", unit, prefix_names[type], scanid, subscan);
//filename[23] = '\0';
filename[54] = '\0';
if (file_exists(filename)) {
// Process each file
printf("processing file: %s\n", filename);
// Is the file a REF HOT?
if ( search_glob_results(glob_result, scanid) && (type==0) ) // check scanID for "REF"
isREFHOT = 1;
printf("isREFHOT? %d\n", isREFHOT);
destination = find_REF_destination(glob_result, scanid);
// Send file to be processed
//callback(filename, isREFHOT, destination); // for REF and REFHOT, suggest fitsfile scanID
callback(filename, isREFHOT);
// reset vars
isREFHOT = 0;
}
}
}
}
// Clean up Python calls
Py_DECREF(pFunc1);
Py_DECREF(pFunc2);
Py_DECREF(pModule);
Py_DECREF(pName);
Py_Finalize();
// Send email notification via perl script if run > 1hr AND completed succesfully
return 0;
}