-
Notifications
You must be signed in to change notification settings - Fork 1
/
Load_Data.jl
224 lines (223 loc) · 9.9 KB
/
Load_Data.jl
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# Load Packages
using CSV, DataFrames, Dates, Statistics
# Functions
# Load data from TEROS input files
function loadteros(path::AbstractString)
Input_FN = readdir(path)
permute!(Input_FN,[3,4,5,6,7,8,9,10,11,1,2]) # need to reorder from 1 to 11
n = length(Input_FN) # this is the number of input files, useful later
data = DataFrame[]
[push!(data, CSV.read(joinpath(path, Input_FN[i]), DataFrame, dateformat="yyyy-mm-dd HH:MM:SS+00:00")) for i in 1:n]
return data
end;
# Download and load met data
function loadmet(path::AbstractString)
[download("http://www.atmos.anl.gov/ANLMET/numeric/2019/"*i*"19met.data", joinpath(path, i*"19met.data")) for i in ["nov", "dec"]];
[download("http://www.atmos.anl.gov/ANLMET/numeric/2020/"*i*"20met.data", joinpath(path, i*"20met.data")) for i in ["jan", "feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec"]];
col_name = [:DOM,:Month,:Year,:Time,:PSC,:WD60,:WS60,:WD_STD60,:T60,:WD10,:WS10,:WD_STD10,:T10,:DPT,:RH,:TD100,:Precip,:RS,:RN,:Pressure,:WatVapPress,:TS10,:TS100,:TS10F]
metdata = DataFrame[]
[push!(metdata, CSV.read(joinpath(path, i*"19met.data"), DataFrame, delim=' ', header=col_name, ignorerepeated=true, datarow=1, footerskip=2)) for i in ["nov", "dec"]]
[push!(metdata, CSV.read(joinpath(path, i*"20met.data"), DataFrame, delim=' ', header=col_name, ignorerepeated=true, datarow=1, footerskip=2)) for i in ["jan", "feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec"]]
metdata = reduce(vcat, metdata)
return metdata
end;
# Load metadata (position of TEROS sensors)
function loadmeta(path::AbstractString)
MD = CSV.read(path, DataFrame)
x = MD.x*12.5
y = MD.y*12.5
x = x[1:end .!= 35]; x = x[1:end .!= 35]
y = y[1:end .!= 35]; y = y[1:end .!= 35]
x = convert(Array{Float64,1}, x)
y = convert(Array{Float64,1}, y)
return x, y
end;
# Rearrange SWC and Tsoil
function loadSWC(data::Array{DataFrame,1}, Dtime::Array{DateTime,1})
m = length(Dtime); n = 11 # 11 files, for 11 ZL6 datalogger
nextit = 0:5:5*n-1
SWC = Array{Union{Float64,Missing}}(missing, m, 66)
for j = 1:11 # For all files
for i = 1:m
k = nextit[j]
t = findfirst(x -> x == Dtime[i], data[j].datetime)
if isnothing(t) == false
SWC[i,j+k] = data[j].value[t]
SWC[i,j+1+k] = data[j].value[t+2]
SWC[i,j+2+k] = data[j].value[t+4]
SWC[i,j+3+k] = data[j].value[t+6]
SWC[i,j+4+k] = data[j].value[t+8]
SWC[i,j+5+k] = data[j].value[t+10]
end
end
end
SWC = replace(SWC, 0.0=>missing)
SWC = SWC[:, 1:size(SWC, 2) .!= 35]; SWC = SWC[:, 1:size(SWC, 2) .!= 35] # Need to find how to delete 35 and 36 simultaneously
return SWC
end;
function loadTsoil(data::Array{DataFrame,1}, Dtime::Array{DateTime,1})
m = length(Dtime); n = 11
Tsoil = Array{Union{Float64,Missing}}(missing, m, 66)
nextit = 0:5:5*n-1
for j = 1:11 # For all files
for i = 1:m
k = nextit[j]
t = findfirst(x -> x == Dtime[i], data[j].datetime)
if isnothing(t) == false
Tsoil[i,j+k] = data[j].value[t+1]
Tsoil[i,j+1+k] = data[j].value[t+3]
Tsoil[i,j+2+k] = data[j].value[t+5]
Tsoil[i,j+3+k] = data[j].value[t+7]
Tsoil[i,j+4+k] = data[j].value[t+9]
Tsoil[i,j+5+k] = data[j].value[t+11]
end
end
end
Tsoil = replace(Tsoil, 0.0=>missing)
Tsoil = Tsoil[:, 1:size(Tsoil, 2) .!= 35]; Tsoil = Tsoil[:, 1:size(Tsoil, 2) .!= 35] # Need to find how to delete 35 and 36 simultaneously
return Tsoil
end;
# Create a DateTime vector from metdata Month, Year and Time
# First, need 4-digits Array for Time
function loadDtimemet(metdata::DataFrame)
met_n = size(metdata, 1)
metdata_time_str = Array{String}(undef, met_n)
for i = 1:met_n
if length(string(metdata.Time[i])) == 2 # if only 2 numbers
metdata_time_str[i] = "00$(metdata.Time[i])"
elseif length(string(metdata.Time[i])) == 3 # if only 3 numbers
metdata_time_str[i] = "0$(metdata.Time[i])"
elseif length(string(metdata.Time[i])) == 4 # 4 numbers
metdata_time_str[i] = string(metdata.Time[i])
end
end
# Then, we can use day of month, month, year and time
Dtime_met = Array{DateTime}(undef, met_n)
for i = 1:met_n
Dtime_met[i] = DateTime(metdata.Year[i]+2000,metdata.Month[i],metdata.DOM[i],parse(Int64,metdata_time_str[i][1:2]),parse(Int64,metdata_time_str[i][3:4]))
end
return Dtime_met
end;
# Integrate daily Precip
# I need to redo this... this is not clean. Maybe fixing latest rain on ANLMET website should be done first.
function PrecipD(metdata::DataFrame, Dtime_met::Array{DateTime,1})
Precip_d = Array{Float64}(undef, 427)
Dtime_met_d = Array{DateTime}(undef, 427)
for i = 1:30
use = findall(x -> Dates.year(x) == 2019 && Dates.day(x) == i && Dates.month(x) == 11, Dtime_met)
Precip_d[i] = sum(metdata.Precip[use])
Dtime_met_d[i] = Date(DateTime(2019,11,i))
end
for i = 1:31
use = findall(x -> Dates.year(x) == 2019 && Dates.day(x) == i && Dates.month(x) == 12, Dtime_met)
Precip_d[30 + i] = sum(metdata.Precip[use])
Dtime_met_d[30 + i] = Date(DateTime(2019,12, i))
end
for i = 1:31
use = findall(x -> Dates.year(x) == 2020 && Dates.day(x) == i && Dates.month(x) == 1, Dtime_met)
Precip_d[61 + i] = sum(metdata.Precip[use])
Dtime_met_d[61 + i] = Date(DateTime(2020, 1, i))
end
for i = 1:29
use = findall(x -> Dates.year(x) == 2020 && Dates.day(x) == i && Dates.month(x) == 2, Dtime_met)
Precip_d[92 + i] = sum(metdata.Precip[use])
Dtime_met_d[92 + i] = Date(DateTime(2020, 2, i))
end
for i = 1:31
use = findall(x -> Dates.year(x) == 2020 && Dates.day(x) == i && Dates.month(x) == 3, Dtime_met)
Precip_d[121 + i] = sum(metdata.Precip[use])
Dtime_met_d[121 + i] = Date(DateTime(2020, 3, i))
end
for i = 1:30
use = findall(x -> Dates.year(x) == 2020 && Dates.day(x) == i && Dates.month(x) == 4, Dtime_met)
Precip_d[152 + i] = sum(metdata.Precip[use])
Dtime_met_d[152 + i] = Date(DateTime(2020, 4, i))
end
for i = 1:31
use = findall(x -> Dates.year(x) == 2020 && Dates.day(x) == i && Dates.month(x) == 5, Dtime_met)
Precip_d[182 + i] = sum(metdata.Precip[use])
Dtime_met_d[182 + i] = Date(DateTime(2020, 5, i))
end
for i = 1:30
use = findall(x -> Dates.year(x) == 2020 && Dates.day(x) == i && Dates.month(x) == 6, Dtime_met)
Precip_d[213 + i] = sum(metdata.Precip[use])
Dtime_met_d[213 + i] = Date(DateTime(2020, 6, i))
end
for i = 1:31
use = findall(x -> Dates.year(x) == 2020 && Dates.day(x) == i && Dates.month(x) == 7, Dtime_met)
Precip_d[243 + i] = sum(metdata.Precip[use])
Dtime_met_d[243 + i] = Date(DateTime(2020, 7, i))
end
for i = 1:31
use = findall(x -> Dates.year(x) == 2020 && Dates.day(x) == i && Dates.month(x) == 8, Dtime_met)
Precip_d[274 + i] = sum(metdata.Precip[use])
Dtime_met_d[274 + i] = Date(DateTime(2020, 8, i))
end
for i = 1:30
use = findall(x -> Dates.year(x) == 2020 && Dates.day(x) == i && Dates.month(x) == 9, Dtime_met)
Precip_d[305 + i] = sum(metdata.Precip[use])
Dtime_met_d[305 + i] = Date(DateTime(2020, 9, i))
end
for i = 1:31
use = findall(x -> Dates.year(x) == 2020 && Dates.day(x) == i && Dates.month(x) == 10, Dtime_met)
Precip_d[335 + i] = sum(metdata.Precip[use])
Dtime_met_d[335 + i] = Date(DateTime(2020, 10, i))
end
for i = 1:30
use = findall(x -> Dates.year(x) == 2020 && Dates.day(x) == i && Dates.month(x) == 11, Dtime_met)
Precip_d[366 + i] = sum(metdata.Precip[use])
Dtime_met_d[366 + i] = Date(DateTime(2020, 11, i))
end
for i = 1:31
use = findall(x -> Dates.year(x) == 2020 && Dates.day(x) == i && Dates.month(x) == 12, Dtime_met)
Precip_d[396 + i] = sum(metdata.Precip[use])
Dtime_met_d[396 + i] = Date(DateTime(2020, 12, i))
end
return Precip_d, Dtime_met_d
end;
function dailyval(X::Array{Union{Missing, Float64},2}, Dtime_all::Array{Date,1})
n_all = length(Dtime_all)
X_daily = [X[15+(i-1)*48, :] for i in 1:n_all]
X_daily = reduce(vcat, adjoint.(X_daily))
X_daily_mean = [mean(skipmissing(X_daily[i,:])) for i = 1:n_all]
X_daily_std = [std(skipmissing(X_daily[i,:])) for i = 1:n_all]
return X_daily, X_daily_mean, X_daily_std
end;
function Precipdaily(Precip_d::Array{Float64,1}, Dtime_all::Array{Date,1}, Dtime_met_d::Array{DateTime,1})
n_all = length(Dtime_all)
Precip_daily = Array{Float64}(undef, n_all)
for i = 1:n_all-1 # up to day before today, in case it's before noon
t = findfirst(x -> x == Dtime_all[i], Dtime_met_d)
if isnothing(t) == false
Precip_daily[i] = Precip_d[t]
end
end
Precip_daily[Precip_daily.>=50] .= 0 # Delete Precip outliers, daily rain > 50 mm which may be calibration day... this should be fixed by Evan in the qc data!!
return Precip_daily
end;
function loadmanuals(path::AbstractString)
inputs = readdir(path)
n = length(inputs)
dataRSM = DataFrame[]
[push!(dataRSM, CSV.read(joinpath(path, inputs[i]), DataFrame, dateformat="yyyy-mm-dd HH:MM:SS", missingstring = "missing")) for i in 1:n]
RSMmean = Array{Float64}(undef,0)
RSMstd = Array{Float64}(undef,0)
[push!(RSMmean, mean(skipmissing(dataRSM[i].Exp_Flux))) for i = 1:n]
[push!(RSMstd, std(skipmissing(dataRSM[i].Exp_Flux))) for i = 1:n]
return dataRSM, RSMmean, RSMstd
end;
function loadauto(path::AbstractString)
inputs = readdir(path)
dataRSA = DataFrame(CSV.File(joinpath(path, inputs[1]), dateformat="yyyy-mm-dd HH:MM:SS"))
select!(dataRSA, Not(:Column9)) # delete last column of missing
dataRSA = dropmissing(dataRSA) # delete rows that are messed up due to instrument errors
RSAmean = Array{Float64}(undef,0)
RSAstd = Array{Float64}(undef,0)
Date_Auto = Date(2020,5,26):Day(1):Date(2020,11,13);
m = length(Date_Auto)
# Need mean and std for each day, for now
[push!(RSAmean, mean(skipmissing(dataRSA.Exp_Flux[findall(x -> x == Date_Auto[j], Date.(dataRSA.Date_IV))]))) for j in 1:m]
[push!(RSAstd, std(skipmissing(dataRSA.Exp_Flux[findall(x -> x == Date_Auto[j], Date.(dataRSA.Date_IV))]))) for j in 1:m]
return dataRSA, RSAmean, RSAstd, Date_Auto
end;
# Example of grabbing data in MakieLayout_data_2D.jl