Skip to content

Commit f684540

Browse files
add normalize dmass
1 parent cb76ef1 commit f684540

15 files changed

+208
-6
lines changed

DESCRIPTION

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: rtorf
22
Type: Package
33
Title: 'rtorf' Tools for Observations, Receptors and Footprints
4-
Version: 2.5.0
4+
Version: 2.5.1
55
Authors@R: c(
66
person(given = "Sergio", family = "Ibarra-Espinosa",
77
role = c("aut", "cre"),
@@ -20,7 +20,7 @@ URL: https://github.com/noaa-gml/rtorf,
2020
BugReports: https://github.com/noaa-gml/rtorf/issues/
2121
Encoding: UTF-8
2222
RoxygenNote: 7.3.2
23-
Date: 2025-09-24
23+
Date: 2025-09-25
2424
Suggests:
2525
knitr,
2626
rmarkdown,

NAMESPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export(obs_footname)
2121
export(obs_format)
2222
export(obs_format_et)
2323
export(obs_freq)
24+
export(obs_grid)
2425
export(obs_hysplit_ascdata)
2526
export(obs_hysplit_control)
2627
export(obs_hysplit_control_read)
@@ -42,6 +43,7 @@ export(obs_meta)
4243
export(obs_month_middle)
4344
export(obs_nc)
4445
export(obs_nc_get)
46+
export(obs_normalize_dmass)
4547
export(obs_out)
4648
export(obs_plot)
4749
export(obs_rbind)

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
NEWS
22
===========
3+
### rtorf 2.5.1 (Release date: 2025-09-25)
4+
5+
add obs_normalize_dmass
36

47
### rtorf 2.5.0 (Release date: 2025-09-24)
58

R/10_obs_hysplit.R

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -926,3 +926,43 @@ obs_hysplit_control_read <- function(
926926
)
927927
return(l_control)
928928
}
929+
930+
931+
#' obs_grid
932+
#' Create grid for footprints
933+
#'
934+
#' @param numpix.x number of pixels in x direction
935+
#' @param numpix.y number of pixels in y direction
936+
#' @param lon.ll lower left corner of grid
937+
#' @param lat.ll lower left corner of grid
938+
#' @param lon.res resolution in degrees longitude
939+
#' @param lat.res resolution in degrees latitude
940+
#' @return A grid
941+
#' @note name it is not obs_hysplit_grid because we can create define grid in CONTROL
942+
#' @export
943+
#' @examples {
944+
#' # Do not run
945+
#' obs_hysplit_grid(control_file)
946+
#'
947+
#' }
948+
#
949+
obs_grid <- function(
950+
numpix.x = 70,
951+
numpix.y = 70,
952+
lon.ll = 10,
953+
lat.ll = -30,
954+
lon.res = 1,
955+
lat.res = 1
956+
) {
957+
flat <- lat.ll + (lat.res) * (0:(numpix.y - 1))
958+
flon <- lon.ll + (lon.res) * (0:(numpix.x - 1))
959+
960+
#get grid indices
961+
#For horizontal grids (lower left corner of south-west gridcell: 11N,145W; resolution: 1/4 lon, 1/6 lat, 376 (x) times 324 (y))
962+
lats <- seq(lat.ll, (lat.ll + (numpix.y - 1) * lat.res), lat.res)
963+
lons <- seq(lon.ll, (lon.ll + (numpix.x - 1) * lon.res), lon.res)
964+
965+
#assumes westernmost gridcell is smallest longitude & easternmost
966+
#gridcell is largest longitude; does not work over dateline
967+
gitx <- floor(1 / lon.res * (part[, "lon"] - lon.ll) + 1)
968+
}

R/helpers_legacy.R

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2773,7 +2773,7 @@ obs_make_hysplit_nc4 <- function(
27732773
#' @title obs_traj_foot
27742774
#' @family helpers legacy
27752775
#' @name obs_traj_foot
2776-
#' @description return time
2776+
#' @description return trajectory
27772777
#' @param ident character value specifying the trajectory ensemble to look at
27782778
#' @param part object containing particle run; if NULL, then determine from ident and pathname
27792779
#' @param pathname path where object with particle locations is saved
@@ -2825,7 +2825,7 @@ obs_traj_foot <- function(
28252825
#Check if object exists
28262826
if (read.r) {
28272827
if (existsr(ident, pathname)) {
2828-
print(paste("obs_traj_foot(): starting with ident=", ident, sep = "")) #found object
2828+
print(paste0("obs_traj_foot(): starting with ident=", ident)) #found object
28292829
part <- getr(ident, pathname) #get it
28302830
} else {
28312831
#if not there, break out of function, return NA
@@ -3188,3 +3188,70 @@ obs_traj_foot <- function(
31883188
dimnames(foot.arr) <- list(lats, lons, foottimes[1:(length(foottimes) - 1)])
31893189
return(foot.arr)
31903190
}
3191+
3192+
3193+
#' @title obs_normalize_dmass
3194+
#' @family helpers legacy
3195+
#' @name obs_normalize_dmass
3196+
#' @description add columns of
3197+
#' @param part particle data.table
3198+
#' @return return footprint
3199+
#' @export
3200+
#' @examples {
3201+
#' \dontrun{
3202+
#' # Do not run
3203+
#' }}
3204+
obs_normalize_dmass <- function(
3205+
part
3206+
) {
3207+
part <- data.table::as.data.table(part)
3208+
3209+
# check if btime is not presnet and add it
3210+
if (!"btime" %in% names(part)) {
3211+
cat("Adding btime\n")
3212+
time <- NULL
3213+
part[, btime := abs(time) / 60]
3214+
}
3215+
3216+
# must be in this order (might not be...)
3217+
# this is from lower time to bigger time by part index
3218+
# index from first to last repeating btime
3219+
data.table::setorderv(part, c("btime", "index")) #data.table
3220+
3221+
# identify particles with too strong dmass violation
3222+
part[
3223+
dmass < 1 / 1e3 |
3224+
dmass > 1e3,
3225+
unique(index)
3226+
] -> ind
3227+
3228+
if (length(ind) >= length(part[, unique(index)]) / 2) {
3229+
stop(cat(
3230+
"More than 50% of particles have mass defect\n"
3231+
))
3232+
}
3233+
3234+
# for those particles with strong dmass violation set dmass entries to NA
3235+
part[
3236+
dmass < 1 / 1e3 |
3237+
dmass > 1e3,
3238+
dmass := NA
3239+
]
3240+
3241+
part[,
3242+
mean_dmass := mean(dmass, na.rm = T),
3243+
by = .(btime)
3244+
]
3245+
3246+
# if mean_dmass is 0
3247+
3248+
part[mean_dmass == 0, mean_dmass := 0.0001] # legacy criteria
3249+
3250+
part[is.na(dmass), dmass := mean_dmass]
3251+
3252+
part[, "dmass"] <- part[, "dmass"] / mean.dmass
3253+
3254+
# normalize
3255+
part[, ndmass := dmass / mean_dmass]
3256+
return(part)
3257+
}

man/obs_decimal_to_POSIX.Rd

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/obs_grid.Rd

Lines changed: 46 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/obs_id2pos.Rd

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/obs_info2id.Rd

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/obs_interpret_udunits_time.Rd

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)