Skip to content

Instantly share code, notes, and snippets.

@mdsumner
Created January 20, 2025 01:45
Show Gist options
  • Save mdsumner/761a985146898e6c0fb1073b42bffc59 to your computer and use it in GitHub Desktop.
Save mdsumner/761a985146898e6c0fb1073b42bffc59 to your computer and use it in GitHub Desktop.
  prj <- "+proj=stere +lat_0=-90 +lat_ts=-70 +datum=WGS84"

f <- "~/FastIce_70_2006.nc"
ex <- c(-2700000, -2700000 + 5625 * 1000, -2397000, -2397000 + 4700 * 1000)

nc <- ncdf4::nc_open(f)
print(nc) ## tells us metadata like
#> File ~/FastIce_70_2006.nc (NC_FORMAT_NETCDF4):
#> 
#>      7 variables (excluding dimension variables):
#>         double x[X]   (Contiguous storage)  
#>             long_name: x co-ordinates
#>             units: meters
#>             description: Meters in polar stereographic projection (true latitude -70) for the centre of each pixel
#>         double y[Y]   (Contiguous storage)  
#>             long_name: y co-ordinates
#>             units: meters
#>             description: Meters in polar stereographic projection (true latitude -70) for the centre of each pixel
#>         float latitude[X,Y]   (Contiguous storage)  
#>             long_name: latitude
#>             units: degrees_north
#>             description: Latitude values for the centre of each pixel
#>         float longitude[X,Y]   (Contiguous storage)  
#>             long_name: longitude
#>             units: degrees_east
#>             description: Longitude values for the centre of each pixel
#>         double area[X,Y]   (Contiguous storage)  
#>             long_name: area
#>             units: km2
#>             description: Per-pixel grid area
#>         int date_alt[time]   (Contiguous storage)  
#>             long_name: alternative date format
#>             description: Start date of 15 or 20-day image mosaic window in YYYYMMDD format
#>         byte Fast_Ice_Time_series[X,Y,time]   (Chunking: [938,784,4])  (Compression: level 1)
#>             long_name: Fast Ice Time Series
#>             description: Classified surface type: 0 = pack ice or ocean; 1 = continent; 2 = islands; 3 = ice shelf; 4 = fast ice; 5 = manual fast ice edge; 6 = auto fast ice edge.
#>             FillValue: 0
#> 
#>      3 dimensions:
#>         time  Size:24 
#>             long_name: time
#>             units: days since 2000-1-1 0:0:0
#>             description: Start date of 15- or 20-day image mosaic window.
#>             calendar: proleptic_gregorian
#>         X  Size:5625 (no dimvar)
#>         Y  Size:4700 (no dimvar)
#> 
#>     8 global attributes:
#>         title: Fast ice time series
#>         institution: Institute for Marine and Antarctic Studies, University of Tasmania
#>         source: MODIS 15-day composite images and automated edge detection
#>         history: Version 2.2
#>         references: Fraser et al., 2020 (Earth System Science Data, submitted)
#>         comment: This is the lat_ts=-70 version of the data - year 2006
#>         contact: Alex Fraser ([email protected])
#>         Conventions: CF-1.7
##Classified surface type: 0 = pack ice or ocean; 1 = continent; 2 = islands; 3 = ice shelf; 4 = fast ice; 5 = manual fast ice edge; 6 = auto fast ice edge.

time <- as.Date("2000-01-01") + ncdf4::ncvar_get(nc, "time")
ncdf4::nc_close(nc)                    


library(terra)
#> terra 1.8.10
r <- rast(f, "Fast_Ice_Time_series")
#> [1] "vobjtovarid4: **** WARNING **** I was asked to get a varid for dimension named X BUT this dimension HAS NO DIMVAR! Code will probably fail at this point"
#> [1] "vobjtovarid4: **** WARNING **** I was asked to get a varid for dimension named Y BUT this dimension HAS NO DIMVAR! Code will probably fail at this point"
set.ext(r, ext(ex))
set.crs(r, crs(prj))

points <- cbind(
  lon = c(136.5, 140.2, 143.8),  # Example longitudes
  lat = c(-65.5, -65.3, -66.8)   # Example latitudes
)

prjpoints <- project(points, to = prj, from = "EPSG:4326")
extract(r, prjpoints)  
#>   Fast_Ice_Time_series_1 Fast_Ice_Time_series_2 Fast_Ice_Time_series_3
#> 1                      0                      0                      0
#> 2                      0                      0                      0
#> 3                      0                      0                      0
#>   Fast_Ice_Time_series_4 Fast_Ice_Time_series_5 Fast_Ice_Time_series_6
#> 1                      0                      0                      0
#> 2                      0                      0                      0
#> 3                      0                      0                      0
#>   Fast_Ice_Time_series_7 Fast_Ice_Time_series_8 Fast_Ice_Time_series_9
#> 1                      0                      0                      0
#> 2                      0                      0                      0
#> 3                      0                      0                      0
#>   Fast_Ice_Time_series_10 Fast_Ice_Time_series_11 Fast_Ice_Time_series_12
#> 1                       4                       4                       0
#> 2                       0                       0                       0
#> 3                       0                       0                       0
#>   Fast_Ice_Time_series_13 Fast_Ice_Time_series_14 Fast_Ice_Time_series_15
#> 1                       4                       4                       4
#> 2                       0                       0                       0
#> 3                       0                       0                       0
#>   Fast_Ice_Time_series_16 Fast_Ice_Time_series_17 Fast_Ice_Time_series_18
#> 1                       4                       4                       4
#> 2                       0                       0                       0
#> 3                       0                       0                       0
#>   Fast_Ice_Time_series_19 Fast_Ice_Time_series_20 Fast_Ice_Time_series_21
#> 1                       0                       0                       0
#> 2                       0                       0                       0
#> 3                       0                       0                       0
#>   Fast_Ice_Time_series_22 Fast_Ice_Time_series_23 Fast_Ice_Time_series_24
#> 1                       0                       0                       0
#> 2                       0                       0                       0
#> 3                       0                       0                       0

Created on 2025-01-20 with reprex v2.1.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment