find number of consecutive days in NC files

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

find number of consecutive days in NC files

ahmet varlı
There are 365 days of soil moisture NC files and I am trying to find out how many days the values are below and above this certain threshold are repeated. However, I couldn't reach exactly what I wanted.

nctoarray <- function(ncfname, varid = NA) { nc <- nc_open(ncfname) a <- aperm(ncvar_get(nc), c(2,1,3)) nc_close(nc) a }

function(x, threshold = 0.28, below = TRUE) { if (below) {

y <- ifelse(x < threshold,1,0)
   } else y <- ifelse(x > threshold,1,0)
 y2 <- rle(y)
 sel <- which(y2$values == 1)
 max(y2$lengths[sel])    }


m1 <- suppressWarnings(apply(a,c(1,2), consechours, 0.3, TRUE))

m2 <- suppressWarnings(apply(a,c(1,2), consechours, 0.4, FALSE))


        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: find number of consecutive days in NC files

Jeff Newmiller
rle( x > thresh )

On August 4, 2020 5:10:42 PM PDT, "ahmet varlı" <[hidden email]> wrote:

>There are 365 days of soil moisture NC files and I am trying to find
>out how many days the values are below and above this certain threshold
>are repeated. However, I couldn't reach exactly what I wanted.
>
>nctoarray <- function(ncfname, varid = NA) { nc <- nc_open(ncfname) a
><- aperm(ncvar_get(nc), c(2,1,3)) nc_close(nc) a }
>
>function(x, threshold = 0.28, below = TRUE) { if (below) {
>
>y <- ifelse(x < threshold,1,0)
>   } else y <- ifelse(x > threshold,1,0)
> y2 <- rle(y)
> sel <- which(y2$values == 1)
> max(y2$lengths[sel])    }
>
>
>m1 <- suppressWarnings(apply(a,c(1,2), consechours, 0.3, TRUE))
>
>m2 <- suppressWarnings(apply(a,c(1,2), consechours, 0.4, FALSE))
>
>
> [[alternative HTML version deleted]]
>
>______________________________________________
>[hidden email] mailing list -- To UNSUBSCRIBE and more, see
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide
>http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.

--
Sent from my phone. Please excuse my brevity.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.