-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdumpcvm.c
More file actions
153 lines (124 loc) · 3.94 KB
/
dumpcvm.c
File metadata and controls
153 lines (124 loc) · 3.94 KB
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
/**
* dumpcvm.c: Dump the content of a cvm database to a binary flat file in
* ascending locational code order.
*
* Copyright (c) 2005 Tiankai Tu
* All rights reserved. May not be used, modified, or copied
* without permission.
*
* Contact:
* Tiankai Tu
* Computer Science Department
* Carnegie Mellon University
* 5000 Forbes Avenue
* Pittsburgh, PA 15213
* tutk@cs.cmu.edu
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include "etree.h"
#include "xplatform.h"
#include "cvm.h"
#include "code.h"
#define KEYSIZE 13
int main(int argc, char **argv)
{
char *cvmetree, *outputfile, *outformat;
FILE *outfp;
etree_t *cvmEp;
cvmpayload_t rawElem;
int64_t totalcount;
struct timeval starttime, endtime;
int scantime;
endian_t myformat, targetformat;
float outVp, outVs, outrho;
int outi, outj, outk;
if (argc != 4) {
printf("\nusage: dumpcvm cvmetree output format\n");
printf("cvmetree: pathname to the CVM etree\n");
printf("output: pathname to the flat output file\n");
printf("format: little or big\n");
exit(1);
}
cvmetree = argv[1];
cvmEp = etree_open(cvmetree, O_RDONLY, 0, 0, 0);
if (!cvmEp) {
fprintf(stderr, "Cannot open CVM material database %s\n", cvmetree);
exit(1);
}
outputfile = argv[2];
outfp = fopen(outputfile, "w+");
if (outfp == NULL) {
perror("fopen");
exit(1);
}
outformat = argv[3];
if (strcmp(outformat, "little") == 0) {
targetformat = little;
} else if (strcmp(outformat, "big") == 0) {
targetformat = big;
} else {
fprintf(stderr, "Unknown target data format\n");
exit(1);
}
myformat = xplatform_testendian();
/* go through all the records stored in the underlying btree */
gettimeofday(&starttime, NULL);
totalcount = 0;
memset(cvmEp->key, 0, KEYSIZE);
if (btree_initcursor(cvmEp->bp, cvmEp->key) != 0) {
fprintf(stderr, "Cannot set cursor in the underlying database\n");
exit(1);
}
do {
etree_tick_t i, j, k;
if (btree_getcursor(cvmEp->bp, cvmEp->hitkey, "*", &rawElem) != 0) {
fprintf(stderr, "Read cursor error\n");
exit(1);
}
code_morton2coord(ETREE_MAXLEVEL + 1, (char *)cvmEp->hitkey + 1,
&i, &j, &k);
/* Write to the output file, do format conversion if necessary */
if (myformat == targetformat) {
outi = i;
outj = j;
outk = k;
outVp = rawElem.Vp;
outVs = rawElem.Vs;
outrho = rawElem.rho;
} else {
xplatform_swapbytes(&outi, &i, 4);
xplatform_swapbytes(&outj, &j, 4);
xplatform_swapbytes(&outk, &k, 4);
xplatform_swapbytes(&outVp, &rawElem.Vp, 4);
xplatform_swapbytes(&outVs, &rawElem.Vs, 4);
xplatform_swapbytes(&outrho, &rawElem.rho, 4);
}
if ((fwrite(&outi, 4, 1, outfp) != 1) ||
(fwrite(&outj, 4, 1, outfp) != 1) ||
(fwrite(&outk, 4, 1, outfp) != 1) ||
(fwrite(&outVp, 4, 1, outfp) != 1) ||
(fwrite(&outVs, 4, 1, outfp) != 1) ||
(fwrite(&outrho, 4, 1, outfp) != 1)) {
fprintf(stderr, "Error writing CVM record\n");
perror("fwrite");
exit(1);
}
totalcount++;
} while (btree_advcursor(cvmEp->bp) == 0);
if (fclose(outfp) != 0) {
perror("fclose");
exit(1);
}
etree_close(cvmEp);
gettimeofday(&endtime, NULL);
scantime = (endtime.tv_sec - starttime.tv_sec);
printf("Dump the CVM database in %d seconds\n", scantime);
printf("Dump %qd octants\n", totalcount);
printf("Output format is %s-endian", outformat);
printf("Output file is %s\n", outputfile);
return 0;
}