mikejohnson51 / climateR

An R 📦 for getting point and gridded climate data by AOI
https://mikejohnson51.github.io/climateR/
MIT License
168 stars 40 forks source link

Incorrect Rounding on getTerraClim Palmer Results? #11

Closed pjgoodling closed 3 years ago

pjgoodling commented 4 years ago

Hi Mike,

I've noticed that the results from getTerraClim seem to be truncated to 1 decimal place, but do not have the correct rounding when compared with TerraClim data retrieved from the Climate Engine. Here's an example:

#Setting up the lat/long point to extract data from
pts <- data.frame(ID = 1,
                  lat = 38.31250,
                  lon = -75.14583)

#Extracting the data for a few months in 2001
getTerraClim(AOI=pts[1,2:3],param=c('palmer'),startDate = "2001-06-01",endDate = "2001-10-31")

#Output 
    source     lat       lon    date palmer
1 terraclim 38.3125 -75.14583 2001-06    1.2
2 terraclim 38.3125 -75.14583 2001-07    1.5
3 terraclim 38.3125 -75.14583 2001-08    1.9
4 terraclim 38.3125 -75.14583 2001-09    1.4
5 terraclim 38.3125 -75.14583 2001-10   -1.1

The palmer results have 2 significant figures.

When I go to The Climate Engine and retrieve the same dataset using the "Make a Graph" function, I get the following results:

#Variable: PDSI
#Data Source: TERRACLIMATE 4000 m (1/24-deg) monthly dataset (University of Idaho)
#Missing Value: -9999
#Time Period: 2001-6-01 to 2001-10-01
#Point (Lat,Lon): 38.3125N,75.1458W
#Date(yyyy-mm-dd) PDSI
2001-06-01 1.2200
2001-07-01 1.5200
2001-08-01 1.9700
2001-09-01 1.4400
2001-10-01 -1.1600

It seems like for August and October 2001, the rounding was incorrectly applied. A few other spot checks shows that the data is truncated rather than rounded. I have only looked through the palmer results, and haven't checked any of the other TerraClim variables.

I was looking through the "fast download" code, but couldn't trace the issue.

Thanks!

Phillip

mikejohnson51 commented 4 years ago

Hi Phillip,

Thanks for the detailed outline of the issue. This appears to go back to how the PDSI values have been aggrageted on the Northwestern Server. Please see below:

library(ncdf4)
# Point to the Aggregated Dataset
url = 'http://thredds.northwestknowledge.net:8080/thredds/dodsC/agg_terraclimate_PDSI_1958_CurrentYear_GLOBE.nc?PDSI[521:1:525][1240:1:1240][2516:1:2516]'

# Open stream to netcdf file
nc = ncdf4::nc_open(url)

# Extract PDSI values
ncdf4::ncvar_get(nc, "PDSI")
#> [1]  1.2  1.5  1.9  1.4 -1.1

# Point to the yearly file datasets:
url2 = 'http://thredds.northwestknowledge.net:8080/thredds/dodsC/TERRACLIMATE_ALL/data/TerraClimate_PDSI_2001.nc?PDSI[5:1:9][1240:1:1240][2516:1:2516]'

# Open stream to netcdf file
nc2 = ncdf4::nc_open(url2)

# Extract PDSI values
ncdf4::ncvar_get(nc2, "PDSI")
#> [1]  1.2  1.5  1.9  1.4 -1.1

Created on 2019-11-25 by the reprex package (v0.3.0)

In both cases the data truncation/rounding have been done on the data preparation end. ClimateR is just reading the data from these resources. I can try emailing them again on this front.

Sorry I cant be more help!

Mike

pjgoodling commented 4 years ago

Thanks for that quick response, Mike! Its frustrating it's on their end, but reassuring it isn't a problem with ClimateR!

Phillip

mikejohnson51 commented 3 years ago

@pjgoodling

Its been a while but I am confirming this is still the case:

# Point to the Aggregated Dataset
url = 'http://thredds.northwestknowledge.net:8080/thredds/dodsC/agg_terraclimate_PDSI_1958_CurrentYear_GLOBE.nc?PDSI[521:1:525][1240:1:1240][2516:1:2516]#fillmismatch'

# Open stream to netcdf file
nc = RNetCDF::open.nc(url)

# Extract PDSI values
RNetCDF::var.get.nc(nc, "PDSI", unpack = TRUE)
#> [1]  1.2  1.5  1.9  1.4 -1.1

# Point to the yearly file datasets:
url2 = 'http://thredds.northwestknowledge.net:8080/thredds/dodsC/TERRACLIMATE_ALL/data/TerraClimate_PDSI_2001.nc?PDSI[5:1:9][1240:1:1240][2516:1:2516]#fillmismatch'

# Open stream to netcdf file
nc = RNetCDF::open.nc(url2)

# Extract PDSI values
RNetCDF::var.get.nc(nc, "PDSI", unpack = TRUE)
#> [1]  1.2  1.5  1.9  1.4 -1.1