[R-sig-Geo] [FORGED] Modifying the length of a matrix variable
Ben Tupper
btupper @end|ng |rom b|ge|ow@org
Sun Mar 31 01:06:13 CET 2019
Hi,
It looks like you are accessing a NetCDF file or it's kin. Like Rolf, I'm challenged to understand from your description what you are after. Assuming you do indeed have a NetCDF file and are using the ncdf4 package, you can retrieve the first 90 layers as shown below using the varid, start and count arguments to ncvar_get(). Obviously, this is untested since your data isn't available to us.
library(ncdf4)
filename = "/path/to/foo.nc"
ncObject <- nc_open(filename)
m <- ncvar_get(ncObject varid = "onedaymax", start = c(1,1,1), count = c(-1,-1, 90))
nc_close(ncObject)
See more details and examples here... https://www.rdocumentation.org/packages/ncdf4/versions/1.16.1/topics/ncvar_get
You might also consider using the raster package which will retrieve multilayer georeferenced rasters for you from the same NetCDF file.
library(raster)
B <- brick(filename, varname = "onedaymax")
B <- B[[-c(91:95)]] # <- drop the last layers 91, 92, ...95
See the docs here... https://www.rdocumentation.org/packages/raster/versions/2.8-19/topics/brick
Cheers,
Ben
> On Mar 30, 2019, at 7:30 PM, rain1290--- via R-sig-Geo <r-sig-geo using r-project.org> wrote:
>
> Yes, I reproduced the example above and it works just fine (and is what I want!!), but I cannot see why it does not work with my data, as it is a 3-dimensional array (latitude, longitude and time).
> This is what comes from print(Model4):
> 3 variables (excluding dimension variables):
> double onedaymax[lon,lat,time] (Contiguous storage)
> units: mm/day
> double fivedaymax[lon,lat,time] (Contiguous storage)
> units: mm/day
> short Year[time] (Contiguous storage)
>
> 3 dimensions:
> time Size:95
> lat Size:64
> units: degree North
> lon Size:128
> units: degree East
> I reviewed it over and over again, but I cannot see why this would not work?
> Thanks,
>
>
> -----Original Message-----
> From: Rolf Turner <r.turner using auckland.ac.nz>
> To: rain1290 <rain1290 using aim.com>
> Cc: r-sig-geo <r-sig-geo using r-project.org>
> Sent: Sat, Mar 30, 2019 6:49 pm
> Subject: Re: [FORGED] [R-sig-Geo] Modifying the length of a matrix variable
>
>
> On 31/03/19 11:14 AM, rain1290 using aim.com wrote:
>
>> Hi Rolf (and others),
>>
>> I tried your suggestion, but when I used dim(Model4.chopped), it still
>> shows 95 layers, as shown below:
>>
>> 8192 95
>>
>> I also find that the total number of cells is rather low for that many
>> layers. I started with 778240 cells over 95 layers.
>
> Well then you're doing something wrong, or there is something that you
> haven't told us.
>
> E.g.:
>> junk <- array(runif(64*128*95),dim=c(64,128,95))
>> junk.chopped <- junk[,,1:90]
>> dim(junk)
>> [1] 64 128 95
>> dim(junk.chopped)
>> [1] 64 128 90
>
> Perhaps Model.4 has some structure other than that of an array.
> (Originally you said it was a matrix.)
>
> You really need to get your terminology and ideas *clear* in order to
> have any hope of receiving useful advice.
>
> I have no idea what you are on about in respect of "the number of
> cells". My mind-reading machine is in the repair shop. I strongly
> suspect that your thoughts are confused.
>
> cheers,
>
> Rolf
>
> --
> Honorary Research Fellow
> Department of Statistics
> University of Auckland
> Phone: +64-9-373-7599 ext. 88276
>
> [[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
Ben Tupper
Bigelow Laboratory for Ocean Sciences
60 Bigelow Drive, P.O. Box 380
East Boothbay, Maine 04544
http://www.bigelow.org
Ecological Forecasting: https://eco.bigelow.org/
More information about the R-sig-Geo
mailing list