I have three netcdf files from three MSWEP, CHIRPS, PERSIANN CDR satellites that I want to convert into readable text data. Can anyone guide me?
Asked
Active
Viewed 86 times
0
-
Welcome to SO, user19607132! StackOverflow is not intended nor set up well to be used for tutorials, howtos, etc. Please (re?)take the [tour], as what you're asking us to do (*"recommend or find a book, tool, software library, tutorial or other off-site resource"*) is explicitly [off-topic](https://stackoverflow.com/help/on-topic). – r2evans Jul 23 '22 at 13:38
-
Questions on SO (especially in R) do much better if they are reproducible and self-contained. By that I mean including attempted code (please be explicit about non-base packages), sample representative data (perhaps via `dput(head(x))` or building data programmatically (e.g., `data.frame(...)`), possibly stochastically), perhaps actual output (with verbatim errors/warnings) versus intended output. Refs: https://stackoverflow.com/q/5963269, [mcve], and https://stackoverflow.com/tags/r/info. – r2evans Jul 23 '22 at 13:38
-
If you don't point us to specific examples of those NetCDF files, do you think anyone will spend time trying to find them? You have to make it easy for people to answer, or they won't. – Spacedman Jul 23 '22 at 15:23
1 Answers
1
I'm not completely sure about your desired output and expectations but let me give a shot.
I acquired a CHIRPS dataset in netCDF format from data.chc.ucsb.edu to have data to work with.
You can import this data by using e.g. the terra
package:
library(terra)
#> terra 1.5.21
nc_data <- terra::rast("chirps-v2.0.2018.days_p25.nc")
# inspect: 400 x 1440 px, 365 layer -> daily data, 0.25° resolution, WGS 84
nc_data
#> class : SpatRaster
#> dimensions : 400, 1440, 365 (nrow, ncol, nlyr)
#> resolution : 0.25, 0.25 (x, y)
#> extent : -180, 180, -50, 50 (xmin, xmax, ymin, ymax)
#> coord. ref. : lon/lat WGS 84
#> source : chirps-v2.0.2018.days_p25.nc
#> varname : precip (Climate Hazards group InfraRed Precipitation with Stations)
#> names : precip_1, precip_2, precip_3, precip_4, precip_5, precip_6, ...
#> unit : mm/day, mm/day, mm/day, mm/day, mm/day, mm/day, ...
#> time : 2018-01-01 to 2018-12-31
terra
makes use of GDAL internally, so this SpatRaster
object can now be converted in one of the well-established human-readable formats for raster data, e.g. an ESRI grid which can be further inspected by a text editor of your choice. Be aware that netCDF container hold multiple layers (here: 365) whereas ESRI grids only contain one layer per file, so you need some (intuitively implemented) subsetting beforehand:
# take the first layer and write this object to disk in ESRI grid format
nc_data[[1]] |> terra::writeRaster(filename = "chirps-v2.0.2018.days_p25.asc")
This should be it - unless you provide further information with precise specifications.

dimfalk
- 853
- 1
- 5
- 15
-
1or `as.data.frame(nc_data) |> write.csv("data.csv", row.names=FALSE)` – Robert Hijmans Jul 24 '22 at 21:37