-
Notifications
You must be signed in to change notification settings - Fork 0
/
extract-test-data.R
188 lines (171 loc) · 6.92 KB
/
extract-test-data.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
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
# set TZ
Sys.setenv(tz="UTC")
# load libraries
library(ncdf4)
library(jsonlite)
library(httr)
# download (if requireed) and open file
filepath <- "/local/test-data/input/Melonhead_496_R.nc"
if( ! file.exists(filepath) ){
GET( url = "https://linkedsystems.uk/erddap/files/Public_Glider_Data_0711/Melonhead_20180207/Melonhead_496_R.nc",
write_disk(filepath, overwrite = TRUE) )
}
ncin <- nc_open(filepath)
# pick one profile and set indices
start <- 3557
phase1 <- 0:66 + start
phase2 <- 67:215 + start
phase3 <- 216:305 + start
surface_end <- 56
# read the data we want,
glider_model <- ncvar_get(ncin,'PLATFORM_TYPE')
serial_number <- ncvar_get(ncin,'GLIDER_SERIAL_NO')
surface_datetime <- ncvar_get(ncin,'TIME_GPS')
surface_datetime <- as.POSIXct(surface_datetime, origin = '1970-01-01')
lat_gps <- ncvar_get(ncin,'LATITUDE_GPS')
lon_gps <- ncvar_get(ncin,'LONGITUDE_GPS')
lon <- ncvar_get(ncin,'LONGITUDE')
lat <- ncvar_get(ncin,'LATITUDE')
datetime <- ncvar_get(ncin,"TIME")
datetime <- as.POSIXct(datetime, origin = '1970-01-01')
depth <- ncvar_get(ncin,'GLIDER_DEPTH')
pressure <- ncvar_get(ncin,"PRES")
temperature <- ncvar_get(ncin,'TEMP')
conductivity <- ncvar_get(ncin,'CNDC')
salinity <- ncvar_get(ncin,'PSAL')
density <- ncvar_get(ncin,'SIGMA_THETA')
# close the file
nc_close(ncin)
# now sequence to encode
wsi_series <- 0
wsi_issuer <- 22000
wsi_issue_number <- 0
wsi_local_identifier <- "MELONHEAD"
wmo_id <- 9999999
station_name <- "MELONHEAD"
agency <- NA # code table
data_system <- NA # code table
glider_model <- glider_model
serial_number <- serial_number
time_significance <- c(25,2,NA)
surface_year <- as.numeric(format.POSIXct(surface_datetime[surface_end],'%Y'))
surface_month <- as.numeric(format.POSIXct(surface_datetime[surface_end],'%m'))
surface_day <- as.numeric(format.POSIXct(surface_datetime[surface_end],'%d'))
surface_hour <- as.numeric(format.POSIXct(surface_datetime[surface_end],'%H'))
surface_minute <- as.numeric(format.POSIXct(surface_datetime[surface_end],'%M'))
surface_second <- as.numeric(format.POSIXct(surface_datetime[surface_end],'%S'))
surface_lat <- round(lat_gps[surface_end],5)
surface_lon <- round(lon_gps[surface_end],5)
heading <- NA
anemometer_type <- c(2,NA)
wind_speed <- NA
wind_direction <- NA
surface_current_speed <- NA
surface_current_direction <- NA
end_datetime <- max(datetime[phase3])
end_year <- as.numeric(format.POSIXct(end_datetime,'%Y'))
end_month <- as.numeric(format.POSIXct(end_datetime,'%m'))
end_day <- as.numeric(format.POSIXct(end_datetime,'%d'))
end_hour <- as.numeric(format.POSIXct(end_datetime,'%H'))
end_minute <- as.numeric(format.POSIXct(end_datetime,'%M'))
end_second <- as.numeric(format.POSIXct(end_datetime,'%S'))
dive_duration <- -1*round(as.numeric(difftime(max(datetime[phase3]), min(datetime[phase1]), units = "min")))
mean_lat <- round(mean( lat[c(phase1, phase2, phase3)]),5)
mean_lon <- round(mean( lon[c(phase1, phase2, phase3)]),5)
mean_current_speed <- NA
mean_current_direction <- NA
profile_number <- 0
profile_uid <- "MYUUID-1234"
delayed_replicator <- c(3,67,149,90)
profile_direction <- c(1,2,0)
year <- as.numeric(format.POSIXct(datetime[c(phase1,phase2,phase3)],'%Y'))
month <- as.numeric(format.POSIXct(datetime[c(phase1,phase2,phase3)],'%m'))
day <- as.numeric(format.POSIXct(datetime[c(phase1,phase2,phase3)],'%d'))
hour <- as.numeric(format.POSIXct(datetime[c(phase1,phase2,phase3)],'%H'))
minute <- as.numeric(format.POSIXct(datetime[c(phase1,phase2,phase3)],'%M'))
second <- as.numeric(format.POSIXct(datetime[c(phase1,phase2,phase3)],'%S'))
profile_lat <- round(lat[c(phase1,phase2,phase3)],5)
profile_lon <- round(lon[c(phase1,phase2,phase3)],5)
gtspp_quality_qualifiers <- rep( c(20, 13, 10, 11, 25, 12, 26), times = length(c(phase1,phase2,phase3)))
gtspp_quality_flags <- rep(c(NA, NA, NA, NA, NA, NA, NA), times = length(c(phase1,phase2,phase3)))
profile_depth <- depth[c(phase1,phase2,phase3)]
profile_pressure <- pressure[c(phase1,phase2,phase3)]*10000 # convert dbar to Pa
profile_temperature <- temperature[c(phase1,phase2,phase3)] + 273.15
profile_conductivity <- conductivity[c(phase1,phase2,phase3)]
profile_salinity <- salinity[c(phase1,phase2,phase3)] # assuming equivalence between 1 ppt and 1 psu
profile_density <- density[c(phase1,phase2,phase3)] # kg m-3
headers <- list(
"inputDelayedDescriptorReplicationFactor" = c(3),
"inputExtendedDelayedDescriptorReplicationFactor" = c(67,149,90),
"edition" = 4,
"masterTableNumber" = 0,
"bufrHeaderCentre" = 0,
"bufrHeaderSubCentre" = 0,
"updateSequenceNumber" = 0,
"dataCategory" = 31,
"internationalDataSubCategory" = 8,
"dataSubCategory" = NA,
"masterTablesVersionNumber" = 39,
"typicalYear" = 2018,
"typicalMonth" = 2,
"typicalDay" = 7,
"typicalHour" = 23,
"typicalMinute" = 23,
"typicalSecond" = 42,
"numberOfSubsets" = 1,
"observedData" = 1,
"compressedData" = 0,
"unexpandedDescriptors" = c(315012)
)
# convert to list, using eccodes keys
data <- list(
"wigosIdentifierSeries" = wsi_series,
"wigosIssuerOfIdentifier" = wsi_issuer,
"wigosIssueNumber" = wsi_issue_number,
"wigosLocalIdentifierCharacter" = wsi_local_identifier,
# change data width
"marineObservingPlatformIdentifier" = wmo_id,
# cancel change data width
"longStationName" = trimws(station_name),
"agencyInChargeOfOperatingObservingPlatform" = agency,
"dataCollectionLocationSystem" = data_system,
"observingPlatformManufacturerModel" = trimws(glider_model),
"observingPlatformManufacturerSerialNumber" = trimws(serial_number),
"timeSignificance" = c(25,2,NA),
"year" = c(surface_year, end_year, year),
"month" = c(surface_month, end_month, month),
"day" = c(surface_day, end_day, day),
"hour" = c(surface_hour, end_hour, hour),
"minute" = c(surface_minute, end_minute, minute),
"second" = c(surface_second, end_second, second),
"latitude" = c(surface_lat, mean_lat, profile_lat),
"longitude" = c(surface_lon, mean_lon, profile_lon),
"aircraftTrueHeading" = NA,
"anemometerType" = c(2,NA),
"windSpeed" = NA,
"windDirection" = NA,
"speedOfSeaSurfaceCurrent" = NA,
"seaSurfaceCurrentDirection" = NA,
"timePeriod" = dive_duration,
"speedOfCurrent" = NA,
"currentDirection" = NA,
"profileNumber" = 1,
"uniqueIdentifierForProfile" = "MYUID-1234",
# "delayedDescriptorReplicationFactor" = 3,
"directionOfProfile" = profile_direction,
# "extendedDelayedDescriptorReplicationFactor" = c(67,149,90),
"qualifierForGtsppQualityFlag" = gtspp_quality_qualifiers,
"globalGtsppQualityFlag" = gtspp_quality_flags,
"depthBelowWaterSurface" = profile_depth,
"oceanographicWaterPressure" = profile_pressure,
"oceanographicWaterTemperature" = profile_temperature,
"oceanographicWaterConductivity" = profile_conductivity,
"salinity" = profile_salinity,
"seawaterPotentialDensity" = profile_density
)
output <- list(
"headers" = headers,
"data" = data
)
# now write to json
writeLines(toJSON( output, pretty=4, digits=NA, na = 'null', auto_unbox = TRUE), '/local/test-data/output/melonhead.json')