-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathugmrtusb2fil.c
90 lines (70 loc) · 2.14 KB
/
ugmrtusb2fil.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
#include "filhead.h"
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
void reverse_channels(uint16_t *data, int nchan){
int i, j;
uint16_t tmp;
for(i=0; i<nchan/2; i++){
j = nchan-i-1;
tmp = data[i];
data[i] = data[j];
data[j] = tmp;
}
}
int main(int argc, char **argv){
char infilename[150], outfilename[150];
char jname[20];
double mjd, freq, bw, tsmpl;
int nchan;
long infile_size, Nsmpl, ismpl;
int data_size;
uint16_t *data;
if(argc != 9){
fprintf(stderr, "Invalid number of arguments.\n");
exit(1);
}
//infile, outfile, jname, mjd, freq, nchan, bw, tsmpl
strcpy(infilename, argv[1]);
strcpy(outfilename, argv[2]);
strcpy(jname, argv[3]);
mjd = atof(argv[4]);
freq = atof(argv[5]);
nchan = atoi(argv[6]);
bw = atof(argv[7]);
tsmpl = atof(argv[8]);
if(mjd==0 || freq<=0 || nchan<=0 || bw==0 || tsmpl<=0){
fprintf(stderr, "Invalid arguments found.\n");
exit(1);
}
FILE *infile = fopen(infilename, "rb");
if(infile==NULL){
fprintf(stderr, "Error opening file %s for reading.\n", infilename);
exit(1);
}
FILE *outfile = fopen(outfilename, "wb");
if(outfile==NULL){
fprintf(stderr, "Error opening file %s for writing.\n", outfilename);
exit(1);
}
data_size = sizeof(uint16_t)*nchan;
fseek(infile, 0, SEEK_END);
infile_size = ftell(infile);
fseek(infile, 0, SEEK_SET);
if((infile_size%data_size) != 0){
fprintf(stderr, "The input file size is incompatible with given nchan.\n");
exit(1);
}
Nsmpl = infile_size/data_size;
filterbank_header(outfile, infilename, jname, mjd, freq, bw, nchan, tsmpl);
data = (uint16_t*)malloc(data_size);
for(ismpl=0; ismpl<Nsmpl; ismpl++){
fread(data, sizeof(uint16_t), nchan, infile);
reverse_channels(data, nchan);
fwrite(data, sizeof(uint16_t), nchan, outfile);
}
free(data);
fclose(infile);
fclose(outfile);
return 0;
}