forked from jhamman/DHSVM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReadMetRecord.c
executable file
·146 lines (130 loc) · 4 KB
/
ReadMetRecord.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
145
146
/*
* SUMMARY: ReadMetRecord.c - Read station meteorological data
* USAGE: Part of DHSVM
* AUTHOR: Bart Nijssen
* ORG: University of Washington, Department of Civil Engineering
* E-MAIL: [email protected]
* ORIG-DATE: Apr-96
* DESCRIPTION: Read station meteorological data
* DESCRIP-END.
* FUNCTIONS: ReadMetRecord()
* COMMENTS:
* $Id: ReadMetRecord.c,v 1.4 2003/07/01 21:26:22 olivier Exp $
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "settings.h"
#include "data.h"
#include "DHSVMerror.h"
#include "functions.h"
#include "constants.h"
#define MAXMETVARS 21 /* Maximum Number of meteorological variables
to read. Hack to be replaced by something
better */
/*****************************************************************************
ReadMetRecord()
*****************************************************************************/
void ReadMetRecord(OPTIONSTRUCT * Options, DATE * Current, int NSoilLayers,
FILES * InFile, unsigned char IsWindModelLocation,
MET * MetRecord)
{
DATE MetDate; /* Date of meteorological record */
float Array[MAXMETVARS]; /* Temporary storage of met variables */
int i;
int NMetVars; /* Number of meteorological variables to
read */
NMetVars = 5;
/* these are - in order:
air temp,
wind,
humidity
shortwave (total i.e. direct+diffuse)
longwave */
if (Options->HeatFlux == TRUE)
NMetVars += NSoilLayers;
/* expect to see temperature for each soil layer */
if (Options->PrecipType == STATION)
NMetVars++;
if (Options->PrecipLapse == VARIABLE)
NMetVars++;
if (Options->TempLapse == VARIABLE)
NMetVars++;
if (IsWindModelLocation)
NMetVars++;
if (!ScanDate(InFile->FilePtr, &MetDate))
ReportError(InFile->FileName, 23);
while (!IsEqualTime(&MetDate, Current) && !feof(InFile->FilePtr)) {
if (ScanFloats(InFile->FilePtr, Array, NMetVars) != NMetVars)
ReportError(InFile->FileName, 5);
if (!ScanDate(InFile->FilePtr, &MetDate))
ReportError(InFile->FileName, 23);
}
if (!IsEqualTime(&MetDate, Current)) {
if (DEBUG) {
printf("Metfile: ");
PrintDate(&MetDate, stdout);
printf("Current: ");
PrintDate(Current, stdout);
}
ReportError(InFile->FileName, 28);
}
if (ScanFloats(InFile->FilePtr, Array, NMetVars) != NMetVars)
ReportError(InFile->FileName, 5);
MetRecord->Tair = Array[0];
MetRecord->Wind = Array[1];
MetRecord->Rh = Array[2];
if (MetRecord->Rh < 0.0 || MetRecord->Rh > 100.0) {
printf("warning: RH out of bounds: %s\n", InFile->FileName);
if (MetRecord->Rh < 0.0)
MetRecord->Rh = 0.0;
if (MetRecord->Rh > 100.0)
MetRecord->Rh = 100.0;
}
MetRecord->Sin = Array[3];
if (MetRecord->Sin > 1380.0) {
printf("warning: Shortwave out of bounds: %s\n", InFile->FileName);
MetRecord->Sin = 1380.0;
}
if (MetRecord->Sin < 0.0) {
printf("Warning: Negative Shortwave, setting to zero: %s\n",
InFile->FileName);
MetRecord->Sin = 0.0;
}
MetRecord->Lin = Array[4];
if (MetRecord->Lin < 0.0 || MetRecord->Lin > 1800.0) {
printf("warning: Longwave out of bounds: %s\n", InFile->FileName);
}
i = 0;
if (Options->HeatFlux == TRUE)
for (i = 0; i < NSoilLayers; i++)
MetRecord->Tsoil[i] = Array[5 + i];
if (Options->PrecipType == STATION) {
MetRecord->Precip = Array[5 + i];
if (MetRecord->Precip < 0) {
printf("Warning: negative precip %s \n", InFile->FileName);
MetRecord->Precip = 0.0;
}
i++;
}
else
MetRecord->Precip = 0.0;
if (Options->PrecipLapse == VARIABLE) {
MetRecord->PrecipLapse = Array[5 + i];
i++;
}
else
MetRecord->PrecipLapse = PRECIPLAPSE;
if (Options->TempLapse == VARIABLE) {
MetRecord->TempLapse = Array[5 + i];
i++;
}
else
MetRecord->TempLapse = TEMPLAPSE;
if (IsWindModelLocation) {
MetRecord->WindDirection = (int) Array[5 + i];
i++;
}
else
MetRecord->WindDirection = NOT_APPLICABLE;
}