# Opening 4 dimensional NetCDF to extract mean values from multiple depths / dates

Hello,

I have copernicus marine netcdf data with 4 dimensions: lat, lon, depth (32 levels) and time (5 days). I would like to be able to do 3 things:

1. read in the "no3" (or any other variable) from the netcdf for individual days but including all depth layers (in this case daily raster bricks of 32 depth levels)
2. calculate the mean of the no3 values for a subset of depths (e.g. mean between 0-10m, 10-20m etc) for individual days
3. extract the mean of the values using spatial data points

I have used raster calc on a brick before but that was to get the mean of multiple days with only a single value of depth, I cannot for the life of me get raster to read in all the depth layers together for a single day.

I have tried following this and the ncdf4 documentation but I don't understand how to extract a spatial point from the arrays produced as there is no crs (which as it is a 1/4 degree grid i think is WGS84).

I do not mind how this is achieved, but an understanding of how to make it work would be hugely appreciated. Apologies if this is too long, I wanted to show where I was falling down.

Thank you

``````library(raster)
library(ncdf4)

# Time and Location data that I would like to extract values from  --------

mDate <- c(20160306, 20160307, 20160308, 20160309, 20160310)
mLat <- c(50.32, 51.0, 51.5, 51.7, 51.8)
mLon <- c(-33.2, -33.6, -33.8, -33.9, -33.2)
mDat <- as.data.frame(cbind(mDate, mLat, mLon))
mDat
#>      mDate  mLat  mLon
#> 1 20160306 50.32 -33.2
#> 2 20160307 51.00 -33.6
#> 3 20160308 51.50 -33.8
#> 4 20160309 51.70 -33.9
#> 5 20160310 51.80 -33.2

# convert to spatial points data frame
locations <- mDat
coordinates(locations) <- ~mLon + mLat # save lat and lon as spatial points

# open a NetCDF file ------------------------------------------------------

ncfile <- "D:/cmems_mod_glo_bgc_my_0.25_P1D-m_TESTFILE.nc"
ncin <- nc_open(ncfile)

# Check contents and structure of nc file ---------------------------------

#Some basic information can be obtained using the print() function, more verbose information can be gotten with the str() function.
print(ncin)
#> File D:/cmems_mod_glo_bgc_my_0.25_P1D-m_TESTFILE.nc (NC_FORMAT_CLASSIC):
#>
#>      6 variables (excluding dimension variables):
#>         float no3[longitude,latitude,depth,time]
#>             long_name: Nitrate
#>             standard_name: mole_concentration_of_nitrate_in_sea_water
#>             units: mmol m-3
#>             unit_long: millimoles of Nitrate per cubic meter
#>             _FillValue: 9.96920996838687e+36
#>             _ChunkSizes: 1
#>              _ChunkSizes: 15
#>              _ChunkSizes: 137
#>              _ChunkSizes: 288
#>         float o2[longitude,latitude,depth,time]
#>             long_name: Dissolved Oxygen
#>             standard_name: mole_concentration_of_dissolved_molecular_oxygen_in_sea_water
#>             units: mmol m-3
#>             unit_long: millimoles of Oxygen per cubic meter
#>             _FillValue: 9.96920996838687e+36
#>             _ChunkSizes: 1
#>              _ChunkSizes: 15
#>              _ChunkSizes: 137
#>              _ChunkSizes: 288
#>         float po4[longitude,latitude,depth,time]
#>             long_name: Phosphate
#>             standard_name: mole_concentration_of_phosphate_in_sea_water
#>             units: mmol m-3
#>             unit_long: millimoles of Phosphate per cubic meter
#>             _FillValue: 9.96920996838687e+36
#>             _ChunkSizes: 1
#>              _ChunkSizes: 15
#>              _ChunkSizes: 137
#>              _ChunkSizes: 288
#>         float chl[longitude,latitude,depth,time]
#>             long_name: Total Chlorophyll
#>             standard_name: mass_concentration_of_chlorophyll_a_in_sea_water
#>             units: mg m-3
#>             unit_long: milligram of Chlorophyll per cubic meter
#>             _FillValue: 9.96920996838687e+36
#>             _ChunkSizes: 1
#>              _ChunkSizes: 15
#>              _ChunkSizes: 137
#>              _ChunkSizes: 288
#>         float si[longitude,latitude,depth,time]
#>             long_name: Dissolved Silicate
#>             standard_name: mole_concentration_of_silicate_in_sea_water
#>             units: mmol m-3
#>             unit_long: millimoles of Silicate per cubic meter
#>             _FillValue: 9.96920996838687e+36
#>             _ChunkSizes: 1
#>              _ChunkSizes: 15
#>              _ChunkSizes: 137
#>              _ChunkSizes: 288
#>         float nppv[longitude,latitude,depth,time]
#>             long_name: Total Primary Production of Phyto
#>             standard_name: net_primary_production_of_biomass_expressed_as_carbon_per_unit_volume_in_sea_water
#>             units: mg m-3 day-1
#>             unit_long: milligrams of Carbon per cubic meter per day
#>             _FillValue: 9.96920996838687e+36
#>             _ChunkSizes: 1
#>              _ChunkSizes: 15
#>              _ChunkSizes: 137
#>              _ChunkSizes: 288
#>
#>      4 dimensions:
#>         time  Size:5
#>             long_name: Time (hours since 1950-01-01)
#>             standard_name: time
#>             calendar: gregorian
#>             units: hours since 1950-01-01 00:00:00
#>             axis: T
#>             _ChunkSizes: 1024
#>             _CoordinateAxisType: Time
#>             valid_min: 580116
#>             valid_max: 580212
#>         depth  Size:32
#>             valid_min: 0.505760014057159
#>             valid_max: 221.141204833984
#>             units: m
#>             positive: down
#>             unit_long: Meters
#>             long_name: Depth
#>             standard_name: depth
#>             axis: Z
#>             _ChunkSizes: 75
#>             _CoordinateAxisType: Height
#>             _CoordinateZisPositive: down
#>         latitude  Size:9
#>             valid_min: 50
#>             valid_max: 52
#>             step: 0.25
#>             units: degrees_north
#>             unit_long: Degrees North
#>             long_name: Latitude
#>             standard_name: latitude
#>             axis: Y
#>             _ChunkSizes: 681
#>             _CoordinateAxisType: Lat
#>         longitude  Size:5
#>             valid_min: -34
#>             valid_max: -33
#>             step: 0.25
#>             units: degrees_east
#>             unit_long: Degrees East
#>             long_name: Longitude
#>             standard_name: longitude
#>             axis: X
#>             _ChunkSizes: 1440
#>             _CoordinateAxisType: Lon
#>
#>     17 global attributes:
#>         product: GLOBAL_REANALYSIS_BIO_001_029
#>         producer: CMEMS - Global Monitoring and Forecasting Centre
#>         title: Daily mean fields for product GLOBAL_REANALYSIS_BIO_001_029
#>         area: GLOBAL
#>         quality_information_document: http://marine.copernicus.eu/documents/QUID/CMEMS-GLO-QUID-001-029.pdf
#>         Conventions: CF-1.6
#>         credit: E.U. Copernicus Marine Service Information (CMEMS)
#>         contact: servicedesk.cmems@mercator-ocean.eu
#>         references: http://marine.copernicus.eu
#>         source: MERCATOR FREEBIORYS2V4
#>         licence: http://marine.copernicus.eu/services-portfolio/service-commitments-and-licence/
#>         dataset: global-reanalysis-bio-001-029-daily
#>         institution: Mercator Ocean
#>         product_user_manual: http://marine.copernicus.eu/documents/PUM/CMEMS-GLO-PUM-001-029.pdf
#>         _CoordSysBuilder: ucar.nc2.dataset.conv.CF1Convention
#>         comment:
#>         history: Data extracted from dataset http://localhost:8080/thredds/dodsC/cmems_mod_glo_bgc_my_0.25_P1D-m

# structure of nc file
names(ncin)
#>  [1] "filename"    "writable"    "id"          "error"       "safemode"
#>  [6] "format"      "is_GMT"      "groups"      "fqgn2Rindex" "ndims"
#> [11] "natts"       "dim"         "unlimdimid"  "nvars"       "var"
# Names of the variables
ncvars <- names(ncin\$var)
ncvars
#> [1] "no3"  "o2"   "po4"  "chl"  "si"   "nppv"
# Names of the dimensions
ncdims <- names(ncin\$dim)
ncdims
#> [1] "time"      "depth"     "latitude"  "longitude"

# 2.2 Get coordinate (and time depth) variables ---------------------------

#how many latitude dimensions are there?
lat <- ncvar_get(ncin, "latitude")
nlat <- dim(lat)
#> [1] 50.00 50.25 50.50 50.75 51.00 51.25

#how many longitude dimensions are there?
lon <- ncvar_get(ncin, "longitude")
nlon <- dim(lon)
#> [1] -34.00 -33.75 -33.50 -33.25 -33.00

# how many lon and lat
print(c(nlon,nlat))
#> [1] 5 9

# Time dimension (with aim of naming and selecting files)

t <- ncvar_get(ncin, "time")
tunits <- ncatt_get(ncin, "time", "units")
tunits\$value # [1] "hours since 1950-01-01"
#> [1] "hours since 1950-01-01 00:00:00"
nctime_origin <- substr(tunits\$value,13,22)
nt <- dim(t)
nt
#> [1] 5

# Convert 't' vector to human readable time vector
th <- as.POSIXct(nctime_origin, tz="UTC") + as.difftime(t, units="hours")
th
#> [1] "2016-03-06 12:00:00 UTC" "2016-03-07 12:00:00 UTC"
#> [3] "2016-03-08 12:00:00 UTC" "2016-03-09 12:00:00 UTC"
#> [5] "2016-03-10 12:00:00 UTC"

# Depth dimension as a vector

d <- ncvar_get(ncin, "depth")
d
#>  [1]   0.505760   1.555855   2.667682   3.856280   5.140361   6.543034
#>  [7]   8.092519   9.822750  11.773680  13.991040  16.525320  19.429800
#> [13]  22.757620  26.558300  30.874559  35.740200  41.180019  47.211891
#> [19]  53.850639  61.112839  69.021683  77.611160  86.929428  97.041313
#> [25] 108.030296 120.000000 133.075806 147.406204 163.164505 180.549896
#> [31] 199.789993 221.141205
dunits <- ncatt_get(ncin, "depth", "units")

# Attempts to read data from a netCDF file ----------------------------------

# I can open this with ncvar_get NOT RUN
#ncvar_get(nc = ncin, varid = "no3")

# From ncdf4 documentation...This illustrates how to read all the data from a variable
v1 <- ncin\$var\$no3  #[[1]]
data1 <- ncvar_get( ncin, v1 ) # by default, reads ALL the data
print(paste("Data for var ",v1\$name,":",sep=""))
#> [1] "Data for var no3:"
#print(data1)

# I can get a all depth slices of the first time slice using
data1[-1,-1,-1, 1]
#> , , 1
#>
#>          [,1]     [,2]     [,3]     [,4]     [,5]     [,6]     [,7]     [,8]
#> [1,] 5.537858 7.385007 7.124174 6.365521 6.583226 6.622046 7.529742 9.653507
#> [2,] 5.182127 6.329475 7.077748 6.622588 6.720418 6.650584 6.753753 8.555264
#> [3,] 5.217476 5.690938 6.834893 6.722522 6.753973 6.809534 6.821626 7.735375
#> [4,] 5.843135 5.774103 6.703818 6.791099 6.769490 6.902397 6.894868 7.256358
#>
#> , , 2
#>
#>          [,1]     [,2]     [,3]     [,4]     [,5]     [,6]     [,7]     [,8]
#> [1,] 5.537708 7.385419 7.123945 6.365561 6.583287 6.622006 7.529591 9.653629
#> [2,] 5.182104 6.329701 7.077746 6.622582 6.720424 6.650621 6.753653 8.555366
#> [3,] 5.217511 5.691038 6.835037 6.722512 6.753935 6.809577 6.821671 7.735439
#> [4,] 5.843380 5.774163 6.704104 6.791114 6.769437 6.902456 6.894872 7.256376
#>
#> , , 3
#>
#>          [,1]     [,2]     [,3]     [,4]     [,5]     [,6]     [,7]     [,8]
#> [1,] 5.537557 7.385956 7.123747 6.365649 6.583399 6.621993 7.529432 9.653808
#> [2,] 5.182114 6.329998 7.077790 6.622617 6.720466 6.650699 6.753555 8.555505
#> [3,] 5.217595 5.691183 6.835248 6.722539 6.753923 6.809659 6.821767 7.735534
#> [4,] 5.843729 5.774275 6.704478 6.791172 6.769410 6.902560 6.894917 7.256421
#>
#> , , 4
#>
#>          [,1]     [,2]     [,3]     [,4]     [,5]     [,6]     [,7]     [,8]
#> [1,] 5.537396 7.386602 7.123594 6.365782 6.583556 6.622006 7.529261 9.654034
#> [2,] 5.182153 6.330349 7.077879 6.622689 6.720538 6.650815 6.753456 8.555668
#> [3,] 5.217719 5.691356 6.835517 6.722599 6.753933 6.809778 6.821908 7.735655
#> [4,] 5.844169 5.774432 6.704923 6.791265 6.769403 6.902703 6.895001 7.256488
#>
#> , , 5
#>
#>          [,1]     [,2]     [,3]     [,4]     [,5]     [,6]     [,7]     [,8]
#> [1,] 5.537228 7.387390 7.123505 6.365962 6.583760 6.622050 7.529076 9.654309
#> [2,] 5.182220 6.330781 7.078017 6.622799 6.720643 6.650969 6.753354 8.555852
#> [3,] 5.217882 5.691551 6.835843 6.722693 6.753965 6.809928 6.822100 7.735794
#> [4,] 5.844700 5.774632 6.705432 6.791394 6.769416 6.902884 6.895128 7.256571
#>
#> , , 6
#>
#>          [,1]     [,2]     [,3]     [,4]     [,5]     [,6]     [,7]     [,8]
#> [1,] 5.537048 7.388340 7.123498 6.366193 6.584018 6.622127 7.528873 9.654641
#> [2,] 5.182318 6.331288 7.078229 6.622953 6.720783 6.651165 6.753245 8.556055
#> [3,] 5.218088 5.691764 6.836229 6.722825 6.754022 6.810116 6.822350 7.735952
#> [4,] 5.845335 5.774876 6.706008 6.791562 6.769454 6.903108 6.895303 7.256673
#>
#> , , 7
#>
#>          [,1]     [,2]     [,3]     [,4]     [,5]     [,6]     [,7]     [,8]
#> [1,] 5.536855 7.389500 7.123608 6.366485 6.584337 6.622248 7.528648 9.655033
#> [2,] 5.182450 6.331876 7.078543 6.623161 6.720964 6.651413 6.753129 8.556273
#> [3,] 5.218342 5.691991 6.836684 6.723002 6.754107 6.810345 6.822668 7.736127
#> [4,] 5.846085 5.775171 6.706655 6.791776 6.769518 6.903380 6.895537 7.256793
#>
#> , , 8
#>
#>          [,1]     [,2]     [,3]     [,4]     [,5]     [,6]     [,7]     [,8]
#> [1,] 5.536653 7.390971 7.123881 6.366853 6.584734 6.622427 7.528398 9.655499
#> [2,] 5.182629 6.332572 7.079002 6.623433 6.721198 6.651722 6.753006 8.556505
#> [3,] 5.218655 5.692225 6.837228 6.723235 6.754227 6.810625 6.823074 7.736321
#> [4,] 5.846972 5.775525 6.707380 6.792046 6.769617 6.903710 6.895846 7.256937
#>
#> , , 9
#>
#>          [,1]     [,2]     [,3]     [,4]     [,5]     [,6]     [,7]     [,8]
#> [1,] 5.536456 7.393010 7.124430 6.367314 6.585225 6.622681 7.528116 9.656059
#> [2,] 5.182863 6.333460 7.079681 6.623789 6.721495 6.652108 6.752872 8.556747
#> [3,] 5.219041 5.692443 6.837960 6.723540 6.754391 6.810966 6.823588 7.736533
#> [4,] 5.848033 5.775949 6.708247 6.792385 6.769761 6.904113 6.896247 7.257102
#>
#> , , 10
#>
#>          [,1]     [,2]     [,3]     [,4]     [,5]     [,6]     [,7]     [,8]
#> [1,] 5.536233 7.396149 7.125557 6.367893 6.585837 6.623040 7.527782 9.656774
#> [2,] 5.183168 6.334797 7.080752 6.624255 6.721875 6.652588 6.752730 8.556979
#> [3,] 5.219525 5.692552 6.838966 6.723941 6.754614 6.811381 6.824241 7.736759
#> [4,] 5.849310 5.776473 6.709414 6.792816 6.769962 6.904604 6.896767 7.257296
#>
#> , , 11
#>
#>          [,1]     [,2]     [,3]     [,4]     [,5]     [,6]     [,7]     [,8]
#> [1,] 5.535914 7.400987 7.127911 6.368625 6.586599 6.623538 7.527323 9.657896
#> [2,] 5.183563 6.336471 7.082694 6.624879 6.722359 6.653189 6.752584 8.557208
#> [3,] 5.220135 5.692365 6.840495 6.724474 6.754913 6.811891 6.825073 7.737038
#> [4,] 5.850865 5.777122 6.710917 6.793370 6.770242 6.905211 6.897440 7.257529
#>
#> , , 12
#>
#>          [,1]     [,2]     [,3]     [,4]     [,5]     [,6]     [,7]     [,8]
#> [1,] 5.535058 7.408381 7.134305 6.369604 6.587550 6.624232 7.526636 9.659605
#> [2,] 5.184054 6.338331 7.086840 6.625784 6.722982 6.653942 6.752441 8.557432
#> [3,] 5.220963 5.691564 6.842882 6.725286 6.755316 6.812520 6.826135 7.737363
#> [4,] 5.853173 5.778005 6.713002 6.794160 6.770628 6.905960 6.898318 7.257803
#>
#> , , 13
#>
#>          [,1]     [,2]     [,3]     [,4]     [,5]     [,6]     [,7]     [,8]
#> [1,] 5.531797 7.419363 7.151794 6.371070 6.588737 6.625192 7.525599 9.662155
#> [2,] 5.184586 6.339219 7.100399 6.627372 6.723788 6.654891 6.752322 8.557556
#> [3,] 5.222327 5.689807 6.847124 6.726920 6.755887 6.813298 6.827496 7.737730
#> [4,] 5.857250 5.779326 6.716148 6.795473 6.771183 6.906924 6.899456 7.258118
#>
#> , , 14
#>
#>          [,1]     [,2]     [,3]     [,4]     [,5]     [,6]     [,7]     [,8]
#> [1,] 5.529997 7.436108 7.209074 6.374372 6.590222 6.626506 7.524268 9.665998
#> [2,] 5.185203 6.333445 7.132703 6.632392 6.724926 6.656121 6.752221 8.557279
#> [3,] 5.224974 5.684684 6.857147 6.731411 6.756835 6.814383 6.829249 7.738061
#> [4,] 5.866986 5.781398 6.722245 6.798242 6.772132 6.908312 6.900959 7.258431
#>
#> , , 15
#>
#>          [,1]     [,2]     [,3]     [,4]     [,5]     [,6]     [,7]     [,8]
#> [1,] 5.554494 7.466051 7.351361 6.388132 6.592422 6.628341 7.521213 9.672402
#> [2,] 5.187208 6.326368 7.254426 6.656094 6.727005 6.658017 6.752189 8.556262
#> [3,] 5.232346 5.668646 6.889036 6.758810 6.758759 6.816113 6.831533 7.738291
#> [4,] 5.932396 5.785263 6.742760 6.808209 6.774099 6.910548 6.903152 7.258680
#>
#> , , 16
#>
#>          [,1]     [,2]     [,3]     [,4]     [,5]     [,6]     [,7]     [,8]
#> [1,] 5.606419 7.541318 7.605372 6.450253 6.596392 6.631625 7.516473 9.680396
#> [2,] 5.191053 6.351455 7.490788 6.739729 6.731248 6.661407 6.752644 8.555297
#> [3,] 5.290118 5.652750 7.027818 6.911483 6.764748 6.819309 6.835104 7.739204
#> [4,] 6.059621 5.799773 6.822146 6.864705 6.779816 6.915273 6.906723 7.259074
#>
#> , , 17
#>
#>          [,1]     [,2]     [,3]     [,4]     [,5]     [,6]     [,7]     [,8]
#> [1,] 5.688033 7.642356 7.795830 6.549219 6.605046 6.638019 7.526621 9.692660
#> [2,] 5.197749 6.430327 7.715649 6.922288 6.743720 6.668456 6.756556 8.548975
#> [3,] 5.370011 5.664723 7.249207 7.208117 6.804768 6.827123 6.841456 7.737175
#> [4,] 6.254987 5.862231 7.001184 7.154300 6.820748 6.930408 6.913535 7.260852
#>
#> , , 18
#>
#>          [,1]     [,2]     [,3]     [,4]     [,5]     [,6]     [,7]     [,8]
#> [1,] 5.836965 7.753840 7.941538 6.678096 6.639621 6.653740 7.659627 9.742351
#> [2,] 5.210543 6.572223 7.947402 7.210479 6.891279 6.697385 6.830579 8.600058
#> [3,] 5.473782 5.735885 7.464224 7.546286 7.029748 6.908837 6.855376 7.777232
#> [4,] 6.400558 5.937618 7.161097 7.551988 7.065963 7.015792 6.926563 7.284251
#>
#> , , 19
#>
#>          [,1]     [,2]     [,3]     [,4]     [,5]     [,6]     [,7]     [,8]
#> [1,] 6.082744 7.882689 8.229897 6.992640 6.954281 6.754431 8.128786 9.897555
#> [2,] 5.245006 6.811812 8.198073 7.550408 7.379888 6.996148 7.231012 8.865812
#> [3,] 5.663528 5.980302 7.703638 7.853531 7.443019 7.235085 6.891505 8.012424
#> [4,] 6.489862 6.114655 7.387437 7.871540 7.434508 7.298419 6.965369 7.436274
#>
#> , , 20
#>
#>          [,1]     [,2]     [,3]     [,4]     [,5]     [,6]     [,7]      [,8]
#> [1,] 6.548213 8.170833 8.584800 7.400650 7.623310 7.579662 8.975219 10.130879
#> [2,] 5.422270 7.306713 8.500136 7.916377 7.779827 7.411973 7.918584  9.286650
#> [3,] 5.779960 6.526395 8.029119 8.230431 7.840356 7.560517 7.502019  8.542720
#> [4,] 6.600040 6.642671 7.914129 8.232358 7.735358 7.580765 7.432014  8.023662
#>
#> , , 21
#>
#>          [,1]     [,2]     [,3]     [,4]     [,5]     [,6]     [,7]      [,8]
#> [1,] 7.582483 8.794374 9.095807 8.304485 8.205111 8.667540 9.835347 10.438334
#> [2,] 6.358978 8.401130 8.958692 8.514236 8.118491 7.897240 8.825016  9.828945
#> [3,] 6.240623 7.682381 8.740997 8.829941 8.164195 7.989731 8.139261  9.278415
#> [4,] 6.923604 7.663139 8.776236 8.789913 8.119610 8.055780 8.149194  8.932666
#>
#> , , 22
#>
#>          [,1]      [,2]      [,3]     [,4]     [,5]     [,6]      [,7]     [,8]
#> [1,] 9.420479 10.033402  9.941433 9.303304 9.127620 9.866754 10.660896 10.73081
#> [2,] 8.446654 10.007237  9.752114 9.287206 8.822512 8.853776 10.166334 10.41151
#> [3,] 8.198873  9.647979  9.880665 9.621028 8.769080 8.751328  9.215031 10.18184
#> [4,] 7.923913  9.206560 10.023355 9.534043 8.782295 8.815980  8.772234 10.00866
#>
#> , , 23
#>
#>           [,1]     [,2]     [,3]     [,4]      [,5]      [,6]      [,7]
#> [1,] 11.454479 11.63573 10.92625 10.42846 10.486797 11.200217 11.551640
#> [2,] 11.011653 11.85041 10.99035 10.31997  9.997056 10.142459 11.323988
#> [3,] 10.367303 11.40974 11.31161 10.60314  9.858772  9.822536 10.319161
#> [4,]  9.323559 10.76677 11.54255 10.54610  9.880587  9.795718  9.641217
#>          [,8]
#> [1,] 11.06521
#> [2,] 11.04735
#> [3,] 11.05830
#> [4,] 11.02541
#>
#> , , 24
#>
#>          [,1]     [,2]     [,3]     [,4]     [,5]     [,6]     [,7]     [,8]
#> [1,] 12.60783 12.64557 11.92579 11.70618 11.87247 12.39516 12.21702 11.48627
#> [2,] 12.40732 12.89925 12.11818 11.49938 11.34657 11.53640 12.41531 11.68332
#> [3,] 11.73530 12.48113 12.43480 11.66397 11.17786 11.02313 11.47462 11.88806
#> [4,] 10.91589 12.01426 12.66193 11.65293 11.19831 10.86197 10.47703 11.88816
#>
#> , , 25
#>
#>          [,1]     [,2]     [,3]     [,4]     [,5]     [,6]     [,7]     [,8]
#> [1,] 13.20080 13.26694 12.74846 12.75329 12.92357 13.22433 12.83529 11.99645
#> [2,] 13.01866 13.39563 12.92205 12.56162 12.53860 12.62490 13.15319 12.27228
#> [3,] 12.56873 13.07260 13.12523 12.62476 12.40516 12.04948 12.37380 12.53854
#> [4,] 12.25094 12.88814 13.30829 12.59762 12.42173 11.77103 11.20126 12.54945
#>
#> , , 26
#>
#>          [,1]     [,2]     [,3]     [,4]     [,5]     [,6]     [,7]     [,8]
#> [1,] 13.59887 13.71439 13.33237 13.34947 13.54960 13.75298 13.37750 12.56052
#> [2,] 13.43682 13.77549 13.46183 13.23474 13.34066 13.38633 13.74774 12.87849
#> [3,] 13.14650 13.50451 13.58613 13.27346 13.27617 12.86202 13.16564 13.15606
#> [4,] 13.06645 13.48156 13.72370 13.25393 13.35638 12.53477 12.02342 13.19737
#>
#> , , 27
#>
#>          [,1]     [,2]     [,3]     [,4]     [,5]     [,6]     [,7]     [,8]
#> [1,] 13.96508 14.05335 13.64447 13.62895 13.89462 14.12240 13.88654 13.04352
#> [2,] 13.82792 14.11029 13.81206 13.56300 13.77105 13.95312 14.24354 13.42205
#> [3,] 13.63276 13.90398 13.93595 13.62523 13.74339 13.57764 13.90482 13.71875
#> [4,] 13.64283 13.93436 14.06199 13.64945 13.88259 13.30086 13.04053 13.81797
#>
#> , , 28
#>
#>          [,1]     [,2]     [,3]     [,4]     [,5]     [,6]     [,7]     [,8]
#> [1,] 14.29300 14.30226 13.79517 13.75042 14.10319 14.41354 14.18771 13.37498
#> [2,] 14.18724 14.39191 14.02849 13.70727 14.00869 14.36296 14.55672 13.79165
#> [3,] 14.06498 14.26161 14.19625 13.77530 13.99211 14.16284 14.40374 14.11617
#> [4,] 14.10667 14.30697 14.34025 13.87192 14.20237 14.00646 13.90955 14.29715
#>
#> , , 29
#>
#>          [,1]     [,2]     [,3]     [,4]     [,5]     [,6]     [,7]     [,8]
#> [1,] 14.55780 14.47818 13.87358 13.81041 14.24936 14.62899 14.34454 13.55262
#> [2,] 14.48551 14.61402 14.16178 13.76283 14.17032 14.64703 14.79096 13.99837
#> [3,] 14.42661 14.55844 14.37869 13.86347 14.16114 14.55810 14.71758 14.35954
#> [4,] 14.48895 14.62003 14.55488 13.99484 14.44533 14.49123 14.44323 14.57477
#>
#> , , 30
#>
#>          [,1]     [,2]     [,3]     [,4]     [,5]     [,6]     [,7]     [,8]
#> [1,] 14.75238 14.59490 13.92012 13.83511 14.34014 14.77774 14.41752 13.60525
#> [2,] 14.71284 14.76220 14.23389 13.78226 14.27445 14.84394 14.94698 14.08155
#> [3,] 14.72374 14.79096 14.49657 13.90346 14.27271 14.82134 14.92610 14.49209
#> [4,] 14.81115 14.88125 14.71417 14.06307 14.61085 14.79388 14.76576 14.74310
#>
#> , , 31
#>
#>          [,1]     [,2]     [,3]     [,4]     [,5]     [,6]     [,7]     [,8]
#> [1,] 14.86410 14.65659 13.93326 13.83536 14.36975 14.85694 14.44542 13.60991
#> [2,] 14.85864 14.84572 14.26898 13.77531 14.30880 14.96078 15.02485 14.08725
#> [3,] 14.95090 14.95622 14.55917 13.91073 14.36401 14.99682 15.08865 14.55311
#> [4,] 15.06463 15.08272 14.81180 14.10075 14.71485 14.98249 14.97258 14.84015

# Attempts are using raster to read -----------------------------------------

r1 <- raster::brick(ncfile, varname="no3") # defaults to first depth all times
#> Warning in .rasterObjectFromCDF(x, type = objecttype, band = band, ...): "level"
#> set to 1 (there are 32 levels)
plot(r1)
``````

``````
r2 <- raster::brick(ncfile, varname="no3", level=20) # selects 20th depth level - all times
plot(r2)
``````

``````
# trying to alter the dimensions it uses
r3 <- raster::brick(ncfile, varname="no3", dims="depth") # trying to alter the dimensions it uses
#> Warning in .rasterObjectFromCDF(x, type = objecttype, band = band, ...): "level"
#> set to 1 (there are 32 levels)
#> Warning in seq_len(nc\$var[[zvar]]\$dim[[dims[1]]]\$len): first element used of
#> 'length.out' argument
#> Error in seq_len(nc\$var[[zvar]]\$dim[[dims[1]]]\$len): argument must be coercible to non-negative integer
r3 <- raster::brick(ncfile, varname="no3", dims=c(1,2,3))
#> Warning in .rasterObjectFromCDF(x, type = objecttype, band = band, ...): "level"
#> set to 1 (there are 32 levels)
plot(r3)
``````

``````r3 <- raster::brick(ncfile, varname="no3", dims=c(1,2,4))
#> Warning in .rasterObjectFromCDF(x, type = objecttype, band = band, ...): "level"
#> set to 1 (there are 32 levels)
plot(r3)

r4 <- raster::brick(ncfile, varname="no3", level=seq(1, 32, 1))
#> Error in if (level <= 0) {: the condition has length > 1
plot(r4)
#> Error in h(simpleError(msg, call)): error in evaluating the argument 'x' in selecting a method for function 'plot': object 'r4' not found
``````

Created on 2022-12-01 with reprex v2.0.2

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.