Data Loading and File Discovery - Hazelbean Tutorial

Data Loading and File Discovery

Overview

Understand intelligent file discovery and data loading with Hazelbean

🎯 Learning Objectives

This tutorial will help you understand:

  • Get_path: Core concepts and practical application

  • Raster Loading: Core concepts and practical application

  • File Discovery: Core concepts and practical application

πŸ’» Hands-on Learning: Each code block is executable - run them step-by-step to build understanding progressively.

Key Concepts

  • Get_path: Intelligent file discovery system that searches multiple data locations automatically

  • Raster Loading: Important concept for effective Hazelbean workflows

  • File Discovery: Important concept for effective Hazelbean workflows

Tutorial

πŸš€ Getting Started
  1. Make sure you have the Hazelbean environment activated
  2. Run each code block in order to see the functionality in action
  3. Try modifying parameters to experiment with different results
  4. Refer back to the Key Concepts if you need clarification

πŸ“ Working Directory: This tutorial assumes you’re running from the project root directory.

Complete Example

The following code demonstrates the complete workflow for this step:

"""
Demonstrates intelligent file location and basic raster loading.
"""

# Initialize project (builds on step 1)
p = hb.ProjectFlow('hazelbean_tutorial')

print("=== Hazelbean Data Loading Demo ===")
print()

# Demonstrate get_path() - Hazelbean's intelligent file finder
print("=== Intelligent File Discovery with get_path() ===")

# Try to find a test raster (get_path searches multiple locations)
try:
    raster_path = p.get_path('tests/ee_r264_ids_900sec.tif')
    # Verify the file actually exists
    if hb.path_exists(raster_path):
        print(f"βœ“ Found raster: {raster_path}")
        found_raster = True
    else:
        raise FileNotFoundError("Path found but file doesn't exist")
except:
    print("βœ— Test raster not found, using alternative...")
    # Fallback to any available data
    try:
        raster_path = p.get_path('pyramids/ha_per_cell_900sec.tif')
        if hb.path_exists(raster_path):
            print(f"βœ“ Found alternative raster: {raster_path}")
            found_raster = True
        else:
            raise FileNotFoundError("Alternative path found but file doesn't exist")
    except:
        print("βœ— No sample raster found in data directories")
        found_raster = False

if found_raster:
    print()
    print("=== Loading and Examining Raster Data ===")

    # Load raster information without reading full data
    raster_info = hb.get_raster_info_hb(raster_path)

    print(f"Raster size: {raster_info['raster_size']} (width x height)")
    print(f"Pixel size: {raster_info['pixel_size']}")
    print(f"Number of bands: {raster_info['n_bands']}")
    print(f"Data type: {raster_info.get('datatype', 'Unknown')}")
    print(f"NoData value: {raster_info['ndv']}")

    # Load raster data as numpy array
    print()
    print("=== Loading Raster as Array ===")
    raster_array = hb.as_array(raster_path)

    print(f"Array shape: {raster_array.shape}")
    print(f"Array data type: {raster_array.dtype}")
    print(f"Min value: {np.nanmin(raster_array):.2f}")
    print(f"Max value: {np.nanmax(raster_array):.2f}")
    print(f"Mean value: {np.nanmean(raster_array):.2f}")

else:
    print()
    print("=== Alternative: Create Sample Data ===")
    print("When sample data isn't available, you can create test arrays:")

    # Create a simple test array
    test_array = np.random.rand(100, 100) * 1000
    print(f"Created test array shape: {test_array.shape}")
    print(f"Test array range: {test_array.min():.1f} to {test_array.max():.1f}")

print()
print("πŸŽ‰ Data loading complete!")
print("Next: Run step_3_basic_processing.py to learn raster operations")

Step-by-Step Breakdown

Expected Output

When you run this tutorial, you should see output similar to:

Tutorial execution completed successfully.

Learning Tips

πŸ“š Try This Next
  • Modify the example parameters to see how results change

  • Combine concepts from this step with previous tutorials

  • Experiment with your own data using the same patterns

  • Continue to Basic Processing Operations for the next learning step

πŸ” Troubleshooting: If you encounter issues, check that your environment is properly activated and all required data files are accessible.