-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreadGHCN.R
153 lines (125 loc) · 5.49 KB
/
readGHCN.R
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
readGHCN <- function() {
baseDirname = "C:/data/GHCNv3"
baseDataFilestr = "C:/data/GHCNv3/baseDataGHCN.RData"
filenameDataRaw = "ghcnm.tavg.v3.2.2.20141219.qcu.dat"
filenameMetaRaw = "ghcnm.tavg.v3.2.2.20141219.qcu.inv"
filenameDataHom = "ghcnm.tavg.v3.2.2.20141219.qca.dat"
filenameMetaHom = "ghcnm.tavg.v3.2.2.20141219.qca.inv"
filenameRef = "ghcnmv2.corr.20141219.tavg"
outputMonthly = FALSE
outputAnnual = TRUE
print("Start")
# Read files indicating which reference stations were used to homogenize a certain candidate
dirFilename = paste(baseDirname, filenameRef, sep="/")
references = readRef(dirFilename)
# Read meta data files (inventory)
dirFilename = paste(baseDirname, filenameMetaRaw, sep="/")
metaRaw = readMeta(dirFilename)
dirFilename = paste(baseDirname, filenameMetaHom, sep="/")
metaHom = readMeta(dirFilename)
a=0
# Read data files
dirFilename = paste(baseDirname, filenameDataRaw, sep="/")
dataRaw = readData(dirFilename)
dirFilename = paste(baseDirname, filenameDataHom, sep="/")
dataHom = readData(dirFilename)
a=0
# Combine datasets for which there is a station in dataHom
uniqueStat = unique(dataHom$stationNo) # Should not be necessary, but just to be sure.
noStations = length(uniqueStat)
minYear = min(dataHom$year)
maxYear = max(dataHom$year)
year = minYear:maxYear
noYears = length(year)
if(file.exists(baseDataFilestr)==TRUE) {
load(baseDataFilestr)
iBegin = iStat+1
# noStations = length(stationNo)
# noYears = length(year)
} else {
iBegin = 1
stationNo = uniqueStat
annualRaw = matrix(NA, noStations, noYears)
annualHom = matrix(NA, noStations, noYears)
refStations = matrix("", noStations, 39)
lat = array(NA, noStations)
lon = array(NA, noStations)
# monthlyRaw = array(NA, c(noStations, noYears, 12))
# DMFLagRaw = array("", c(noStations, noYears, 12))
# QCFlagRaw = array("", c(noStations, noYears, 12))
# DSFlagRaw = array("", c(noStations, noYears, 12))
# monthlyHom = array(NA, c(noStations, noYears, 12))
# DMFLagHom = array("", c(noStations, noYears, 12))
# QCFlagHom = array("", c(noStations, noYears, 12))
# DSFlagHom = array("", c(noStations, noYears, 12))
# Only use temp values that are not flagged
index = which(dataHom$QCFlag!=" ")
dataHom$temp[index] = NA
index = which(dataRaw$QCFlag!=" ")
dataRaw$temp[index] = NA
} # If baseDateFile is present
# iBegin = 1
# noStations=10
#
for(iStat in iBegin:noStations) {
print(paste(iStat, noStations), quote=FALSE)
indexStat = which(dataRaw$stationNo==stationNo[iStat])
yearRawStat = dataRaw$year[indexStat]
tempRawStat = dataRaw$temp[indexStat,]
indexStat = which(dataHom$stationNo==stationNo[iStat])
yearHomStat = dataHom$year[indexStat]
tempHomStat = dataHom$temp[indexStat,]
monthlyRawStat = matrix(NA, noYears, 12) # A matrix for the data for one station
monthlyHomStat = matrix(NA, noYears, 12)
for(iYear in 1:noYears) {
# index = which(dataRaw$stationNo==stationNo[iStat] & dataRaw$year==year[iYear])
# index = which(dataRaw$year==year[iYear])
index = which(yearRawStat==year[iYear])
if(length(index) > 0) {
# monthlyRaw[iStat, iYear,] = dataRaw$temp[index, ]
# monthlyTemp = dataRaw$temp[index, ]
monthlyTemp = tempRawStat[index, ]
monthlyRawStat[iYear, ] = monthlyTemp
# annualRaw [iStat, iYear] = mean(monthlyTemp)
# DMFLagRaw [iStat, iYear,] = dataRaw$DMFlag[index, ]
# QCFlagRaw [iStat, iYear,] = dataRaw$QCFlag[index, ]
# DSFlagRaw [iStat, iYear,] = dataRaw$DSFlag[index, ]
} # If this year and station contains data
# index = which(dataHom$stationNo==stationNo[iStat] & dataHom$year==year[iYear])
index = which(yearHomStat==year[iYear])
if(length(index) > 0) {
# monthlyHom[iStat, iYear,] = dataHom$temp[index, ]
# annualHom [iStat, iYear] = mean(dataHom$temp[index, ])
monthlyTemp = tempHomStat[index, ]
monthlyHomStat[iYear, ] = monthlyTemp
# annualHom [iStat, iYear] = mean(monthlyTemp)
# DMFLagHom [iStat, iYear,] = dataHom$DMFlag[index, ]
# QCFlagHom [iStat, iYear,] = dataHom$QCFlag[index, ]
# DSFlagHom [iStat, iYear,] = dataHom$DSFlag[index, ]
} # If this year and station contains data
} # For iYear
annualRaw[iStat,] = compute_anomalies(monthlyRawStat)
annualHom[iStat,] = compute_anomalies(monthlyHomStat)
index = which(references[,1]==stationNo[iStat])
refStations[iStat,] = references[index, 2:40]
index = which(metaHom$ID==stationNo[iStat])
lat[iStat] = metaHom$lat[index]
lon[iStat] = metaHom$lon[index]
if(iStat %% 10 == 0 | iStat == noStations) {
save(annualRaw, annualHom, refStations, lat, lon, year, stationNo, iStat, file = baseDataFilestr)
}
} # For iStat, loop over all stations
annualDiff = annualRaw - annualHom
print("Ready")
} # End of function
# # Wastebin
# baseDirname = "c:/data/GHCNv3",
# filenameDataRaw = "ghcnm.tavg.v3.2.2.20141219.qcu.dat",
# filenameMetaRaw = "ghcnm.tavg.v3.2.2.20141219.qcu.inv",
# filenameDataHom = "ghcnm.tavg.v3.2.2.20141219.qca.dat",
# filenameMetaHom = "ghcnm.tavg.v3.2.2.20141219.qca.inv",
# filenameRef = "ghcnmv2.corr.20141219.tavg",
# outputMonthly = FALSE,
# outputAnnual = TRUE
# data
# return(data)