This repository has been archived by the owner on Aug 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsu.c
112 lines (98 loc) · 2.32 KB
/
su.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
/*
* Copyright 2014-2015 Ian Liu Rodrigues <[email protected]>
*
* This file is part of CMP.
*
* CMP is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* CMP is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with CMP. If not, see <http://www.gnu.org/licenses/>.
*/
#include "su.h"
#include <stdlib.h>
#include <unistd.h>
#include <math.h>
static float get_scalco(su_trace_t *tr)
{
if (tr->scalco == 0)
return 1;
if (tr->scalco > 0)
return tr->scalco;
return 1.0f / tr->scalco;
}
void su_init(su_trace_t *tr)
{
if (tr->ns <= 0)
return;
tr->data = malloc(sizeof(float) * tr->ns);
}
void su_free(su_trace_t *tr)
{
free(tr->data);
}
int su_fgettr(FILE *file, su_trace_t *tr)
{
size_t size = fread(tr, SU_HEADER_SIZE, 1, file);
if (size < 1)
return 0;
tr->data = malloc(tr->ns * sizeof(float));
size = fread(tr->data, sizeof(float), tr->ns, file);
if (size < tr->ns) {
su_free(tr);
return 0;
}
return 1;
}
int su_gettr(su_trace_t *tr)
{
return su_fgettr(stdin, tr);
}
int su_fputtr(FILE *file, su_trace_t *tr)
{
size_t size = SU_HEADER_SIZE;
if (fwrite(tr, size, 1, file) < 1)
return 0;
if (fwrite(tr->data, sizeof(float), tr->ns, file) < tr->ns)
return 0;
return 1;
}
int su_puttr(su_trace_t *tr)
{
return su_fputtr(stdout, tr);
}
int su_get_cdp(su_trace_t *tr)
{
return tr->cdp;
}
void su_get_source(su_trace_t *tr, float *sx, float *sy)
{
float s = get_scalco(tr);
*sx = s * tr->sx;
*sy = s * tr->sy;
}
void su_get_receiver(su_trace_t *tr, float *gx, float *gy)
{
float s = get_scalco(tr);
*gx = s * tr->gx;
*gy = s * tr->gy;
}
void su_get_midpoint(su_trace_t *tr, float *mx, float *my)
{
float s = get_scalco(tr);
*mx = s * (tr->gx + tr->sx) * 0.5;
*my = s * (tr->gy + tr->sy) * 0.5;
}
void su_get_halfoffset(su_trace_t *tr, float *hx, float *hy)
{
float s = get_scalco(tr);
*hx = s * (tr->gx - tr->sx) * 0.5;
*hy = s * (tr->gy - tr->sy) * 0.5;
}