[R-sig-Geo] extract data from multilayer nc file

Antonio Silva @o||nto@|@t @end|ng |rom gm@||@com
Thu May 30 23:46:15 CEST 2019


Thanks Thiago

Good idea to change stack to brick with level command.

Best regards,

A.O.


Em qui, 30 de mai de 2019 às 17:03, Thiago V. dos Santos <
thi_veloso using yahoo.com.br> escreveu:

> Alternatively, there is a "level" argument for the brick command that
> allows accessing the 4th dimension variable of the netcdf file - usually
> height for atmosphere-related variables or depth for surface- or
> ocean-related variables.
>
> Depending on your purpose, you might want to access a specific depth (say
> the first one):
>
> rst.data <- brick('~/Downloads/
> dataset-armor-3d-rep-monthly_1559213337443.nc', varname='so', level=1)
>
> Or you could loop over the depths:
>
> # Loop over depths
> for (i in 1:2){
>   rst.data <- brick('~/Downloads/
> dataset-armor-3d-rep-monthly_1559213337443.nc', varname='so', level=i)
>
>   # Do whatever you need to do with each layer
> }
>
> See ?brick
>
> Greetings,
>  -- Thiago V. dos Santos
>
> Postdoctoral Research Fellow
> Department of Climate and Space Science and Engineering
> University of Michigan
>
>
> On Thursday, May 30, 2019, 3:42:01 PM EDT, Antonio Silva <
> aolinto.lst using gmail.com> wrote:
>
>
> Thanks a lot Sarah. I ran your examples. Since I use Linux Ubuntu command
> lines just worked fine.
>
> I will learn more about NCO.
>
> Once more thanks for your help.
>
> All the best
>
> A.O.
>
> Em qui, 30 de mai de 2019 às 13:12, Sarah Goslee <sarah.goslee using gmail.com>
> escreveu:
>
> > Hi Antonio,
> >
> > I work with NetCDF files a lot, and I find it enormously easier to
> > split up multidimensional files before importing them into R.
> >
> > I use NCO for this (http://nco.sourceforge.net/) but I'm sure there
> > are other options. I'm afraid I have no idea how well that works on
> > windows.
> >
> > For your sample file, here's how you could split it by depth and by
> > variable.
> >
> > ncks -d depth,0,0 -v to dataset-armor-3d-rep-monthly_1559213337443.nc
> > vo50.nc
> > ncks -d depth,0,0 -v so dataset-armor-3d-rep-monthly_1559213337443.nc
> > so50.nc
> >
> > ncks -d depth,1,1 -v to dataset-armor-3d-rep-monthly_1559213337443.nc
> > vo75.nc
> > ncks -d depth,1,1 -v so dataset-armor-3d-rep-monthly_1559213337443.nc
> > so75.nc
> >
> > > library(raster)
> > Loading required package: sp
> > > vo75 <- stack("vo75.nc")
> > Loading required namespace: ncdf4
> > > dim(vo75)
> > [1] 29 41 12
> >
> > Sarah
> >
> > On Thu, May 30, 2019 at 10:24 AM Antonio Silva <aolinto.lst using gmail.com>
> > wrote:
> > >
> > > Hello all,
> > >
> > > I want to extract monthly sea water temperature and salinity (50 and
> 75 m
> > > depth) for a given area.
> > >
> > > To do so I downloaded dataset-armor-3d-rep-monthly (product
> > > MULTIOBS_GLO_PHY_REP_015_002) from Copernicus Marine environment
> > monitoring
> > > service (http://marine.copernicus.eu/). A sample file can be found at
> > > https://app.box.com/s/f8yyf3jn9kye1htqc7aktjk38jp39yrm.
> > >
> > > I also prepared a shapefile delimiting the area of interest (
> > > https://app.box.com/s/ciq76w7918zir3hu89l8d3y9dn3csuxp).
> > >
> > > Some time ago I dealt with monthly salinity data files, that is, with
> > only
> > > one variable. I prepared a script and made it available at
> > > https://gist.github.com/aolinto/59e43cdbac3ff3e0cd5eb2eab5efcbc4.
> > >
> > > Now I have a nc file with several variables (so and to) and each one
> has
> > 4
> > > dimensions (time (for each month), lat, lon and depth (50 and 75 m)).
> > >
> > > For a file with 3 dimensions per variable I write
> > > rst.data <- stack("dataset-sss-ssd-rep-monthly_sos_2008_2017.nc
> > ",varname="sos")
> > >
> > >
> > > But this turn I receive the message
> > > > rst.data <- stack("dataset-armor-3d-rep-monthly_1559213337443.nc
> > ",varname="to")
> > > # indicate the nc file
> > > Warning messages:
> > > 1: In .stackCDF(x, varname = varname, bands = bands) :
> > >  to has 4 dimensions, I am using the last one
> > > 2: In .rasterObjectFromCDF(x, type = objecttype, band = band, ...) :
> > >  "level" set to 1 (there are 2 levels)
> > >
> > > I tried several commands (bands, layers) but I couldn't make a stack to
> > 50
> > > and another to 75 data.
> > >
> > > I appreciate any help.
> > >
> > > Best regards,
> > >
> > > Antônio Olinto
> > > Instituto de Pesca (Fisheries Institute)
> > > São Paulo, Brasil
> > >
> > >        [[alternative HTML version deleted]]
> > >
> > > _______________________________________________
> > > R-sig-Geo mailing list
> > > R-sig-Geo using r-project.org
> > > https://stat.ethz.ch/mailman/listinfo/r-sig-geo
> >
> >
> >
> > --
> > Sarah Goslee (she/her)
> > http://www.numberwright.com
>
> >
>
>     [[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo using r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>

	[[alternative HTML version deleted]]



More information about the R-sig-Geo mailing list