Skip to content

Commit 16e6d7a

Browse files
committed
trying to figure out nc issues
1 parent 25dd028 commit 16e6d7a

8 files changed

Lines changed: 156 additions & 0 deletions
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
UTF-8
Binary file not shown.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137.0,298.257223563]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]]
Binary file not shown.
Binary file not shown.

data-raw/scripts/2026/FISHBOT.png

137 KB
Loading
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
## Download FISHBOT data from ERDDAP
2+
end = Sys.Date()
3+
start = end - lubridate::days(7)
4+
url = paste0(
5+
"https://erddap.ondeckdata.com/erddap/tabledap/fishbot_realtime.csvp?time%2Ctemperature%2Cgrid_id%2Clatitude%2Clongitude&time%3E=",
6+
lubridate::year(start),
7+
"-",
8+
lubridate::month(start),
9+
"-",
10+
lubridate::day(start),
11+
"T00%3A00%3A00Z&time%3C=",
12+
lubridate::year(Sys.Date()),
13+
"-",
14+
lubridate::month(Sys.Date()),
15+
"-",
16+
lubridate::day(Sys.Date()),
17+
"T00%3A00%3A00Z"
18+
)
19+
# url=paste0(
20+
# 'https://erddap.ondeckdata.com/erddap/tabledap/fishbot_realtime.csvp?time%2Ctemperature%2Ctemperature_count%2Cdata_provider%2Cgrid_id%2Clatitude%2Clongitude&time%3E=2025-08-21T00%3A00%3A00Z&time%3C=2025-08-28T00%3A00%3A00Z'
21+
# )
22+
data = read.csv(url)
23+
## Download bathymetric data
24+
bath = marmap::getNOAA.bathy(
25+
lon1 = min(-80.83),
26+
lon2 = max(-56.79),
27+
lat1 = min(34),
28+
lat2 = max(46.89),
29+
resolution = 1
30+
)
31+
## Create color ramp
32+
blues = c(
33+
"lightsteelblue4",
34+
"lightsteelblue3",
35+
"lightsteelblue2",
36+
"lightsteelblue1"
37+
)
38+
## Set up plot
39+
filename = paste0(here::here("data-raw/scripts/2026/FISHBOT.png"))
40+
file.create(filename)
41+
42+
png(
43+
filename,
44+
height = 1000,
45+
width = 800,
46+
units = "px"
47+
)
48+
plot(
49+
bath,
50+
step = 100,
51+
deepest.isobath = -1000,
52+
shallowest.isobath = 0,
53+
col = "darkgray",
54+
image = TRUE,
55+
land = TRUE,
56+
lwd = 0.1,
57+
bpal = list(
58+
c(0, max(bath), "gray"),
59+
c(min(bath), 0, blues)
60+
),
61+
main = paste0(
62+
"Gridded Bottom Temperature Observations ",
63+
start,
64+
" to ",
65+
end
66+
),
67+
xlim = c(-76, -66),
68+
ylim = c(34, 45)
69+
)
70+
## Load the shapefile
71+
grid = sf::read_sf(here::here(
72+
"data-raw/scripts/2026/7km_trimmed_fixedgeometries/7km_grid_clipped_fixed_geometry.shp"
73+
))
74+
grid = subset(grid, grid$id %in% data$grid_id)
75+
grid$temp = NA
76+
grid$color = NA
77+
for (i in 1:nrow(grid)) {
78+
grid$temp[i] = mean(
79+
subset(data, data$grid_id == grid$id[i])$temperature..degree_C.
80+
)
81+
}
82+
grid$TEMP = grid$temp * 9 / 5 + 32
83+
mintemp = floor(min(grid$TEMP))
84+
maxtemp = ceiling(max(grid$TEMP))
85+
tempcol = viridis::magma(n = length(seq(mintemp, maxtemp, 1)))
86+
for (i in 1:nrow(grid)) {
87+
grid$color[i] = tempcol[which(
88+
seq(mintemp, maxtemp, 1) == round(grid$TEMP[i], 0)
89+
)]
90+
}
91+
plot(
92+
sf::st_geometry(grid),
93+
max.plot = 1,
94+
add = TRUE,
95+
border = grid$color,
96+
col = grid$color
97+
)
98+
datacolors = data.frame(
99+
TEMP = seq(mintemp, maxtemp, 1),
100+
COL = tempcol
101+
)
102+
datacolors = subset(
103+
datacolors,
104+
datacolors$TEMP %in% round(quantile(datacolors$TEMP), 0)
105+
)
106+
legend(
107+
'bottomright',
108+
fill = datacolors$COL,
109+
border = datacolors$COL,
110+
legend = datacolors$TEMP,
111+
title = "Bottom Temperature (F)"
112+
)
113+
dev.off()

data-raw/scripts/2026/debug_nc.R

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
file <- here::here("data-raw/2026/fishbot_2000_2026.nc")
2+
3+
terra::rast(file)
4+
terra::sds(file)
5+
6+
nc_check <- ncdf4::nc_open(file)
7+
print(nc_check)
8+
ncdf4::nc_close(nc_check)
9+
10+
# 1. Open the file and see what's inside
11+
nc_file <- ncdf4::nc_open(here::here("data-raw/2026/fishbot_2000_2026.nc"))
12+
13+
# Print to find the exact variable names for lon, lat, and your data
14+
# print(nc_file)
15+
16+
# 2. Extract the coordinates and the data variable
17+
# Replace 'lon', 'lat', and 'variable_name' with the names found in print(nc_file)
18+
lons <- ncdf4::ncvar_get(nc_file, "longitude")
19+
lats <- ncdf4::ncvar_get(nc_file, "latitude")
20+
data_array <- ncdf4::ncvar_get(nc_file, "temperature") # e.g., "temp" or "biomass"
21+
22+
ncdf4::nc_close(nc_file)
23+
24+
# 3. Handle the data structure
25+
# NetCDF data is often stored [lon, lat]. terra expects [row, col]
26+
# We might need to transpose the matrix
27+
data_mat <- t(data_array)
28+
29+
# 4. Manually create the SpatRaster
30+
# We define the extent based on the min/max of our lon/lat vectors
31+
r <- terra::rast(
32+
data_mat,
33+
extent = terra::ext(min(lons), max(lons), min(lats), max(lats)),
34+
crs = "EPSG:4326" # Assuming standard WGS84
35+
)
36+
37+
# 5. Fix orientation
38+
# NetCDF often stores data "upside down" compared to standard GIS formats
39+
r <- terra::flip(r, direction = "vertical")
40+
41+
terra::plot(r)

0 commit comments

Comments
 (0)