user19607132
user19607132

Reputation: 1

NETCDF code in R software to convert to TEXT data

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?

Upvotes: 0

Views: 218

Answers (1)

dimfalk
dimfalk

Reputation: 1423

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.

Upvotes: 1

Related Questions