01: Opening and understanding#

library(IRdisplay)
display_html('<iframe width="560" height="315" src="https://www.youtube.com/embed/Xer1XBm3sns" frameborder="0" allowfullscreen></iframe>')

In this notebook we will go through how to open a NetCDF file in R. We will also try to understand the file and extract the data and metadata from it.

Firstly, let’s import the modules that we will work with.

if (!requireNamespace("RNetCDF", quietly = TRUE)) {
  install.packages("RNetCDF")
}
library(RNetCDF)

You can find the documentation for the RNetCDF package here: https://cran.r-project.org/web/packages/RNetCDF/RNetCDF.pdf

The syntax for functions in RNetCDF is very repetitive as you will learn throughout this course, which makes it easy to learn. However, there are other libraries you can use like ncdf4. https://cran.r-project.org/web/packages/ncdf4/ncdf4.pdf

In this tutorial, we will stick to RNetCDF.

Importing some data from OPeNDAP#

Have you heard of OPeNDAP? Try running the cell below.

OPeNDAP, which stands for “Open-source Project for a Network Data Access Protocol,” makes it easier to access and share scientific data over the internet. One advantage of using OPeNDAP is that you don’t need to download the data to use them!

netcdf_file <- 'https://www.ncei.noaa.gov/thredds/dodsC/noaa-global-temp-v5/NOAAGlobalTemp_v5.0.0_gridded_s188001_e202212_c20230108T133308.nc'
data <- open.nc(netcdf_file)

If we want some quick information about the file we can make an inquiry

file.inq.nc(data)
$ndims
4
$nvars
5
$ngatts
66
$unlimdimid
<NA>
$format
'classic'
$libvers
'4.8.1 of Sep 29 2021 09:36:14 $'

The inquiry is a list of key value pairs, so we can access the values like this

file.inq.nc(data)['format']
$format = 'classic'

Though often we will want more information, so we can do this instead:

print.nc(data)
netcdf classic {
dimensions:
	lat = 36 ;
	lon = 72 ;
	time = 1716 ;
	z = 1 ;
variables:
	NC_FLOAT time(time) ;
		NC_CHAR time:long_name = "reference time of global temperature anomalies" ;
		NC_CHAR time:standard_name = "time" ;
		NC_CHAR time:coverage_content_type = "coordinate" ;
		NC_CHAR time:units = "days since 1800-01-01 00:00:00" ;
		NC_CHAR time:calendar = "gregorian" ;
		NC_CHAR time:axis = "T" ;
	NC_FLOAT lat(lat) ;
		NC_CHAR lat:long_name = "Latitude" ;
		NC_CHAR lat:standard_name = "latitude" ;
		NC_CHAR lat:coverage_content_type = "coordinate" ;
		NC_CHAR lat:units = "degrees_north" ;
		NC_CHAR lat:grids = "Uniform grid from -87.5 to 87.5 by 5" ;
		NC_FLOAT lat:valid_min = -87.5 ;
		NC_FLOAT lat:valid_max = 87.5 ;
		NC_CHAR lat:axis = "Y" ;
		NC_CHAR lat:_CoordinateAxisType = "Lat" ;
		NC_CHAR lat:coordinate_defines = "center" ;
	NC_FLOAT lon(lon) ;
		NC_CHAR lon:long_name = "Longitude" ;
		NC_CHAR lon:standard_name = "longitude" ;
		NC_CHAR lon:coverage_content_type = "coordinate" ;
		NC_CHAR lon:units = "degrees_east" ;
		NC_CHAR lon:grids = "Uniform grid from 2.5 to 357.5 by 5" ;
		NC_CHAR lon:axis = "X" ;
		NC_FLOAT lon:valid_min = 2.5 ;
		NC_FLOAT lon:valid_max = 357.5 ;
		NC_CHAR lon:_CoordinateAxisType = "Lon" ;
		NC_CHAR lon:coordinate_defines = "center" ;
	NC_FLOAT z(z) ;
		NC_CHAR z:long_name = "height of surface temperature" ;
		NC_CHAR z:standard_name = "height" ;
		NC_CHAR z:coverage_content_type = "coordinate" ;
		NC_CHAR z:geospatial_bounds_vertical_crs = "EPSG:5703" ;
		NC_CHAR z:units = "meters" ;
		NC_CHAR z:positive = "up" ;
		NC_FLOAT z:valid_min = 0 ;
		NC_FLOAT z:valid_max = 0 ;
		NC_CHAR z:axis = "Z" ;
		NC_CHAR z:_CoordinateAxisType = "Height" ;
		NC_CHAR z:comment = "Actual measurement depth of in situ sea surface temperature varies from 0.2 to 10 m, but corrected to the nominal depth of buoy at 0.2 m; for air temperature over land is 2m." ;
	NC_FLOAT anom(lon, lat, z, time) ;
		NC_CHAR anom:long_name = "Global Temperature Anomalies" ;
		NC_CHAR anom:standard_name = "surface_temperature_anomaly" ;
		NC_CHAR anom:coverage_content_type = "physicalMeasurement" ;
		NC_CHAR anom:units = "degrees C" ;
		NC_FLOAT anom:missing_value = -999.900024414062 ;
		NC_FLOAT anom:_FillValue = -999.900024414062 ;
		NC_FLOAT anom:add_offset = 0 ;
		NC_FLOAT anom:scale_factor = 1 ;
		NC_FLOAT anom:valid_min = -40 ;
		NC_FLOAT anom:valid_max = 40 ;
		NC_CHAR anom:coordinates = "time z lat lon" ;

// global attributes:
		NC_CHAR :Conventions = "CF-1.6, ACDD-1.3" ;
		NC_CHAR :title = "NOAA Merged Land Ocean Global Surface Temperature Analysis (NOAAGlobalTemp)" ;
		NC_CHAR :summary = "NOAAGlobalTemp is a merged land-ocean surface temperature analysis, formerly abbreviated as MLOST. The SST dataset is the Extended Reconstructed Sea Surface Temperature (ERSST) and the land surface air temperature dataset is from the Global Historical Climatology Network Monthly (GHCN-M) database." ;
		NC_CHAR :institution = "DOC/NOAA/NESDIS/National Centers for Environmental Information(NCEI)" ;
		NC_CHAR :id = " gov.noaa.ncdc:C00934 " ;
		NC_CHAR :naming_authority = " gov.noaa.ncei " ;
		NC_CHAR :project = " NOAAGlobalTemp " ;
		NC_CHAR :platform = "Ships, moored buoys, surface drifting buoys, Argo floats, and weather stations" ;
		NC_CHAR :instrument = "Conventional thermometers" ;
		NC_CHAR :history = "Version 5.0.0, blending ERSST V5 and GHCN-M V4." ;
		NC_CHAR :comment = "Merged land ocean surface temperature anomalies." ;
		NC_CHAR :product_version = "5.0.0" ;
		NC_CHAR :source = "NOAAGlobalTemp combines a global sea surface temperature with a global land surface air temperature data" ;
		NC_CHAR :ncei_template_version = "NCEI_NetCDF_Grid_Template_v2.0" ;
		NC_CHAR :Metadata_Conventions = "Unidata Dataset Discovery v1.0" ;
		NC_CHAR :license = "These data are available for use without restriction." ;
		NC_CHAR :creator_name = "Boyin Huang, Xungang Yin, Ph.D" ;
		NC_CHAR :creator_email = "Boyin.Huang@noaa.gov, Xungang.Yin@noaa.gov" ;
		NC_CHAR :creator_type = "group" ;
		NC_CHAR :creator_institution = "US DOC/NOAA/NESDIS/National Centers for Environmental Information(NCEI)" ;
		NC_CHAR :contributor_name = "Huai-min Zhang, Ph.D" ;
		NC_CHAR :contributor_role = "Chief, NOAA/NESDIS/NCEI/CCOG/OSB Ocean Surface Section" ;
		NC_CHAR :publisher_name = "NCEI" ;
		NC_CHAR :publisher_institution = "NCEI" ;
		NC_CHAR :publisher_url = "https://www.ncdc.noaa.gov/" ;
		NC_CHAR :publisher_email = "ncei.info@noaa.gov" ;
		NC_CHAR :publisher_type = "institution" ;
		NC_CHAR :keywords = "Earth Science > Oceans > Ocean Temperature > Sea Surface Temperature, Earth Science > Atmosphere > Atmospheric Temperature > Surface Temperature > Air Temperature" ;
		NC_CHAR :keywords_vocabulary = "NASA Global Change Master Directory (GCMD) Science Keywords" ;
		NC_CHAR :standard_name_vocabulary = "Climate and Forecast (CF) Standard Name Table (Version 46, 25 July 2017)" ;
		NC_CHAR :date_created = "2023-01-08T18:33:09Z" ;
		NC_CHAR :necdf_version_id = "4.2.1.1" ;
		NC_CHAR :netcdf_creator_name = "Yongsheng Zhang, Ph.D" ;
		NC_CHAR :netcdf_creator_email = "yongsheng.zhang@noaa.gov" ;
		NC_CHAR :dataset_citation_product = "NOAA Merged Land Ocean Global Surface Temperature Analysis (NOAAGlobalTemp)" ;
		NC_CHAR :dataset_citation_version = "5.0.0" ;
		NC_CHAR :dataset_citation_institution = "NOAA/NESDIS/NCEI, Asheville, NC, USA" ;
		NC_CHAR :dataset_citation_url = "https://doi.org/10.25921/9qth-2p70" ;
		NC_CHAR :metadata_link = "https://doi.org/10.25921/9qth-2p70" ;
		NC_CHAR :cdm_data_type = "Grid" ;
		NC_CHAR :grid_mapping_name = "latitude_longitude" ;
		NC_CHAR :processing_level = "NOAA Processing Level 4" ;
		NC_FLOAT :geospatial_lon_min = 2.5 ;
		NC_FLOAT :geospatial_lon_max = 357.5 ;
		NC_CHAR :geospatial_lon_units = "degrees_east" ;
		NC_CHAR :geospatial_lon_resolution = "5.0 degree" ;
		NC_FLOAT :geospatial_lat_min = -87.5 ;
		NC_FLOAT :geospatial_lat_max = 87.5 ;
		NC_CHAR :geospatial_lat_units = "degrees_north" ;
		NC_CHAR :geospatial_lat_resolution = "5.0 degree" ;
		NC_CHAR :geospatial_bounds = "POLYGON ((2.5 -87.5, 2.5 87.5, 357.5 87.5, 357.5 -87.5, 2.5 -87.5))" ;
		NC_CHAR :geospatial_bounds_crs = "EPSG:4326" ;
		NC_FLOAT :geospatial_vertical_min = 0 ;
		NC_FLOAT :geospatial_vertical_max = 0 ;
		NC_CHAR :geospatial_vertical_units = "meters" ;
		NC_CHAR :geospatial_vertical_resolution = "0.f" ;
		NC_CHAR :geospatial_vertical_positive = "up" ;
		NC_CHAR :time_coverage_start = "1880-01" ;
		NC_CHAR :time_coverage_end = "2022-12" ;
		NC_CHAR :time_coverage_resolution = "P1M" ;
		NC_CHAR :time_coverage_duration = "P143Y0M" ;
		NC_CHAR :references = "Vose, R. S., et al., 2012: NOAAs merged land-ocean surface temperature analysis. Bulletin of the American Meteorological Society, 93, 1677-1685. doi: 10.1175/BAMS-D-11-00241.1. Huang, B., Peter W. Thorne, et. al, 2017: Extended Reconstructed Sea Surface Temperature version 5 (ERSSTv5), Upgrades, validations, and intercomparisons. J. Climate, 30, 8179-8205. doi: 10.1175/JCLI-D-16-0836.1" ;
		NC_CHAR :climatology = "Climatology is based on 1971-2000 monthly climatology" ;
		NC_CHAR :acknowledgment = "The NOAA Global Surface Temperature Dataset is provided by the NOAA National Centers for Environmental Information(NCEI)" ;
		NC_CHAR :date_modified = "2023-01-08T18:33:09Z" ;
		NC_CHAR :date_issued = "2023-01-08T18:33:09Z" ;
}

Let’s break down what we see above.

A classic NetCDF file like this one can be broken down into 3 components - dimensions, variables and attributes.

  • Dimensions: Define the size of the variables.

  • Variables: Where the data are stored. The variables can be broken down into coordinate variables and data variables. In RNetCDF, the coordinate variables and data variables are displayed together, but if you open a NetCDF file using different software they might be separated.

    • Coordinate variables: Coordinate variables are things like longitude, latitude, time and elevation/depth. The first variable listed is time which has 1 dimension with the same name in brackets. So we know that the time variable is a 1D array with 1716 data points. It is common practice for coordinate variables to have the same name as their respective dimension, but this might not always be the case.

    • Data variables: This file has only one data variable, anom. This has 4 dimensions, so we can see that the data are stored in a 4D array with size 72 x 36 x 1 x 1716.

  • Attributes: The metadata describing the data. A CF-NetCDF file should have both global and variable attributes.

    • Variable attributes: Describe each variable

    • Global attributes: Describe the whole dataset (e.g. title, creator, keywords, a bounding box for the coordinates)

We will now have a closer look at how to access each of these components, starting at the bottom with attributes.

Attributes#

Accessing an attribute using RNetCDF can be done like

att.get.nc(nc_object, variable_name, attribute_name)

Global attributes#

For a global attribute, we assign a special variable name, NC_GLOBAL.

att.get.nc(nc_object, "NC_GLOBAL", attribute_name)

If we want to just access the creator_name attribute for example:

att.get.nc(data, "NC_GLOBAL", "creator_name")
'Boyin Huang, Xungang Yin, Ph.D'

If you want more information than just the value, you can make an inquiry. See how the syntax is almost the same for ‘getting’ and attribute as it is for ‘inquiring’ about one?

att.inq.nc(data, "NC_GLOBAL", "creator_name")
$id
16
$name
'creator_name'
$type
'NC_CHAR'
$length
30

Conventions is probably the most important global attribute because it tells you (and a machine) how to interpret the rest of the file.

att.get.nc(data, "NC_GLOBAL", "Conventions")
'CF-1.6, ACDD-1.3'

CF-1.6 refers to version 1.6 of the CF conventions, which you can find here:

https://cfconventions.org/ https://cfconventions.org/Data/cf-conventions/cf-conventions-1.6/cf-conventions.html

The CF conventions are a set of standards that define how a NetCDF file should be structured. The document linked above is extensive, but the aim is to provide a standardised way to organise many different types of data. You don’t need to read it all, but it should be your go-to place if you want to know how to structure or understand a CF-NetCDF file, or encode certain types of data.

However, the CF conventions are light on discovery metadata. Discovery metadata are metadata that can be used to find data. For example, when and where the data were collected and by whom, some keywords etc. So we also use the ACDD convention - The Attribute Convention for Data Discovery.

https://wiki.esipfed.org/Attribute_Convention_for_Data_Discovery_1-3

In most cases, if you want to find out what a global attribute means, you can visit the ACDD convention page above to find a description of the attribute. There are other conventions that someone might have included that you can also find online, but we recommend that you always follow the CF and ACDD conventions as a minimum when creating a NetCDF file.

The person who created this file should have read the relevant sections of these documents to make sure that the files comply with these conventions. There are also validators you can run your files by to make sure that you file is compliant with the conventions before you publish it. For example:

https://compliance.ioos.us/index.html

By following conventions, the data creator and user, human or machine, should be able to understand the data in the same way. A NetCDF file itself is not neccessarily FAIR because you could include any attributes or structure your data however you like. A CF-NetCDF file is FAIR.

Variable attributes#

Let’s extract a variable attribute now from the anom variable:

att.get.nc(data, "anom", "standard_name")
'surface_temperature_anomaly'

The variable name anom is not standardised. Fortunately, the standard_name variable attribute is standardised. You can find sea_water_temperature in the list of CF standard names here. https://cfconventions.org/Data/cf-standard-names/current/build/cf-standard-name-table.html

Hopefully, the data creator has selected the standard_name from the CF standard name table after reading the description for that term. You, the data user, can now also find that term and its description. This way, the data provider and the data user can share the same understanding about what the these data are. Any other CF-NetCDF file that contains the same type of data should also include the same standard_name. Controlled vocabularies like this one are understandable to machines too!

The data in the file doesn’t need to be stored with the same units, but should be stored with units that are physically equivalent. For example, in this case the units degrees_C are physically equivalent to K.

Dimensions#

To extract information about a dimension, you can do this:

dim.inq.nc(data,'lat')
$id
0
$name
'lat'
$length
36
$unlim
FALSE

If you didn’t know the name of the dimension, you could use the index of the variable to call it instead. Remember that 0 is always the first.

dim.inq.nc(data,0)
$id
0
$name
'lat'
$length
36
$unlim
FALSE

The lat dimension has 36 points. Dimensions tell you about the shape and size of your variables. In this case, we know that any variable with a dimension of only lat will have 36 data points - though some could be NaN. The dimension doesn’t tell you anything about what the values are - that information is in the variables.

Variables#

Finally, the most important part - getting at the data!

If you know the variable name, you can get the data like this:

var.get.nc(data,"lat")
  1. -87.5
  2. -82.5
  3. -77.5
  4. -72.5
  5. -67.5
  6. -62.5
  7. -57.5
  8. -52.5
  9. -47.5
  10. -42.5
  11. -37.5
  12. -32.5
  13. -27.5
  14. -22.5
  15. -17.5
  16. -12.5
  17. -7.5
  18. -2.5
  19. 2.5
  20. 7.5
  21. 12.5
  22. 17.5
  23. 22.5
  24. 27.5
  25. 32.5
  26. 37.5
  27. 42.5
  28. 47.5
  29. 52.5
  30. 57.5
  31. 62.5
  32. 67.5
  33. 72.5
  34. 77.5
  35. 82.5
  36. 87.5

Or inquire about it

var.inq.nc(data, "lat")
$id
1
$name
'lat'
$type
'NC_FLOAT'
$ndims
1
$dimids
0
$natts
10

We can get just a subset of the data. For example, to get 3 values starting from the 5th value in the array, where start is the first value to extract and count is how many values:

var.get.nc(data, "lat", start=c(5), count=c(3))
  1. -67.5
  2. -62.5
  3. -57.5

There is a lot of data in the anom variable, so we won’t display all of that below! But let’s try and extract data for a single date.

Let’s again first select based on the index using start and *count. The order in the lists corresponds to the order of the dimensions that the variable has. In this case, we are extracting data for all latitudes and longitudes (so NA is used) and from the 1st (and only) vertical coordinate. I have selected data for an arbitrary time slice, 700 in this case.

lat <- var.get.nc(data, 'lat')
lon <- var.get.nc(data, 'lon')
anom <- var.get.nc(data, 'anom', start=c(NA, NA, 1, 700), count=c(NA,NA,1,1))
anom
A matrix: 72 × 36 of type dbl
NANANANANANA-0.6202122-0.28983244 0.127612844 0.105352372-0.537931740-1.87294960 0.24818414 0.49265805 0.28210214 0.449737489 1.12946840.9146796 NANA
NANANANANANA-0.4854550-0.29407427 0.026559843 0.000770571-0.835091114-2.66000009-1.08594847 0.69367015 0.10752933 0.591097713 1.10517590.7053886 NANA
NANANANANANA-0.7393103-0.28969392 0.041384175-0.090124525-1.269892812-3.05221272-1.84167469 0.43207514 0.35837406 0.443900764 0.98414460.8509724 NANA
NANANANANANA-0.8334374-0.22463292 0.190988734-0.151320428-1.601708531-3.17991972-2.66849828 0.27449751 0.36497775 0.345626265 1.0454960 NA NANA
NANANANANANA-0.8196298-0.20011082 0.178636834-0.218324393-2.145020962-2.74029255-2.38935113-0.28059667 0.50999999 0.259044379 1.0828582 NA NANA
NANANANANANA-0.5987538-0.09562428 0.127850562-0.302159876-1.020416260-1.85013807-2.57099295-1.79307616-0.99419832 0.613359451 0.9467384 NA NANA
NANANANANANA-0.4492744-0.14773342 0.020136885-0.136485785-0.401134670-1.10070395-2.19099236-1.69648826-0.55620253 0.760314703 0.8679525 NA NANA
NANANANANANA NA NA 0.009670788-0.005256775-0.638019681-1.44743681-1.64928043-1.89835858-1.24357367 1.259999990 0.7236950 NA NANA
NANANANANANA NA NA-0.158918560-0.102225631-0.917013168-1.16364527-0.99239308-1.57622480-0.27919686-0.005481333-0.1899090 NA NANA
NANANANANANA NA-0.64733446-0.345341444-0.261111975-0.276104450-0.50022191 0.31061247-0.72260940 0.48036933 4.380000114 NA NA NANA
NANANANANANA NA-0.67590743-0.373588830-0.263846487-0.268124402 0.10986871 1.17465675 0.57172859 0.61704886 1.343866348 2.6400001 NA NANA
NANANANANANA NA-0.52490014-0.261390358-0.183519289 1.109488249 1.09304774 1.24661541 1.00859094 1.40338135 1.853716373 NA NA0.65NA
NANANANANANA NA-0.44366845-0.299334317-0.142208368 1.495387554 1.39396477 1.81454659 1.60197926 2.63795424 3.119392395 NA3.3599999 NANA
NANANANANANA NA-0.44151002-0.365199476-0.055469848 1.322556496 2.03410816 2.52891922 2.52818990 3.64879513 3.072127104 NA4.1599998 NANA
NANANANANANA NA-0.37778386-0.301796347-0.091428578 2.109943390 1.86116982 2.27722478 3.05860758 4.44784117 3.276115417 2.7100000 NA NANA
NANANANANANA NA-0.36512563-0.313503146-0.129802525 1.826648235 1.65793383 2.10578156 3.15935707 4.85814810 4.668457985 4.2700000 NA NANA
NANANANANANA NA NA-0.343594402-0.210632339 1.205056548 2.22496557 2.11366200 3.61913133 4.79895020 4.541970730 1.90195004.3800001 NANA
NANANANANANA NA NA-0.523310781-0.357023031 0.448161513 1.32473087 2.34931517 3.54763889 4.18121338 4.248987198 2.81112745.0000000 NANA
NANANANANANA NA NA-0.708950460-0.583143473-0.023835853 0.97581047 2.51909184 3.73701119 4.25832701 4.222610950 4.4118767 NA NANA
NANANANANANA NA-0.83585399-0.982789934-0.909944713 0.727405906 1.36702776 2.68381667 3.36054611 4.29253864 4.651895523 4.20358373.8599999 NANA
NANANANANANA NA-1.08239102-1.135215282-1.018131852 1.607886434 2.05043435 3.00426292 3.71028423 4.09760380 4.630562782 4.38286504.1199999 NANA
NANANANANANA NA-1.20429385-1.073588252-1.074453712 2.145003080 2.14999557 2.75228047 3.36074185 3.17259288 3.993427038 3.4505026 NA NANA
NANANANANANA NA-1.24920452-1.097619534-1.098227978 2.495729923 2.06742120 2.35480118 3.17945910 2.44510150 3.006891251 1.4027667 NA NANA
NANANANANANA NA-1.17536151-1.127874255-1.053290129 1.671961427 1.51226854 2.00921392 2.47920394 2.22450638 2.667634249 1.5593848 NA NANA
NANANANANANA NA-1.03222525-0.958126485-0.963152111 0.750443339 0.97895187 1.00236785 1.29838908 1.22042096 2.404286861 1.5253580 NA NANA
NANANANANANA NA-0.74122697-0.859642327-0.899472177-0.182048351 0.08869952 0.35434717 0.69949293 1.16713667 1.793119788 0.8606514 NA NANA
NANANANANANA NA-0.46319559-0.705830514-0.703707695-0.664974868-0.31978831 0.02538815 0.40935487 0.04900825 0.772662342 NA NA NANA
NANANANANANA NA-0.34032953-0.554033041-0.565362215-0.562718034-0.67619538-0.45037004-0.02868681 0.10339553 0.832687557 0.39000002.7400000 NANA
NANANANANANA NA-0.13787335-0.366101772-0.402459711 0.088325746-0.57997417-0.51999998 0.33000001 0.21330257 0.599803090 3.6099999 NA NANA
NANANANANANA NA NA-0.153727695-0.090792619-0.007916592-0.74654436 NA NA 0.25405270 0.460937053 NA NA NANA
NANANANANA NA NA NA-1.03765130-1.14787817-0.72745198-0.539152145-0.63528079-0.35526615 0.761579692 0.1559132 NANANANA
NANANANANA NA NA NA NA-1.33770728-0.70461655-0.496376604-0.40371022-0.07586693 1.017054319 1.0395586 NANANANA
NANANANANA NA NA NA NA-1.32537293-0.58864594-0.390182823-0.27330157 0.06163443-0.013497770-0.1581344 NANANANA
NANANANANA NA NA NA NA-1.23415732-0.45375887-0.406053036-0.15399772 0.08400441-0.975186229-1.3216939 NANANANA
NANANANANA NA NA NA NA-1.02880096-0.13268386-0.197947741 0.12996706-0.59248364-1.486913204-1.5317039 NANANANA
NANANANANA NA NA NA NA NA 0.28471875 0.528546691 0.49920291-0.81128365-3.235391378-2.7874804 NANANANA
NANANANANA NA NA NA NA NA 0.42999995 0.270000011 0.22992519-0.44362128-3.800447464-2.4703212 NANANANA
NANANANANA NA NA NA NA NA 0.51999998 0.070000000-0.57999998-2.13038158-3.040197372-1.4691701 NANANANA
NANANANANA NA NA NA NA NA 0.25999999-0.140000001-0.93999994-2.43839717-2.530547380-0.1766288 NANANANA
NANANANANA NA NA NA NA NA 0.25999999 0.189999998-1.00000024-1.90921295-2.355106115-1.1711433 NANANANA
NANANANANA NA NA NA NA NA 0.56000000-0.070000000-1.39589906-2.19961476-1.992772341-1.1521786 NANANANA
NANANANANA NA NA NA NA NA 0.28999999-0.271055728-1.62058520-4.23999977-4.360000134 NA NANANANA
NANANANANA NA NA NA NA NA 0.21408066-0.627142847-2.09491158 NA NA NA-2.50000000NANANA
NANANANANA NA-0.1697439-0.4409237-0.55458266-0.57835609-0.24019694-0.417958498-1.04271126 NA NA NA-0.62603480NANANA
NANANANANA-0.06098633-0.4206600-0.7029645-0.78887171-0.79945225 0.60682619 0.639952779-1.07690096-1.52999997-2.730000019 NA-3.19000006NANANA
NANANANANA-0.15486561-0.5678198-0.4074512-0.72860694-1.02617061 0.44225967 0.289999992-1.45528984-2.36751652 NA-2.0213497 NANANANA
NANANANANA-0.24057946-0.6678612-0.9029381-0.94882452-1.42196906-0.29198876 0.007856488-1.49762917-1.47038269-3.339999914-6.6900001 NANANANA
NANANANANA-0.22199807-0.8262340-1.3077309-1.35774004-1.31461942 0.09749806 0.119999997-1.31053293 NA-2.190000057 NA NANANANA
NANANANANA-0.36582714-0.9412854-1.2547942-1.65094185-1.47020209 0.95338994-1.179990530-0.94999999 NA NA NA 2.97000003NANANA
NANANANANA NA-0.9192676-1.4885594-1.86747491-1.41876101 0.55572647-0.991240323 NA-1.30097270-1.104978204 NA-1.09486389NANANA
NANANANANA NA NA-1.4861391-1.74675107-1.11806643 0.54016954-0.087156199-0.09771429-0.58162260-1.226686239-1.3258067-1.16933286NANANA
NANANANANA 0.34000000 NA NA NA-0.90929210 1.22658181 1.544310927 0.96281517 0.11501031 NA 0.3753299 0.05857182NANANA
NANANANANA NA NA 1.2000000 NA-0.65815240 1.07102656 1.912538052 1.35502267 0.56753224-0.107015751 0.3371410 0.77885288NANANA
NANANANANA NA-0.7499329-0.8363801 NA-0.56528622 1.04670095 1.791487932 1.47122908 0.86064625 0.440668404 NA 1.12747109NANANA
NANANANANA NA-0.7914029-0.8402827-0.76294339-0.47780380 1.11061668 1.632037044 1.31927037 0.89700294 0.494957596-0.1191137 1.26109028NANANA
NANANANANA NA-0.9720980-0.7398673-0.54816222-0.36572853 1.05496168 1.368819237 1.11766005 0.84499192 0.295163870-0.1554662 1.75000000NANANA
NANANANANA NA-0.9025080-0.6139256-0.30291057-0.30004954 0.86234093 1.040015578 0.95735621 0.59824568 0.320065022 0.5698152 NANANANA
NANANANANA NA-0.8384985-0.5307279-0.23365818-0.39668068 0.68489653 0.727147460 0.64485824 0.44672173 0.202944711 0.2979381 NANANANA
NANANANANA NA-0.7920504-0.4944986-0.16763553-0.39215466 0.53223640 0.371893227 0.33779463 0.35033759 0.096019298 0.4784548 1.41840124NANANA
NANANANANA NA-0.7084152-0.3460619 0.04035293-0.04183052-0.17381990-0.145835012 0.18178961 0.32527083-0.002842225 0.3367361 1.53253829NANANA

But in most cases, we want to select based on time (or whichever coordinate), not based on the index. So let’s now select based on a given time. The units for time are

att.get.nc(data, "time", "units")
'days since 1800-01-01 00:00:00'

There are specific recommendations on how time should be stored in CF-NetCDF files. I will try to explain briefly here, and there is a nice explanation here too: https://www.unidata.ucar.edu/software/netcdf/time/recs.html

The time variable has units that count from a user defined origin, for example “hours since 2020-01-01 00:00 UTC” or “days since 2014-01-01”. The units may be in years, days, seconds, nanoseconds, etc. Whilst this approach may seem strange at a glance, it allows the times to be stored in conventional numerical formats such as integers or floats, and to our desired precision. This is much more efficient than using a long timestamp string for each coordinate.

Some softwares know how to interpret this and will convert the data into timestamps in when you extract the data from a CF-NetCDF file. For example, xarray in Python does this, so does Panoply. Unfortunately, RNetCDF does not - at least not at the time of writing.

So now let’s extract the time series, work out what index in the time series our desired date corresponds to, and then select from the anom variable based on that index.

desired_date <- as.Date('2020-01-01')
days_since_1800 <- as.numeric(difftime(desired_date, as.Date('1800-01-01'), units = 'days'))

time <- var.get.nc(data, "time") 
# Finding index of the value
time_index <- which(time == days_since_1800)

anom <- var.get.nc(data, 'anom', start=c(NA, NA, 1, time_index), count=c(NA,NA,1,1))
anom
A matrix: 72 × 36 of type dbl
NA NA NA-0.09-0.4774149-0.28590867-0.147390142 0.162335649 0.3555031419 0.0853288400.9572873 2.7277462 1.795067 1.0600646 0.9244725 0.901413739 0.7911479 1.0034733 NANA
NA NA NA NA-0.5136998-0.28014940-0.160545558 0.158801332 0.3941240311 0.1554638890.7803320 2.8799996 2.655742 2.0247676 2.4968357 0.878529668 0.6843208 0.8648409 NANA
NA NA NA 0.30-0.4267291-0.27559584-0.183748201 0.202584669 0.4098227322 0.2115975171.1051872 2.7000260 3.316797 3.3139091 6.5279703 1.357819557 0.7310358 0.5478907 NANA
NA NA NA NA-0.2727111-0.23345284-0.168438852 0.203091472 0.3795720339 0.1547373831.1962049 1.5803304 4.079368 2.4789727 5.1615229 4.826246738 0.8632154-0.7200000 NANA
NA NA NA NA-0.1808161-0.16997650-0.113978557 0.188084275 0.3441761434 0.1556928161.5916607 0.6660571 5.440616 2.8465347 3.7294011 4.428432941 0.8453974 NA NANA
NA NA NA NA-0.1048512-0.08315404-0.078388907 0.161499396 0.3325377405 0.2421529141.3523049 3.2296455 6.082641 6.5513883 7.1897578 3.872640371 0.7872372-0.9600000 NANA
NA NA NA NA-0.2039971-0.03734734-0.050673340 0.083849691 0.2672915161 0.3200768831.2378309 3.4596205 6.688095 8.1772079 7.9278369 3.297797918 0.7249542 NA NANA
NA NA 2.24 NA 0.4100000 0.04984033-0.030647313 0.002807403 0.2073578984 0.0603833271.0983480 3.6145263 7.006868 8.3200865 7.6957769 2.810969353 0.2470920 NA NANA
NA NA NA 1.85 NA 0.09618616-0.021199374-0.029650999-0.0445042588-0.0360179801.8151871 5.7194242 7.489800 8.2420597 7.5445147 1.702046871 0.3923281 NA NANA
NA NA NA NA 0.0800000 0.09327671 0.001323494-0.059333336-0.1571347415-0.1771692632.0461519 5.5822577 7.721563 8.3688650 7.5512500 2.840023041 0.4435947 NA NANA
NA NA NA NA NA 0.10112605 0.023530630-0.102159321-0.1461249292-0.3156257272.3329496 5.2566156 7.872991 8.2446203 6.9870377 4.032630920 0.8986476 NA NANA
NA NA NA NA NA 0.15525936 0.039516296-0.160253525-0.4670970440-0.6940613394.3591695 5.3242002 7.156808 7.2001529 6.7234974 4.168582439 NA NA5.44NA
NA NA NA NA 0.1200000 0.17133424 0.020352060-0.183834001-0.5728002191-0.8924338823.5030200 6.0381327 6.840115 6.3887873 6.9327917 4.684739113 NA NA NANA
NA NA NA 0.51-0.1045271 0.12875792 0.010437957-0.192575157-0.6314202547-1.0800383091.9361970 4.4810176 6.170854 5.7854176 5.9653182 4.766786575 NA NA NANA
NA NA NA NA NA 0.12464470 0.036503941-0.108508967-0.4493696690-0.9685895441.8712949 3.4052238 6.302463 5.9270997 6.3741350 3.730565071 5.4499998 NA NANA
NA-2.74-6.49 NA 0.2366797 0.18782446 0.133120105 0.082596302-0.3003827631-0.7513652442.1469994 3.8464892 5.916843 6.3851500 6.4738703 5.140623093 NA 2.2100000 NANA
NA NA NA NA NA 0.18006249 0.205805823 0.132874653-0.2052679509-0.6812006242.6520324 3.8627431 4.681370 6.7149715 7.6431904 8.325294495 2.4645054 NA NANA
-3.23 NA NA NA NA 0.21591021 0.297305465 0.191144824 0.0002424157-0.4752186831.5729918 3.1421788 3.056231 6.2712049 8.8811817 8.233824730 4.2742991 2.9200001 NANA
NA NA NA NA 1.4100000 0.24058017 0.389482826 0.454154611 0.3260739446-0.1302784091.1721898 1.9892517 4.083584 7.7274904 8.9799690 8.900437355 7.3149533 3.1400001 NANA
NA NA NA NA NA 0.08222742 0.462933093 0.622475207 0.6073665023 0.2006321401.4139801 1.8027588 2.745549 7.4859943 9.009973510.146947861 7.4159012 NA NANA
NA NA NA NA NA 0.22611250 0.510275364 0.726758242 0.6986377239 0.3182548282.4283347 1.5985538 2.620578 5.839755110.706704110.549728394 7.0739231 1.8500000 NANA
NA NA 1.25 NA NA 0.26540589 0.580444694 0.760604620 0.6664374471 0.2397194212.6909816 3.2365203 1.660322 6.376709511.3682051 9.817378998 6.7846031 NA NANA
NA NA NA NA 1.4400001 0.23749603 0.580396712 0.748356700 0.6247741580 0.1044428051.9244378 1.9029524 2.724763 8.6504669 9.4975271 7.550381184 5.2764120 NA NANA
NA NA NA NA NA 0.18075460 0.548415363 0.727091372 0.5926325321 0.1064045282.4785178 3.5415964 3.099287 6.4127460 9.1109905 7.136222363 3.9328668 NA NANA
NA NA 2.81 NA NA 0.16377415 0.523425102 0.596909940 0.3836425245 0.1623751072.8302016 3.0401354 3.149857 5.7515340 8.5855122 6.447008133 3.2445862 NA NANA
NA NA NA NA NA 0.15745857 0.393516779 0.449483484 0.2567939758 0.1809070413.3748968 2.8877025 3.324666 8.0333014 7.5390706 4.663214207 2.3414955 NA NANA
NA NA NA NA NA 0.09594598 0.209859416 0.192347363 0.1322625130 0.1194960552.9328752 4.0491333 3.670716 5.6424479 5.5709038 3.862676620 NA NA NANA
NA NA NA NA 0.5700000-0.01971135 0.086351745 0.022201179-0.0598816760 0.0093541721.2883152 2.4070759 3.116643 3.3065968 1.3663971 1.190155149-0.2100000 1.8000000 NANA
NA NA NA NA 0.0400000-0.05072745 0.079560325-0.014219291-0.2375546545-0.0145742891.0367877 1.1600000 2.320000 1.5100000-0.6620501-0.005278349 NA NA NANA
NA NA NA NA NA-0.01008736 0.107823193-0.026469907-0.4114962518 0.0904239120.8417667-1.3527929-1.477125-0.9154379-0.7692944-0.279117882-0.1200000 NA NANA
NA-1.28 NA-0.4698156-0.484841824 0.09154083 0.6440986395 0.86868376 1.36309803 1.814970611.23196691.09131396 0.6522085071 0.52465075-5.436241150-4.5521069-3.09999990 NA NANA
NA NA NA-0.3934032-0.431364000 0.14756560 0.5137752891 0.61684585 1.08714056 1.542632821.12220751.25820720 0.9412134290 0.69965726-5.388529778-3.2256734-2.76999998 NA NANA
NA-2.87 NA NA-0.414644599 0.16411288 0.3302588761 0.34657753 0.78035277 1.232452870.77967551.34756613 1.1005250216 0.42685211-3.570737123-1.3942188 NA NA NANA
NA NA NA NA-0.280095577 0.17187184 0.1874384582 0.13639207 0.44986501 0.777683500.40361150.90187240 0.6559444666-0.16429505-1.692900538-0.3730847 NA NA NANA
NA NA NA NA-0.268713385 0.12222777 0.0106154578-0.06838228 0.11478064 0.310414910.23446770.44264463 0.1042358428-1.29303682-1.633660436-0.9327446 1.82000005 NA NANA
NA-0.92 NA NA-0.197808459 0.11562244-0.0397457480-0.14111784-0.07678779-0.097379600.90990601.16355646 1.2394207716-1.14657307-1.999983311 1.0552218 1.53999996 NA NANA
NA-0.03 NA-0.5619973-0.124478303 0.22203135 0.1132662669-0.07467450-0.20421869-0.420117772.41000012.74000001 0.4900098443-0.49499625 0.268961966 2.1826148 2.40000010 0.6700000 NANA
NA NA NA NA-0.064553842 0.28961024 0.1855113804-0.09331908-0.28049749-0.453464362.26000003.00000000-0.0399999619 1.77955723 2.390084028 1.3966146 NA NA NANA
NA NA NA NA 0.014955498 0.37070805 0.2341449112-0.03662325-0.17936961-0.211004822.30000002.85999990 2.6600768566 4.73088789 3.017217636 0.2097187 1.55999994 NA NANA
NA NA NA-0.4620867 0.151447281 0.49756023 0.3641515374 0.17027101 0.09872701 0.156365603.03000002.63000011 4.0700082779 4.46887016 3.263908625 1.9812412-8.56999969 0.3200000 NANA
NA NA NA NA 0.220768183 0.60737258 0.4621249437 0.31238753 0.34462538 0.427488362.13000013.33999991 5.2585630417 5.04434347 4.643915176 1.7200259 NA NA NANA
NA NA NA NA 0.250222027 0.62426901 0.4832903743 0.44083375 0.49659431 0.568564953.64000014.28217030 6.2233800888 5.96999979 6.269999981 1.8399289-6.21999979 NA NANA
NA NA NA NA 0.136668548 0.56714785 0.4752582014 0.51889729 0.57429111 0.626320063.65358072.14575386 5.9827761650 NA NA 4.1399999-1.72000003-2.1800001 NANA
NA NA NA NA 0.097286239 0.47690770 0.4453872740 0.56508142 0.55497044 0.493222651.97829903.54007483 3.8028151989 NA 3.039999962 1.4100000-0.11851262-2.1099999 NANA
NA NA NA NA 0.009490349 0.41094211 0.4368603826 0.58647507 0.48691010 0.357133183.07590894.80000353 3.9606328011 3.91000009 3.099999905-0.6500000-2.15000010 NA NANA
NA NA NA 0.4600000-0.044147618 0.40844291 0.5009096265 0.52736115 0.55674899 0.441668632.81523284.57000017 4.1282920837 3.25530601 NA 0.3736914 NA NA NANA
NA NA NA 1.6100000-0.044302195 0.33518147 0.4985094666 0.48975295 0.35365939 1.247406961.20599403.00945592 2.3990960121 1.93532622 0.680000007-4.4299998-0.89999998-1.6400000 NANA
NA NA NA 0.8700000-0.225116432 0.14925167 0.4378926158 0.40267450 0.07255031-0.044564571.83859880.07792632 1.3569607735 1.65999997 NA 0.3300000 NA NA NANA
NA NA2.78 NA NA 0.05853839 0.4594276845 0.36315942-0.17322914-0.207915321.93924500.25425702 0.4623103738 1.67879176 1.897409081 NA 1.22000003 NA NANA
NA NA NA NA NA-0.51736933 0.4211530983 0.29534271-0.42727485-0.555699291.20385420.28530520 0.8195887208 1.55946314 1.503227830 1.0871487-0.12800276 NA NANA
NA NA NA NA NA-0.42621243 0.3630766273 0.41877541-0.13659766-0.303128420.80466930.69055551 1.0235382318 1.11682236-0.628074944-1.5977148-1.20875096 NA NANA
NA NA NA NA NA-0.16678715 0.2627283633 0.57024199 0.33911827 0.148979110.70987150.91861933 0.8771149516 0.85675263 0.784333229 0.5525564 0.04455871 NA NANA
NA NA NA NA-0.194812909-0.22618596 0.1277418733 0.42979327 0.35964090 0.311775060.70692810.64331269 0.4695768356 0.56522942 0.747213602 1.0081477 0.47750920 NA NANA
NA NA1.57 NA-0.130373791-0.22180483 0.0003080616 0.35777867 0.40363389 0.333340560.73668060.14018732 0.0000656803 0.26645169 0.239929050 NA 0.46911913 NA NANA
NA NA0.94 NA-0.153345466-0.22729057-0.0965210348 0.38904455 0.59542358 0.431899610.76645510.03444019-0.2368861586-0.01529107 0.045474380 NA 0.44222745 NA NANA
NA NA NA NA-0.158705324-0.23096702-0.1192000657 0.35051736 0.65484744 0.513235990.77154140.12901822-0.0952277705-0.05363240 0.078593031 0.1726537 5.59999990 NA NANA
NA NA NA NA-0.120913103-0.20881623-0.0960118026 0.30507845 0.58462906 0.472047920.74758510.26955667-0.0668849200-0.20265251-0.004614398 0.5638793 NA 2.9200001-0.25NA
NA 0.47 NA NA-0.149685591-0.19056554-0.0408856384 0.30412224 0.44861937 0.257046910.59286070.39582220 0.2603332698-0.14342251 0.102634087 0.4348391 0.84614593 NA NANA
NA NA NA 0.8000000-0.259972930-0.20408601-0.0251651946 0.26172209 0.35818422 0.232276500.79172970.61566544 0.8431401253 0.48186198 0.462726474 0.6100513 0.92998093 NA NANA
-2.64 NA0.97 0.2700000-0.361352175-0.23032261-0.0848020315 0.20081067 0.32587203 0.113054671.02208411.14713442 1.6009719372 0.96171165 0.840205550 0.7000809 0.71420395 0.6590008 NANA

If you don’t know the variable name, you can use the index instead.

var.inq.nc(data, 2)
$id
2
$name
'lon'
$type
'NC_FLOAT'
$ndims
1
$dimids
1
$natts
10

Citing data#

Remember, if you use a scientific dataset in a publication you should cite it in the same way you cite a paper - in the list of references. You can also include a statement in the data availability statement, but this should be as well as citing the dataset in the list of references.

This is the recommended citation for the data used in this tutorial - remember to update the data accessed:

H.-M. Zhang, B. Huang, J. H. Lawrimore, M. J. Menne, and T. M. Smith (2019): NOAA Global Surface Temperature Dataset (NOAAGlobalTemp), Version 5.0. NOAA National Centers for Environmental Information. doi:10.25921/9qth-2p70 Accessed 2024-04-10.

How to cite this course#

If you think this course contributed to the work you are doing, consider citing it in your list of references. Here is a recommended citation:

Marsden, L. (2024, May 31). NetCDF in R - from beginner to pro. Zenodo. https://doi.org/10.5281/zenodo.11400754

And you can navigate to the publication and export the citation in different styles and formats by clicking the icon below.

DOI