Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
aqdl
blobs
core-client
era5spoblobs
Commits
7d296581
Commit
7d296581
authored
May 04, 2022
by
Jean-Marie Lepioufle
Browse files
typo
parent
7f04649d
Changes
2
Hide whitespace changes
Inline
Side-by-side
R/era5spoblobs.R
View file @
7d296581
#' era5spoblobs
#'
#' era5spoblobs
#' @param path_metadata path or url of metadata
#' @param path_metadata_url path or url of metadata_url
#' @keywords era5spoblobs
#' @export
#' @examples
#' \dontrun{
#' era5spoblobs(path)
#' }
era5spoblobs
<-
function
(
path_metadata
,
path_metadata_url
){
era5spoblobs_object
(
path_metadata
,
path_metadata_url
)
}
era5spoblobs_object
<-
function
(
path_metadata
,
path_metadata_url
){
# load metadatas
metadata
<-
data.table
::
fread
(
path_metadata
)
metadata_url
<-
data.table
::
fread
(
path_metadata_url
)
object
<-
local
({
meta
<-
function
(
stationIDs
=
NULL
,
west
=
NULL
,
south
=
NULL
,
east
=
NULL
,
north
=
NULL
,
elementIDs
=
NULL
){
if
(
!
is.null
(
stationIDs
)
&&
(
is.null
(
north
)
&&
is.null
(
south
)
&&
is.null
(
east
)
&&
is.null
(
west
))
)
{
res
<-
unique
(
metadata
[
metadata
$
AirQualityStationEoICode
%in%
stationIDs
,
])
}
else
if
(
is.null
(
stationIDs
)
&&
(
!
is.null
(
north
)
&&
!
is.null
(
south
)
&&
!
is.null
(
east
)
&&
!
is.null
(
west
))
)
{
res
<-
unique
(
metadata
[(
as.numeric
(
metadata
$
lon
)
>=
west
&
as.numeric
(
metadata
$
lon
)
<=
east
&
as.numeric
(
metadata
$
lat
)
>=
south
&
as.numeric
(
metadata
$
lat
)
<=
north
)
,
])
}
else
res
<-
metadata
if
(
!
is.null
(
elementIDs
)){
res
<-
res
[
res
$
AirPollutant
%in%
elementIDs
,]
}
return
(
res
)
}
ts
<-
function
(
fromDateTime
,
toDateTime
,
stationIDs
,
elementIDs
,
levelIDs
){
# get the years and months between start_date and end_date
date_seq
<-
as.POSIXlt
(
seq
(
as.POSIXlt
(
fromDateTime
,
tz
=
"UTC"
),
as.POSIXlt
(
toDateTime
,
tz
=
"UTC"
),
by
=
"month"
))
years_months
<-
unique
(
paste0
(
get_year
(
date_seq
,
CHAR
=
TRUE
),
"_"
,
get_month
(
date_seq
,
CHAR
=
TRUE
),
".csv"
))
metadata_target
<-
unique
(
metadata_url
[
metadata_url
$
station
%in%
stationIDs
&
metadata_url
$
variable
%in%
elementIDs
&
metadata_url
$
pressure_level
%in%
levelIDs
&
grepl
(
paste
(
years_months
,
collapse
=
"|"
),
metadata_url
$
url
),
])
res
=
data.table
::
data.table
(
date
=
as.POSIXct
(
integer
(
0
),
tz
=
"UTC"
),
values
=
numeric
(
0
),
variable
=
character
(
0
),
level
=
character
(
0
),
unit
=
character
(
0
),
SamplingPoint
=
character
(
0
),
station
=
character
(
0
),
stringsAsFactors
=
FALSE
)
# Access and join SPO timeseries
for
(
i
in
1
:
nrow
(
metadata_target
)){
tmp
<-
data.table
::
fread
(
metadata_target
$
url
[
i
])
tmp
$
SamplingPoint
<-
rep
(
metadata_target
$
spo
[
i
],
nrow
(
tmp
))
tmp
$
station
<-
rep
(
metadata_target
$
station
[
i
],
nrow
(
tmp
))
res
<-
rbind
(
res
,
tmp
)
}
return
(
res
)
}
environment
()
})
lockEnvironment
(
object
,
TRUE
)
structure
(
object
,
class
=
c
(
"era5spoblobs"
,
class
(
object
)))
}
#' era5spoblobs
#'
#' era5spoblobs
#' @param path_metadata path or url of metadata
#' @param path_metadata_url path or url of metadata_url
#' @keywords era5spoblobs
#' @export
#' @examples
#' \dontrun{
#' era5spoblobs(path)
#' }
era5spoblobs
<-
function
(
path_metadata
,
path_metadata_url
){
era5spoblobs_object
(
path_metadata
,
path_metadata_url
)
}
era5spoblobs_object
<-
function
(
path_metadata
,
path_metadata_url
){
# load metadatas
metadata
<-
data.table
::
fread
(
path_metadata
)
metadata_url
<-
data.table
::
fread
(
path_metadata_url
)
object
<-
local
({
meta
<-
function
(
stationIDs
=
NULL
,
west
=
NULL
,
south
=
NULL
,
east
=
NULL
,
north
=
NULL
,
elementIDs
=
NULL
){
if
(
!
is.null
(
stationIDs
)
&&
(
is.null
(
north
)
&&
is.null
(
south
)
&&
is.null
(
east
)
&&
is.null
(
west
))
)
{
res
<-
unique
(
metadata
[
metadata
$
AirQualityStationEoICode
%in%
stationIDs
,
])
}
else
if
(
is.null
(
stationIDs
)
&&
(
!
is.null
(
north
)
&&
!
is.null
(
south
)
&&
!
is.null
(
east
)
&&
!
is.null
(
west
))
)
{
res
<-
unique
(
metadata
[(
as.numeric
(
metadata
$
lon
)
>=
west
&
as.numeric
(
metadata
$
lon
)
<=
east
&
as.numeric
(
metadata
$
lat
)
>=
south
&
as.numeric
(
metadata
$
lat
)
<=
north
)
,
])
}
else
res
<-
metadata
if
(
!
is.null
(
elementIDs
)){
res
<-
res
[
res
$
AirPollutant
%in%
elementIDs
,]
}
return
(
res
)
}
ts
<-
function
(
fromDateTime
,
toDateTime
,
stationIDs
,
elementIDs
,
levelIDs
){
# get the years and months between start_date and end_date
date_seq
<-
as.POSIXlt
(
seq
(
as.POSIXlt
(
fromDateTime
,
tz
=
"UTC"
),
as.POSIXlt
(
toDateTime
,
tz
=
"UTC"
),
by
=
"month"
))
years_months
<-
unique
(
paste0
(
get_year
(
date_seq
,
CHAR
=
TRUE
),
"_"
,
get_month
(
date_seq
,
CHAR
=
TRUE
),
".csv"
))
metadata_target
<-
unique
(
metadata_url
[
metadata_url
$
station
%in%
stationIDs
&
metadata_url
$
variable
%in%
elementIDs
&
metadata_url
$
pressure_level
%in%
levelIDs
&
grepl
(
paste
(
years_months
,
collapse
=
"|"
),
metadata_url
$
url
),
])
res
=
data.table
::
data.table
(
date
=
as.POSIXct
(
integer
(
0
),
tz
=
"UTC"
),
values
=
numeric
(
0
),
variable
=
character
(
0
),
level
=
character
(
0
),
unit
=
character
(
0
),
SamplingPoint
=
character
(
0
),
station
=
character
(
0
),
stringsAsFactors
=
FALSE
)
# Access and join SPO timeseries
for
(
i
in
1
:
nrow
(
metadata_target
)){
tmp
<-
data.table
::
fread
(
metadata_target
$
url
[
i
])
tmp
$
SamplingPoint
<-
rep
(
metadata_target
$
spo
[
i
],
nrow
(
tmp
))
tmp
$
station
<-
rep
(
metadata_target
$
station
[
i
],
nrow
(
tmp
))
res
<-
rbind
(
res
,
tmp
)
}
return
(
res
)
}
environment
()
})
lockEnvironment
(
object
,
TRUE
)
structure
(
object
,
class
=
c
(
"era5spoblobs"
,
class
(
object
)))
}
README.md
View file @
7d296581
...
...
@@ -13,7 +13,7 @@ cd ~/
# if windows cd ~/Documents
mkdir
tmp
cd
tmp
git clone git@git.nilu.no:blob/core-client/era5spoblobs.git
git clone git@git.nilu.no:
aqdl/
blob
s
/core-client/era5spoblobs.git
```
In R, write:
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment