Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

aggregate.R created #3317

Merged
merged 17 commits into from
Jul 25, 2024
Merged

aggregate.R created #3317

merged 17 commits into from
Jul 25, 2024

Conversation

Snafkin547
Copy link
Contributor

@Snafkin547 Snafkin547 commented Jun 26, 2024

Description

The aggregate function, created using exactextractr, enables the aggregation (sum and average) of previously downscaled carbon flux output from the NA_downscale function. Users can aggregate data for spatial areas of their choice by providing appropriate polygon data.

Motivation and Context

This change is required to enable the aggregation of downscaled carbon flux data to user-specified spatial units. It solves the problem of needing to summarize large-scale raster data into meaningful aggregated values (sum and average) for specific areas, aiding in spatial analysis and decision-making.

Review Time Estimate

  • Immediately
  • Within one week
  • When possible

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • My change requires a change to the documentation.
  • My name is in the list of CITATION.cff
  • I have updated the CHANGELOG.md.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

@Snafkin547
Copy link
Contributor Author

You can run this function in this way:

Download a shapefile of U.S. states (if not already available)

url <- "https://www2.census.gov/geo/tiger/GENZ2020/shp/cb_2020_us_state_20m.zip"
download.file(url, destfile = "polygon/us_states.zip")
unzip("polygon/us_states.zip", exdir = "polygon/us_states")

Load the shapefile

us_states <- st_read("polygon/us_states/cb_2020_us_state_20m.shp")
saveRDS(us_states, "polygon/polygon_us_states.rds")

Load the downscale_output list

downscale_output <- readRDS("path/to/your raster file")

read_raster <- function(file_path) {
  rast(file_path)
}

downscale_output$maps <- lapply(downscale_output$maps, read_raster)
us_states <- readRDS("polygon/us_states.rds")
state = "MA"
polygon_data <- st_transform(us_states[us_states$STUSPS == state, ], crs = "EPSG:4326")

massachusetts <- aggregate_by_state(downscale_output, polygon_data)

print(massachusetts)
plot(st_geometry(massachusetts))

modules/assim.sequential/R/aggregate.R Outdated Show resolved Hide resolved
modules/assim.sequential/R/aggregate.R Outdated Show resolved Hide resolved
modules/assim.sequential/R/aggregate.R Outdated Show resolved Hide resolved
@Snafkin547 Snafkin547 requested a review from mdietze July 15, 2024 20:38
modules/assim.sequential/R/aggregate.R Outdated Show resolved Hide resolved
@mdietze
Copy link
Member

mdietze commented Jul 25, 2024

Because you added dependencies you need to run scripts/generate_dependencies.R and then commit the updated docker/depends/pecan_package_dependencies.csv

Copy link
Member

@mdietze mdietze left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggesting small changes to Description file that should eliminate the need for the previous suggestion about the dependencies script and csv

modules/assim.sequential/DESCRIPTION Outdated Show resolved Hide resolved
modules/assim.sequential/DESCRIPTION Outdated Show resolved Hide resolved
modules/assim.sequential/DESCRIPTION Show resolved Hide resolved
modules/assim.sequential/DESCRIPTION Outdated Show resolved Hide resolved
@mdietze
Copy link
Member

mdietze commented Jul 25, 2024

FYI, the suggested changes I made didn't eliminate the need for a running the dependencies script, but you will want to pull the changes I've made before doing so.

@github-actions github-actions bot added the Tests label Jul 25, 2024
@mdietze mdietze merged commit 733af5c into PecanProject:develop Jul 25, 2024
12 of 13 checks passed
@infotroph infotroph mentioned this pull request Jul 29, 2024
13 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants