Reputation: 21
I've been searching quit a wile to solve my problem, but I still couldn't solve it. I want to Import a nedCDF file into R. Like this:
ncdata <- nc_open("prec_daily_2005-2005.nc")
print(ncdata)
and I get the following
File prec_daily_2005-2005.nc (NC_FORMAT_CLASSIC):
1 variables (excluding dimension variables):
float prec[longitude,latitude,z,time]
source: Reanalysis daily precipitation, statistically corrected for number of raindays, monthly amounts and diurnal cycle at 1.0deg; interpolated to 0.1deg; available GHCN/GSOD daily station data assimilated into gridded data
name: prec
title: Daily bias corrected precipitation
date: 01/01/05
time: 00:00
long_name: Precipitation
units: kg m-2 s-1
missing_value: 2.00000004008175e+20
_FillValue: 2.00000004008175e+20
valid_min: 0
valid_max: 0.0045476695522666
4 dimensions:
longitude Size:800
units: degrees_east
point_spacing: even
latitude Size:300
units: degrees_north
point_spacing: even
z Size:1
units: level
positive: up
time Size:365 *** is unlimited ***
units: days since 2005-01-01 00:00:00
time_origin: 01-JAN-2005:00:00:00
6 global attributes:
history: Thu May 22 10:21:12 EDT 2014: created by JS using convert2alma.sh
title: Princeton University Hydroclimatology Group Bias Corrected African (1979-2005) Meteorological Forcing Dataset V1.0
institution: Princeton University
contact: Justin Sheffield ([email protected])
source: Forcings are a hybrid of NCEP/NCAR reanalysis and observations
comment: This dataset is described in Chaney and Sheffield (2012) (Chaney, N., and J. Sheffield, 2012: High Resolution Gridded Daily Meteorological Data for Africa: Dataset Development and Analysis of Trends in Means and Extremes, J. Climate, to be submitted) and is related to the original global version reported in Sheffield et al., J. Climate (2006). Updates/changes include: i) African continent domain; ii) extension to 2005; iii) assimilation of available GHCN/GSOD daily station observations; iv) step change detection and correction for observational datasets; v) improved sampling procedure for correction of rain day statistics; vi) use of latest versions of CRU, SRB and TRMM products; vii) improved consistency between specific and relative humidity and air temperature. See Sheffield et al., J. Climate (2006) for details of the observations used and the bias correction and downscaling methodology.
and then I want to extract precipitation data, for example at one grid cell at one day:
n_prec <- ncvar_get(ncdata, 'prec')
print(n_prec[1, 1, 1, 1])
but I get en error message like: error in n_prec[1, 1, 1, 1] : wrong number of dimensions
and I don't get it because the dataset has for dimensions. But I maybe misunderstood something, as I'm pretty new to R.
I'm glad for any help. Manuel
Upvotes: 2
Views: 555
Reputation: 397
The number of dimensions for n_prec is 3 instead of 4. The 'z' dimension only has 1 option/level so R ignores it when reading in the precip array.
> library(ncdf4)
> ncdata <- nc_open("prec_daily_2005-2005.nc")
> n_prec <- ncvar_get(ncdata, 'prec')
> dim(n_prec)
[1] 800 300 365
> n_prec[1,1,1]
[1] NA
> n_prec[400,100,6]
[1] 1.13913e-05
Upvotes: 1