7.2 Some basic operations on stars objects

7.2.1 Subset a stars object by index

In order to access the value of an attribute (say ppt) at a particular location at a particular time from the stars object (prcp_tmax_PRISM_m8_y09), you need to tell R that you are interested in the ppt attribute and specify the corresponding index of x, y, and date. Here, is how you get the ppt value of (3, 4) cell at date = 10.

prcp_tmax_PRISM_m8_y09["ppt", 3, 4, 10]
stars object with 3 dimensions and 1 attribute
attribute(s):
     Min. 1st Qu. Median Mean 3rd Qu. Max.
ppt     0       0      0    0       0    0
dimension(s):
     from to     offset      delta refsys point values x/y
x       3  3   -121.729  0.0416667  NAD83 FALSE   NULL [x]
y       4  4    46.6458 -0.0416667  NAD83 FALSE   NULL [y]
date   10 10 2009-08-11     1 days   Date    NA   NULL    

This is very much similar to accessing a value from a matrix except that we have more dimensions. The first argument selects the attribute of interest, 2nd x, 3rd y, and 4th date.

Of course, you can subset a stars object to access the value of multiple cells like this:

prcp_tmax_PRISM_m8_y09["ppt", 3:6, 3:4, 5:10]
stars object with 3 dimensions and 1 attribute
attribute(s):
     Min. 1st Qu. Median     Mean 3rd Qu.  Max.
ppt     0       0      0 0.043125       0 0.546
dimension(s):
     from to     offset      delta refsys point values x/y
x       3  6   -121.729  0.0416667  NAD83 FALSE   NULL [x]
y       3  4    46.6458 -0.0416667  NAD83 FALSE   NULL [y]
date    5 10 2009-08-11     1 days   Date    NA   NULL    

7.2.2 Set attribute names

When you read a raster dataset from a GeoTIFF file, the attribute name is the file name by default. So, you will often encounter cases where you want to change the attribute name. You can set the name of attributes using setNames().

prcp_tmax_PRISM_m8_y09_dif_names <- setNames(prcp_tmax_PRISM_m8_y09, c("precipitation", "maximum_temp"))

Note that the attribute names of prcp_tmax_PRISM_m8_y09 have not changed after this operation (just like mutate() or other dplyr functions do):

prcp_tmax_PRISM_m8_y09
stars object with 3 dimensions and 2 attributes
attribute(s):
       Min.  1st Qu. Median      Mean  3rd Qu.   Max.
ppt   0.000  0.00000  0.000  1.292334  0.01100 30.851
tmax  1.833 17.55575 21.483 22.035435 26.54275 39.707
dimension(s):
     from to     offset      delta refsys point values x/y
x       1 20   -121.729  0.0416667  NAD83 FALSE   NULL [x]
y       1 20    46.6458 -0.0416667  NAD83 FALSE   NULL [y]
date    1 10 2009-08-11     1 days   Date    NA   NULL    

If you want to reflect changes in the variable names while keeping the same object name, you need to assign the output of setNames() to the object as follows:

#--- NOT RUN ---#
# the codes in the rest of the chapter use "ppt" and "tmax" as the variables names, not these ones
prcp_tmax_PRISM_m8_y09 <- setNames(prcp_tmax_PRISM_m8_y09, c("precipitation", "maximum_temp"))

We will be using this function in Chapter 7.7.2.

7.2.3 Get the coordinate reference system

You can get the CRS of a stars object using st_crs(), which is actually the same function name that we used to extract the CRS of an sf object.

st_crs(prcp_tmax_PRISM_m8_y09)
Coordinate Reference System:
  User input: GEOGCS["NAD83",DATUM["North_American_Datum_1983",SPHEROID["GRS 1980",6378137,298.2572221010042,AUTHORITY["EPSG","7019"]],AUTHORITY["EPSG","6269"]],PRIMEM["Greenwich",0],UNIT["degree",0.0174532925199433],AUTHORITY["EPSG","4269"]] 
  wkt:
GEOGCS["NAD83",
    DATUM["North_American_Datum_1983",
        SPHEROID["GRS 1980",6378137,298.2572221010042,
            AUTHORITY["EPSG","7019"]],
        AUTHORITY["EPSG","6269"]],
    PRIMEM["Greenwich",0],
    UNIT["degree",0.0174532925199433],
    AUTHORITY["EPSG","4269"]]

You will use this to change the projection of vector datasets when you interact them:

  • crop the stars raster dataset to the spatial extent of sf objects
  • extract values from the stars raster dataset for sf objects

as we will see in Chapter 5. Notice also that we used exactly the same function name (st_crs()) to get the CRS of sf objects (see Chapter 2).

7.2.4 Get the dimension characteristics and values

You can access these dimension values using st_dimensions().

#--- get dimension characteristics ---#
(
dim_prcp_tmin <- st_dimensions(prcp_tmax_PRISM_m8_y09)
)
     from to     offset      delta refsys point values x/y
x       1 20   -121.729  0.0416667  NAD83 FALSE   NULL [x]
y       1 20    46.6458 -0.0416667  NAD83 FALSE   NULL [y]
date    1 10 2009-08-11     1 days   Date    NA   NULL    

The following shows what we can get from this object.

str(dim_prcp_tmin)
List of 3
 $ x   :List of 7
  ..$ from  : num 1
  ..$ to    : num 20
  ..$ offset: num -122
  ..$ delta : num 0.0417
  ..$ refsys:List of 2
  .. ..$ input: chr "GEOGCS[\"NAD83\",DATUM[\"North_American_Datum_1983\",SPHEROID[\"GRS 1980\",6378137,298.2572221010042,AUTHORITY["| __truncated__
  .. ..$ wkt  : chr "GEOGCS[\"NAD83\",\n    DATUM[\"North_American_Datum_1983\",\n        SPHEROID[\"GRS 1980\",6378137,298.25722210"| __truncated__
  .. ..- attr(*, "class")= chr "crs"
  ..$ point : logi FALSE
  ..$ values: NULL
  ..- attr(*, "class")= chr "dimension"
 $ y   :List of 7
  ..$ from  : num 1
  ..$ to    : num 20
  ..$ offset: num 46.6
  ..$ delta : num -0.0417
  ..$ refsys:List of 2
  .. ..$ input: chr "GEOGCS[\"NAD83\",DATUM[\"North_American_Datum_1983\",SPHEROID[\"GRS 1980\",6378137,298.2572221010042,AUTHORITY["| __truncated__
  .. ..$ wkt  : chr "GEOGCS[\"NAD83\",\n    DATUM[\"North_American_Datum_1983\",\n        SPHEROID[\"GRS 1980\",6378137,298.25722210"| __truncated__
  .. ..- attr(*, "class")= chr "crs"
  ..$ point : logi FALSE
  ..$ values: NULL
  ..- attr(*, "class")= chr "dimension"
 $ date:List of 7
  ..$ from  : num 1
  ..$ to    : int 10
  ..$ offset: Date[1:1], format: "2009-08-11"
  ..$ delta : 'difftime' num 1
  .. ..- attr(*, "units")= chr "days"
  ..$ refsys: chr "Date"
  ..$ point : logi NA
  ..$ values: NULL
  ..- attr(*, "class")= chr "dimension"
 - attr(*, "raster")=List of 3
  ..$ affine     : num [1:2] 0 0
  ..$ dimensions : chr [1:2] "x" "y"
  ..$ curvilinear: logi FALSE
  ..- attr(*, "class")= chr "stars_raster"
 - attr(*, "class")= chr "dimensions"

For example, you can get offset of x as follows:

dim_prcp_tmin$x$offset
[1] -121.7292

You can extract dimension values using st_get_dimension_values(). For example, to get values of date,

#--- get date values ---#
st_get_dimension_values(prcp_tmax_PRISM_m8_y09, "date")
 [1] "2009-08-11" "2009-08-12" "2009-08-13" "2009-08-14" "2009-08-15"
 [6] "2009-08-16" "2009-08-17" "2009-08-18" "2009-08-19" "2009-08-20"

This can be handy as you will see in Chapter 9.4.2. Later in Chapter 7.5, we will learn how to set dimensions using st_set_dimensions().

7.2.5 Attributes to dimensions, and vice versa

You can make attributes to dimensions using merge().

(
prcp_tmax_four <- merge(prcp_tmax_PRISM_m8_y09)
)
stars object with 4 dimensions and 1 attribute
attribute(s):
   Min. 1st Qu. Median     Mean 3rd Qu.   Max.
X     0       0 11.799 11.66388  21.535 39.707
dimension(s):
           from to     offset      delta refsys point     values x/y
x             1 20   -121.729  0.0416667  NAD83 FALSE       NULL [x]
y             1 20    46.6458 -0.0416667  NAD83 FALSE       NULL [y]
date          1 10 2009-08-11     1 days   Date    NA       NULL    
attributes    1  2         NA         NA     NA    NA ppt , tmax    

As you can see, the new stars object has an additional dimension called X, which represents attributes and has two dimension values: the first for ppt and second for tmax. Now, if you want to access ppt, you can do the following:

prcp_tmax_four[, , , , "ppt"]
stars object with 4 dimensions and 1 attribute
attribute(s):
   Min. 1st Qu. Median     Mean 3rd Qu.   Max.
X     0       0      0 1.292334   0.011 30.851
dimension(s):
           from to     offset      delta refsys point values x/y
x             1 20   -121.729  0.0416667  NAD83 FALSE   NULL [x]
y             1 20    46.6458 -0.0416667  NAD83 FALSE   NULL [y]
date          1 10 2009-08-11     1 days   Date    NA   NULL    
attributes    1  1         NA         NA     NA    NA    ppt    

We can do this because the merge kept the attribute names as dimension values as you can see in values.

You can revert it back to the original state using split(). Since we want the fourth dimension to dissolve,

split(prcp_tmax_four, 4)
stars object with 3 dimensions and 2 attributes
attribute(s):
       Min.  1st Qu. Median      Mean  3rd Qu.   Max.
ppt   0.000  0.00000  0.000  1.292334  0.01100 30.851
tmax  1.833 17.55575 21.483 22.035435 26.54275 39.707
dimension(s):
     from to     offset      delta refsys point values x/y
x       1 20   -121.729  0.0416667  NAD83 FALSE   NULL [x]
y       1 20    46.6458 -0.0416667  NAD83 FALSE   NULL [y]
date    1 10 2009-08-11     1 days   Date    NA   NULL