This repository has been archived by the owner on Oct 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreaddata.c
executable file
·116 lines (95 loc) · 3.36 KB
/
readdata.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
/*
* readdata.c
*
* David Garen 6/92
*
* Read input data for DK program
*
* Modified for Version 4.7 by adding variable mtper (to replace 366)
* and removing dayfrac
*/
#include <malloc/malloc.h>
#include <stdio.h>
#include <string.h>
#include "dk_x.h"
void readdata()
{
double atof(); /* ascii-to-float function */
char buf[4]; /* buffer for parsing lat. and lng. */
char clat[7]; /* character version of latitude */
char clng[8]; /* character version of longitude */
float decmin; /* decimal minutes */
float decsec; /* decimal seconds */
int i, j, k; /* loop indexes */
int iyear; /* year -- temporary variable for reading */
int len; /* string length */
/* Read number of stations, number of years, first and last days for each year */
/* (Delete day fraction) */
fscanf(fpin1, "%d%d", &nsta, &nyear);
year = ivector(nyear);
firstday = ivector(nyear);
lastday = ivector(nyear);
for (k = 0; k < nyear; k++)
fscanf(fpin1, "%d%d", &firstday[k], &lastday[k]);
/* fscanf(fpin1, "%d", &dayfrac); */
/* Read station i.d., elevation, northing (or latitude),
and easting (or longitude), and allocate array space for data */
for (i = 0; i < nsta; i++) {
if (icoord == 1)
fscanf(fpin1, "%s%f%s%s", (char*) &sta[i].id, &sta[i].elev, clat, clng);
else
fscanf(fpin1, "%s%f%f%f", (char*) &sta[i].id, &sta[i].elev, &sta[i].north,
&sta[i].east);
sta[i].elev /= 1000;
sta[i].data = matrix(mtper, nyear);
for (j = 0; j < mtper; j++)
for (k = 0; k < nyear; k++)
sta[i].data[j][k] = missing;
if (icoord == 1) {
/* Convert latitude into decimal degrees */
len = strlen(clat);
strncpy(buf, &clat[len-2], 2);
buf[2] = '\0';
decsec = (float) (atof(buf) / 3600);
strncpy(buf, &clat[len-4], 2);
buf[2] = '\0';
decmin = (float) (atof(buf) / 60);
strncpy(buf, clat, len-4);
buf[len-4] = '\0';
sta[i].north = (float) (atof(buf) + decmin + decsec);
/* Convert longitude into decimal degrees */
len = strlen(clng);
strncpy(buf, &clng[len-2], 2);
buf[2] = '\0';
decsec = (float) (atof(buf) / 3600);
strncpy(buf, &clng[len-4], 2);
buf[2] = '\0';
decmin = (float) (atof(buf) / 60);
strncpy(buf, clng, len-4);
buf[len-4] = '\0';
sta[i].east = (float) (atof(buf) + decmin + decsec);
}
}
/* Read column format data */
k = 0;
fscanf(fpin1, "%d%d", &year[k], &j);
j--;
for (i = 0; i < nsta; i++)
fscanf(fpin1, "%f", &sta[i].data[j][k]);
while (1) {
if (fscanf(fpin1, "%d%d", &iyear, &j) == EOF)
break;
if (iyear != year[k])
year[++k] = iyear;
j--;
for (i = 0; i < nsta; i++)
fscanf(fpin1, "%f", &sta[i].data[j][k]);
}
fclose(fpin1);
/* Experiment -- scaling factor
for (i = 0; i < nsta; i++)
for (j = 0; j < mtper; j++)
for (k = 0; k < nyear; k++)
sta[i].data[j][k] *= 100.;
End experiment */
}