Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Writing the new dataset to disk

Code Block
languager
# Function to generate the output filename
generate_output_filename <- function(template_path, availability_date, feature_name) {
  # Extract the base parts of the template filename
  template_basename <- basename(template_path)
  parts <- strsplit(template_basename, "_")[[1]]
  
  # Construct the new filename
  new_filename <- paste(parts[1], parts[2], availability_date, feature_name, sep="_")
  
  # Ensure the filename ends with .tif
  if (!endsWith(new_filename, ".tif")) {
    new_filename <- paste0(new_filename, ".tif")
  }
  
  return(new_filename)
}

# Get user input for availability date and feature name
availability_date <- readline(prompt = "Enter the availability date (YYYY-MM-DD): ")
feature_name <- readline(prompt = "Enter the name of your new feature: ")

# Generate the output filename
output_filename <- generate_output_filename(template_raster, availability_date, feature_name)

# Construct the full output path
output_path <- file.path(dirname(template_raster), output_filename)

# Write the result to a file
writeRaster(final_raster, output_path)

cat("Raster saved as:", output_path, "\n")

This script does the following:

...

Defines a function generate_output_filename that takes the template path, availability date, and feature name as inputs.

...

The standard format is: {TILE_ID}_{DATE}_{FEATURE}.tif

Where:

  • {TILE_ID} is the geographic identifier (e.g., "00N_010E")

...

Constructs a new filename using the tile identifier, user-specified availability date, and feature name.

...

Ensures the filename ends with ".tif".

...

Prompts the user to input the availability date and feature name.

...

Generates the output filename using the function.

...

Constructs the full output path in the same directory as the template raster.

...

Writes the raster to the generated filename.

...

Prints the path where the raster was saved.

Example usage:

...

  • {DATE} is the availability or creation date in YYYY-MM-01 format. The day numbers should always be 01

  • {FEATURE} is a descriptive name of the raster's content. This should not contain underscores

Example: "00N_010E_20212023-0106-01_forestfirevegetationdensity.tif", and the user inputs:

  • Availability date: 2023-06-30

  • Feature name: vegetation_density

The output file would be named: "00N_010E_2023-06-30_vegetation_density.tif"

This approach ensures that the output filename maintains the structure of the template filename while allowing for user-specified dates and feature names. It also places the new file in the same directory as the template, maintaining the file organization.

When creating new rasters, users should:

  1. Use the same tile identifier as the template raster.

  2. Choose an appropriate date that represents when the data becomes available or relevant.

  3. Select a clear, concise name for the feature they've created.

  4. Ensure the file is saved as a GeoTIFF (.tif extension).

The raster should be stored in the input/preprocessed/{TILE_ID} folder