forked from kettner/hydrotrend
-
Notifications
You must be signed in to change notification settings - Fork 1
/
hydroreadearthquake.c
145 lines (132 loc) · 5.15 KB
/
hydroreadearthquake.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
/*-------------------------------------------------------------------------------------------
* hydroreadearthquake.c
*
* Author: Albert Kettner, September 2011
*
* Reads digitized hypsometeric integral data from file.
*
* Variable Def.Location Type Units Usage
* -------- ------------ ---- ----- -----
*
*-------------------------------------------------------------------------------------------*/
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "hydroinout.h"
#include "hydroparams.h" /* includes <stdio.h> */
#include "hydroalloc_mem.h"
/*----------------------------
* Start of HydroReadHypsom
*----------------------------*/
int hydroreadearthquake (char* path, char* in_file_prefix){
/*-------------------
* Local Variables
*-------------------*/
double binerror;
int dumint, err, kk, k, ep;
char chs[220], dummystring[100];
/*------------------------
* Initialize Variables
*------------------------*/
err = 0;
/*------------------
* Alocate memory
*------------------*/
fidquake = allocate_1d_F (nepochs);
earthquakedatafile = (int *) malloc ((nepochs) * sizeof (int));
quakeeventcounter = (int *) malloc ((nepochs) * sizeof (int));
if (!quakeeventcounter){
perror ("HYDROTREND ERROR; hydroreadearthquake.c\n");
exit (-1);
}
quakeeventyear = (int **) malloc ((nepochs) * sizeof (int *));
if (!quakeeventyear){
perror ("HYDROTREND ERROR; hydroreadearthquake.c\n");
exit (-1);
}
quakeeventenergy = (double **) malloc ((nepochs) * sizeof (double *));
if (!quakeeventenergy){
perror ("HYDROTREND ERROR; hydroreadearthquake.c\n");
exit (-1);
}
quakeeventdistance = (double **) malloc ((nepochs) * sizeof (double *));
if (!quakeeventdistance){
perror ("HYDROTREND ERROR; hydroreadearthquake.c\n");
exit (-1);
}
quakeeventduration = (double **) malloc ((nepochs) * sizeof (double *));
if (!quakeeventduration){
perror ("HYDROTREND ERROR; hydroreadearthquake.c\n");
exit (-1);
}
/*--------------------------------------------
* Read the earthquake data for each epoch
*--------------------------------------------*/
for (ep = 0; ep < nepochs; ep++){
/*-----------------------
* Open the input file
*-----------------------*/
sprintf (dummystring, "%s/%s%d", path, in_file_prefix, ep);
//sprintf (dummystring, "%s/%s%d", path, fnamehyps, ep);
strcpy (ffnamequake, dummystring);
strcat (ffnamequake, fnamequakeext);
if ((fidquake[ep] = fopen (ffnamequake, "r")) == NULL){
fprintf (stderr, " openfiles WARNING: Unable to open the earthquake data file %s \n", ffnamequake);
fprintf (stderr, " Hydrotrend will run without the earthquake routine for epoch %d\n", ep);
earthquakedatafile[ep] = 0;
}
else
earthquakedatafile[ep] = 1;
if (earthquakedatafile[ep] == 1){
/*------------------------
* Take off file header
*------------------------*/
for (k = 0; k < 5; k++)
fgets (chs, 220, fidquake[ep]);
fgets (chs, 220, fidquake[ep]);
dumint = sscanf (chs, "%d\n", &quakeeventcounter[ep]);
if (dumint == 1){
quakeeventyear[ep] = (int *) malloc ((quakeeventcounter[ep]) * sizeof (int));
if (!quakeeventyear[ep]){
perror ("HYDROTREND ERROR; hydroreadearthquake.c\n");
exit (-1);
}
quakeeventenergy[ep] = (double *) malloc ((quakeeventcounter[ep]) * sizeof (double));
if (!quakeeventenergy[ep]){
perror ("HYDROTREND ERROR; hydroreadearthquake.c\n");
exit (-1);
}
quakeeventdistance[ep] = (double *) malloc ((quakeeventcounter[ep]) * sizeof (double));
if (!quakeeventdistance[ep]){
perror ("HYDROTREND ERROR; hydroreadearthquake.c\n");
exit (-1);
}
quakeeventduration[ep] = (double *) malloc ((quakeeventcounter[ep]) * sizeof (double));
if (!quakeeventduration[ep]){
perror ("HYDROTREND ERROR; hydroreadearthquake.c\n");
exit (-1);
}
for (kk = 0; kk < quakeeventcounter[ep]; kk++){
fgets (chs, 220, fidquake[ep]);
dumint = sscanf (chs, "%d %lf %lf %lf\n", &quakeeventyear[ep][kk], &quakeeventenergy[ep][kk], &quakeeventdistance[ep][kk], &quakeeventduration[ep][kk]);
if (dumint != 4){
printf ("hydrotrend error: hydroreadearthquake.c; %d, %d, %lf, %lf, %lf\n", dumint, quakeeventyear[ep][kk], quakeeventenergy[ep][kk], quakeeventdistance[ep][kk], quakeeventduration[ep][kk]);
exit (-1);
}
}
fgets (chs, 220, fidquake[ep]); /*not sure if we need this here*/
}
else if (dumint > 1){
fprintf (stderr, "Hydrotrend earthquake error in file %s \n",ffnamequake);
fprintf (stderr, " Expected a single number, expressing the number of earthquake events; not %d values", dumint);
fprintf (stderr, " Program aborted \n");
exit (-1);
}
/*------------------
* Close the file
*------------------*/
fclose (fidquake[ep]);
}
}
return (err);
} /* end of HydroReadearthquake */