ropensci / rerddap

R client for working with ERDDAP servers
https://docs.ropensci.org/rerddap
Other
40 stars 14 forks source link

have rerrdap do last math #65

Closed sckott closed 2 years ago

sckott commented 7 years ago

from the emailer:

So "last" that is not in () is in index space. And any arithmetic with it, e.g., last-5, is in index space. https://coastwatch.pfeg.noaa.gov/erddap/griddap/documentation.html#last And "last" that is in () is in the variable's space. And any arithmetic with it, e.g., last-5 is in the variable's space. For non-timestamp variables, e.g., longitude, the units are the variable's units, e.g., degrees_east. So (last-5) is like saying "the last longitude value, minus 5 degrees_east". For timestamp variables, the units are seconds. So (last-5) is like saying "the last time, minus 5 seconds". https://coastwatch.pfeg.noaa.gov/erddap/griddap/documentation.html#lastInParentheses

rmendels commented 7 years ago

@sckott:

Just for the record, I will post the request the user made, and some examples that I hope will make it clear.

The user made the request:

wind <- griddap( 'erdQMwindmday', time = c('last-5', 'last'), latitude = c(30, 50), longitude = c(210, 240), fields = 'x_wind' )

If do that and look in the attributes of the object wind, you will see it produced the URL:

https://upwell.pfeg.noaa.gov/erddap/griddap/erdQMwindmday.nc?x_wind[(last-5):1:(last)][(10.0):1:(10.0)][(30):1:(50)][(210):1:(240)]

Put that in a browser, and use ncdump to look at the time variable, you get:

time = 1500163200

That is because the time coordinate is given in (), which means in coordinate space, and the units if time are seconds, so the time difference is 5 seconds for monthly data.

Now do the same for this URL:

https://upwell.pfeg.noaa.gov/erddap/griddap/erdQMwindmday.nc?x_wind[last-5:1:last][(10.0):1:(10.0)][(30):1:(50)][(210):1:(240)]

You get for the times in the netcdf file:

time = 1487116800, 1489622400, 1492300800, 1494892800, 1497571200, 1500163200

That is because the the time dimension is not given in coordinate space but index space, so it references the time from indices from the end to the end , like [n-5:n] in R, where n is the length of the array.

Now let's look at this URL:

https://upwell.pfeg.noaa.gov/erddap/griddap/erdQMwindmday.nc?x_wind[(last-10368000):1:(last)][(10.0):1:(10.0)][(30):1:(50)][(210):1:(240)]

You get:

time = 1489622400, 1492300800, 1494892800, 1497571200, 1500163200 ;

Now the time coordinate is in parenthesis, so it is given in time coordinates, which is seconds, and this is monthly data, so

606024*120 = 10368000

Most people when they do arithmetic using last are thinking in terms of index space - it is monthly data, but they want 5 "time indices" before. Particularly for things like months, converting that into seconds correctly can be difficult, so my solution has been to treat the request as being in index space, finding the values, and sending the request in coordinate space, so for that dataset I find that the value for the last time is 2017-07-16, and five time indices before that has a value 2017-02-15, and then use that in my request.

HTH

sckott commented 7 years ago

thanks @rmendels !