0

I'm trying to read precipitation data from the Global Meteorological Forcing Dataset for land surface modeling dataset (http://hydrology.princeton.edu/data.pgf.php).

library(terra)
pp <- rast("~/Downloads/prec_monthly_1948-2016.nc"

gives

class       : SpatRaster 
dimensions  : 600, 1440, 828  (nrow, ncol, nlyr)
resolution  : 0.0006944444, 0.001666667  (x, y)
extent      : 0, 1, 0, 1  (xmin, xmax, ymin, ymax)
coord. ref. : lon/lat WGS 84 
source      : prec_monthly_1948-2016.nc 
names       : prec_~016_1, prec_~016_2, prec_~016_3, prec_~016_4, prec_~016_5, prec_~016_6, ...

[edited]

Is there any way of getting rast() to read the dimensions (and extent) from the NetCDF file, or must this be assigned manually?

UPDATE

I have tried reinstalling everything (R v. 4.2.1, XQuartz v. 2.8.2, RStudio v. 2022.07.0, GDAL v.3.2.2). My Mac is running MacOS Big Sur v.11.6.7.

After installing GDAL I set the path using

export PATH=/Library/Frameworks/GDAL.framework/Programs:$PATH

Still no joy. When I try updating terra to v.1.5-34 from source it recognizes the GDAL is present but then exits with errors

./configure: line 3662: pkg-config: command not found

and

configure: error: proj_api.h not found in standard or given locations.

I'm far outside my competence now so will revert back to raster package for now and try again when new package versions are released.

4
  • works for me, maybe upgrade terra is still pretty new - the georeferencing is handled by gdal, but you can change it with set.ext() note that works ?inplace gist.github.com/mdsumner/b7fbf974dd1811186ec87c7e1e6d98d4
    – mdsumner
    Commented Jul 7, 2022 at 13:02
  • it must be gdal version, not terra strictly 🙏 can be different on different systems
    – mdsumner
    Commented Jul 8, 2022 at 4:52
  • fwiw raster uses ncdf4 which is entirely independent, so handy for flexibility - but has an unclear future
    – mdsumner
    Commented Jul 8, 2022 at 22:10
  • Did you create the NetCDF file you're trying to read?
    – Corbjn
    Commented Nov 2, 2022 at 7:56

1 Answer 1

1

I can read this with various versions of terra, including with older versions of R and GDAL on windows and linux. It also works for me on OSX with terra 1.5.34 and gdal() returning 3.2.2.

library(terra)
(r <- rast("prec_monthly_1948-2016.nc"))
#class       : SpatRaster 
#dimensions  : 600, 1440, 828  (nrow, ncol, nlyr)
#resolution  : 0.25, 0.25  (x, y)
#extent      : 0, 360, -60, 90  (xmin, xmax, ymin, ymax)
#coord. ref. : lon/lat WGS 84 
#source      : prec_monthly_1948-2016.nc 
#varname     : data (The data) 
#names       : data_1, data_2, data_3, data_4, data_5, data_6, ... 
#time        : 1948-01-03 to 2016-12-03 UTC 

And to get the standard longitude extent (-180 to 180)

(x <- rotate(r))
#class       : SpatRaster 
#dimensions  : 600, 1440, 828  (nrow, ncol, nlyr)
#resolution  : 0.25, 0.25  (x, y)
#extent      : -180, 180, -60, 90  (xmin, xmax, ymin, ymax)
#coord. ref. : lon/lat WGS 84 
#source      : memory 
#names       :    data_1,    data_2,    data_3,    data_4,    data_5,    data_6, ... 
#min values  :         0,         0,         0,         0,         0,         0, ... 
#max values  :  797.0862,  723.6213,  915.4195, 1284.7316, 1975.4684, 1486.1422, ... 
#time        : 1948-01-03 to 2016-12-03 UTC 
 

Can you provide a sessionInfo()?

5
  • I'm on Mac, sessionInfo() is: R version 4.2.0 (2022-04-22) Platform: x86_64-apple-darwin17.0 (64-bit) Running under: macOS Big Sur 11.6.7 Matrix products: default LAPACK: /Library/Frameworks/R.framework/Versions/4.2/Resources/lib/libRlapack.dylib locale: [1] en_GB.UTF-8/en_GB.UTF-8/en_GB.UTF-8/C/en_GB.UTF-8/en_GB.UTF-8 attached base packages: [1] stats graphics grDevices utils datasets methods base other attached packages: [1] terra_1.5-21 rgdal_1.5-30 sp_1.5-0 ...
    – Dan B
    Commented Jul 10, 2022 at 7:37
  • I've tried reinstalling GDAL framework from www.kyngchaos.com but no joy. I've tried updating terra to version 1.5-34 from source but get checking for gdal-config... no no configure: error: gdal-config not found or not executable. ERROR: configuration failed for package ‘terra’
    – Dan B
    Commented Jul 10, 2022 at 8:48
  • For the sessionInfo, it is better to edit your question instead. Either way, it also works for me on OSX, so is appears to be something peculiar with your install. Perhaps the next CRAN release will fix this. Commented Jul 10, 2022 at 17:51
  • OK, thanks for confirming. I may try a complete reinstall of everything.
    – Dan B
    Commented Jul 11, 2022 at 8:35
  • Package sf seems to link fine with GDAL etc. Linking to GEOS 3.10.2, GDAL 3.4.2, PROJ 8.2.1; sf_use_s2() is TRUE
    – Dan B
    Commented Jul 13, 2022 at 8:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.