-
Notifications
You must be signed in to change notification settings - Fork 37
/
rfio.c
261 lines (217 loc) · 6.29 KB
/
rfio.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include "rftime.h"
#include "rfio.h"
#include "zscale.h"
struct spectrogram read_spectrogram(char *prefix,int isub,int nsub,double f0,double df0,int nbin,double foff)
{
int i,j,k,l,flag=0,status,msub,ibin,nadd,nbits=-32;
char filename[128],header[256],nfd[32];
FILE *file;
struct spectrogram s;
float *z,zavg,zstd;
char *cz;
int nch,j0,j1;
double freq,samp_rate;
float length;
int nchan,dummy;
float s1,s2;
// Open first file to get number of channels
sprintf(filename,"%s_%06d.bin",prefix,isub);
// Open file
file=fopen(filename,"r");
if (file==NULL) {
printf("%s does not exist\n",filename);
s.nsub=0;
return s;
}
// Read header
status=fread(header,sizeof(char),256,file);
if (strstr(header,"NBITS 8")==NULL) {
status=sscanf(header,"HEADER\nUTC_START %s\nFREQ %lf Hz\nBW %lf Hz\nLENGTH %f s\nNCHAN %d\nNSUB %d\n",s.nfd0,&s.freq,&s.samp_rate,&length,&nch,&msub);
} else {
status=sscanf(header,"HEADER\nUTC_START %s\nFREQ %lf Hz\nBW %lf Hz\nLENGTH %f s\nNCHAN %d\nNSUB %d\nNBITS 8\nMEAN %f\nRMS %f",s.nfd0,&s.freq,&s.samp_rate,&length,&nch,&msub,&zavg,&zstd);
nbits=8;
}
s.freq+=foff;
// Close file
fclose(file);
// Compute plotting channel
if (f0>0.0 && df0>0.0) {
s.nchan=(int) (df0/s.samp_rate*(float) nch);
j0=(int) ((f0-0.5*df0-s.freq+0.5*s.samp_rate)*(float) nch/s.samp_rate);
j1=(int) ((f0+0.5*df0-s.freq+0.5*s.samp_rate)*(float) nch/s.samp_rate);
if (j0<0 || j1>nch) {
fprintf(stderr,"Requested frequency range out of limits\n");
s.nsub=0;
s.nchan=0;
return s;
}
} else {
s.nchan=nch;
j0=0;
j1=s.nchan;
}
// Read whole file if not specified
if (nsub==0 && msub>0)
nsub=msub;
// Number of subints
s.nsub=nsub/nbin;
s.msub=msub;
s.isub=isub;
printf("Allocating %.2f MB of memory\n",(4* (float) s.nchan * (float) s.nsub)/(1024 * 1024));
// Allocate
s.z=(float *) malloc(sizeof(float)*s.nchan*s.nsub);
s.zavg=(float *) malloc(sizeof(float)*s.nsub);
s.zstd=(float *) malloc(sizeof(float)*s.nsub);
z=(float *) malloc(sizeof(float)*nch);
cz=(char *) malloc(sizeof(char)*nch);
s.mjd=(double *) malloc(sizeof(double)*s.nsub);
s.length=(float *) malloc(sizeof(float)*s.nsub);
// Initialize
for (j=0;j<s.nchan*s.nsub;j++)
s.z[j]=0.0;
for (j=0;j<s.nsub;j++)
s.mjd[j]=0.0;
// Loop over files
for (k=0,i=0,l=0,ibin=0,nadd=0;l<nsub;k++) {
// Generate filename
sprintf(filename,"%s_%06d.bin",prefix,k+isub);
// Open file
file=fopen(filename,"r");
if (file==NULL) {
printf("%s does not exist\n",filename);
break;
}
printf("opened %s\n",filename);
// Loop over contents of file
for (;l<nsub;l++,ibin++) {
// Read header
status=fread(header,sizeof(char),256,file);
if (status==0)
break;
if (nbits==-32)
status=sscanf(header,"HEADER\nUTC_START %s\nFREQ %lf Hz\nBW %lf Hz\nLENGTH %f s\nNCHAN %d\nNSUB %d\n",nfd,&freq,&samp_rate,&length,&nchan,&msub);
else if (nbits==8)
status=sscanf(header,"HEADER\nUTC_START %s\nFREQ %lf Hz\nBW %lf Hz\nLENGTH %f s\nNCHAN %d\nNSUB %d\nNBITS 8\nMEAN %f\nRMS %f",nfd,&freq,&samp_rate,&length,&nchan,&dummy,&zavg,&zstd);
s.mjd[i]+=nfd2mjd(nfd)+0.5*length/86400.0;
s.length[i]+=length;
nadd++;
// Read buffer
if (nbits==-32) {
status=fread(z,sizeof(float),nch,file);
} else if (nbits==8) {
status=fread(cz,sizeof(char),nch,file);
for (j=0;j<nch;j++)
z[j]=6.0/256.0*(float) cz[j]*zstd+zavg;
}
if (status==0)
break;
// Copy
for (j=0;j<s.nchan;j++)
s.z[i+s.nsub*j]+=z[j+j0];
// Increment
if (l%nbin==nbin-1) {
// Scale
s.mjd[i]/=(float) nadd;
for (j=0;j<s.nchan;j++)
s.z[i+s.nsub*j]/=(float) nadd;
ibin=0;
nadd=0;
i++;
}
}
// Close file
fclose(file);
}
// Scale last subint
if(nadd>0)
{
s.mjd[i]/=(float) nadd;
for (j=0;j<s.nchan;j++)
s.z[i+s.nsub*j]/=(float) nadd;
}
// Swap frequency range
if (f0>0.0 && df0>0.0) {
s.freq=f0;
s.samp_rate=df0;
}
// Compute averages
for (i=0;i<s.nsub;i++) {
s.zavg[i]=0.0;
for (j=0;j<s.nchan;j++)
if (!isnan(s.z[i+s.nsub*j]) && !isinf(s.z[i+s.nsub*j]))
s.zavg[i]+=s.z[i+s.nsub*j];
s.zavg[i]/=(float) s.nchan;
}
// Compute deviations
for (i=0;i<s.nsub;i++) {
s.zstd[i]=0.0;
for (j=0;j<s.nchan;j++)
if (!isnan(s.z[i+s.nsub*j]) && !isinf(s.z[i+s.nsub*j]))
s.zstd[i]+=pow(s.zavg[i]-s.z[i+s.nsub*j],2);
s.zstd[i]=sqrt(s.zstd[i]/(float) s.nchan);
}
// Compute limits
for (i=0;i<s.nsub;i++) {
if (i==0) {
s.zmin=s.zavg[i]-1.0*s.zstd[i];
s.zmax=s.zavg[i]+1.0*s.zstd[i];
} else {
if (s.zavg[i]-1.0*s.zstd[i]<s.zmin) s.zmin=s.zavg[i]-1.0*s.zstd[i];
if (s.zavg[i]+1.0*s.zstd[i]>s.zmax) s.zmax=s.zavg[i]+1.0*s.zstd[i];
}
}
double z1,z2;
zscale(&s, s.nsub, 0.25,&z1, &z2);
printf("z1 = %f, z2 = %f\n", z1, z2);
s.zmin = z1;
s.zmax = z2;
// Free
free(z);
free(cz);
return s;
}
void write_spectrogram(struct spectrogram s,char *prefix)
{
int i,j;
FILE *file;
char header[256]="",filename[256],nfd[32];
float *z;
double mjd;
// Allocate
z=(float *) malloc(sizeof(float)*s.nchan);
// Generate filename
sprintf(filename,"%s_%06d.bin",prefix,0);
// Open file
file=fopen(filename,"w");
// Loop over subints
for (i=0;i<s.nsub;i++) {
// Date
mjd=s.mjd[i]-0.5*s.length[i]/86400.0;
mjd2nfd(mjd,nfd);
// Generate header
sprintf(header,"HEADER\nUTC_START %s\nFREQ %lf Hz\nBW %lf Hz\nLENGTH %f s\nNCHAN %d\nEND\n",nfd,s.freq,s.samp_rate,s.length[i],s.nchan);
// Copy buffer
for (j=0;j<s.nchan;j++)
z[j]=s.z[i+s.nsub*j];
// Dump contents
fwrite(header,sizeof(char),256,file);
fwrite(z,sizeof(float),s.nchan,file);
}
// Close file
fclose(file);
// Free
free(z);
return;
}
void free_spectrogram(struct spectrogram s)
{
free(s.z);
free(s.zavg);
free(s.zstd);
free(s.mjd);
free(s.length);
}