added DownloadCreditsCommand some updates

This commit is contained in:
Martin Folkerts 2024-10-23 09:34:52 +02:00
parent e51e8d7312
commit f08da04c3b
6 changed files with 21726 additions and 636 deletions

View file

@ -0,0 +1,91 @@
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Storage;
class DownloadCreditsCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'app:download-credits-command';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';
private $processingUnits = [];
private $totalSum = 0;
private $totalCount = 0;
private $allValues = [];
private $stdDev = 0;
private $overallAvg;
/**
* Execute the console command.
*/
public function handle()
{
$path = 'Bagamoyo_trial/single_images/';
$datesDir = Storage::directories($path);
$this->extractData($datesDir);
// dd($this->processingUnits);
$this->calculateStatistics();
// Output the statistics
$this->info('Total sum: '.$this->totalSum);
$this->info('Total count: '.$this->totalCount);
$this->info('Overall average: '.$this->overallAvg);
$this->info('Standard deviation: '.$this->stdDev);
}
private function extractProcessingUnitsFromFile($filePath)
{
$data = json_decode(Storage::get($filePath), true);
$responseHeaders = $data['response']['headers'];
$processingUnits = $responseHeaders['x-processingunits-spent'];
return $processingUnits;
}
private function calculateStatistics()
{
foreach ($this->processingUnits as $date => $values) {
$processingUnitsPerDate = array_sum($values);
$this->totalSum += $processingUnitsPerDate;
$this->totalCount++;
$this->allValues[$date] = $processingUnitsPerDate;
}
$this->overallAvg = $this->totalSum / $this->totalCount;
$variance = array_reduce($this->allValues, function ($carry, $item) {
return $carry + pow($item - $this->overallAvg, 2);
}, 0) / $this->totalCount;
$this->stdDev = sqrt($variance);
}
private function extractData(array $datesDir): void
{
foreach ($datesDir as $dateDir) {
$date = basename($dateDir);
$subdirectories = Storage::directories($dateDir);
$this->processingUnits[$date] = [];
foreach ($subdirectories as $subdirectory) {
$requestPath = $subdirectory.'/request.json';
if (Storage::fileExists($requestPath)) {
$this->processingUnits[$date][] = $this->extractProcessingUnitsFromFile($requestPath);
}
}
}
}
}

File diff suppressed because it is too large Load diff

Binary file not shown.

View file

@ -26,7 +26,7 @@ if (file.exists(file_path)) {
group_by(field, sub_field) %>%
summarise(across(everything(), ~ first(na.omit(.))), .groups = "drop")
}
head(pivot_stats2)
#%>% drop_na(pivot_quadrant)
# gather data into long format for easier calculation and visualisation
@ -61,20 +61,23 @@ extract_CI_data <- function(field_names, harvesting_data, field_CI_data, season)
return(data.frame())
}
# Create a linear interpolation function for the CI data
ApproxFun <- approxfun(x = filtered_field_CI_data$Date, y = filtered_field_CI_data$value)
Dates <- seq.Date(ymd(min(filtered_field_CI_data$Date)), ymd(max(filtered_field_CI_data$Date)), by = 1)
LinearFit <- ApproxFun(Dates)
# Combine interpolated data with the original CI data
CI <- data.frame(Date = Dates, FitData = LinearFit) %>%
left_join(., filtered_field_CI_data, by = "Date") %>%
left_join(., filtered_field_CI_data, by = "Date") %>%
filter(Date > filtered_harvesting_data$season_start & Date < filtered_harvesting_data$season_end)
# If CI is empty after filtering, return an empty dataframe
if (nrow(CI) == 0) {
message ('CI empty after filtering')
return(data.frame())
}
# Add additional columns if data exists
CI <- CI %>%
mutate(DOY = seq(1, n(), 1),
@ -85,7 +88,7 @@ extract_CI_data <- function(field_names, harvesting_data, field_CI_data, season)
return(CI)
}
message(harvesting_data)
CI_all <- map_df(years, function(yr) {
# yr = 2021
@ -96,11 +99,13 @@ CI_all <- map_df(years, function(yr) {
filter(year == yr) %>%
filter(!is.na(season_start)) %>%
pull(sub_field)
# Filter sub_fields to only include those with value data in pivot_stats_long
valid_sub_fields <- sub_fields %>%
keep(~ any(pivot_stats_long$sub_field == .x))
# Extract data for each valid field
# Extract data for each valid field
map(valid_sub_fields, ~ extract_CI_data(.x, harvesting_data = harvesting_data, field_CI_data = pivot_stats_long, season = yr)) %>%
list_rbind()
})
@ -112,9 +117,7 @@ CI_all <- map_df(years, function(yr) {
mutate(CI_per_day = FitData - lag(FitData), cumulative_CI = cumsum(FitData))
# }
message('CI_all cumulative')
message('show head')
saveRDS(CI_all, here(cumulative_CI_vals_dir,"All_pivots_Cumulative_CI_quadrant_year_v2.rds"))
message('rds saved')

View file

@ -29,9 +29,10 @@ dir.create(here(daily_vrt), showWarnings = FALSE)
dir.create(merged_final,showWarnings = FALSE)
dir.create(harvest_dir,showWarnings = FALSE)
field_boundaries_sf <- st_read(here(data_dir, "pivot.geojson"))
field_boundaries_sf <- st_read(here(data_dir, "pivot.geojson"), crs = 4326)
names(field_boundaries_sf) <- c("field", "sub_field", "geometry")
field_boundaries <- field_boundaries_sf %>% terra::vect()
harvesting_data <- read_excel(here(data_dir, "harvest.xlsx")) %>%

View file

@ -1,8 +1,8 @@
#!/bin/bash
end_date=$(date +'%Y-%m-%d')
offset=7
project_dir="chemba"
offset=28
project_dir="Bagamoyo_trial"
# Parse command line arguments
for arg in "$@"; do
@ -29,7 +29,7 @@ echo "offset: $offset"
# Check if required arguments are set
if [ -z "$end_date" ] || [ -z "$project_dir" ] || [ -z "$offset" ]; then
echo "Missing arguments. Use: update_RDS.sh --end_date=2024-01-01 --offset=7 --project_dir=chemba"
echo "Missing arguments. Use: update_RDS.sh --end_date=2024-01-01 --offset=28 --project_dir=Bagamoyo_trial"
exit 1
fi