Re: [R] Question regarding reservoir volume and water level

2024-04-07 Thread javad bayat
Dear all;
Many thanks for your replies. This was not homework. I apologize.
Let me explain more.
There is a dam constructed in a valley with the highest elevation of 1255
m. The area of its reservoir can be calculated by drawing a polygon around
the water and it is known.
I have the Digital Elevation Model (DEM) of the region (reservoir and its
surrounding area). I have calculated the volume of the current reservoir
(7e6 m3) using the following codes.
library(raster)
library(terra)
library(exactextractr)
library(dplyr)
library(sf)
# Calculate volume for polygon
# Read the DEM raster file
r <- rast("E:/...DEM.tif")
# Read the polygon shapefile
p <- st_read("E:/...Dam.shp")

r <- crop(r, extent(p))
r <- mask(r, p)

# Extract the cells in each polygon and calculate the area of each cell
x <- exact_extract(r, p, coverage_area = TRUE)
# Extract polygon values as a dataframe
x1 = as.data.frame(x[1])
head(x1)
x1 = na.omit(x1)
# Calculate the height above the minimum elevation in the polygon
x1$Height = max(x1[,1]) - x1[,1]
# Calculate the volume of each cell
x1$Vol = x1[,2] * x1[,3]
sum(x1$Vol)
x2 = x1[,c(1,2,4)]
x2 = sort(x2,'value')
head(x2)
x3 <- aggregate(Vol ~ value, data = x2, FUN = sum)
x4 <- aggregate(coverage_area ~ value, data = x2, FUN = sum)
x5 = cbind(x3, Area = x4[,2])
library(dplyr)
x6 <- x5 %>%
  mutate(V_sum = cumsum(Vol)) %>%
  mutate(A_sum = cumsum(Area))
plot(x6$value~x6$V_sum)

And I thought that it is possible to get the elevation for a specific
volume by linear model between elevation and volume, as follow:

# Get a linear model between elevation and the volume
lm1 <- lm(value ~ V_sum, data = x6)
d <- data.frame(V_sum = 14e6)  #
predict(lm1, newdata = d)

But it is not possible through the LM.
Now I want to know what would be the water level in the reservoir if the
reservoir volume doubled or we adding a known volume to it?
Also what would be the volume if the water level increases to 1250 m?

I would be more than happy if you help me to do this.
Sincerely

On Mon, Apr 8, 2024 at 12:23 AM  wrote:

> John,
>
> Your reaction was what my original reaction was until I realized I had to
> find out what a DEM file was and that contains enough of the kind of
> depth-dimension data you describe albeit what may be a very irregular cross
> section to calculate for areas and thence volumes.
>
> If I read it correctly, this can be a very real-world problem worthy of a
> solution, such as in places like California where they had a tad more rain
> than usual and some reservoirs may overflow. Someone else provided what
> sounds like a mathematical algorithm but my guess is what is needed here is
> perhaps less analytic since there may be no trivial way to create formulas
> and take integrals and so on, but simply an approximate way to calculate
> incremental volumes for each horizontal "slice" and keep adding or
> subtracting them till you reach a target and then read off another variable
> at that point such as depth.
>
> Some care must be taken as water level has to be relative to something and
> many natural reservoirs have no unique bottom level. Some water may also be
> stored underground and to the side and pour in if the level lowers or can
> be
> used to escape if the level rises.
>
>
> -Original Message-
> From: R-help  On Behalf Of Sorkin, John
> Sent: Sunday, April 7, 2024 3:08 PM
> To: Rui Barradas ; javad bayat  >;
> R-help 
> Subject: Re: [R] Question regarding reservoir volume and water level
>
> Aside from the fact that the original question might well be a class
> exercise (or homework), the question is unanswerable given the data given
> by
> the original poster. One needs to know the dimensions of the reservoir,
> above and below the current waterline. Are the sides, above and below the
> waterline smooth? Is the region currently above the waterline that can
> store
> water a mirror image of the region below the waterline? Is the region above
> the reservoir include a flood plane? Will the additional water go into the
> flood plane?
>
> The lack of required detail in the question posed by the original poster
> suggests that there are strong assumptions, assumptions that typically
> would
> be made in a class-room example or exercise.
>
> John
>
> John David Sorkin M.D., Ph.D.
> Professor of Medicine, University of Maryland School of Medicine;
> Associate Director for Biostatistics and Informatics, Baltimore VA Medical
> Center Geriatrics Research, Education, and Clinical Center;
> PI Biostatistics and Informatics Core, University of Maryland School of
> Medicine Claude D. Pepper Older Americans Independence Center;
> Senior Statistician University of Maryland Center for Vascular Research;
>
> Division of Gerontology and Paliative Care,
> 10 North Greene Street
> GRECC (BT/18/GR)
> Baltimore, MD 21201-1524
> Cell phone 443-418-5382
>
>
>
>
> 
> From: R-help  on behalf of Rui Barradas
> 
> Sent: Sunday, April 7, 2024 10:53 AM
> To: javad 

Re: [R] Question regarding reservoir volume and water level

2024-04-07 Thread avi.e.gross
John,

Your reaction was what my original reaction was until I realized I had to
find out what a DEM file was and that contains enough of the kind of
depth-dimension data you describe albeit what may be a very irregular cross
section to calculate for areas and thence volumes.

If I read it correctly, this can be a very real-world problem worthy of a
solution, such as in places like California where they had a tad more rain
than usual and some reservoirs may overflow. Someone else provided what
sounds like a mathematical algorithm but my guess is what is needed here is
perhaps less analytic since there may be no trivial way to create formulas
and take integrals and so on, but simply an approximate way to calculate
incremental volumes for each horizontal "slice" and keep adding or
subtracting them till you reach a target and then read off another variable
at that point such as depth.

Some care must be taken as water level has to be relative to something and
many natural reservoirs have no unique bottom level. Some water may also be
stored underground and to the side and pour in if the level lowers or can be
used to escape if the level rises.


-Original Message-
From: R-help  On Behalf Of Sorkin, John
Sent: Sunday, April 7, 2024 3:08 PM
To: Rui Barradas ; javad bayat ;
R-help 
Subject: Re: [R] Question regarding reservoir volume and water level

Aside from the fact that the original question might well be a class
exercise (or homework), the question is unanswerable given the data given by
the original poster. One needs to know the dimensions of the reservoir,
above and below the current waterline. Are the sides, above and below the
waterline smooth? Is the region currently above the waterline that can store
water a mirror image of the region below the waterline? Is the region above
the reservoir include a flood plane? Will the additional water go into the
flood plane?

The lack of required detail in the question posed by the original poster
suggests that there are strong assumptions, assumptions that typically would
be made in a class-room example or exercise.

John

John David Sorkin M.D., Ph.D.
Professor of Medicine, University of Maryland School of Medicine;
Associate Director for Biostatistics and Informatics, Baltimore VA Medical
Center Geriatrics Research, Education, and Clinical Center;
PI Biostatistics and Informatics Core, University of Maryland School of
Medicine Claude D. Pepper Older Americans Independence Center;
Senior Statistician University of Maryland Center for Vascular Research;

Division of Gerontology and Paliative Care,
10 North Greene Street
GRECC (BT/18/GR)
Baltimore, MD 21201-1524
Cell phone 443-418-5382





From: R-help  on behalf of Rui Barradas

Sent: Sunday, April 7, 2024 10:53 AM
To: javad bayat; R-help
Subject: Re: [R] Question regarding reservoir volume and water level

Às 13:27 de 07/04/2024, javad bayat escreveu:
> Dear all;
> I have a question about the water level of a reservoir, when the volume
> changed or doubled.
> There is a DEM file with the highest elevation 1267 m. The lowest
elevation
> is 1230 m. The current volume of the reservoir is 7,000,000 m3 at 1240 m.
> Now I want to know what would be the water level if the volume rises to
> 1250 m? or what would be the water level if the volume doubled (14,000,000
> m3)?
>
> Is there any way to write codes to do this in R?
> I would be more than happy if anyone could help me.
> Sincerely
>
>
>
>
>
>
>
>
Hello,

This is a simple rule of three.
If you know the level l the argument doesn't need to be named but if you
know the volume v then it must be named.


water_level <- function(l, v, level = 1240, volume = 7e6) {
   if(missing(v)) {
 volume * l / level
   } else level * v / volume
}

lev <- 1250
vol <- 14e6

water_level(l = lev)
#> [1] 7056452
water_level(v = vol)
#> [1] 2480


Hope this helps,

Rui Barradas


--
Este e-mail foi analisado pelo software antivírus AVG para verificar a
presença de vírus.
http://www.avg.com/

__
R-help@r-project.org 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.

__
R-help@r-project.org 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.

__
R-help@r-project.org 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.


Re: [R] duplicated() on zero-column data frames returns empty

2024-04-07 Thread Mark Webster via R-help
 With respect to duplicated.data.frame taking account of row names to return 
all the rows as unique: thinking about this some more, I can see that making 
sense in isolation, but it's at odds with the usual behaviour of duplicated for 
other classes, e.g. primitive vectors, where it doesn't take account of names.
> Would you suggest similar changes to duplicated.matrix too? Currently
> it too returns 0-length output for 0-column inputs:

duplicated.matrix is an interesting one. I think a similar change would make 
sense, because it would have the dimensions that people would expect when using 
the default MARGIN = 1. However, it could be argued that it's not a needed 
change, because the Value section of its documentation only guarantees the 
dimensions of the output when using MARGIN = 0. In that case, duplicated.matrix 
does indeed return the expected 5x0 matrix for your example:
str(duplicated(matrix(0, 5, 0), MARGIN = 0))# logi[1:5, 0 ]
Best Regards,
Mark Webster  
[[alternative HTML version deleted]]

__
R-help@r-project.org 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.


Re: [R] Question regarding reservoir volume and water level

2024-04-07 Thread avi.e.gross
Chris, since it does indeed look like homework, albeit a deeper looks
suggests it may not beI think we can safely answer the question:

>Is there any way to write codes to do this in R?

The answer is YES.

And before you ask, it can be done in Python, Java, C++, Javascript, BASIC,
FORTRAN and probably even COBOL and many forms of assembler.

And, it can be done even without a computer using your mind and pencil and
paper.

I have seen similar problems discussed using a search and wonder if that is
where you should go, or perhaps consult your textbook or class notes.

OK, levity aside, what is the real question? 

If you want help designing an algorithm that solves the problem, that is
outside the scope of this forum and may indeed count as helping someone for
free with their homework or other work.

If this was a place for tutoring help you might  be asked to try to show
some work and point out where one step seems stuck. You might get answers.

Perhaps a better question is to look at your problem and see what it might
need and ask if someone knows of one or more R packages that handle your
needs.

But as I read your message, assume people reading it have no idea what a DEM
file is. I looked it up and it a Digital Elevation Model. I then searched to
see if anyone discussed how to bring the contents of the file into an R
session and found some suggestions but note I have not, nor plan, to try any
of them.

Your request does not specify any particular shape for the containment of
existing water or what is above. If it is from a DES file, it would have
info about what likely may be quite irregular surfaces that vary with depth.
That is not as simple a calculation as asking what happens if the container
is a cylinder or cone . It depends on the data we cannot see. It sounds way
beyond basic R as it likely involves working with 3-D matrices or something
similar.

So I looked for packages you can search for too and I see one called,
appropriately, DEM. I see other packages called Terra and CopernicusDEM  and
whitebox and you may want to do searching and see if anything solves parts
of your problem.

And, of course, it may be something you find will do it easily for you if
someone has provided say a module for Python.

Good Luck.


-Original Message-
From: R-help  On Behalf Of Chris Ryan via
R-help
Sent: Sunday, April 7, 2024 9:26 AM
To: r-help@r-project.org; javad bayat ; R-help

Subject: Re: [R] Question regarding reservoir volume and water level

Homework?
-- 
Sent from my Android device with K-9 Mail. Please excuse my brevity.

On April 7, 2024 8:27:18 AM EDT, javad bayat  wrote:
>Dear all;
>I have a question about the water level of a reservoir, when the volume
>changed or doubled.
>There is a DEM file with the highest elevation 1267 m. The lowest elevation
>is 1230 m. The current volume of the reservoir is 7,000,000 m3 at 1240 m.
>Now I want to know what would be the water level if the volume rises to
>1250 m? or what would be the water level if the volume doubled (14,000,000
>m3)?
>
>Is there any way to write codes to do this in R?
>I would be more than happy if anyone could help me.
>Sincerely
>
>
>
>
>
>
>
>

__
R-help@r-project.org 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.

__
R-help@r-project.org 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.


Re: [R] Question regarding reservoir volume and water level

2024-04-07 Thread Sorkin, John
Aside from the fact that the original question might well be a class exercise 
(or homework), the question is unanswerable given the data given by the 
original poster. One needs to know the dimensions of the reservoir, above and 
below the current waterline. Are the sides, above and below the waterline 
smooth? Is the region currently above the waterline that can store water a 
mirror image of the region below the waterline? Is the region above the 
reservoir include a flood plane? Will the additional water go into the flood 
plane?

The lack of required detail in the question posed by the original poster 
suggests that there are strong assumptions, assumptions that typically would be 
made in a class-room example or exercise.

John

John David Sorkin M.D., Ph.D.
Professor of Medicine, University of Maryland School of Medicine;
Associate Director for Biostatistics and Informatics, Baltimore VA Medical 
Center Geriatrics Research, Education, and Clinical Center;
PI Biostatistics and Informatics Core, University of Maryland School of 
Medicine Claude D. Pepper Older Americans Independence Center;
Senior Statistician University of Maryland Center for Vascular Research;

Division of Gerontology and Paliative Care,
10 North Greene Street
GRECC (BT/18/GR)
Baltimore, MD 21201-1524
Cell phone 443-418-5382





From: R-help  on behalf of Rui Barradas 

Sent: Sunday, April 7, 2024 10:53 AM
To: javad bayat; R-help
Subject: Re: [R] Question regarding reservoir volume and water level

Às 13:27 de 07/04/2024, javad bayat escreveu:
> Dear all;
> I have a question about the water level of a reservoir, when the volume
> changed or doubled.
> There is a DEM file with the highest elevation 1267 m. The lowest elevation
> is 1230 m. The current volume of the reservoir is 7,000,000 m3 at 1240 m.
> Now I want to know what would be the water level if the volume rises to
> 1250 m? or what would be the water level if the volume doubled (14,000,000
> m3)?
>
> Is there any way to write codes to do this in R?
> I would be more than happy if anyone could help me.
> Sincerely
>
>
>
>
>
>
>
>
Hello,

This is a simple rule of three.
If you know the level l the argument doesn't need to be named but if you
know the volume v then it must be named.


water_level <- function(l, v, level = 1240, volume = 7e6) {
   if(missing(v)) {
 volume * l / level
   } else level * v / volume
}

lev <- 1250
vol <- 14e6

water_level(l = lev)
#> [1] 7056452
water_level(v = vol)
#> [1] 2480


Hope this helps,

Rui Barradas


--
Este e-mail foi analisado pelo software antivírus AVG para verificar a presença 
de vírus.
http://www.avg.com/

__
R-help@r-project.org 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.

__
R-help@r-project.org 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.


Re: [R] Question regarding reservoir volume and water level

2024-04-07 Thread Rui Barradas

Às 13:27 de 07/04/2024, javad bayat escreveu:

Dear all;
I have a question about the water level of a reservoir, when the volume
changed or doubled.
There is a DEM file with the highest elevation 1267 m. The lowest elevation
is 1230 m. The current volume of the reservoir is 7,000,000 m3 at 1240 m.
Now I want to know what would be the water level if the volume rises to
1250 m? or what would be the water level if the volume doubled (14,000,000
m3)?

Is there any way to write codes to do this in R?
I would be more than happy if anyone could help me.
Sincerely









Hello,

This is a simple rule of three.
If you know the level l the argument doesn't need to be named but if you 
know the volume v then it must be named.



water_level <- function(l, v, level = 1240, volume = 7e6) {
  if(missing(v)) {
volume * l / level
  } else level * v / volume
}

lev <- 1250
vol <- 14e6

water_level(l = lev)
#> [1] 7056452
water_level(v = vol)
#> [1] 2480


Hope this helps,

Rui Barradas


--
Este e-mail foi analisado pelo software antivírus AVG para verificar a presença 
de vírus.
www.avg.com

__
R-help@r-project.org 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.


Re: [R] Question regarding reservoir volume and water level

2024-04-07 Thread Chris Ryan via R-help
Homework?
-- 
Sent from my Android device with K-9 Mail. Please excuse my brevity.

On April 7, 2024 8:27:18 AM EDT, javad bayat  wrote:
>Dear all;
>I have a question about the water level of a reservoir, when the volume
>changed or doubled.
>There is a DEM file with the highest elevation 1267 m. The lowest elevation
>is 1230 m. The current volume of the reservoir is 7,000,000 m3 at 1240 m.
>Now I want to know what would be the water level if the volume rises to
>1250 m? or what would be the water level if the volume doubled (14,000,000
>m3)?
>
>Is there any way to write codes to do this in R?
>I would be more than happy if anyone could help me.
>Sincerely
>
>
>
>
>
>
>
>

__
R-help@r-project.org 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.


[R] Question regarding reservoir volume and water level

2024-04-07 Thread javad bayat
Dear all;
I have a question about the water level of a reservoir, when the volume
changed or doubled.
There is a DEM file with the highest elevation 1267 m. The lowest elevation
is 1230 m. The current volume of the reservoir is 7,000,000 m3 at 1240 m.
Now I want to know what would be the water level if the volume rises to
1250 m? or what would be the water level if the volume doubled (14,000,000
m3)?

Is there any way to write codes to do this in R?
I would be more than happy if anyone could help me.
Sincerely








-- 
Best Regards
Javad Bayat
M.Sc. Environment Engineering
Alternative Mail: bayat...@yahoo.com

[[alternative HTML version deleted]]

__
R-help@r-project.org 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.


Re: [R] duplicated() on zero-column data frames returns empty

2024-04-07 Thread Ivan Krylov via R-help
В Fri, 5 Apr 2024 16:08:13 +
Jorgen Harmse  пишет:

> if duplicated really treated a row name as part of the row then
> any(duplicated(data.frame(…))) would always be FALSE. My expectation
> is that if key1 is a subset of key2 then all(duplicated(df[key1]) >=
> duplicated(df[key2])) should always be TRUE.

That's a good argument, thank you!

Would you suggest similar changes to duplicated.matrix too? Currently
it too returns 0-length output for 0-column inputs:

# 0-column matrix for 0-column input
str(duplicated(matrix(0, 5, 0)))
# logi[1:5, 0 ] 

# 1-column matrix for 1-column input
str(duplicated(matrix(0, 5, 1)))
# logi [1:5, 1] FALSE TRUE TRUE TRUE TRUE

# a dim-1 array for >1-column input
str(duplicated(matrix(0, 5, 10)))
# logi [1:5(1d)] FALSE TRUE TRUE TRUE TRUE

-- 
Best regards,
Ivan

__
R-help@r-project.org 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.