Skip to content

Opening NetCDF files

In this example we are going to use a NetCDF file. To open a single data file we first need to load the appropriate backend package via using NetCDF.

File with one variable

julia
using YAXArrays, NetCDF
using DiskArrays
using Downloads
url = "https://www.unidata.ucar.edu/software/netcdf/examples/tos_O1_2001-2002.nc"
filename = Downloads.download(url, "tos_O1_2001-2002.nc") # you pick your own path
julia
julia> c = Cube(filename)
╭────────────────────────────────────────────────╮
180×170×24 YAXArray{Union{Missing, Float32},3}
├────────────────────────────────────────────────┴─────────────────────── dims ┐
lon Sampled{Float64} 1.0:2.0:359.0 ForwardOrdered Regular Points,
lat Sampled{Float64} -79.5:1.0:89.5 ForwardOrdered Regular Points,
Ti  Sampled{CFTime.DateTime360Day} [CFTime.DateTime360Day(2001-01-16T00:00:00), …, CFTime.DateTime360Day(2002-12-16T00:00:00)] ForwardOrdered Irregular Points
├──────────────────────────────────────────────────────────────────── metadata ┤
  Dict{String, Any} with 10 entries:
  "units"          => "K"
  "missing_value"  => 1.0f20
  "history"        => " At   16:37:23 on 01/11/2005: CMOR altered the data in t…
  "cell_methods"   => "time: mean (interval: 30 minutes)"
  "name"           => "tos"
  "long_name"      => "Sea Surface Temperature"
  "original_units" => "degC"
  "standard_name"  => "sea_surface_temperature"
  "_FillValue"     => 1.0f20
  "original_name"  => "sosstsst"
├─────────────────────────────────────────────────────────────────── file size ┤
  file size: 2.8 MB
└──────────────────────────────────────────────────────────────────────────────┘

File with multiple variables, mixed dimensions

When the dataset contains variables with different dimensions you should use open_dataset as in

julia
path2file = "https://www.unidata.ucar.edu/software/netcdf/examples/sresa1b_ncar_ccsm3-example.nc"
filename = Downloads.download(path2file, "sresa1b_ncar_ccsm3-example.nc")
c = open_dataset(filename)
julia
julia> c
YAXArray Dataset
Shared Axes:
lon Sampled{Float32} 0.0f0:1.40625f0:358.59375f0 ForwardOrdered Regular Points,
lat Sampled{Float32} -88.927734f0:1.4004368f0:88.927734f0 ForwardOrdered Regular Points
Variables:

pr
Ti Sampled{CFTime.DateTimeNoLeap} [CFTime.DateTimeNoLeap(2000-05-16T12:00:00)] ForwardOrdered Irregular Pointsmsk_rgn, area,
ua
plev Sampled{Float64} [100000.0, 92500.0, …, 2000.0, 1000.0] ReverseOrdered Irregular Points,
Ti   Sampled{CFTime.DateTimeNoLeap} [CFTime.DateTimeNoLeap(2000-05-16T12:00:00)] ForwardOrdered Irregular Points
tas
Ti Sampled{CFTime.DateTimeNoLeap} [CFTime.DateTimeNoLeap(2000-05-16T12:00:00)] ForwardOrdered Irregular Points
Properties: Dict{String, Any}("cmd_ln" => "bds -x 256 -y 128 -m 23 -o /data/zender/data/dst_T85.nc", "references" => "Collins, W.D., et al., 2005:\n The Community Climate System Model, Version 3\n Journal of Climate\n \n Main website: http://www.ccsm.ucar.edu", "CVS_Id" => "\$Id\$", "model_name_english" => "NCAR CCSM", "creation_date" => "", "acknowledgment" => " Any use of CCSM data should acknowledge the contribution\n of the CCSM project and CCSM sponsor agencies with the \n following citation:\n 'This research uses data provided by the Community Climate\n System Model project (www.ccsm.ucar.edu), supported by the\n Directorate for Geosciences of the National Science Foundation\n and the Office of Biological and Environmental Research of\n the U.S. Department of Energy.'\nIn addition, the words 'Community Climate System Model' and\n 'CCSM' should be included as metadata for webpages referencing\n work using CCSM data or as keywords provided to journal or book\npublishers of your manuscripts.\nUsers of CCSM data accept the responsibility of emailing\n citations of publications of research using CCSM data to\n ccsm@ucar.edu.\nAny redistribution of CCSM data must include this data\n acknowledgement statement.", "realization" => 1, "contact" => "ccsm@ucar.edu", "Conventions" => "CF-1.0", "history" => "Tue Oct 25 15:08:51 2005: ncks -O -x -v va -m sresa1b_ncar_ccsm3_0_run1_200001.nc sresa1b_ncar_ccsm3_0_run1_200001.nc\nTue Oct 25 15:07:21 2005: ncks -d time,0 sresa1b_ncar_ccsm3_0_run1_200001_201912.nc sresa1b_ncar_ccsm3_0_run1_200001.nc\nTue Oct 25 13:29:43 2005: ncks -d time,0,239 sresa1b_ncar_ccsm3_0_run1_200001_209912.nc /var/www/html/tmp/sresa1b_ncar_ccsm3_0_run1_200001_201912.nc\nThu Oct 20 10:47:50 2005: ncks -A -v va /data/brownmc/sresa1b/atm/mo/va/ncar_ccsm3_0/run1/sresa1b_ncar_ccsm3_0_run1_va_200001_209912.nc /data/brownmc/sresa1b/atm/mo/tas/ncar_ccsm3_0/run1/sresa1b_ncar_ccsm3_0_run1_200001_209912.nc\nWed Oct 19 14:55:04 2005: ncks -F -d time,01,1200 /data/brownmc/sresa1b/atm/mo/va/ncar_ccsm3_0/run1/sresa1b_ncar_ccsm3_0_run1_va_200001_209912.nc /data/brownmc/sresa1b/atm/mo/va/ncar_ccsm3_0/run1/sresa1b_ncar_ccsm3_0_run1_va_200001_209912.nc\nWed Oct 19 14:53:28 2005: ncrcat /data/brownmc/sresa1b/atm/mo/va/ncar_ccsm3_0/run1/foo_05_1200.nc /data/brownmc/sresa1b/atm/mo/va/ncar_ccsm3_0/run1/foo_1192_1196.nc /data/brownmc/sresa1b/atm/mo/va/ncar_ccsm3_0/run1/sresa1b_ncar_ccsm3_0_run1_va_200001_209912.nc\nWed Oct 19 14:50:38 2005: ncks -F -d time,05,1200 /data/brownmc/sresa1b/atm/mo/va/ncar_ccsm3_0/run1/va_A1.SRESA1B_1.CCSM.atmm.2000-01_cat_2099-12.nc /data/brownmc/sresa1b/atm/mo/va/ncar_ccsm3_0/run1/foo_05_1200.nc\nWed Oct 19 14:49:45 2005: ncrcat /data/brownmc/sresa1b/atm/mo/va/ncar_ccsm3_0/run1/va_A1.SRESA1B_1.CCSM.atmm.2000-01_cat_2079-12.nc /data/brownmc/sresa1b/atm/mo/va/ncar_ccsm3_0/run1/va_A1.SRESA1B_1.CCSM.atmm.2080-01_cat_2099-12.nc /data/brownmc/sresa1b/atm/mo/va/ncar_ccsm3_0/run1/va_A1.SRESA1B_1.CCSM.atmm.2000-01_cat_2099-12.nc\nCreated from CCSM3 case b30.040a\n by wgstrand@ucar.edu\n on Wed Nov 17 14:12:57 EST 2004\n \n For all data, added IPCC requested metadata"…)

Afterwards, selecting a variable as usual works, i.e.

julia
julia> c["ua"]
╭──────────────────────────────────────────────────╮
256×128×17×1 YAXArray{Union{Missing, Float32},4}
├──────────────────────────────────────────────────┴───────────────────── dims ┐
lon  Sampled{Float32} 0.0f0:1.40625f0:358.59375f0 ForwardOrdered Regular Points,
lat  Sampled{Float32} -88.927734f0:1.4004368f0:88.927734f0 ForwardOrdered Regular Points,
plev Sampled{Float64} [100000.0, 92500.0, …, 2000.0, 1000.0] ReverseOrdered Irregular Points,
Ti   Sampled{CFTime.DateTimeNoLeap} [CFTime.DateTimeNoLeap(2000-05-16T12:00:00)] ForwardOrdered Irregular Points
├──────────────────────────────────────────────────────────────────── metadata ┤
  Dict{String, Any} with 11 entries:
  "cell_methods"   => "time: mean (interval: 1 month)"
  "long_name"      => "eastward_wind"
  "_FillValue"     => 1.0f20
  "original_units" => "m s-1"
  "missing_value"  => 1.0f20
  "history"        => "Interpolated U with NCL 'vinth2p_ecmwf'"
  "name"           => "ua"
  "units"          => "m s-1"
  "standard_name"  => "eastward_wind"
  "comment"        => "Created using NCL code CCSM_atmm_2cf.ncl on\n machine ea…
  "original_name"  => "U"
├─────────────────────────────────────────────────────────────────── file size ┤
  file size: 2.12 MB
└──────────────────────────────────────────────────────────────────────────────┘

or

julia
julia> c["tas"]
╭───────────────────────────────────────────────╮
256×128×1 YAXArray{Union{Missing, Float32},3}
├───────────────────────────────────────────────┴──────────────────────── dims ┐
lon Sampled{Float32} 0.0f0:1.40625f0:358.59375f0 ForwardOrdered Regular Points,
lat Sampled{Float32} -88.927734f0:1.4004368f0:88.927734f0 ForwardOrdered Regular Points,
Ti  Sampled{CFTime.DateTimeNoLeap} [CFTime.DateTimeNoLeap(2000-05-16T12:00:00)] ForwardOrdered Irregular Points
├──────────────────────────────────────────────────────────────────── metadata ┤
  Dict{String, Any} with 13 entries:
  "cell_method"    => "time: mean"
  "coordinates"    => "height"
  "cell_methods"   => "time: mean (interval: 1 month)"
  "long_name"      => "air_temperature"
  "_FillValue"     => 1.0f20
  "original_units" => "K"
  "missing_value"  => 1.0f20
  "history"        => "Added height coordinate"
  "name"           => "tas"
  "units"          => "K"
  "standard_name"  => "air_temperature"
  "comment"        => "Created using NCL code CCSM_atmm_2cf.ncl on\n machine ea…
  "original_name"  => "TREFHT"
├─────────────────────────────────────────────────────────────────── file size ┤
  file size: 128.0 KB
└──────────────────────────────────────────────────────────────────────────────┘

Note that their output is a YAXArray.