diff --git a/build_mosaic.sh b/build_mosaic.sh index 81aad6a..a873366 100755 --- a/build_mosaic.sh +++ b/build_mosaic.sh @@ -33,7 +33,7 @@ if [ -z "$end_date" ] || [ -z "$data_dir" ] || [ -z "$offset" ]; then exit 1 fi -echo 2_CI_data_prep.R $end_date $offset $data_dir +echo mosaic_creation.R $end_date $offset $data_dir cd ../r_app -Rscript 2_CI_data_prep.R $end_date $offset $data_dir \ No newline at end of file +Rscript mosaic_creation.R $end_date $offset $data_dir \ No newline at end of file diff --git a/build_report.sh b/build_report.sh index 6cb4097..d811879 100755 --- a/build_report.sh +++ b/build_report.sh @@ -6,9 +6,6 @@ for arg in "$@"; do --filename=*) filename="${arg#*=}" ;; - --weeks_ago=*) - weeks_ago="${arg#*=}" - ;; --report_date=*) report_date="${arg#*=}" ;; @@ -23,14 +20,13 @@ for arg in "$@"; do done # Check if required arguments are set -if [ -z "$filename" ] || [ -z "$weeks_ago" ] || [ -z "$report_date" ] || [ -z "$data_dir" ]; then - echo "Missing arguments. Use: build_reports.sh --filename=hello.txt --weeks_ago=3 --report_date=2020-01-01 --data_dir=chemba" +if [ -z "$filename" ] || [ -z "$report_date" ] || [ -z "$data_dir" ]; then + echo "Missing arguments. Use: build_reports.sh --filename=hello.txt --report_date=2020-01-01 --data_dir=chemba" exit 1 fi # Script logic here echo "Filename: $filename" -echo "Weeks ago: $weeks_ago" echo "Report date: $report_date" echo "Data directory: $data_dir" diff --git a/laravel_app/app/Jobs/ProjectInterpolateGrowthModelJob.php b/laravel_app/app/Jobs/ProjectInterpolateGrowthModelJob.php index 0e02540..4389baf 100644 --- a/laravel_app/app/Jobs/ProjectInterpolateGrowthModelJob.php +++ b/laravel_app/app/Jobs/ProjectInterpolateGrowthModelJob.php @@ -31,7 +31,7 @@ public function __construct(Project $project) public function handle(): void { $command = [ - sprintf('%interpolate_growth_model.sh', base_path('../')), + sprintf('%s/interpolate_growth_model.sh', base_path('../')), sprintf('--project_dir=%s', $this->project->download_path), ]; diff --git a/laravel_app/app/Jobs/ProjectMosiacGeneratorJob.php b/laravel_app/app/Jobs/ProjectMosiacGeneratorJob.php index 179aded..7e634a3 100644 --- a/laravel_app/app/Jobs/ProjectMosiacGeneratorJob.php +++ b/laravel_app/app/Jobs/ProjectMosiacGeneratorJob.php @@ -80,6 +80,10 @@ public static function handleFor(Project $project,Carbon $endDate, int $offset): logger("ProjecMosaicGeneratorJob::handleFor(end_date: $endDate, offset: $offset): InvalidMosaic."); return new NullJob(); } + + $week = $endDate->clone()->next($project->mail_day)->week; + $year = $endDate->clone()->next($project->mail_day)->year; + /** * @var ProjectMosaic $mosaic */ @@ -89,11 +93,11 @@ public static function handleFor(Project $project,Carbon $endDate, int $offset): 'offset' => $offset, ], [ - 'name' => sprintf('Period %s - %s', $endDate->format('Y-m-d'), $endDate->copy()->subDays($offset)->format('Y-m-d')), + 'name' => sprintf('Week_%s_%s', $week, $year), 'path' => sprintf('%s/%s/%s', $project->download_path, 'mosaics', - ProjectMosaic::getFilenameByPeriod($endDate,$offset) + sprintf('week_%s_%s.tif', $week, $year) ), 'end_date' => $endDate->format('Y-m-d'), 'offset' => $offset, diff --git a/laravel_app/app/Jobs/ProjectReportGeneratorJob.php b/laravel_app/app/Jobs/ProjectReportGeneratorJob.php index 731ff88..3a89011 100644 --- a/laravel_app/app/Jobs/ProjectReportGeneratorJob.php +++ b/laravel_app/app/Jobs/ProjectReportGeneratorJob.php @@ -36,14 +36,11 @@ public function __construct(ProjectReport $projectReport, $sendMail = false) public function handle() { // TODO check the changements due to migration - //$this->projectReport->weeksAgo(); - $projectFolder = base_path('../'); $command = [ sprintf('%sbuild_report.sh', $projectFolder), sprintf('--filename=%s', $this->projectReport->getFullPathName()), - sprintf('--weeks_ago=%s', $this->projectReport->weeksAgo()), sprintf('--report_date=%s', $this->projectReport->getReportDate()), sprintf('--data_dir=%s', $this->projectReport->project->download_path), ]; diff --git a/laravel_app/app/Models/Project.php b/laravel_app/app/Models/Project.php index 2dc31ae..ee19e51 100644 --- a/laravel_app/app/Models/Project.php +++ b/laravel_app/app/Models/Project.php @@ -281,7 +281,7 @@ public function hasInvalidMosaicFor(Carbon $endDate,int $offset): bool // parameters : $ // check if the mail day happens the day before mosaic -> good $dayOfWeekIso = Carbon::parse($this->mail_day)->dayOfWeekIso; - $min_updated_at_date = $endDate + $min_updated_at_date = $endDate->copy() ->startOfWeek() ->addDays($dayOfWeekIso - 1) ->format('Y-m-d'); @@ -296,7 +296,7 @@ public function hasInvalidMosaicFor(Carbon $endDate,int $offset): bool public function scheduleReport(?Carbon $endDate = null, ?int $offset = null) { - if($endDate->isFuture() || $endDate->isToday() || $offset <= 0){ + if($endDate?->isFuture() || $endDate?->isToday() || $offset <= 0){ logger('EndDate is today or in the future.'); $endDate = null; $offset = null; diff --git a/laravel_app/app/Models/ProjectMosaic.php b/laravel_app/app/Models/ProjectMosaic.php index db2298f..024ddbc 100644 --- a/laravel_app/app/Models/ProjectMosaic.php +++ b/laravel_app/app/Models/ProjectMosaic.php @@ -27,14 +27,17 @@ class ProjectMosaic extends Model ]; - public static function getFilenameByPeriod(Carbon $endDate,int $offset) + public static function getFilenameByPeriod(Carbon $endDate, int $offset) { - return sprintf('period_%s_%s.tif',(clone $endDate)->subdays($offset)->format('Y-m-d'),$endDate->format('Y-m-d')); + return sprintf('week_%s_%s.tif', (clone $endDate)->subdays($offset)->week, $endDate->year); } - public static function projectMosaicNameFormat(Carbon $endDate,int $offset):string + public static function projectMosaicNameFormat(Carbon $endDate, int $offset): string { - return 'Period '.$endDate->copy()->subDays($offset)->toDateString().' - '.$endDate->toDateString(); + return sprintf('Week_%s_%s', + $endDate->clone()->subDays($offset)->week, + $endDate->clone()->subDays($offset)->year + ); } public function project() diff --git a/laravel_app/tests/Unit/Models/ProjectTest.php b/laravel_app/tests/Unit/Models/ProjectTest.php index ba1bf44..7e8aa98 100644 --- a/laravel_app/tests/Unit/Models/ProjectTest.php +++ b/laravel_app/tests/Unit/Models/ProjectTest.php @@ -159,13 +159,13 @@ public function when_getFileDownloadsFor_is_called_it_returns_a_collection_of_se 'download_path' => 'project_download_path', ]); - $downloads = $project->getFileDownloadsFor(new Carbon('2024-06-18'),7); + $downloads = $project->getFileDownloadsFor(new Carbon('2024-06-18'), 7); $this->assertCount(28, $downloads); collect($downloads)->each(fn($job) => $this->assertInstanceOf(ProjectDownloadTiffJob::class, $job)); } /** @test */ - public function when_getMosaicsFor_is_called_it_returns_a_collection_of_seven_downloads_jobs() + public function when_getMosaicsFor_is_called_it_returns_a_collection_of_4_jobs() { /* @var Project $project */ $project = Project::create([ @@ -178,10 +178,60 @@ public function when_getMosaicsFor_is_called_it_returns_a_collection_of_seven_do collect($mosaics)->each(fn($job) => $this->assertInstanceOf(ProjectMosiacGeneratorJob::class, $job)); } + /** @test */ + public function when_getMosaicsFor_is_called_it_returns_a_collection_of_4_jobs_with_correct_week_numbers() + { + /* @var Project $project */ + $project = Project::create([ + 'name' => 'project_name', + 'download_path' => 'project_download_path', + 'mail_day' => 'Friday', + ]); + + $mosaics = $project->getMosaicsFor(new Carbon('2024-07-02')); + $this->assertCount(4, $mosaics); + collect($mosaics)->each(fn($job) => $this->assertInstanceOf(ProjectMosiacGeneratorJob::class, $job)); + + $this->assertEquals([ + "Week_27_2024", + "Week_26_2024", + "Week_25_2024", + "Week_24_2024", + ], + collect($mosaics)->map(function ($job) { + return $job->mosaic->name; + })->toArray()); + } + + /** @test */ + public function when_getMosaicsFor_is_called_it_returns_a_collection_of_4_jobs_with_where_current_day_is_in_week_26_but_the_mail_day_is_in_week_27() + { + /* @var Project $project */ + $project = Project::create([ + 'name' => 'project_name', + 'download_path' => 'project_download_path', + 'mail_day' => 'Friday', + ]); + + $mosaics = $project->getMosaicsFor(new Carbon('2024-06-28')); + $this->assertCount(4, $mosaics); + collect($mosaics)->each(fn($job) => $this->assertInstanceOf(ProjectMosiacGeneratorJob::class, $job)); + + $this->assertEquals([ + "Week_27_2024", + "Week_26_2024", + "Week_25_2024", + "Week_24_2024", + ], + collect($mosaics)->map(function ($job) { + return $job->mosaic->name; + })->toArray()); + } + /** @test */ public function when_getReport_is_called_it_returns_a_jobs() { - /* @var Project $project*/ + /* @var Project $project */ $project = Project::create([ 'name' => 'project_name', 'download_path' => 'project_download_path', @@ -195,14 +245,14 @@ public function when_getReport_is_called_it_returns_a_jobs() /** @test */ public function it_can_create_a_chain_of_batches_that_result_in_a_report() { - /* @var Project $project*/ + /* @var Project $project */ $project = Project::create([ 'name' => 'project_name', 'download_path' => 'project_download_path', ]); Bus::fake(); - $project->scheduleReport(new Carbon('2023-01-01'),7); + $project->scheduleReport(new Carbon('2023-01-01'), 7); Bus::assertChained([ Bus::chainedBatch(function (PendingBatch $batch) { $batch; @@ -228,14 +278,15 @@ public function when_friday_and_first_week_it_should_schedule($date, $day, $resu Carbon::setTestNow(Carbon::parse($day)); $project = Project::factory()->create([ 'mail_frequency' => 'weekly', - 'mail_day' => 'Friday', + 'mail_day' => 'Friday', ]); $this->assertEquals($result, $project->shouldSchedule()); } - public static function scheduleDayProvider(){ - return [ + public static function scheduleDayProvider() + { + return [ ['date' => '2024-03-01', 'day' => 'Friday', 'result' => true], ['date' => '2024-03-02', 'day' => 'Saturday', 'result' => false], ['date' => '2024-03-03', 'day' => 'Sunday', 'result' => false], diff --git a/r_app/CI_report_dashboard_planet.Rmd b/r_app/CI_report_dashboard_planet.Rmd index 44eb7cf..4cda350 100644 --- a/r_app/CI_report_dashboard_planet.Rmd +++ b/r_app/CI_report_dashboard_planet.Rmd @@ -3,7 +3,7 @@ params: ref: "word-styles-reference-var1.docx" output_file: CI_report.docx report_date: "2024-04-18" - data_dir: "chemba" + data_dir: "Sony" output: # html_document: # toc: yes @@ -70,10 +70,10 @@ source(here("r_app", "parameters_project.R")) ```{r week, message=FALSE, warning=FALSE, include=FALSE} -# week <- 5 -#today = "2023-12-12" today <- as.character(report_date) week <- week(today) + week <- 25 +today = "2024-06-21" #today = as.character(Sys.Date()) @@ -115,7 +115,7 @@ This PDF-dashboard shows the status of your fields on a weekly basis. We will sh # remove_pivots <- c("1.1", "1.12", "1.8", "1.9", "1.11", "1.14") CI_quadrant <- readRDS(here(cumulative_CI_vals_dir,"All_pivots_Cumulative_CI_quadrant_year_v2.rds"))# %>% - # rename(pivot_quadrant = Field) + # rename(pivot_quadrant = field) CI <- brick(here(weekly_CI_mosaic, paste0("week_",week, "_", year, ".tif"))) %>% subset("CI") CI_m1 <- brick(here(weekly_CI_mosaic, paste0("week_",week_minus_1, "_", year_1, ".tif"))) %>% subset("CI") @@ -140,16 +140,16 @@ AllPivots0 <- field_boundaries_sf # pivots_dates <- readRDS(here(harvest_dir, "harvest_data_new")) # pivots_dates$pivot <- factor(pivots_dates$pivot, levels = c("1.1", "1.2", "1.3", "1.4", "1.6", "1.7", "1.8", "1.9", "1.10", "1.11", "1.12", "1.13", "1.14" , "1.16" , "1.17" , "1.18" ,"2.1", "2.2", "2.3" , "2.4", "2.5", "3.1", "3.2", "3.3", "4.1", "4.2", "4.3", "4.4", "4.5", "4.6", "5.1" ,"5.2", "5.3", "5.4", "6.1", "6.2", "DL1.1", "DL1.3")) -AllPivots <- merge(AllPivots0, harvesting_data, by = c("Field", "subField")) #%>% -#rename(Field = pivot, subField = pivot_quadrant) #%>% select(-pivot.y) -head(AllPivots) +#AllPivots <- merge(AllPivots0, harvesting_data, by = c("field", "sub_field")) #%>% +#rename(field = pivot, sub_field = pivot_quadrant) #%>% select(-pivot.y) +#head(AllPivots) -AllPivots_merged <- AllPivots %>% #dplyr::select(Field, subField, sub_area) %>% unique() %>% - group_by(Field) %>% summarise(sub_area = first(sub_area)) +#AllPivots_merged <- AllPivots %>% #dplyr::select(field, sub_field, sub_area) %>% unique() %>% +# group_by(field) %>% summarise(sub_area = first(sub_area)) -AllPivots_merged <- st_transform(AllPivots_merged, crs = proj4string(CI)) +#AllPivots_merged <- st_transform(AllPivots_merged, crs = proj4string(CI)) -pivot_names <- unique(CI_quadrant$Field) +#pivot_names <- unique(CI_quadrant$field) ``` @@ -220,7 +220,7 @@ create_CI_map <- function(pivot_raster, pivot_shape, pivot_spans, show_legend = tm_layout(main.title = paste0("\nMax CI week ", week,"\n", age, " weeks old"), main.title.size = 0.7, legend.show = show_legend) + tm_shape(pivot_shape) + - tm_borders(lwd = 3) + tm_text("subField", size = 1/2) + + tm_borders(lwd = 3) + tm_text("sub_field", size = 1/2) + tm_shape(pivot_spans) + tm_borders(lwd = 0.5, alpha=0.5) } @@ -230,16 +230,16 @@ create_CI_diff_map <- function(pivot_raster, pivot_shape, pivot_spans, show_lege tm_layout(main.title = paste0("CI change week ", week_1, "- week ",week_2, "\n", age," weeks old"), main.title.size = 0.7, legend.show = show_legend) + tm_shape(pivot_shape) + - tm_borders(lwd = 3) + tm_text("subField", size = 1/2) + + tm_borders(lwd = 3) + tm_text("sub_field", size = 1/2) + tm_shape(pivot_spans) + tm_borders(lwd = 0.5, alpha=0.5) } ci_plot <- function(pivotName){ # pivotName = "1.1" - pivotShape <- AllPivots_merged %>% terra::subset(Field %in% pivotName) %>% st_transform(crs(CI)) - age <- AllPivots %>% dplyr::filter(Field %in% pivotName) %>% st_drop_geometry() %>% dplyr::select(Age) %>% unique() + pivotShape <- AllPivots0 %>% terra::subset(field %in% pivotName) %>% st_transform(crs(CI)) + age <- harvesting_data %>% dplyr::filter(field %in% pivotName) %>% dplyr::select(age) %>% unique() - AllPivots2 <- AllPivots %>% dplyr::filter(Field %in% pivotName) + AllPivots2 <- AllPivots0 %>% dplyr::filter(field %in% pivotName) singlePivot <- CI %>% crop(., pivotShape) %>% mask(., pivotShape) @@ -250,9 +250,9 @@ ci_plot <- function(pivotName){ abs_CI_last_week <- last_week_dif_raster_abs %>% crop(., pivotShape) %>% mask(., pivotShape) abs_CI_three_week <- three_week_dif_raster_abs %>% crop(., pivotShape) %>% mask(., pivotShape) - planting_date <- harvesting_data %>% dplyr::filter(Field %in% pivotName) %>% ungroup() %>% dplyr::select(Season_start) %>% unique() + planting_date <- harvesting_data %>% dplyr::filter(field %in% pivotName) %>% ungroup() %>% dplyr::select(season_start) %>% unique() - joined_spans2 <- joined_spans %>% st_transform(crs(pivotShape)) %>% dplyr::filter(Field %in% pivotName) #%>% unique() %>% st_crop(., pivotShape) + joined_spans2 <- AllPivots0 %>% st_transform(crs(pivotShape)) %>% dplyr::filter(field %in% pivotName) #%>% unique() %>% st_crop(., pivotShape) CImap_m2 <- create_CI_map(singlePivot_m2, AllPivots2, joined_spans2, show_legend= T, legend_is_portrait = T, week = week_minus_2, age = age -2) CImap_m1 <- create_CI_map(singlePivot_m1, AllPivots2, joined_spans2, show_legend= F, legend_is_portrait = F, week = week_minus_1, age = age -1) @@ -264,7 +264,7 @@ ci_plot <- function(pivotName){ tst <- tmap_arrange(CImap_m2, CImap_m1, CImap,CI_max_abs_last_week, CI_max_abs_three_week, nrow = 1) - cat(paste("## Pivot", pivotName, "-", age$Age[1], "weeks after planting/harvest", "\n")) + cat(paste("## Pivot", pivotName, "-", age, "weeks after planting/harvest", "\n")) # cat("\n") # cat('