MaxMacros	60
Macro	0
MacroName START_REYES

################BEFORE STARTING###############
#  INTEGRATED MULTI-GRID PIPELINE
#  Supports: Single-grid and Multi-grid modes
#  Supports: Automated (FSD/Screen/Montage) workflows, with a new-run /
#            resume-from-any-stage prompt
#  Supports: Manual (FSD/Screen) workflows, single grid only
#
#  TURN OFF LOW DOSE ON SCOPE
#  TURN ON LOW DOSE IN SERIALEM
#  LOAD VIEW PARAMETERS
#  CHECK SPOT SIZE
#  LOAD RECORD PARAMETERS
#  CHECK PARALLEL BEAM CONDITION
#  For Multi-Grid: ENSURE AUTOLOADER IS PROPERLY LOADED
###################SETTINGS###################

ClearPersistentVars

Echo
Echo ================================================
Echo ===== INTEGRATED MULTI-GRID PIPELINE =====
Echo ================================================
Echo
Echo This pipeline supports:
Echo - Single-grid and Multi-grid acquisition
Echo - Automated modes: FSD, Screen, Montage
Echo - Manual modes: FSD, Screen (single-grid only)
Echo

# Call setup helper to collect all parameters
Call Setup_Helper

# =============================================================================
# NEW RUN OR RESUME
# =============================================================================
# One question, for the automated FSD and Screen modes, single grid or
# multi-grid. Pressing Enter starts a new run. Answering 0 asks which stage to
# restart from, and that number has NO default: a stray Enter there must not
# skip stages. Automated_Pipeline_Helper carries a gate on every stage, so a
# run can be re-entered anywhere.
#
# WHAT A RESUME RELIES ON
#   - the SAME base directory and the SAME sample name as the interrupted run;
#   - every acquisition stage skipping the items it already finished (the
#     *_done.flag files), so resuming AT the stage that stalled re-collects only
#     what is missing;
#   - the navigator: left open it is used as it is, otherwise the helper reads
#     <sample>_115LM_NxN_nav.nav back from the grid's folder;
#   - the block of crystal targets REyes_DFMV merged, which it records in
#     targets\range_dfmv.txt, so no item numbers have to be read off a log.
#
# MULTI-GRID. resume_pending is handed to the FIRST grid that is processed and
# then reset to 1, so only that grid resumes and every later grid starts from
# its montage. Give the slot range starting AT the grid to resume.
#
# The manual workflow is not offered a resume (single grid, operator at the
# microscope).
resume_pending := 1
resume_from := 1
If $workflow_type == 1 AND $pipeline_mode != 3
   If $pipeline_mode == 1
      resume_max = 7
   Else
      resume_max = 5
   EndIf

   Echo
   Echo ================================================
   Echo NEW RUN OR RESUME?
   Echo ================================================
   Echo   1 = NEW run, start from the montage (default - just press Enter)
   Echo   0 = RESUME an interrupted run in this SAME base directory
   EnterDefaultedNumber 1 -1 Is this a NEW run? (1 = yes, 0 = no, resume):
   new_run = $reportedValue1
   If $new_run != 0 AND $new_run != 1
      Exit Answer 1 for a new run or 0 to resume
   EndIf

   If $new_run == 0
      Echo
      Echo Restart from which stage?  Pipeline: $pipeline_name
      Echo   1  montage             (an existing montage file is kept, not retaken)
      Echo   2  grid squares        (waits for REyes to write grid_squares\eucentricity.nav)
      Echo   3  eucentricity        (needs grid_squares\eucentricity.nav on disk)
      Echo   4  eucentric plane     (waits for grid_squares\eucentricity_correction.png)
      If $pipeline_mode == 1 OR $pipeline_mode == 2
         Echo   5  step-scan           (grid squares already mapped are skipped)
      EndIf
      If $pipeline_mode == 1
         Echo   6  wait for targets    (waits for targets\targets.nav)
         Echo   7  rotation movies     (crystals already collected are skipped)
      EndIf
      Echo
      Echo If you are unsure between two stages, choose the EARLIER one: a stage
      Echo that is already complete costs only the time to walk its done flags.
      Echo Use the SAME base directory and sample name as the interrupted run.
      If $single_grid_mode == 0
         Echo
         Echo MULTI-GRID: the resume applies to the FIRST grid of the slot range
         Echo only (slot $MinSlotNum unless it is SKIPped). Later grids start from
         Echo their montage. The grid must be back IN ITS SLOT, not on the stage.
      EndIf
      EnterOneNumber Restart from which stage number (1 to $resume_max)?
      If $reportedValue1 < 1 OR $reportedValue1 > $resume_max
         Exit Stage must be between 1 and $resume_max for this pipeline
      EndIf
      resume_pending := $reportedValue1
   EndIf
   Echo RUN_START | new_run=$new_run | resume_from=$resume_pending | pipeline=$pipeline_name
EndIf

# Check for unsupported combination: Multi-grid + Manual
If $single_grid_mode == 0 AND $workflow_type == 2
   Echo
   Echo ================================================
   Echo ERROR: UNSUPPORTED COMBINATION
   Echo ================================================
   Echo
   Echo Multi-grid manual mode is not supported.
   Echo Please use single-grid mode for manual workflows.
   Echo
   Echo ================================================
   Exit Multi-grid manual mode is not supported
Endif

# Slot verification (multi-grid only)
If $single_grid_mode == 0
   Call Slot_Verification_Helper
Endif

# Directory setup
Call Directory_Setup_Helper

# REyes setup prompt (for automated FSD/Screen, optional for Montage, required for Manual)
If $workflow_type == 1
   # Automated mode
   If $pipeline_mode == 1 OR $pipeline_mode == 2
      # FSD or Screen - REyes required
      Call REyes_Setup_Automated
   ElseIf $pipeline_mode == 3
      # Montage - REyes optional
      Call REyes_Setup_Optional
   Endif
ElseIf $workflow_type == 2
   # Manual mode - REyes will be prompted after manual selection
   Echo
   Echo ================================================
   Echo Note: REyes setup will be required after manual grid square selection
   Echo ================================================
Endif

################# MAIN ACQUISITION LOOP #################

Echo
Echo ================================================
Echo ===== STARTING ACQUISITION =====
Echo ================================================

If $single_grid_mode == 0
   SlotNum = $MinSlotNum
Else
   # Single-grid mode has no slot, but $SlotNum is referenced by messages
   # further down (including the error handler), so it must be defined.
   SlotNum = 0
Endif
successful_grids = 0
failed_grids = 0
skipped_grids = 0

NewArray grid_results 1 $ItrNum
NewArray failure_reasons 1 $ItrNum

Loop $ItrNum slotIndex

   current_sample = $sample_names[$slotIndex]
   current_base_name = $base_names[$slotIndex]

   If $single_grid_mode == 1
      Echo
      Echo ========================================
      Echo ===== PROCESSING SINGLE GRID ==========
      Echo ========================================
   Else
      Echo
      Echo ========================================
      Echo ========== PROCESSING SLOT $SlotNum ==========
      Echo ========================================

      CompareNoCase current_sample SKIP
      If $reportedValue1 == 0
         Echo Skipping slot $SlotNum as requested
         grid_results[$slotIndex] := SKIPPED
         skipped_grids = $skipped_grids + 1
         SlotNum = $SlotNum + 1
         Continue
      Endif
   Endif

   # The resume stage belongs to the first grid that reaches this point; every
   # grid after it starts from stage 1. See NEW RUN OR RESUME above.
   resume_from := $resume_pending
   resume_pending := 1

   Echo Sample: $current_sample
   If $single_grid_mode == 0
      Echo Slot: $SlotNum
   Endif
   ReportDateTime

   # Skip if slot was marked as problematic during setup (multi-grid only)
   If $single_grid_mode == 0
      If $slot_status[$slotIndex] != 1
         Echo WARNING: Skipping slot $SlotNum - Status: $slot_status[$slotIndex]
         grid_results[$slotIndex] := FAILED

         # Set failure reason based on status code
         If $slot_status[$slotIndex] == 0
            failure_reasons[$slotIndex] := User_requested_skip
         ElseIf $slot_status[$slotIndex] == -1
            failure_reasons[$slotIndex] := No_grid_in_slot
         ElseIf $slot_status[$slotIndex] == -2
            failure_reasons[$slotIndex] := Directory_creation_error
         Else
            failure_reasons[$slotIndex] := Unknown_setup_error
         Endif

         failed_grids = $failed_grids + 1
         SlotNum = $SlotNum + 1
         Continue
      Endif
   Endif

   # Set the sample name for pipeline scripts (use base name without -reyes suffix)
   samplename := $current_base_name

   # Set sample directory path
   sample_dir_path = $base_directory\$current_sample
   Echo Preparing grid: $current_sample
   If $single_grid_mode == 0
      Echo Slot: $SlotNum
   Endif

   # LOG ORDER: SaveLogOpenNew BEFORE SetDirectory
   # SaveLogOpenNew saves previous log in CURRENT directory (wherever we are)
   # and opens new log. Even if REyes changed directory, this captures it.
   #
   # Log name is built by concatenating VARIABLES ONLY, never `$var_text$var`.
   # Underscores are legal in SerialEM variable names, so `$current_base_name_`
   # is read as a variable called `current_base_name_`, not as the variable
   # followed by a literal underscore. Building the name through $log_sep
   # removes the ambiguity; the resulting filename is unchanged.
   log_sep @= _
   grid_log_name @= $current_base_name$log_sep$pipeline_name$log_sep$yyyymmdd
   Echo Opening new log file for: $current_base_name
   SaveLogOpenNew $grid_log_name
   Echo New log opened: $grid_log_name.log

   # Now change to grid directory
   Try
      SetDirectory $sample_dir_path
      set_directory := $sample_dir_path
      Echo Working directory set to: $sample_dir_path
   Catch
      Echo ERROR: Could not access directory: $sample_dir_path
      If $single_grid_mode == 1
         Exit Cannot proceed without sample directory
      Else
         Echo Skipping slot $SlotNum ($current_sample)
         grid_results[$slotIndex] := FAILED
         failure_reasons[$slotIndex] := Directory_access_error
         failed_grids = $failed_grids + 1
         SlotNum = $SlotNum + 1
         Continue
      Endif
   EndTry

   # Log header information now that we're in the correct directory
   Echo ================================================
   Echo Log file: $grid_log_name.log
   Echo Grid directory: $sample_dir_path
   Echo Sample: $current_sample
   If $single_grid_mode == 0
      Echo Slot: $SlotNum
   Endif
   ReportDateTime
   Echo ================================================

   # Load the cartridge (multi-grid mode only)
   If $single_grid_mode == 0
      Echo Loading cartridge from slot $SlotNum...
      Try
         SetColumnOrGunValve 0
         LoadCartridge $SlotNum
         Echo Successfully loaded cartridge from slot $SlotNum
         Delay 3 sec
         # Do not hand the grid to the pipeline while the stage still reports
         # busy after the exchange: the first navigator move would be refused
         # ("The stage is busy or not ready; move aborted"). Navigator moves
         # in the acquisition macros also wait and retry on their own.
         stage_settled = 0
         Loop 120
            ReportStageBusy
            If $reportedValue1 == 0
               stage_settled = 1
               Break
            EndIf
            Delay 1 sec
         EndLoop
         If $stage_settled == 0
            Echo WARNING: the stage still reports busy 2 min after the exchange
         EndIf
      Catch
         Echo ERROR: Failed to load cartridge from slot $SlotNum
         grid_results[$slotIndex] := FAILED
         failure_reasons[$slotIndex] := Cartridge_load_error
         failed_grids = $failed_grids + 1
         SlotNum = $SlotNum + 1
         Continue
      EndTry
   Else
      Echo Using grid already loaded on stage
   Endif

   # Reset partial completion flag
   partial_completion := 0

   # Execute the appropriate pipeline helper
   If $workflow_type == 1
      # Automated mode - entire pipeline in Try block
      Try
         Call Automated_Pipeline_Helper

         # partial_completion means the helper returned early or recorded the
         # grid's outcome itself, and has already updated the matching counter
         # (PARTIAL: no targets; FAILED: stopped by the spot-size guard or no
         # grid square mapped; SUCCESS with grid squares or crystals set aside).
         # Counting it again here would put one grid in two totals.
         If $partial_completion == 1
            Echo Outcome recorded by the pipeline helper
         Else
            # Mark as successful
            grid_results[$slotIndex] := SUCCESS
            failure_reasons[$slotIndex] := None
            successful_grids = $successful_grids + 1
            Echo SUCCESS: $current_sample completed without errors
         Endif

      Catch
         Echo
         Echo !!!!! ERROR DURING PIPELINE EXECUTION !!!!!
         If $single_grid_mode == 0
            Echo Grid: $current_sample (Slot $SlotNum)
         Else
            Echo Grid: $current_sample
         Endif
         Echo The pipeline encountered an error and stopped
         Echo Check individual log files for details
         grid_results[$slotIndex] := FAILED
         failure_reasons[$slotIndex] := Pipeline_execution_error
         failed_grids = $failed_grids + 1
      EndTry

   ElseIf $workflow_type == 2
      # Manual mode - Split around Pause to keep Pause OUTSIDE Try blocks

      # PART 1: Montage acquisition (before Pause)
      Try
         Call Manual_Pipeline_Part1
      Catch
         Echo
         Echo !!!!! ERROR DURING MONTAGE ACQUISITION !!!!!
         Echo Grid: $current_sample
         Echo Check log files for details
         grid_results[$slotIndex] := FAILED
         failure_reasons[$slotIndex] := Montage_acquisition_error
         failed_grids = $failed_grids + 1
         Continue
      EndTry

      # -----------------------------------------------------------
      # CRITICAL PAUSE - OUTSIDE ALL TRY/CATCH BLOCKS
      # -----------------------------------------------------------

      Echo
      Echo ================================================
      Echo ===== MANUAL INTERVENTION REQUIRED =====
      Echo ================================================
      Echo
      Echo CRITICAL: You must now MANUALLY select grid squares
      Echo
      Echo The montage has been acquired and is in the Navigator.
      Echo
      Echo Instructions:
      Echo 1. Open the Navigator window (if not already open)
      Echo 2. Review the montage that was just acquired
      Echo 3. Manually ADD POINT ITEMS for desired grid squares
      Echo 4. Make sure points are added to the navigator
      Echo 5. You can add as many or as few points as you want
      Echo 6. A dialog will appear asking if grid squares have been added
      Echo 7. Click NO to add them manually (script stays paused)
      Echo 8. When completely done, click YES to continue the pipeline
      Echo
      Echo ================================================

      Pause Have the grid squares been already added? (Press NO to add them manually)

      Echo
      Echo User confirmed manual grid square selection is complete

      # -----------------------------------------------------------
      # PART 2: Continue pipeline after Pause
      # -----------------------------------------------------------

      Try
         Call Manual_Pipeline_Part2

         # partial_completion means Manual_Pipeline_Part2 returned early or
         # recorded the grid's outcome itself, and has already updated the
         # matching counter. Counting it again here would put one grid in two
         # totals.
         If $partial_completion == 1
            Echo Outcome recorded by the pipeline helper
         Else
            # Mark as successful
            grid_results[$slotIndex] := SUCCESS
            failure_reasons[$slotIndex] := None
            successful_grids = $successful_grids + 1
            Echo SUCCESS: $current_sample completed without errors
         Endif

      Catch
         Echo
         Echo !!!!! ERROR DURING PIPELINE EXECUTION !!!!!
         Echo Grid: $current_sample
         Echo The pipeline encountered an error and stopped
         Echo Check individual log files for details
         grid_results[$slotIndex] := FAILED
         failure_reasons[$slotIndex] := Pipeline_execution_error
         failed_grids = $failed_grids + 1
      EndTry

   Endif

   # Close navigator for this grid (multi-grid automated mode only)
   # Single-grid mode and manual mode keep Navigator open for user
   If $workflow_type == 1 AND $single_grid_mode == 0
      Try
         CloseNavigator
         Echo Navigator closed for this grid
      Catch
         Echo Note: Navigator was already closed or could not be closed
      EndTry
   Else
      If $single_grid_mode == 1
         Echo Single-grid mode: Navigator remains open
      Else
         Echo Manual mode: Navigator remains open
      Endif
   Endif

   # Grid processing complete
   # Log will be closed by next iteration or by summary log opening
   Echo Grid processing complete: $current_base_name
   Echo ================================================

   # Increment slot number (multi-grid only)
   If $single_grid_mode == 0
      SlotNum = $SlotNum + 1

      # Pause between grids
      If $SlotNum <= $MaxSlotNum
         Echo
         Echo Preparing for next grid...
         Echo Brief pause to ensure system is ready...
         Delay 5 sec
      Endif
   Endif

EndLoop

################# CLEANUP #################

# LOG ORDER: SaveLogOpenNew BEFORE SetDirectory
# This saves last grid's log (wherever we are) and opens summary log
Echo All grids processed - opening summary log
# Variables only -- see the note on log naming above. Every piece of literal
# text lives in its own variable so no name can absorb the text after it.
log_sep @= _
summary_tag @= _summary
If $single_grid_mode == 1
   grid_prefix @= SingleGrid
Else
   grid_prefix @= MultiGrid
Endif
summary_log_name @= $grid_prefix$log_sep$pipeline_name$log_sep$yyyymmdd$summary_tag
SaveLogOpenNew $summary_log_name

# Now return to base directory
SetDirectory $base_directory
Echo ================================================
Echo SUMMARY LOG
Echo Directory: $base_directory
Echo ================================================

Echo
Echo ============= CLEANUP =============

If $single_grid_mode == 0
   Echo Unloading final grid back to autoloader...
   Try
      SetColumnOrGunValve 0
      UnloadCartridge
      Echo Successfully unloaded final grid
   Catch
      Echo WARNING: Could not unload final grid - may need manual unload
   EndTry
Else
   Echo Single grid mode - grid remains on stage
   Echo Please unload manually when ready
Endif

################# FINAL SUMMARY #################

Echo
Echo
Echo ================================================
Echo ============= PROCESSING COMPLETE ==============
Echo ================================================
If $single_grid_mode == 1
   Echo Acquisition mode: Single Grid
Else
   Echo Acquisition mode: Multi-Grid
Endif
If $workflow_type == 1
   Echo Workflow: Automated
Else
   Echo Workflow: Manual
Endif
Echo Pipeline mode: $pipeline_name
If $single_grid_mode == 1
   Echo Total grids: 1
Else
   Echo Total grids in range: $ItrNum
Endif
Echo Successful: $successful_grids
Echo Failed: $failed_grids
If $single_grid_mode == 0
   Echo Skipped: $skipped_grids
Endif
Echo
Echo Detailed Results:
Echo ------------------------------------------------
Echo (PARTIAL = screening completed but no targets found)
Echo

If $single_grid_mode == 1
   Echo Grid ($sample_names[1]): $grid_results[1] - $failure_reasons[1]
Else
   SlotNum = $MinSlotNum
   Loop $ItrNum slotIndex
      Echo Slot $SlotNum ($sample_names[$slotIndex]): $grid_results[$slotIndex] - $failure_reasons[$slotIndex]
      SlotNum = $SlotNum + 1
   EndLoop
Endif

Echo ================================================
Echo
Echo Next Steps:
If $pipeline_mode == 1
   Echo - Check movie data in movies\ subdirectory for each sample
   Echo - Review diffraction patterns in StepScan folders
   Echo - Analyze data with REyes processing pipeline
ElseIf $pipeline_mode == 2
   Echo - Review diffraction patterns in StepScan folders
   Echo - Select targets manually if needed for movie collection
ElseIf $pipeline_mode == 3
   Echo - Review montage images in each sample directory
   Echo - Assess grid quality and ice thickness
Endif
Echo - Check individual log files in each sample directory
Echo - Check log file: $summary_log_name.log

# Ensure column valve is closed for safety
SetColumnOrGunValve 0
Echo
Echo Column valve closed for safety
Echo All processing complete!

# Final logging
SaveLog
EndMacro
Macro	1
ScriptName BatchED_Main

################BEFORE STARTING###############
#
#  BatchED - Unified Batch Electron Diffraction & Imaging
#
#  Parent script that collects parameters and dispatches to:
#  - BatchED_Rotation: Continuous rotation diffraction (Record mode)
#  - BatchED_Still: Still diffraction snapshots (Record mode)
#  - BatchED_MultiAngle: Snapshots at +60, +30, 0, -30, -60 degrees (Record mode)
#  - BatchED_Imaging: Still imaging snapshots (Search mode)
#
#  Requirements:
#  - Navigator must be open with targets already added
#
##############################################

ClearPersistentVars

Echo
Echo ================================================
Echo ===== BATCHED - UNIFIED SETUP =====
Echo ================================================
Echo

##############ACQUISITION MODE#############

Echo Choose acquisition mode:
Echo 1 = Diffraction (Record mode - parallel beam)
Echo 2 = Imaging (Search mode - imaging beam)
Echo
EnterOneNumber Enter acquisition mode (1 = Diffraction, 2 = Imaging):
If $reportedValue1 != 1 AND $reportedValue1 != 2
   Exit Acquisition mode must be 1 (Diffraction) or 2 (Imaging)
Endif
acq_mode := $reportedValue1

If $acq_mode == 1
   Echo Mode: DIFFRACTION (Record)

   # Ask diffraction type
   Echo
   Echo Choose diffraction type:
   Echo 1 = Continuous rotation (tilt during exposure)
   Echo 2 = Still snapshot (no tilt during exposure)
   Echo 3 = Multi-angle snapshots (5 angles: +60, +30, 0, -30, -60)
   Echo
   EnterOneNumber Enter diffraction type (1 = Rotation, 2 = Still, 3 = Multi-angle):
   If $reportedValue1 != 1 AND $reportedValue1 != 2 AND $reportedValue1 != 3
      Exit Diffraction type must be 1 (Rotation), 2 (Still), or 3 (Multi-angle)
   Endif
   diff_type := $reportedValue1

   If $diff_type == 1
      Echo Type: CONTINUOUS ROTATION
   ElseIf $diff_type == 2
      Echo Type: STILL SNAPSHOT
   ElseIf $diff_type == 3
      Echo Type: MULTI-ANGLE SNAPSHOTS (+60, +30, 0, -30, -60)
   Endif

ElseIf $acq_mode == 2
   Echo Mode: IMAGING (Search)
   diff_type := 0
Endif

##############COMMON PARAMETERS#############

# Working directory
ReportDirectory
set_directory := $reportedValue1
Echo Working directory: $set_directory

# Sample/Compound Name
Echo
EnterString compound_name Enter your compound/sample name:
compoundname := $compound_name
Echo Compound name: $compoundname

# Navigator item counting
Echo
Echo Navigator Setup:
Echo - You should have already added your target points
Echo - The script needs to know where your targets start
Echo
EnterOneNumber Enter the LAST navigator item number BEFORE your targets were added:
If $reportedValue1 < 0
   Exit Navigator offset must be positive
Endif
xtalct := $reportedValue1
Echo Starting from navigator item: $xtalct

EnterOneNumber Enter the NUMBER of targets you want to collect:
If $reportedValue1 < 1
   Exit Must collect at least 1 target
Endif
nitems := $reportedValue1
Echo Number of targets: $nitems

##############MODE-SPECIFIC PARAMETERS#############

# Frame acquisition rate
Echo
Echo Frame Acquisition Rate:
Echo - Time per frame in seconds
Echo - Typical values: 0.5 to 2.0 seconds
Echo
EnterOneNumber Enter frame time in seconds:
If $reportedValue1 < 0.1 or $reportedValue1 > 10
   Exit Frame time must be between 0.1 and 10 seconds
Endif
acqrate := $reportedValue1
Echo Frame time: $acqrate seconds per frame

# Binning
Echo
Echo Camera Binning:
Echo - Recommended values: 2, 4, or 8
Echo - Higher binning = faster readout, lower resolution
Echo - Maximum binning: 16
Echo
EnterOneNumber Enter binning factor (1, 2, 4, 8, or 16):
If $reportedValue1 != 1 AND $reportedValue1 != 2 AND $reportedValue1 != 4 AND $reportedValue1 != 8 AND $reportedValue1 != 16
   Exit Binning must be 1, 2, 4, 8, or 16
Endif
binning := $reportedValue1
Echo Binning: $binning

# Rotation-specific parameters
If $acq_mode == 1 AND $diff_type == 1

   # Tilt range
   Echo
   Echo Tilt Range:
   Echo - Typical values: 30-60 degrees
   Echo - Range: 5 to 70 degrees
   Echo
   EnterOneNumber Enter maximum positive tilt angle:
   If $reportedValue1 < 5 or $reportedValue1 > 70
      Exit Tilt angle must be between 5 and 70 degrees
   Endif
   startangle := $reportedValue1
   finalangle := -1 * $startangle
   tiltrange := 2 * $startangle
   range := P$startangletoN$finalangle
   Echo Tilt range: +$startangle to $finalangle degrees

   # Rotation rate
   Echo
   Echo Rotation Rate:
   Echo - Typical values: 0.3 to 2.0 dps (degrees per second)
   Echo - Slower = better signal but longer exposure
   Echo
   EnterOneNumber Enter rotation rate in degrees per second:
   If $reportedValue1 < 0.1 or $reportedValue1 > 5
      Exit Rotation rate must be between 0.1 and 5 dps
   Endif
   rotrate := $reportedValue1
   Echo Rotation rate: $rotrate dps

   # Calculate rotation increment (fractional tilt stage rate)
   rotincr := $rotrate / 30
   Echo Rotation increment: $rotincr

   # Calculate recording time
   recordtime := $tiltrange / $rotrate
   Echo Exposure time: $recordtime seconds

Endif

# Number of sweeps (Diffraction modes only)
If $acq_mode == 1
   Echo
   Echo Multiple Sweeps:
   Echo - Collect multiple acquisitions on the same crystal
   Echo - Typically 1 (single collection per target)
   Echo
   EnterOneNumber Enter number of sweeps per target:
   If $reportedValue1 < 1 or $reportedValue1 > 10
      Exit Number of sweeps must be between 1 and 10
   Endif
   nsweeps := $reportedValue1
   Echo Sweeps per target: $nsweeps
Endif

##############SCOPE PARAMETERS#############

# Spot size and camera length (Diffraction modes only)
If $acq_mode == 1
   ReportSpotSize
   spsz := $reportedValue1
   Echo Spot size: $spsz

   ReportCameraLength
   dist := $reportedValue1 * 1000
   Echo Talos camera length: $dist mm

   # Convert to Apollo camera length
   If $dist == 420
      apollodist := 560
   ElseIf $dist == 960
      apollodist := 1280
   Else
      Echo Warning: Unusual camera length detected
      apollodist := $dist
   Endif
   Echo Apollo camera length: $apollodist mm

   # Temperature label
   temp := c
Endif

##############SUMMARY#############

Echo
Echo ================================================
Echo ===== ACQUISITION SUMMARY =====
Echo ================================================
Echo Compound: $compoundname

If $acq_mode == 1
   If $diff_type == 1
      Echo Mode: DIFFRACTION - CONTINUOUS ROTATION
      Echo Tilt range: +$startangle to $finalangle degrees ($tiltrange total)
      Echo Rotation rate: $rotrate dps
      Echo Exposure time: $recordtime seconds
   ElseIf $diff_type == 2
      Echo Mode: DIFFRACTION - STILL SNAPSHOT
   ElseIf $diff_type == 3
      Echo Mode: DIFFRACTION - MULTI-ANGLE SNAPSHOTS
      Echo Angles: +60, +30, 0, -30, -60 degrees (5 per target)
   Endif
   Echo Frame time: $acqrate s/frame
   Echo Binning: $binning
   Echo Sweeps per target: $nsweeps
   Echo Camera length: $apollodist mm (Apollo)
   Echo Spot size: $spsz
ElseIf $acq_mode == 2
   Echo Mode: IMAGING (Search)
   Echo Frame time: $acqrate s/frame
   Echo Binning: $binning
Endif

Echo Starting navigator item: $xtalct
Echo Number of targets: $nitems
Echo ================================================
Echo

YesNoBox Ready to start BatchED acquisition?
If $reportedValue1 == 0
   Exit User cancelled acquisition
Endif

##############DISPATCH#############

SaveLogOpenNew $compoundname_BatchED
Echo
Echo Starting BatchED acquisition...
Echo
ReportDateTime

If $acq_mode == 1 AND $diff_type == 1
   MakeDirectory $set_directory\movies
   Echo Created subdirectory: movies
   Echo Dispatching to: BatchED_Rotation
   Call BatchED_Rotation
ElseIf $acq_mode == 1 AND $diff_type == 2
   MakeDirectory $set_directory\snapshots
   Echo Created subdirectory: snapshots
   Echo Dispatching to: BatchED_Still
   Call BatchED_Still
ElseIf $acq_mode == 1 AND $diff_type == 3
   MakeDirectory $set_directory\snapshots
   Echo Created subdirectory: snapshots
   Echo Dispatching to: BatchED_MultiAngle
   Call BatchED_MultiAngle
ElseIf $acq_mode == 2
   MakeDirectory $set_directory\imaging
   Echo Created subdirectory: imaging
   Echo Dispatching to: BatchED_Imaging
   Call BatchED_Imaging
Endif

##############CLEANUP#############

Echo
Echo ================================================
Echo BATCHED ACQUISITION COMPLETE
Echo ================================================
Echo
Echo Processed $nitems targets
Echo
ReportDateTime
ReportClock

# Return scope to safe state
TiltTo 0
ScreenDown
SetBeamBlank 1
SetColumnOrGunValve 0

Echo
Echo Scope returned to safe state
Echo - Tilt: 0 degrees
Echo - Screen: Down
Echo - Beam: Blanked
Echo - Column valve: Closed
Echo
Echo BatchED complete!

SaveLog
EndMacro
Macro	2
MacroName REYES_Setup_Standalone

################BEFORE STARTING###############
#
#  Use this script when you reopen SerialEM and want to:
#  - Run REyes_STPSCN on existing grid
#  - Run REyes_DFMV with custom targets
#  - Continue any partial workflow
#
#  This sets up all required parameters:
#  - Sample name (samplename)
#  - Working directory (set_directory)
#  - Montage dimension (mnt_size) - for REyes_MNT
#  - Diffraction map dimension (map_size) - for REyes_STPSCN
#  - Tilt range (selected_tilt) - for REyes_DFMV
#
###################SETTINGS###################

Echo
Echo ================================================
Echo ===== REYES PARAMETER SETUP =====
Echo ================================================
Echo
Echo This script establishes parameters for REyes helpers
Echo Use this when reopening SerialEM to continue work
Echo

##############LOCATIONS AND NAMES#############

#LOG DATE
ReportDateTime
yyyymmdd := $reportedValue1
Echo Date: $yyyymmdd

#SET DIRECTORY
UserSetDirectory [Choose the sample directory where files are saved]
set_directory := $reportedValue1
SetDirectory $set_directory
Echo Chosen directory: $set_directory

#SET SAMPLE NAME
EnterString sample_name What is your sample name?
samplename := $sample_name
Echo Sample name: $samplename

Echo
Echo ================================================
Echo ===== MONTAGE PARAMETERS =====
Echo ================================================
Echo

#MONTAGE DIMENSIONS
Echo Enter montage dimension (used by REyes_MNT)
Echo - Creates an NxN grid montage at 115x magnification
Echo - Typical values: 3x3, 4x4, or 5x5
Echo - Range: 2 to 9
Echo
EnterOneNumber Enter desired montage dimension:
If $reportedValue1 < 2 or $reportedValue1 > 9
   Exit Montage size must be between 2 and 9
Endif
mnt_size := $reportedValue1
Echo Montage size: $mnt_size x $mnt_size

Echo
Echo ================================================
Echo ===== DIFFRACTION MAP PARAMETERS =====
Echo ================================================
Echo

#STEPSCAN/DIFFRACTION MAP DIMENSIONS
Echo Enter diffraction map dimension (used by REyes_STPSCN)
Echo - Recommended: Odd numbers (3, 5, 7, 9, etc.)
Echo - Typical values: 5x5 or 7x7
Echo - Range: 3 to 21
Echo
EnterOneNumber Enter desired map dimension (odd values recommended):
If $reportedValue1 < 3 or $reportedValue1 > 21
   Exit Map size must be between 3 and 21
Endif
map_size := $reportedValue1
Echo Diffraction map size: $map_size x $map_size

Echo
Echo ================================================
Echo ===== TILT RANGE PARAMETERS =====
Echo ================================================
Echo

#TILT RANGE FOR MOVIE (REyes_DFMV)
Echo Enter tilt range for continuous rotation movies (used by REyes_DFMV)
Echo - This sets the maximum tilt angle (+/-) for diffraction data
Echo - REyes_DFMV will rotate from +angle to -angle
Echo - Typical values: 50-60 degrees
Echo - Range: 5 to 70 degrees
Echo
EnterOneNumber Enter max positive tilt angle:
If $reportedValue1 < 5 or $reportedValue1 > 70
   Exit Tilt angle must be between 5 and 70 degrees
Endif
selected_tilt := $reportedValue1

Echo Tilt range: +$selected_tilt to -$selected_tilt degrees

Echo
Echo ================================================
Echo ===== SETUP COMPLETE =====
Echo ================================================
Echo
Echo Parameters established:
Echo - Sample name: $samplename
Echo - Working directory: $set_directory
Echo - Montage size: $mnt_size x $mnt_size
Echo - Diffraction map size: $map_size x $map_size
Echo - Tilt range: +$selected_tilt to -$selected_tilt degrees
Echo
Echo You can now run:
Echo - REyes_MNT (for grid montage)
Echo - REyes_STPSCN (for diffraction mapping)
Echo - REyes_DFMV (for target movie collection)
Echo - Or any other REyes helper macro
Echo
Echo ================================================

# Optional: Open a log file for subsequent work
YesNoBox Do you want to open a new log file for this session?
If $reportedValue1 == 1
   SaveLogOpenNew $samplename_resumed_$yyyymmdd
   Echo Log file opened: $samplename_resumed_$yyyymmdd.log
   Echo Working directory: $set_directory
   Echo Sample: $samplename
   ReportDateTime
Else
   Echo No log file opened - you can start logging manually if needed
Endif

Echo
Echo Setup complete! Ready to run REyes macros.

SaveLog
EndMacro
Macro	3

EndMacro
Macro	4
MacroName Setup_Helper

# This helper collects all pipeline parameters
# Sets persistent variables for main script to use

#LOG DATE
ReportDateTime
yyyymmdd := $reportedValue1
Echo Date: $yyyymmdd

##################USER INPUTS##################

Echo
Echo ================================================
Echo ========== PIPELINE SETUP ==========
Echo ================================================
Echo
Echo Starting Pipeline Handler....
Echo Please provide the following information:

#GET ACQUISITION MODE (SINGLE vs MULTI-GRID)
Echo
Echo Choose acquisition mode:
Echo 1 = Single Grid (grid already loaded on stage)
Echo 2 = Multi-Grid (use autoloader for multiple grids)
EnterOneNumber Enter acquisition mode (1 for Single, 2 for Multi-Grid):
If $reportedValue1 < 1 or $reportedValue1 > 2
   Exit Acquisition mode must be 1 (Single) or 2 (Multi-Grid)
Endif
acquisition_mode := $reportedValue1

If $acquisition_mode == 1
   Echo Acquisition mode: SINGLE GRID (grid already on stage)
   single_grid_mode := 1
   ItrNum := 1
ElseIf $acquisition_mode == 2
   Echo Acquisition mode: MULTI-GRID (autoloader)
   single_grid_mode := 0
Endif

#GET WORKFLOW TYPE (AUTOMATED vs MANUAL)
Echo
Echo Choose workflow type:
Echo 1 = Automated (no manual intervention)
Echo 2 = Manual (pause for manual grid square selection)
EnterOneNumber Enter workflow type (1 for Automated, 2 for Manual):
If $reportedValue1 < 1 or $reportedValue1 > 2
   Exit Workflow type must be 1 (Automated) or 2 (Manual)
Endif
workflow_type := $reportedValue1

If $workflow_type == 1
   Echo Workflow: AUTOMATED
Else
   Echo Workflow: MANUAL
Endif

#MODE-SPECIFIC PRE-FLIGHT CHECKS
Echo
Echo ================================================

If $single_grid_mode == 1
   Echo ========== SINGLE GRID PRE-FLIGHT ==========
   Echo ================================================
   Echo
   Echo CRITICAL PRE-FLIGHT CHECKS:
   Echo
   Echo 1. Align beam and check microscope settings
   Echo 2. LOAD your grid onto the stage
   Echo 3. Ensure grid is properly centered
   Echo 4. Check focus and beam settings
   Echo
   Echo ================================================

   YesNoBox Is a grid LOADED on the stage and ready for acquisition? Click YES to continue.
   If $reportedValue1 == 0
      Echo
      Echo User indicated grid is NOT ready on stage
      Echo Please load grid and verify microscope is ready
      Exit User aborted - Pre-flight checks not complete
   Endif
Else
   Echo ========== MULTI-GRID PRE-FLIGHT ==========
   Echo ================================================
   Echo
   Echo CRITICAL PRE-FLIGHT CHECKS:
   Echo
   Echo 1. Align beam and check microscope settings
   Echo 2. UNLOAD any grid currently in the stage
   Echo 3. Ensure all grids are in autoloader slots only
   Echo 4. If a grid is loaded, the script will skip that slot
   Echo
   Echo ================================================

   YesNoBox Is the stage EMPTY and all grids in the autoloader? Click YES to continue.
   If $reportedValue1 == 0
      Echo
      Echo User indicated stage is NOT empty or setup incomplete
      Echo Please unload any grid and verify autoloader is ready
      Exit User aborted - Pre-flight checks not complete
   Endif
Endif

#SET BASE DIRECTORY
UserSetDirectory [Choose the BASE directory where files will be saved]
base_directory := $reportedValue1
Echo Base directory: $base_directory

# The SESSION folder is <base>\<sample>-reyes and the -reyes suffix is appended
# automatically, so the base directory must be the PARENT. Choosing the session
# folder instead creates <sample>-reyes\<sample>-reyes, and navigators and log
# files then end up one level away from where the run looks for them.
# Tested by looking for what a SESSION folder contains rather than by parsing
# the name: SerialEM has no substring function, and the file test catches the
# real condition instead of a naming convention.
DoesFileExist $base_directory\grid_squares
looks_like_session = $reportedValue1
DoesFileExist $base_directory\targets
If $reportedValue1 == 1
   looks_like_session = 1
Endif
If $looks_like_session == 1
   Echo
   Echo ############################################################
   Echo # WARNING: this looks like a SESSION folder, not a base folder
   Echo #   $base_directory
   Echo #
   Echo # It already contains grid_squares\ or targets\.
   Echo # REyes appends -reyes itself, so this would create
   Echo #   $base_directory\<sample>-reyes
   Echo # and the run would look for its files one level too deep.
   Echo # You probably want the PARENT of this folder.
   Echo ############################################################
   Echo
   YesNoBox Use this directory anyway? NO lets you re-choose.
   If $reportedValue1 == 0
      Exit Re-run and choose the PARENT directory
   Endif
Endif

#GET SLOT RANGE (ONLY FOR MULTI-GRID MODE)
If $single_grid_mode == 0
   EnterOneNumber Enter minimum slot number (typically 1-12):
   If $reportedValue1 < 1 or $reportedValue1 > 12
      Exit Minimum slot number must be between 1 and 12
   Endif
   MinSlotNum := $reportedValue1
   Echo Minimum slot: $MinSlotNum

   EnterOneNumber Enter maximum slot number (must be >= minimum):
   If $reportedValue1 < $MinSlotNum or $reportedValue1 > 12
      Exit Maximum slot must be >= minimum slot and <= 12
   Endif
   MaxSlotNum := $reportedValue1
   Echo Maximum slot: $MaxSlotNum

   #CALCULATE NUMBER OF SLOTS
   ItrNum := $MaxSlotNum - $MinSlotNum + 1
   Echo Will process $ItrNum slots (slots $MinSlotNum to $MaxSlotNum)
Endif

#GET PIPELINE MODE
Echo
If $workflow_type == 1
   # Automated workflow
   Echo Choose pipeline mode:
   Echo 1 = FSD (Montage -> Eucentricity -> StepScan -> Targets -> Movies)
   Echo 2 = Screen (Montage -> Eucentricity -> StepScan, NO movies)
   Echo 3 = Montage Only (Grid overview images ONLY, no processing)
   EnterOneNumber Enter pipeline mode (1 for FSD, 2 for Screen, 3 for Montage):
   If $reportedValue1 < 1 or $reportedValue1 > 3
      Exit Pipeline mode must be 1 (FSD), 2 (Screen), or 3 (Montage)
   Endif
Else
   # Manual workflow - no Montage-only option
   Echo Choose pipeline mode for MANUAL workflow:
   Echo 1 = Manual FSD (Montage -> PAUSE -> Manual Selection -> Full Pipeline with Movies)
   Echo 2 = Manual Screen (Montage -> PAUSE -> Manual Selection -> Screen Pipeline, NO movies)
   EnterOneNumber Enter pipeline mode (1 for Manual FSD, 2 for Manual Screen):
   If $reportedValue1 < 1 or $reportedValue1 > 2
      Exit Pipeline mode must be 1 (Manual FSD) or 2 (Manual Screen)
   Endif
Endif
pipeline_mode := $reportedValue1

If $pipeline_mode == 1
   If $workflow_type == 1
      Echo Pipeline mode: AUTOMATED FSD (with diffraction movies)
      pipeline_name := FSD
   Else
      Echo Pipeline mode: MANUAL FSD (with diffraction movies)
      pipeline_name := Manual_FSD
   Endif
ElseIf $pipeline_mode == 2
   If $workflow_type == 1
      Echo Pipeline mode: AUTOMATED SCREEN (no movies)
      pipeline_name := Screen
   Else
      Echo Pipeline mode: MANUAL SCREEN (no movies)
      pipeline_name := Manual_Screen
   Endif
ElseIf $pipeline_mode == 3
   Echo Pipeline mode: MONTAGE ONLY (overview images)
   pipeline_name := Montage
Endif

#GET MONTAGE DIMENSIONS
If $single_grid_mode == 1
   EnterDefaultedNumber 8 -1 Enter desired montage dimension (between 2 and 9):
Else
   EnterDefaultedNumber 8 -1 Enter desired montage dimension for ALL grids (between 2 and 9):
Endif
If $reportedValue1 < 2 or $reportedValue1 > 9
   Exit Montage size must be between 2 and 9
Endif
mnt_size := $reportedValue1
If $single_grid_mode == 1
   Echo Montage size: $mnt_size
Else
   Echo Montage size for all grids: $mnt_size
Endif

#GET STEPSCAN DIMENSIONS (ONLY IF FSD OR SCREEN MODE)
If $pipeline_mode == 1 or $pipeline_mode == 2
   If $single_grid_mode == 1
      EnterDefaultedNumber 11 -1 Enter desired map dimension (odd values recommended, 3-21):
   Else
      EnterDefaultedNumber 11 -1 Enter desired map dimension for ALL grids (odd values recommended, 3-21):
   Endif
   If $reportedValue1 < 3 or $reportedValue1 > 21
      Exit Map size must be between 3 and 21
   Endif
   map_size := $reportedValue1
   If $single_grid_mode == 1
      Echo StepScan map size: $map_size
   Else
      Echo StepScan map size for all grids: $map_size
   Endif
Endif

#GET TILT RANGE (ONLY IF FSD MODE)
If $pipeline_mode == 1
   EnterDefaultedNumber 50 -1 Enter max positive tilt angle for movies (5-70 degrees):
   If $reportedValue1 < 5 or $reportedValue1 > 70
      Exit Tilt angle must be between 5 and 70 degrees
   Endif
   selected_tilt := $reportedValue1
   If $single_grid_mode == 1
      Echo Tilt range: +/- $selected_tilt degrees
   Else
      Echo Tilt range for all grids: +/- $selected_tilt degrees
   Endif

   # Rotation rate and frame time. Asked here rather than in REyes_DFMV so that
   # an unattended run never stops at a dialog in the middle of the pipeline.
   #
   # No macro in this package changes the spot size, so the rotation rate is
   # how dose is controlled. Keep rate x frame time at 1 deg per frame so the
   # angular sampling stays the same (default: 1 deg/s at 1.00 s per frame;
   # 4 deg/s at 0.25 s per frame gives a quarter of the dose).
   EnterDefaultedNumber 1 2 Rotation rate in deg/s:
   If $reportedValue1 < 0.1 or $reportedValue1 > 10
      Exit Rotation rate must be between 0.1 and 10 deg/s
   Endif
   dfmv_rotrate := $reportedValue1

   EnterDefaultedNumber 1 2 Frame time in s (rate x frametime should be 1 deg):
   If $reportedValue1 < 0.02 or $reportedValue1 > 5
      Exit Frame time must be between 0.02 and 5 s
   Endif
   dfmv_acqrate := $reportedValue1
   Echo Rotation: $dfmv_rotrate deg/s at $dfmv_acqrate s per frame
Endif

# SPOT SIZE FOR DIFFRACTION (1 FSD, 2 Screen)
# REyes_STPSCN and REyes_DFMV both call REyes_SpotGuard, which compares the
# microscope's spot size in the Record area against this value. Without a
# declared value the guard only records the spot size and judges nothing.
If $pipeline_mode == 1 OR $pipeline_mode == 2
   Echo
   Echo Spot size the DIFFRACTION should be collected at (NOT the LM value).
   Echo   The guard verifies this after every LM -> SA transition. It never
   Echo   SETS the spot size -- it only refuses to collect at the wrong one.
   EnterDefaultedNumber 8 -1 Expected diffraction spot size:
   If $reportedValue1 < 1 or $reportedValue1 > 11
      Exit Spot size must be between 1 and 11
   Endif
   spot_expected := $reportedValue1
   Echo Expected spot size: $spot_expected

   Echo If the microscope reports a different spot size:
   Echo   1 = ABORT the grid (recommended - fix the microscope and restart)
   Echo   2 = warn and CONTINUE anyway
   EnterDefaultedNumber 1 -1 Spot size mismatch policy (1 abort, 2 continue):
   If $reportedValue1 != 1 and $reportedValue1 != 2
      Exit Policy must be 1 (abort) or 2 (continue)
   Endif
   spot_mismatch_policy := $reportedValue1
Endif

# RESUME DEFAULTS
# REyes_STPSCN and REyes_DFMV ask how to treat work that is already finished
# when these are not set. Setting them here keeps that question from stopping an
# unattended run. 0 = skip grid squares / crystals that already have a done
# flag (nothing is collected twice); 1 = redo everything.
stpscn_redo_all := 0
dfmv_redo_all := 0

#COLLECT SAMPLE NAMES
Echo
If $single_grid_mode == 1
   Echo Enter sample name for the grid on stage:
   EnterString temp_name Sample name:
   # Store base name for file naming
   base_sample_name := $temp_name
   # Append -reyes suffix: REyes session folders are named <sample>-reyes
   full_sample_name = $temp_name-reyes
   Echo Sample: $temp_name (folder: $full_sample_name)
   NewArray sample_names 1 1
   NewArray base_names 1 1
   sample_names[1] := $full_sample_name
   base_names[1] := $base_sample_name
Else
   Echo Now entering sample names for each slot...
   NewArray sample_names 1 $ItrNum
   NewArray base_names 1 $ItrNum
   SlotNum = $MinSlotNum

   Loop $ItrNum slotIndex
      Echo Enter sample name for slot $SlotNum (or 'SKIP' to skip this slot):
      EnterString temp_name Sample name for slot $SlotNum:

      CompareNoCase temp_name SKIP
      If $reportedValue1 == 0
         Echo Slot $SlotNum will be skipped
         sample_names[$slotIndex] := SKIP
         base_names[$slotIndex] := SKIP
      Else
         # Append -reyes suffix: REyes session folders are named <sample>-reyes
         full_sample_name = $temp_name-reyes
         Echo Slot $SlotNum: $temp_name (folder: $full_sample_name)
         sample_names[$slotIndex] := $full_sample_name
         base_names[$slotIndex] := $temp_name
      Endif

      SlotNum = $SlotNum + 1
   EndLoop
Endif

#CONFIRMATION
Echo
Echo ============= CONFIRMATION =============
Echo Base directory: $base_directory
If $single_grid_mode == 1
   Echo Acquisition mode: Single Grid (on stage)
Else
   Echo Acquisition mode: Multi-Grid (autoloader)
   Echo Slots to process: $MinSlotNum to $MaxSlotNum ($ItrNum total)
Endif
If $workflow_type == 1
   Echo Workflow: Automated
Else
   Echo Workflow: Manual
Endif
Echo Pipeline mode: $pipeline_name
Echo Montage size: $mnt_size x $mnt_size
If $pipeline_mode == 1 or $pipeline_mode == 2
   Echo StepScan map size: $map_size x $map_size
Endif
If $pipeline_mode == 1
   Echo Tilt range: +/- $selected_tilt degrees
Endif
Echo
If $single_grid_mode == 1
   Echo Sample folder name (with -reyes suffix):
   Echo   $sample_names[1]
Else
   Echo Sample folder names (with -reyes suffix):
   SlotNum = $MinSlotNum
   Loop $ItrNum slotIndex
      Echo   Slot $SlotNum: $sample_names[$slotIndex]
      SlotNum = $SlotNum + 1
   EndLoop
Endif
Echo =======================================

YesNoBox Do you want to proceed with these settings?
If $reportedValue1 == 0
   Exit User cancelled the operation
Endif

# Start overall logging.
# Build the name from VARIABLES ONLY, never `$var_text$var`: underscores are
# legal in SerialEM variable names, so `$pipeline_name_` would be read as a
# variable called `pipeline_name_` rather than the variable plus a literal
# underscore. The resulting filename is unchanged.
SetDirectory $base_directory
log_sep @= _
If $single_grid_mode == 1
   grid_prefix @= SingleGrid
Else
   grid_prefix @= MultiGrid
Endif
setup_log_name @= $grid_prefix$log_sep$pipeline_name$log_sep$yyyymmdd
SaveLogOpenNew $setup_log_name
EndMacro
Macro	5
MacroName Slot_Verification_Helper

# This helper verifies autoloader slots contain grids
# Called only in multi-grid mode
# Sets slot_status array for main script to use

Echo
Echo ============= STEP 1: SLOT VERIFICATION =============
Echo Checking autoloader slots for grids...
Echo

SlotNum = $MinSlotNum
missing_grids = 0
NewArray slot_status 1 $ItrNum

Loop $ItrNum slotIndex
   current_sample = $sample_names[$slotIndex]

   CompareNoCase current_sample SKIP
   If $reportedValue1 == 0
      Echo Slot $SlotNum: SKIP (user requested)
      slot_status[$slotIndex] := 0
      SlotNum = $SlotNum + 1
      Continue
   Endif

   # Check if grid is present in slot
   Echo Checking slot $SlotNum ($current_sample)...
   ReportSlotStatus $SlotNum

   If $reportedValue1 != 1
      Echo   ERROR: No grid detected in slot $SlotNum!
      slot_status[$slotIndex] := -1
      missing_grids = $missing_grids + 1
   Else
      Echo   OK: Grid present in slot $SlotNum
      slot_status[$slotIndex] := 1
   Endif

   SlotNum = $SlotNum + 1
EndLoop

Echo
Echo Slot verification complete
Echo Grids detected: $ItrNum - $missing_grids
Echo Missing grids: $missing_grids

# Handle missing grids with recheck option
If $missing_grids > 0
   Echo
   Echo ================================================
   Echo WARNING: MISSING GRIDS DETECTED
   Echo ================================================
   Echo The following slots are missing grids:
   Echo
   SlotNum = $MinSlotNum
   Loop $ItrNum slotIndex
      If $slot_status[$slotIndex] == -1
         Echo   Slot $SlotNum: $sample_names[$slotIndex] - NO GRID FOUND
      Endif
      SlotNum = $SlotNum + 1
   EndLoop
   Echo
   Echo ================================================
   YesNoBox Load missing grids now? Click NO to abort, or YES to recheck slots.
   If $reportedValue1 == 0
      Exit User aborted - Missing grids in autoloader
   Else
      Echo
      Echo Please load the missing grids into their slots
      Echo Then click OK to verify all grids are present
      Pause Load missing grids, then click OK to recheck

      # Recheck all slots
      Echo
      Echo Rechecking all slots...
      SlotNum = $MinSlotNum
      missing_grids = 0

      Loop $ItrNum slotIndex
         current_sample = $sample_names[$slotIndex]

         CompareNoCase current_sample SKIP
         If $reportedValue1 == 0
            SlotNum = $SlotNum + 1
            Continue
         Endif

         If $slot_status[$slotIndex] == -1
            Echo Rechecking slot $SlotNum ($current_sample)...
            ReportSlotStatus $SlotNum

            If $reportedValue1 != 1
               Echo   STILL MISSING: No grid in slot $SlotNum!
               missing_grids = $missing_grids + 1
            Else
               Echo   OK: Grid now present in slot $SlotNum
               slot_status[$slotIndex] := 1
            Endif
         Endif

         SlotNum = $SlotNum + 1
      EndLoop

      If $missing_grids > 0
         Echo
         Echo ERROR: $missing_grids grids still missing after recheck
         Exit Cannot proceed - Missing grids in autoloader
      Else
         Echo
         Echo SUCCESS: All grids are now present!
      Endif
   Endif
Endif

Echo
Echo Slot verification complete
EndMacro
Macro	6
MacroName Directory_Setup_Helper

# This helper creates sample directories
# For single-grid: creates one directory
# For multi-grid: creates directories for all slots with verified grids

Echo
Echo ============= STEP 2: DIRECTORY SETUP =============

If $single_grid_mode == 1
   Echo Creating sample directory...
   sample_dir_path = $base_directory\$sample_names[1]
   Echo Creating directory: $sample_dir_path

   Try
      MakeDirectory $sample_dir_path
      Echo   SUCCESS: Created $sample_dir_path
   Catch
      Echo   ERROR: Could not create directory $sample_dir_path
      Exit Cannot proceed without sample directory
   EndTry

   # Create dummy slot_status array for compatibility with multi-grid code
   NewArray slot_status 1 1
   slot_status[1] := 1
Else
   Echo Creating sample directories for verified grids...

   SlotNum = $MinSlotNum
   setup_failures = 0

   Loop $ItrNum slotIndex
      current_sample = $sample_names[$slotIndex]

      # Only create directories for slots with grids (status = 1)
      If $slot_status[$slotIndex] != 1
         Echo Slot $SlotNum: Skipping directory creation (no grid or skipped)
         SlotNum = $SlotNum + 1
         Continue
      Endif

      sample_dir_path = $base_directory\$current_sample
      Echo Creating directory for slot $SlotNum: $sample_dir_path

      Try
         MakeDirectory $sample_dir_path
         Echo   SUCCESS: Created $sample_dir_path
      Catch
         Echo   ERROR: Could not create directory $sample_dir_path
         setup_failures = $setup_failures + 1
         slot_status[$slotIndex] := -2
      EndTry

      SlotNum = $SlotNum + 1
   EndLoop

   Echo
   Echo Directory setup complete

   If $setup_failures > 0
      Echo WARNING: $setup_failures directories could not be created
      YesNoBox Continue anyway? (Failed samples will be skipped during acquisition)
      If $reportedValue1 == 0
         Exit User cancelled due to directory creation failures
      Endif
   Endif
Endif
EndMacro
Macro	7
MacroName Automated_Pipeline_Helper

# This helper executes the automated pipeline
# Supports FSD (mode 1), Screen (mode 2), and Montage-only (mode 3)
# Every stage carries a resume gate -- see RESUME POINT below.
# Called for each grid in the main loop.
#
# A FAILED GRID SQUARE OR CRYSTAL NEVER ENDS THE GRID.
#   REyes_STPSCN and REyes_DFMV set a failed grid square / crystal aside and go
#   on. This helper:
#     - fails the grid as All_blocks_failed, straight away, when the StepScan
#       left NO grid square with data ($stpscn_collected == 0) -- there is
#       nothing for REyes to find targets in, so it does not wait for them;
#     - otherwise carries on, and records a grid that set anything aside as
#       SUCCESS with the count in the reason: Blocks_set_aside_N and/or
#       Items_set_aside_N (one good grid square can be enough for a structure).

Echo
Echo ====== STARTING AUTOMATED PIPELINE FOR $current_sample ======

# What this grid set aside, for the summary line (see the end of the macro).
grid_blocks_aside = 0
grid_items_aside = 0

If $pipeline_mode == 1
   Echo Running FULL FSD Pipeline (7 steps total)
   Echo This will take several hours per grid
   total_steps := 7
ElseIf $pipeline_mode == 2
   Echo Running SCREEN Pipeline (5 steps total)
   Echo This will take 1-3 hours per grid
   total_steps := 5
ElseIf $pipeline_mode == 3
   Echo Running MONTAGE ONLY (1 step)
   Echo This will take ~5-10 minutes per grid
   total_steps := 1
Endif

# =============================================================================
# RESUME POINT  (a gate on every stage)
# =============================================================================
# $resume_from is the first stage number to execute. START_REYES sets it from
# the "new run or resume" prompt -- to the chosen stage for the first grid of a
# run, and to 1 for every grid after it. Stages below it are skipped ENTIRELY:
# the operator asserted they are done.
#
# This is the coarse layer. It sits on top of, and does not replace, the
# per-item resume inside each stage: REyes_STPSCN skips grid squares it has
# mapped, and REyes_DFMV skips crystals whose movie folder already carries a
# done flag. So re-running a stage is always safe, and this only saves the time
# of walking it.
IsVariableDefined resume_from
If $reportedValue1 == 0
   resume_from := 1
EndIf

If $resume_from > $total_steps
   Echo RESUME | stage $resume_from does not exist in this pipeline ($total_steps stages)
   Exit Cannot resume from stage $resume_from: this pipeline has $total_steps stages
EndIf

If $resume_from > 1
   Echo
   Echo RESUME | from_stage=$resume_from - earlier stages will be SKIPPED
   Echo

   # --- THE NAVIGATOR -------------------------------------------------------
   # Stage 1 is what normally opens it. With stage 1 skipped it has to be open
   # already, or be read back from the file REyes_MNT saved it under. The name
   # is built exactly as REyes_MNT builds it.
   rs_montage = _115LM_$mnt_sizex$mnt_size
   rs_navtail = _nav.nav
   rs_nav_file = $samplename$rs_montage$rs_navtail
   ReportIfNavOpen
   If $reportedValue1 == 0
      DoesFileExist $rs_nav_file
      If $reportedValue1 == 0
         Echo
         Echo RESUME | no navigator is open and $rs_nav_file is not in
         Echo          $set_directory
         Echo Check the base directory, the sample name and the montage size:
         Echo all three must match the interrupted run.
         Exit Cannot resume: no navigator is open and its file was not found
      EndIf
      ReadNavFile $rs_nav_file
      Echo RESUME | navigator=read_from_file | file=$rs_nav_file
   Else
      Echo RESUME | navigator=already_open (expected: $rs_nav_file)
   EndIf
   ReportNumTableItems
   rs_nav_items = $reportedValue1
   Echo RESUME | navigator_items=$rs_nav_items

   # --- STAGES 2 AND 3 expect ONLY the montage map --------------------------
   # REyes_ECNT merges the eucentricity points and then walks items 2..N. Any
   # item left over from the interrupted attempt would be walked as well, and
   # would shift every later item number.
   If $resume_from == 2 OR $resume_from == 3
      rs_extra = $rs_nav_items - 1
      If $rs_extra > 0
         Echo RESUME | removing $rs_extra item(s): eucentricity starts from the montage map alone
         Loop $rs_extra
            DeleteNavigatorItem -1
         EndLoop
         SaveNavigator
      EndIf
   EndIf

   # --- STAGE 5 needs the navigator stage 4 would have left ------------------
   # The montage map and the grid squares, and NOTHING after them --
   # REyes_STPSCN walks every item from 2 to the end as a grid square.
   #   - squares already there are KEPT, with their REYES_DONE_STPSCN notes;
   #   - if the navigator came back from a file saved before they were merged,
   #     they are merged again from disk;
   #   - if the interrupted run had got as far as merging the crystal targets,
   #     those sit after the squares and would be step-scanned as squares. The
   #     block REyes_DFMV recorded says where they start, so they are removed
   #     here; DFMV merges them again when the chain reaches it, and crystals
   #     already collected are skipped by their dfmv_done.flag.
   If $resume_from == 5
      If $rs_nav_items <= 1
         Echo RESUME | navigator holds no grid squares - merging grid_squares.nav again
         Call REyes_postECNT
         SaveNavigator
      Else
         rs_ok = 0
         OpenTextFile RSRANGE T 0 targets\range_dfmv.txt
         If $reportedValue1 == 1
            ReadLineToArray RSRANGE rs_vals
            rs_ok = $reportedValue1
            CloseTextFile RSRANGE
         EndIf
         If $rs_ok == 1
            rs_drop = $rs_nav_items - $rs_vals[1] + 1
            If $rs_drop > 0 AND $rs_vals[1] >= 3
               Echo RESUME | removing $rs_drop crystal target item(s) from $rs_vals[1] on - the step-scan walks grid squares only
               Loop $rs_drop
                  DeleteNavigatorItem -1
               EndLoop
               RemoveFile targets\range_dfmv.txt
               SaveNavigator
            EndIf
         EndIf
      EndIf
   EndIf
EndIf

# STEP 1: Montage (ALL modes)
If $resume_from <= 1
   Echo [1/$total_steps] Starting grid montage acquisition...
   Call REyes_MNT
   Echo [1/$total_steps] Grid montage completed successfully
   Echo STAGE_DONE | stage=MNT
Else
   Echo [1/$total_steps] SKIPPED (resuming from stage $resume_from)
EndIf

# STEPS 2-5: Screening workflow (FSD and Screen modes only)
If $pipeline_mode == 1 or $pipeline_mode == 2

   # Step 2: Grid Square Finding
   If $resume_from <= 2
      Echo [2/$total_steps] Finding grid squares in the montage...
      Call ECNT_nav_ready
      Echo [2/$total_steps] Grid squares found successfully
      Echo STAGE_DONE | stage=GRIDSQUARES
   Else
      Echo [2/$total_steps] SKIPPED (resuming from stage $resume_from)
   Endif

   # Step 3: Eucentricity
   If $resume_from <= 3
      Echo [3/$total_steps] Determining eucentricity for the grid...
      Call REyes_ECNT
      Echo [3/$total_steps] Eucentricity points determined successfully
      Echo STAGE_DONE | stage=ECNT
   Else
      Echo [3/$total_steps] SKIPPED (resuming from stage $resume_from)
   Endif

   # Step 4: Eucentricity Analysis
   If $resume_from <= 4
      Echo [4/$total_steps] Processing eucentricity data...
      Call ECNT_corrected
      Call REyes_postECNT
      # REyes_postECNT does not save. Without this the navigator on DISK still
      # holds the eucentricity points until the first grid square is finished,
      # and a resume from a file would step-scan those instead.
      SaveNavigator
      Echo [4/$total_steps] Eucentric plane found successfully
      Echo STAGE_DONE | stage=POSTECNT
   Else
      Echo [4/$total_steps] SKIPPED (resuming from stage $resume_from)
   Endif

   # Step 5: StepScan
   If $resume_from > 5
      Echo [5/$total_steps] SKIPPED (resuming from stage $resume_from)
   Endif
   If $resume_from <= 5
      Echo [5/$total_steps] Performing StepScan...
      Call REyes_STPSCN
      # REyes_SpotGuard reports through a flag rather than Exit (an Exit inside
      # the caller's Try block would lose the reason), so the verdict is read
      # here. Without this check a grid whose StepScan refused to start would
      # be reported as a success.
      If $reyes_spot_ok == 0
         Echo
         Echo StepScan abandoned: the microscope is not in a usable state.
         Echo NOTHING was mapped for this grid.
         Echo
         grid_results[$slotIndex] := FAILED
         failure_reasons[$slotIndex] := Spot_guard_stopped_stepscan
         failed_grids = $failed_grids + 1
         partial_completion := 1
         Return
      Endif
      # A grid square that failed is set aside and the StepScan goes on. Only
      # a StepScan that left NO square with data fails the grid -- and then at
      # once: there is nothing for REyes to find targets in, so waiting for
      # targets.nav would only hold up the next grid.
      If $stpscn_collected == 0
         Echo
         Echo StepScan finished with NO grid square mapped - every square was set aside.
         Echo Not waiting for targets. See BLOCK_SKIPPED lines in the block logs.
         Echo
         grid_results[$slotIndex] := FAILED
         failure_reasons[$slotIndex] := All_blocks_failed
         failed_grids = $failed_grids + 1
         partial_completion := 1
         Return
      Endif
      grid_blocks_aside = $stpscn_set_aside
      If $stpscn_set_aside > 0
         Echo [5/$total_steps] $stpscn_set_aside grid square(s) set aside - continuing with the $stpscn_collected that have data
      Endif
      Echo [5/$total_steps] All grid squares mapped
   Endif

Endif

# STEPS 6-7: Targets and Movies (FSD mode only)
If $pipeline_mode == 1
   # Step 6: Wait for targets
   If $resume_from <= 6
      Echo [6/7] Processing Diffraction Map...
      Call Targets_nav_ready

      # Check if targets were found (persistent variable set by Targets_nav_ready)
      If $targets_found == 0
         Echo
         Echo Skipping movie collection for this grid
         Echo
         grid_results[$slotIndex] := PARTIAL
         failure_reasons[$slotIndex] := No_targets_found
         successful_grids = $successful_grids + 1
         partial_completion := 1
         Echo PARTIAL SUCCESS: $current_sample completed screening but no targets for movies

         # Return early - main script will handle navigator closure and continue
         Return
      Endif

      Echo [6/7] All targets identified successfully
   Else
      # REyes_DFMV exits by itself if targets\targets.nav is not there. If the
      # targets are already in the navigator it resumes on the block recorded
      # in targets\range_dfmv.txt and skips crystals that are already done.
      Echo [6/7] SKIPPED (resuming from stage $resume_from)
   Endif

   # Step 7: Diffraction Movie
   Echo [7/7] Collecting continuous rotation data...
   Call REyes_DFMV
   If $reyes_spot_ok == 0
      Echo
      Echo Movie collection abandoned: the microscope is not in a usable state.
      Echo NO rotation data was collected for this grid.
      Echo
      grid_results[$slotIndex] := FAILED
      failure_reasons[$slotIndex] := Spot_guard_stopped_dfmv
      failed_grids = $failed_grids + 1
      partial_completion := 1
      Return
   Endif
   grid_items_aside = $grid_items_aside + $dfmv_set_aside
   Echo [7/7] All movies collected

   Echo ====== FSD PIPELINE COMPLETED FOR $current_sample ======
ElseIf $pipeline_mode == 2
   Echo ====== SCREEN PIPELINE COMPLETED FOR $current_sample ======
ElseIf $pipeline_mode == 3
   Echo ====== MONTAGE COMPLETED FOR $current_sample ======
Endif

# =============================================================================
# ALWAYS LEAVE THE NAVIGATOR AND THE LOG ON DISK
# =============================================================================
# The last thing a grid does is write its state out. REyes_MNT does not save
# the navigator itself, so a montage-only run would otherwise leave it only in
# memory, and the log file could lag behind the run.
#
# Both commands are cheap and idempotent. The early Returns above (PARTIAL and
# FAILED paths) bypass this: those already saved what they had, and the
# summary records the reason.
#
# A GRID THAT SET SOMETHING ASIDE IS STILL A SUCCESS, and says so. It is
# recorded here (partial_completion := 1 tells START_REYES the outcome is
# already written) so that the count reaches the summary line, e.g.
#     Slot 3 (sample-reyes): SUCCESS - Blocks_set_aside_1
# Each piece of literal text is its own variable so no name can absorb the
# text after it (see the log-naming note in START_REYES).
grid_aside_total = $grid_blocks_aside + $grid_items_aside
If $grid_aside_total > 0
   sa_blocks_txt @= Blocks_set_aside_
   sa_items_txt @= Items_set_aside_
   sa_sep @= _
   grid_results[$slotIndex] := SUCCESS
   If $grid_blocks_aside > 0 AND $grid_items_aside > 0
      failure_reasons[$slotIndex] := $sa_blocks_txt$grid_blocks_aside$sa_sep$sa_items_txt$grid_items_aside
   ElseIf $grid_blocks_aside > 0
      failure_reasons[$slotIndex] := $sa_blocks_txt$grid_blocks_aside
   Else
      failure_reasons[$slotIndex] := $sa_items_txt$grid_items_aside
   Endif
   successful_grids = $successful_grids + 1
   partial_completion := 1
   Echo SUCCESS: $current_sample completed - $grid_blocks_aside grid square(s) and $grid_items_aside item(s) set aside
Endif

SaveNavigator
SaveLog
EndMacro
Macro	8
MacroName Manual_Pipeline_Part1

# PART 1 of Manual Pipeline (before Pause)
# This executes montage acquisition only
# Pause will happen in main script (outside Try block)
# Then Part2 will be called to continue the pipeline

Echo
Echo ====== STARTING MANUAL PIPELINE FOR $current_sample ======

If $pipeline_mode == 1
   Echo Running MANUAL FSD Pipeline (7 steps total)
   Echo This will take several hours
   total_steps := 7
ElseIf $pipeline_mode == 2
   Echo Running MANUAL SCREEN Pipeline (5 steps total)
   Echo This will take 1-3 hours
   total_steps := 5
Endif

###########################################
# PART 1: MONTAGE ACQUISITION
###########################################

Echo
Echo ================================================
Echo ===== PART 1: MONTAGE ACQUISITION =====
Echo ================================================

Echo [1/$total_steps] Starting grid montage acquisition...
Call REyes_MNT
Echo [1/$total_steps] Grid montage completed successfully

# Return to main script for Pause (must be outside Try block)
Echo
Echo Montage complete - returning to main script for manual selection
Echo
EndMacro
Macro	9
MacroName Manual_Pipeline_Part2

# PART 2 of Manual Pipeline (after Pause)
# This continues the pipeline after user has manually selected grid squares
# Assumes Pause has already happened in main script
#
# The StepScan and the movies set a failed grid square / crystal aside and go
# on. A StepScan that left NO square with data fails the grid as
# All_blocks_failed at once, without waiting for targets; a grid that set
# something aside is recorded as SUCCESS with the count in the reason
# (Blocks_set_aside_N / Items_set_aside_N), as in Automated_Pipeline_Helper.

grid_blocks_aside = 0
grid_items_aside = 0

Echo
Echo ================================================
Echo ===== CONTINUING MANUAL PIPELINE AFTER PAUSE =====
Echo ================================================
Echo
Echo NOTE: Ensure REyes is running in the sample directory:
Echo   $base_directory\$current_sample
Echo
Echo REyes will process the manually selected grid squares.
Echo

###########################################
# PART 2: MANUAL PIPELINE CONTINUATION
###########################################

Echo
Echo ================================================
Echo ===== PART 2: CONTINUING PIPELINE =====
Echo ================================================

# Step 2a: Save Manual Squares
Echo [2/$total_steps] Saving manually added grid squares to navigator...
Call REyes_saveNAV
Call Manual_ready
Echo [2/$total_steps] Navigator saved and selected squares are ready

# Step 2b: Prepare Navigator for Eucentricity
Echo Preparing navigator for eucentricity...
Call REyes_premanualECNT
Echo Navigator is ready for Eucentricity step

# Step 2c: Process Manual Grid Squares
Echo Processing manually selected grid squares...
Call ECNT_nav_ready
Echo Manual grid squares processed successfully

# Step 3: Eucentricity
Echo [3/$total_steps] Determining eucentricity for the grid...
Call REyes_ECNT
Echo [3/$total_steps] Eucentricity points determined successfully

# Step 4: Eucentricity Analysis
Echo [4/$total_steps] Processing eucentricity data...
Call ECNT_corrected
Call REyes_postECNT
Echo [4/$total_steps] Eucentric plane found successfully

# Step 5: StepScan
Echo [5/$total_steps] Performing StepScan...
Call REyes_STPSCN
# REyes_SpotGuard reports through a flag rather than Exit, so the verdict is
# read here, exactly as the automated path reads it in
# Automated_Pipeline_Helper. Without this the manual workflow would print
# "All grid squares mapped" for a grid the guard had just stopped.
If $reyes_spot_ok == 0
   Echo
   Echo StepScan abandoned: the microscope is not in a usable state.
   Echo NOTHING was mapped for this grid.
   Echo
   grid_results[$slotIndex] := FAILED
   failure_reasons[$slotIndex] := Spot_guard_stopped_stepscan
   failed_grids = $failed_grids + 1
   partial_completion := 1
   Return
Endif
If $stpscn_collected == 0
   Echo
   Echo StepScan finished with NO grid square mapped - every square was set aside.
   Echo Not waiting for targets. See BLOCK_SKIPPED lines in the block logs.
   Echo
   grid_results[$slotIndex] := FAILED
   failure_reasons[$slotIndex] := All_blocks_failed
   failed_grids = $failed_grids + 1
   partial_completion := 1
   Return
Endif
grid_blocks_aside = $stpscn_set_aside
If $stpscn_set_aside > 0
   Echo [5/$total_steps] $stpscn_set_aside grid square(s) set aside - continuing with the $stpscn_collected that have data
Endif
Echo [5/$total_steps] All grid squares mapped

# STEPS 6-7: Targets and Movies (Manual FSD mode only)
If $pipeline_mode == 1
   # Step 6: Wait for targets
   Echo [6/7] Processing Diffraction Map...
   Call Targets_nav_ready

   # Check if targets were found (persistent variable set by Targets_nav_ready)
   If $targets_found == 0
      Echo
      Echo Skipping movie collection for this grid
      Echo
      grid_results[$slotIndex] := PARTIAL
      failure_reasons[$slotIndex] := No_targets_found
      successful_grids = $successful_grids + 1
      partial_completion := 1
      Echo PARTIAL SUCCESS: $current_sample completed screening but no targets for movies
      Echo Status: PARTIAL - Screening completed but no targets for movies
      Echo Steps completed: 1-5 (montage through StepScan)
      Echo Steps skipped: 6-7 (target selection and movie collection)

      # Return early - main script will handle navigator closure and continue
      Return
   Endif

   Echo [6/7] All targets identified successfully

   # Step 7: Diffraction Movie
   Echo [7/7] Collecting continuous rotation data...
   Call REyes_DFMV
   If $reyes_spot_ok == 0
      Echo
      Echo Movie collection abandoned: the microscope is not in a usable state.
      Echo NO rotation data was collected for this grid.
      Echo
      grid_results[$slotIndex] := FAILED
      failure_reasons[$slotIndex] := Spot_guard_stopped_dfmv
      failed_grids = $failed_grids + 1
      partial_completion := 1
      Return
   Endif
   grid_items_aside = $grid_items_aside + $dfmv_set_aside
   Echo [7/7] All movies collected

   Echo
   Echo ================================================
   Echo ===== MANUAL FSD PIPELINE COMPLETED =====
   Echo ================================================
ElseIf $pipeline_mode == 2
   Echo
   Echo ================================================
   Echo ===== MANUAL SCREEN PIPELINE COMPLETED =====
   Echo ================================================
Endif

# A grid that set something aside is still a SUCCESS, with the count in the
# reason -- see the same block at the end of Automated_Pipeline_Helper.
grid_aside_total = $grid_blocks_aside + $grid_items_aside
If $grid_aside_total > 0
   sa_blocks_txt @= Blocks_set_aside_
   sa_items_txt @= Items_set_aside_
   sa_sep @= _
   grid_results[$slotIndex] := SUCCESS
   If $grid_blocks_aside > 0 AND $grid_items_aside > 0
      failure_reasons[$slotIndex] := $sa_blocks_txt$grid_blocks_aside$sa_sep$sa_items_txt$grid_items_aside
   ElseIf $grid_blocks_aside > 0
      failure_reasons[$slotIndex] := $sa_blocks_txt$grid_blocks_aside
   Else
      failure_reasons[$slotIndex] := $sa_items_txt$grid_items_aside
   Endif
   successful_grids = $successful_grids + 1
   partial_completion := 1
   Echo SUCCESS: $current_sample completed - $grid_blocks_aside grid square(s) and $grid_items_aside item(s) set aside
Endif
EndMacro
Macro	10
MacroName REyes_Setup_Automated

# This helper prompts user to start REyes instances
# Required for automated FSD and Screen modes

Echo
Echo ================================================
Echo ============= IMPORTANT: REYES SETUP ===========
Echo ================================================
Echo
Echo CRITICAL STEP - READ CAREFULLY:
Echo
Echo 1. You must now start a SEPARATE REyes Python instance
Echo    in EACH sample directory that was just created.
Echo
Echo 2. Directory created:
Echo
If $single_grid_mode == 1
   Echo    - $base_directory\$sample_names[1]
Else
   SlotNum = $MinSlotNum
   Loop $ItrNum slotIndex
      current_sample = $sample_names[$slotIndex]
      If $slot_status[$slotIndex] == 1
         Echo    - $base_directory\$current_sample
      Endif
      SlotNum = $SlotNum + 1
   EndLoop
Endif
Echo
Echo 3. Each REyes instance must be configured to monitor
Echo    its respective sample directory.
Echo
Echo 4. Do NOT proceed until ALL REyes instances are
Echo    running and ready to process data.
Echo
Echo 5. Verify each REyes instance is actively monitoring
Echo    by checking for startup messages in each terminal.
Echo
Echo ================================================

YesNoBox Have you started REyes in ALL sample folders and confirmed they are running?
If $reportedValue1 == 0
   Echo
   Echo User indicated REyes is NOT ready
   Echo ABORTING: Please start all REyes instances and run the script again
   Echo No data has been acquired - you can safely restart
   Exit User aborted - REyes instances not ready
Endif

Echo
Echo User confirmed ALL REyes instances are running
If $single_grid_mode == 0
   Echo Proceeding with multi-grid acquisition...
Else
   Echo Proceeding with acquisition...
Endif
EndMacro
Macro	11
MacroName REyes_Setup_Optional

# This helper for Montage-only mode where REyes is optional

Echo
Echo ================================================
Echo Montage-only mode selected - REyes NOT required
Echo Proceeding directly to acquisition...
Echo ================================================
EndMacro
Macro	13
ScriptName BatchED_Rotation

# Continuous rotation diffraction acquisition
# Called by BatchED_Main with persistent variables:
#   $compoundname, $xtalct, $nitems, $acqrate, $binning,
#   $startangle, $finalangle, $tiltrange, $range, $rotrate, $rotincr, $recordtime,
#   $nsweeps, $spsz, $apollodist, $temp, $set_directory

# Open column valve, unblank beam, raise screen
SetColumnOrGunValve 1
SetBeamBlank 0
ScreenUp

# Set camera parameters for continuous rotation
SetContinuous R 1
SetExposure R $recordtime
SetBinning R $binning
SetFrameTime R $acqrate

# Main acquisition loop
Loop $nitems
   xtalct := $xtalct + 1
   sweepcount = 0

   Echo
   Echo ================================================
   Echo Processing target: $xtalct
   Echo ================================================

   Loop $nsweeps
      sweepcount = $sweepcount + 1

      # Create folder name for frames
      # Format: compoundname-movN_XXXmm_X.Xdps_X.Xspf_PXXtoNXXX_spXXg8_sweepN_c
      movdir = $compoundname-mov$xtalct_$apollodistmm_$rotratedps_$acqratespf_$range_sp$spszg8_sweep$sweepcount_$temp
      SetFolderForFrames $movdir

      # Create movie filename for logging
      movname = $compoundname-mov$xtalct_$apollodist_$rotrate_$acqrate_$range_g8sp$spsz_$temp.mrc

      Echo Sweep $sweepcount of $nsweeps for target $xtalct
      Echo Movie folder: $movdir
      Echo Movie name: $movname

      # Move to target
      MoveToNavItem $xtalct
      Delay 1 sec

      # Overshoot tilt by 3 degrees, then settle to start angle
      TiltTo $startangle + 3
      TiltTo $startangle
      Delay 2 sec

      ReportTiltAngle
      Echo Starting tilt angle: $reportedValue1

      # Go to Record area
      GoToLowDoseArea R

      # Enable continuous frames
      UseContinuousFrames 1

      # Report stage position before tilt
      ReportStageXYZ
      Echo Stage position before tilt: X=$reportedValue1 Y=$reportedValue2 Z=$reportedValue3

      Echo STARTING TILT
      ReportClock

      # Start background tilt
      BackgroundTilt $finalangle $rotincr

      # Acquire movie
      Record

      # Stop continuous acquisition
      StopContinuous

      Echo END TILT
      Echo Movie folder: $movdir
      Echo Acquired movie: $movname
      ReportClock

      # Report final stage position
      ReportStageXYZ
      Echo Stage position after tilt: X=$reportedValue1 Y=$reportedValue2 Z=$reportedValue3

      # Return to zero tilt
      TiltTo 0
      ReportTiltAngle
      Echo Returned to tilt: $reportedValue1

      # Copy frames from detector to working directory
      Delay 5 sec
      RunInShell mkdir $set_directory\movies\$movdir
      RunInShell copy D:\DEOutput\Apollo\$movdir $set_directory\movies\$movdir
      If $reportedValue1 != 0
         Echo WARNING: Error copying frames for $movdir
      Endif

      Echo Completed sweep $sweepcount for target $xtalct
      Echo

   EndLoop

   Echo Completed all sweeps for target $xtalct
   Echo

EndLoop
EndMacro
Macro	14
ScriptName BatchED_Still

# Still diffraction snapshot acquisition
# Called by BatchED_Main with persistent variables:
#   $compoundname, $xtalct, $nitems, $acqrate, $binning,
#   $nsweeps, $spsz, $apollodist, $temp, $set_directory

# Open column valve, unblank beam, raise screen
SetColumnOrGunValve 1
SetBeamBlank 0
ScreenUp

# Set camera parameters for still snapshots
SetContinuous R 0
SetExposure R $acqrate
SetBinning R $binning
SetFrameTime R $acqrate

# Main acquisition loop
Loop $nitems
   xtalct := $xtalct + 1
   sweepcount = 0

   Echo
   Echo ================================================
   Echo Processing target: $xtalct
   Echo ================================================

   Loop $nsweeps
      sweepcount = $sweepcount + 1

      # Move to target
      MoveToNavItem $xtalct
      Delay 1 sec

      # Go to Record area
      GoToLowDoseArea R

      # Report current stage angle for folder naming
      ReportTiltAngle
      currentangle = $reportedValue1
      Echo Current tilt angle: $currentangle

      # Create folder name for frames
      # Format: samplename-movN_XXXmm_X.Xspf_spXX_angle_sweepN_c
      movdir = $compoundname-mov$xtalct_$apollodistmm_$acqratespf_sp$spsz_$currentangle_sweep$sweepcount_$temp
      SetFolderForFrames $movdir

      Echo Sweep $sweepcount of $nsweeps for target $xtalct
      Echo Frame folder: $movdir

      # Report stage position
      ReportStageXYZ
      Echo Stage position: X=$reportedValue1 Y=$reportedValue2 Z=$reportedValue3

      Echo ACQUIRING SNAPSHOT
      ReportClock

      # Acquire still snapshot
      Record

      Echo Frame folder: $movdir
      ReportClock

      # Copy frames from detector to working directory
      Delay 5 sec
      RunInShell mkdir $set_directory\snapshots\$movdir
      RunInShell copy D:\DEOutput\Apollo\$movdir $set_directory\snapshots\$movdir
      If $reportedValue1 != 0
         Echo WARNING: Error copying frames for $movdir
      Endif

      Echo Completed sweep $sweepcount for target $xtalct
      Echo

   EndLoop

   Echo Completed all sweeps for target $xtalct
   Echo

EndLoop
EndMacro
Macro	15
ScriptName BatchED_MultiAngle

# Multi-angle still diffraction snapshot acquisition
# Collects 5 still snapshots at +60, +30, 0, -30, -60 degrees per target
# Called by BatchED_Main with persistent variables:
#   $compoundname, $xtalct, $nitems, $acqrate, $binning,
#   $nsweeps, $spsz, $apollodist, $temp, $set_directory

# Define tilt angles
angles = {60 30 0 -30 -60}
angleslabel = P60P30Z0N30N60

# Open column valve, unblank beam, raise screen
SetColumnOrGunValve 1
SetBeamBlank 0
ScreenUp

# Set camera parameters for still snapshots
SetContinuous R 0
SetExposure R $acqrate
SetBinning R $binning
SetFrameTime R $acqrate

# Main acquisition loop
Loop $nitems
   xtalct := $xtalct + 1
   sweepcount = 0

   Echo
   Echo ================================================
   Echo Processing target: $xtalct
   Echo ================================================

   Loop $nsweeps
      sweepcount = $sweepcount + 1

      # Move to target
      MoveToNavItem $xtalct
      Delay 1 sec

      # Go to Record area
      GoToLowDoseArea R

      # Create folder name for frames
      # Format: samplename-movN_XXXmm_X.Xspf_spXX_P60P30Z0N30N60_sweepN_c
      movdir = $compoundname-mov$xtalct_$apollodistmm_$acqratespf_sp$spsz_$angleslabel_sweep$sweepcount_$temp
      SetFolderForFrames $movdir

      Echo Sweep $sweepcount of $nsweeps for target $xtalct
      Echo Frame folder: $movdir
      Echo Collecting 5 angles: +60, +30, 0, -30, -60 degrees

      # Report stage position
      ReportStageXYZ
      Echo Stage position: X=$reportedValue1 Y=$reportedValue2 Z=$reportedValue3

      # Collect snapshot at each angle
      Loop 5 idx
         tiltangle = $angles[$idx]

         Echo Tilting to $tiltangle degrees...
         TiltTo $tiltangle
         Delay 2 sec

         ReportTiltAngle
         Echo Acquiring snapshot at tilt angle: $reportedValue1
         ReportClock

         Record

         Echo Snapshot $idx of 5 complete (angle: $tiltangle)
      EndLoop

      Echo Frame folder: $movdir
      ReportClock

      # Return to zero tilt
      TiltTo 0
      ReportTiltAngle
      Echo Returned to tilt: $reportedValue1

      # Copy frames from detector to working directory
      Delay 5 sec
      RunInShell mkdir $set_directory\snapshots\$movdir
      RunInShell copy D:\DEOutput\Apollo\$movdir $set_directory\snapshots\$movdir
      If $reportedValue1 != 0
         Echo WARNING: Error copying frames for $movdir
      Endif

      Echo Completed sweep $sweepcount for target $xtalct
      Echo

   EndLoop

   Echo Completed all sweeps for target $xtalct
   Echo

EndLoop
EndMacro
Macro	16
ScriptName BatchED_Imaging

# Still imaging snapshot acquisition (Search mode)
# Called by BatchED_Main with persistent variables:
#   $compoundname, $xtalct, $nitems, $acqrate, $binning, $set_directory

# Open column valve, unblank beam, raise screen
SetColumnOrGunValve 1
SetBeamBlank 0
ScreenUp

# Set camera parameters for Search snapshots
SetContinuous S 0
SetExposure S $acqrate
SetBinning S $binning
SetFrameTime S $acqrate

# Main acquisition loop
Loop $nitems
   xtalct := $xtalct + 1

   Echo
   Echo ================================================
   Echo Processing target: $xtalct
   Echo ================================================

   # Move to target
   MoveToNavItem $xtalct
   Delay 1 sec

   # Create folder name for frames
   searchdir = $compoundname_item$xtalct
   SetFolderForFrames $searchdir

   Echo Frame folder: $searchdir

   # Go to Search area
   GoToLowDoseArea S

   # Report stage position
   ReportStageXYZ
   Echo Stage position: X=$reportedValue1 Y=$reportedValue2 Z=$reportedValue3

   Echo ACQUIRING SEARCH IMAGE
   ReportClock

   # Acquire Search image
   Search

   Echo Frame folder: $searchdir
   ReportClock

   # Copy frames from detector to working directory
   Delay 5 sec
   RunInShell mkdir $set_directory\imaging\$searchdir
   RunInShell copy D:\DEOutput\Apollo\$searchdir $set_directory\imaging\$searchdir
   If $reportedValue1 != 0
      Echo WARNING: Error copying frames for $searchdir
   Endif

   Echo Completed target: $xtalct
   Echo

EndLoop
EndMacro
Macro	19
MacroName AutoloaderScreen

########## SETTINGS ##########

MinSlotNum    := 2
MaxSlotNum  := 5

########## END SETTINGS ##########

ItrNum := $MaxSlotNum - $MinSlotNum + 1
SlotNum = $MinSlotNum

Loop $ItrNum

   ReportSlotStatus $SlotNum
   If $reportedValue1 == 1

      SetColumnOrGunValve 0
      LoadCartridge $SlotNum
      SetColumnOrGunValve 1
      TiltTo 0
      MoveStageTo 0 0 20
      M
      NewMap

   ElseIf $SlotNum == $MinSlotNum

      SetColumnOrGunValve 1
      TiltTo 0
      MoveStageTo 0 0 20
      M
      NewMap

   Endif

   SlotNum = $SlotNum + 1

EndLoop

SetColumnOrGunValve 0
EndMacro
Macro	30
MacroName REyes_MNT

##############LOCATIONS AND NAMES#############

#EXTENSIONS
mrc_ext =  .mrc
nav_ext = .nav
log_ext = .log
mdoc_ext = .mdoc

#FILE NAMES
#FOR MNT SCRIPT
montage = _115LM_$mnt_sizex$mnt_size
navigator = _nav

log_file_name = $samplename$montage$log_ext
nav_file_name := $samplename$montage$navigator$nav_ext
mnt_file_name = $samplename$montage$mrc_ext
mdoc_file_name = $mnt_file_name$mdoc_ext



##################INITIATION##################

#START LOGGING

DoesFileExist $log_file_name
log_file_exists = $reportedValue1

If $log_file_exists == 0
   SaveLogOpenNew $samplename$montage

ElseIf $log_file_exists == 1
   Echo Log file with this name already exists and will be saved with an increment index
   SaveLogOpenNew $samplename$montage

Endif

Echo Chosen directory: $set_directory
Echo Sample name: $samplename
Echo Montage size: $mnt_size

#OPEN NAVIGATOR

DoesFileExist $nav_file_name
nav_file_exists = $reportedValue1

ReportIfNavOpen
nav_file_open = $reportedValue1

If $nav_file_exists == 0 and $nav_file_open == 0
   OpenNavigator $samplename$montage$navigator$nav_ext

ElseIf $nav_file_exists == 1 and $nav_file_open == 0
   ReadNavFile $nav_file_name

ElseIf $nav_file_exists == 0 and $nav_file_open == 1
   SaveNavigator $samplename$montage$navigator$nav_ext

Endif

# SET FILE TYPE TO MRC

SetNewFileType 0



########TAKE INITIAL MONTAGE AT 115 LM########

DoesFileExist $mnt_file_name
If $reportedValue1 == 0

   # ADJUST EUCENTRICITY

   SetColumnOrGunValve 1
   ScreenUp
   SetBeamBlank 0

   Eucentricity 1
   ReportStageXYZ
   z_value_rough = $reportedValue3

   #SetLowDoseMode 1
   OpenNewMontage $mnt_size $mnt_size $mnt_file_name

   Echo Starting 115x LM montage collection
   ReportDateTime
   ReportNumMontagePieces

   # FULL MONTAGE SETTINGS AND ACQUISITION

   SetColumnOrGunValve 1
   TiltTo 0
   MoveStageTo 0 0 $z_value_rough

   Delay 1 sec

   SetContinuous M 0
   SetExposure M 1
   SetBinning M 4

   M
   NewMap

   ScreenDown

   SetBeamBlank 1
   SetColumnOrGunValve 0

   Echo Completed 115x LM montage data collection
   ReportDateTime

ElseIf $reportedValue1 == 1
   Echo You have already acquired grid montage

Endif

# CHECK THAT MDOC FILE WAS SAVED

DoesFileExist $mdoc_file_name
mdoc_file_exists = $reportedValue1

If $mdoc_file_exists == 0
   Echo .mdoc file for montage was not found. Please check settings.
ElseIf $mdoc_file_exists == 1
   Echo .mdoc file for montage has been saved successfully.
Endif

SaveNavigator
SaveLog
EndMacro
Macro	31
MacroName REyes_ECNT

##############LOCATIONS AND NAMES#############

#EXTENSIONS
mrc_ext =  .mrc
nav_ext = .nav
log_ext = .log
mdoc_ext = .mdoc

#FILE NAMES
#FOR ECNT SCRIPT
sqrs = _grid_squares
sqrs_file_name = grid_squares\eucentricity$nav_ext

log_sqrs_file_name = $samplename$sqrs$log_ext


##################INITIATION##################

#START LOGGING

DoesFileExist $log_sqrs_file_name
log_file_exists = $reportedValue1

If $log_file_exists == 0
   SaveLogOpenNew $samplename$sqrs

ElseIf $log_file_exists == 1
   Echo Log file with this name already exists and will be saved with an increment index
   SaveLogOpenNew $samplename$sqrs

Endif

Echo Chosen directory: $set_directory
Echo Current sample: $samplename

ReportIfNavOpen
nav_file_open = $reportedValue1

If $nav_file_open == 0
   Exit Could not locate Navigator.

Endif

#MERGE GRID SQUARES TO NAVIGATOR

DoesFileExist $sqrs_file_name
sqrs_file_exists = $reportedValue1

If $sqrs_file_exists == 1
   MergeNavFile $sqrs_file_name

ElseIf $sqrs_file_exists == 0
   Exit $sqrs_file_name was not found

Endif

ReportNumTableItems

Echo $reportedValue1

nitems := $reportedValue1 - 1

Echo Current number of items: $nitems

posoffset = 0
posct = $posoffset + 1

Echo Current position: $posct



#########RUN FINE EUCENTRICITY IN VIEW########

SetColumnOrGunValve 1
SetBeamBlank 0
ScreenUp

Loop $nitems

   posct = $posct + 1

   # --- stage-safe move: wait for the stage, retry a refused move 5 times ---
   move_ok = 0
   Loop 5
      Loop 60
         ReportStageBusy
         If $reportedValue1 == 0
            Break
         EndIf
         Delay 1 sec
      EndLoop
      Try
         MoveToNavItem $posct
         move_ok = 1
      Catch
         Echo STAGE_RETRY | the stage refused: MoveToNavItem $posct
         Echo STAGE_RETRY | waiting 20 s before trying again
         Delay 20 sec
      EndTry
      If $move_ok == 1
         Break
      EndIf
   EndLoop
   If $move_ok == 0
      Exit The stage stayed busy or not ready through 5 attempts: MoveToNavItem $posct
   EndIf

   Echo Moved to Navigator item $posct

   Delay 1 sec

   # EXPOSURE 0.5 s. The frame time is set to the same value so the Apollo
   # saves ONE frame per View exposure (with both at 1 s the camera has been
   # seen to return two summed frames on some sessions and one on others), and
   # eucentricity needs neither the extra dose nor the second frame.
   SetContinuous V 0
   SetExposure V 0.5
   SetBinning V 4
   SetFrameTime V 0.5

   Eucentricity 3

   UpdateItemZ $posct

   ReportStageXYZ

EndLoop

Echo STOPPING

ReportClock

Echo Completed fine eucentricity for all grid squares

ReportDateTime

ScreenDown
SetBeamBlank 1
SetColumnOrGunValve 0

SaveNavigator
SaveLog
EndMacro
Macro	32
MacroName REyes_STPSCN

# =============================================================================
# REyes_STPSCN -- A FAILED GRID SQUARE LOSES ONLY ITSELF
# =============================================================================
# A grid square that fails -- for ANY reason: the column valve closed under it,
# a microscope COM error, a stage that never frees up -- loses that square and
# nothing else. The grid goes on to targets and movies if at least ONE square
# was mapped. There is no consecutive-failure limit: every square is tried.
#
# How:
#   - each square's body runs inside its own Try; $sq_phase names the step;
#   - a square whose valve was found closed, or whose body raised an error, is
#     SET ASIDE: not copied, no done flag, no navigator note, the camera's
#     staging folder renamed -VALVECLOSED (valve) or -SKIPPED (error), valve
#     re-opened. A resume maps it again. Its own <sample>_<N>.log carries
#       BLOCK_SKIPPED | stage=STPSCN | square=N | item=I | reason=R
#     and never the per-square completion phrase;
#   - after the loop: STPSCN_RESULT | result=complete|partial|failed | ...
#     and the all-squares phrase only when at least one square has data.
#     Zero squares -> no phrase, $stpscn_collected := 0 and a plain Return:
#     the CALLER fails the grid (All_blocks_failed) without waiting for targets.
#   - the Record / ReportStageXYZ pair is untouched (see FROZEN PAIR below).
# =============================================================================

##############LOCATIONS AND NAMES#############

#EXTENSIONS
mrc_ext =  .mrc
nav_ext = .nav
log_ext = .log
mdoc_ext = .mdoc

#FILE NAMES
#FOR STPCSN SCRIPT
stpscn = _stepscan

log_stpscn_file_name = $samplename$stpscn$log_ext


##################INITIATION##################

#START LOGGING

DoesFileExist $log_stpscn_file_name
log_file_exists = $reportedValue1

If $log_file_exists == 0
   SaveLogOpenNew $samplename$stpscn

ElseIf $log_file_exists == 1
   Echo Log file with this name already exists and will be saved with an increment index
   SaveLogOpenNew $samplename$stpscn

Endif

Echo Chosen directory: $set_directory
Echo Current sample: $samplename

# Results for the caller (Automated_Pipeline_Helper / Manual_Pipeline_Part2),
# reset before anything can return so a grid never reads the previous grid's.
stpscn_collected := 0
stpscn_set_aside := 0

ReportIfNavOpen
nav_file_open = $reportedValue1

If $nav_file_open == 0
   Exit Could not locate Navigator.

Endif

ReportNumTableItems
nitems := $reportedValue1 - 1
Echo Current number of items: $nitems

#SET POSITIONS OFFSET TO SKIP MONTAGE POSITIONS '1'
posoffset = 1
posct = $posoffset
Echo Current position: $posct



###############STEPSCAN SETTINGS##############

#GRID SETTINGS
imgnum = 10000

offset_x = 0
offset_y = 0

increment_x = 5 # microns
increment_y = 5 # microns

num_loopsx = $map_size # steps in x
num_loopsy =  $map_size # steps in y

half_loopsx = nearint ( $num_loopsx / 2)
half_loopsy = nearint ( $num_loopsy / 2 )

x_travel =  $num_loopsx * ( 0 - $increment_x)
y_travel = $num_loopsy * ( 0 - $increment_y)


############RUN STEPSCAN IN RECORD############

SetColumnOrGunValve 1
SetBeamBlank 0
ScreenUp

# =============================================================================
# SPOT SIZE GUARD (REyes_SpotGuard): Low Dose check, GoToLowDoseArea R, spot check
# =============================================================================
# GoToLowDoseArea R does nothing when Low Dose is off, so the map could be
# collected with whatever optics the previous stage left, and the spot size is
# recorded only in the per-frame .mdoc.
#
# The expected spot size is optional here. When run from START_REYES,
# Setup_Helper has declared one and it is enforced; when run standalone, the
# guard records the spot size without judging it. Low Dose being off stops the
# StepScan in both cases.
stage_name @= STPSCN
IsVariableDefined spot_expected
If $reportedValue1 == 0
   stage_spot_expected := 0
Else
   stage_spot_expected := $spot_expected
Endif
Call REyes_SpotGuard
If $reyes_spot_ok == 0
   Echo StepScan abandoned by the spot guard -- see the reason above
   Return
Endif

SetContinuous R 0
# EXPOSURE 0.25 s WITH FRAME TIME 0.25 s: one frame per diffraction pattern.
#   With a longer exposure than the frame time the DE camera saves several
#   sub-frames, each .mrc becomes a frame stack (e.g. 2 x 2048 x 2048), and the
#   diffraction-map analysis expects a single 2D image per position.
#   Keep exposure equal to frame time.
#
# ONE FRAME IS CHECKED, NOT ASSUMED.
#   Even with exposure equal to frame time, the number of saved frames has been
#   seen to differ between sessions. The count is therefore READ BACK after the
#   first exposure of every grid square (see the loop). A wrong count is
#   repaired if it can be and logged either way; it never stops the grid --
#   dif-map-2 sums a frame stack, so the data stay usable. Where the camera
#   does not report a frame count, STPSCN_FRAMES says frames=unknown.
frame_check := 1
stp_exp = 0.25
stp_fix_step = 0
n_multiframe = 0
SetExposure R $stp_exp
SetBinning R 4
SetFrameTime R $stp_exp
If $frame_check == 1
   stp_ft = $reportedValue1
   ReportExposure R
   Echo STPSCN_CAMERA | requested=$stp_exp | frame_time=$stp_ft | exposure=$reportedValue1
Endif

# The done flag is written only if the column valve is still open when the
# square finishes, so a square exposed with the valve closed is never marked
# done (a resume maps it again).
valve_check := 1
n_valve_lost = 0

# --- Resume accounting -------------------------------------------------------
n_collected = 0
n_skipped = 0
n_set_aside = 0

IsVariableDefined stpscn_redo_all
If $reportedValue1 == 0
   EnterDefaultedNumber 0 -1 Resume mode: 0 = skip grid squares already mapped, 1 = redo all:
   stpscn_redo_all := $reportedValue1
Endif


Loop $nitems

   posname = $posct
   # posct is advanced FIRST: it is this square's navigator item from here
   # on -- the item MoveToNavItem goes to and the item a BLOCK_SKIPPED record
   # names -- whatever happens to the square below. Navigator item 1 is the
   # montage map, so the squares start at item 2 and posname (the FOLDER
   # number) is posct - 1.
   posct = $posct + 1

   # sq_state: 0 = to be mapped now, 1 = mapped by an earlier run (resume),
   #           2 = mapped now, 3 = SET ASIDE (valve closed or an error)
   sq_state = 0
   sq_valve = 0
   sq_phase @= resume_check

   # ===========================================================================
   # ONE SQUARE, INSIDE ITS OWN Try
   # ===========================================================================
   # Any error in here -- a COM error from the microscope, a stage that stays
   # busy through every move attempt, a failed copy -- jumps to the Catch below
   # and costs THIS square only. Nothing in here may Break or Continue out to
   # the square loop; the outcome is carried out in $sq_state instead.
   Try
      # --- RESUME: skip grid squares a previous run already finished ----------
      # Tested BEFORE SaveLogOpenNew, so a skipped square does not get a second,
      # empty block log -- the monitor reads those by name and the earlier one
      # already carries its completion phrase.
      DoesFileExist $set_directory\$samplename_$posname\stpscn_done.flag
      If $reportedValue1 == 1 AND $stpscn_redo_all == 0
         sq_state = 1
         n_skipped = $n_skipped + 1
         Echo RESUME | stage=STPSCN | square=$posname | action=skipped
         # Note the skipped square too, so every finished square carries the
         # REYES_DONE_STPSCN note. No SaveNavigator here: the end-of-macro
         # SaveNavigator persists it, and a resume that skips everything should
         # stay fast.
         ChangeItemNote $posct REYES_DONE_STPSCN
      Endif

      If $sq_state == 0
         sq_phase @= open_log
         SaveLogOpenNew $samplename_$posname
         SetFolderForFrames $samplename_$posname

         # Re-open the column valve if something closed it since the last square.
         sq_phase @= valve_reopen
         If $valve_check == 1
            ReportColumnOrGunValve
            If $reportedValue1 != 1
               Echo VALVE_REOPENED | stage=STPSCN | square=$posname
               SetColumnOrGunValve 1
               Delay 2 sec
            Endif
         Endif

         # The first exposures of this square have their saved-frame count read back.
         frame_checks_left = 3

         sq_phase @= move
         # --- stage-safe move: wait for the stage, retry a refused move 5 times ---
         move_ok = 0
         Loop 5
            Loop 60
               ReportStageBusy
               If $reportedValue1 == 0
                  Break
               EndIf
               Delay 1 sec
            EndLoop
            Try
               MoveToNavItem $posct
               move_ok = 1
            Catch
               Echo STAGE_RETRY | the stage refused: MoveToNavItem $posct
               Echo STAGE_RETRY | waiting 20 s before trying again
               Delay 20 sec
            EndTry
            If $move_ok == 1
               Break
            EndIf
         EndLoop
         If $move_ok == 0
            Throw The stage stayed busy or not ready through 5 attempts: MoveToNavItem $posct
         EndIf

         ReportStageXYZ

         Delay 1 sec

         x_temp = $offset_x - ( $half_loopsx * $increment_x )
         y_temp = $offset_y - ( $half_loopsy * $increment_y )

         MoveStage $x_temp $y_temp

         sq_phase @= acquire
         Loop $num_loopsy

            Delay 1 sec

            Loop $num_loopsx

               imgnum = $imgnum + 1
               MoveStage $increment_x 0

               Delay 1 sec

               # ===============================================================
               # FROZEN PAIR -- NOTHING MAY SIT BETWEEN THESE TWO LINES.
               # REyes (dif-map-2) finds a frame's stage position by locating
               # the log line that names its MRC (printed by Record) and
               # reading "Stage x y z" exactly serialEM_log lines later (1 for
               # the DE Apollo configurations). An Echo, a Report*, a SaveLog
               # in between and that frame has no coordinates: it can never
               # become a target.
               # ===============================================================
               Record
               ReportStageXYZ

               # --- ONE FRAME PER EXPOSURE: read back, repair, never stop ------
               # ReportNumFramesSaved reports the last acquisition, so it is
               # still valid here, after the stage read.
               # Checked on the first exposure of the square, and again after
               # each repair. The repairs, in order, once per run:
               #   1  re-apply 0.25 / 0.25
               #   2  0.26 / 0.26   (one sensor frame longer)
               #   3  0.24 / 0.24   (one sensor frame shorter)
               # 2 and 3 move the exposure by one ~16 ms sensor frame, which is
               # what changes the count if it is being decided by rounding. A
               # setting that gives one frame is KEPT for the rest of the run.
               # If none does, the macro goes back to 0.25 and carries on with
               # a summed stack.
               If $frame_check == 1 AND $frame_checks_left > 0
                  Try
                     ReportNumFramesSaved
                     stp_frames = $reportedValue1
                  Catch
                     stp_frames = -1
                  EndTry
                  If $stp_frames == 1
                     frame_checks_left = 0
                     Echo STPSCN_FRAMES | square=$posname | image=$imgnum | frames=1 | exposure=$stp_exp | action=none
                  ElseIf $stp_frames < 1
                     frame_checks_left = 0
                     Echo STPSCN_FRAMES | square=$posname | image=$imgnum | frames=unknown | exposure=$stp_exp | action=none
                  Else
                     n_multiframe = $n_multiframe + 1
                     stp_fix_step = $stp_fix_step + 1
                     frame_checks_left = $frame_checks_left - 1
                     stp_was = $stp_exp
                     If $stp_fix_step == 1
                        stp_exp = 0.25
                     ElseIf $stp_fix_step == 2
                        stp_exp = 0.26
                     ElseIf $stp_fix_step == 3
                        stp_exp = 0.24
                     Else
                        stp_exp = 0.25
                        frame_checks_left = 0
                     Endif
                     SetExposure R $stp_exp
                     SetFrameTime R $stp_exp
                     stp_ft = $reportedValue1
                     If $stp_fix_step <= 3
                        Echo STPSCN_FRAMES | square=$posname | image=$imgnum | frames=$stp_frames | exposure=$stp_was | action=repair_$stp_fix_step | next_exposure=$stp_exp | frame_time=$stp_ft
                     Else
                        Echo STPSCN_FRAMES | square=$posname | image=$imgnum | frames=$stp_frames | exposure=$stp_was | action=gave_up | next_exposure=$stp_exp | frame_time=$stp_ft
                        Echo WARNING: the Apollo is saving $stp_frames frames per exposure and no repair
                        Echo          changed that. Continuing at 0.25 s; dif-map-2 sums the stack.
                     Endif
                  Endif
               Endif

            EndLoop

            MoveStage (-1 * $num_loopsx * $increment_x ) $increment_y
            ReportClock


         EndLoop

         # --- VALVE CHECK, before the copy and before the done flag ------------
         # A square exposed with the valve closed is not copied, not flagged and
         # not noted, so it stays NOT done and a resume maps it again. It is set
         # aside below, together with the squares that raised an error.
         sq_phase @= valve_check
         stp_valve_ok = 1
         If $valve_check == 1
            ReportColumnOrGunValve
            If $reportedValue1 != 1
               stp_valve_ok = 0
            Endif
         Endif

         If $stp_valve_ok == 0
            sq_valve = 1
            sq_state = 3
         Else
            sq_phase @= copy
            RunInShell mkdir $set_directory\$samplename_$posname
            RunInShell copy D:\DEOutput\Apollo\$samplename_$posname $set_directory\$samplename_$posname
            # No wait here: RunInShell blocks until the command returns and
            # places its exit status in reportedValue1, tested on the next
            # line. A failed copy is an error for THIS square, not the end of
            # the grid.
            If $reportedValue1 != 0
               Throw Error creating StepScan directory for grid square $posname
            Endif
            # Marker for the resume check above, written only after the copy
            # returned. MakeDirectory is idempotent and guarantees the folder
            # exists before the flag file is opened.
            sq_phase @= done_flag
            MakeDirectory $set_directory\$samplename_$posname
            OpenTextFile STPDONE O 0 $set_directory\$samplename_$posname\stpscn_done.flag
            WriteLineToFile STPDONE done
            CloseTextFile STPDONE
            # Per-square navigator status (REYES_DONE_DFMV is the equivalent
            # note in REyes_DFMV). posct is the item MoveToNavItem went to.
            ChangeItemNote $posct REYES_DONE_STPSCN
            # The square is DONE from here on: an error after this line must
            # not set it aside (its flag is written and its data copied).
            sq_state = 2
            n_collected = $n_collected + 1

            Echo Completed StepScan for current grid square
         Endif
      Endif
   Catch
      # A square that already has its done flag and its completion phrase is
      # kept; only a square that did not get that far is set aside.
      If $sq_state != 2 AND $sq_state != 1
         sq_state = 3
      Endif
   EndTry

   # --- SET ASIDE: valve closed or an error -- this square only -----------------
   # No copy, no done flag, no note; the camera's staging folder is renamed out
   # of the way (a second pass writes into the same folder and the copy would
   # otherwise deliver the bad frames with the good ones); the valve is
   # re-opened. Every command that touches the microscope or the disk has its
   # own Try, so cleaning up one square can never cost the next one. A resume
   # maps the square again.
   If $sq_state == 3
      n_set_aside = $n_set_aside + 1
      If $sq_valve == 1
         n_valve_lost = $n_valve_lost + 1
         sq_reason @= valve_closed
         sq_suffix @= -VALVECLOSED
         Echo VALVE_CLOSED | stage=STPSCN | square=$posname | action=not_copied_not_marked_done
         Echo WARNING: the column valve was closed when square $posname finished.
         Echo          Its frames are set aside; it will be mapped again on a resume.
      Else
         sq_reason @= error_$sq_phase
         sq_suffix @= -SKIPPED
         Echo WARNING: grid square $posname failed during $sq_phase and is set aside.
         Echo          The grid goes on; it will be mapped again on a resume.
      Endif
      Echo BLOCK_SKIPPED | stage=STPSCN | square=$posname | item=$posct | reason=$sq_reason
      Try
         RunInShell cmd /c ren D:\DEOutput\Apollo\$samplename_$posname $samplename_$posname$sq_suffix
      Catch
         Echo WARNING: could not rename the staging folder of grid square $posname
      EndTry
      Try
         SetColumnOrGunValve 1
         Delay 2 sec
      Catch
         Echo WARNING: could not re-open the column valve after grid square $posname
      EndTry
   Endif

   If $sq_state >= 2
      Try
         SaveNavigator
         SaveLog
      Catch
         Echo WARNING: could not save the navigator or the log after grid square $posname
      EndTry
   Endif

EndLoop

Echo STOPPING

ReportClock

# set_aside is appended LAST, so field-keyed parsers of this record are unaffected.
Echo STPSCN_SUMMARY | squares=$nitems | collected=$n_collected | skipped=$n_skipped | set_aside=$n_set_aside
Echo STPSCN_CHECKS | multiframe_exposures=$n_multiframe | final_exposure=$stp_exp | valve_lost=$n_valve_lost

# =============================================================================
# THE RESULT, AND THE GATED COMPLETION PHRASE
# =============================================================================
# REyes stops watching the diffraction map when it sees the all-squares phrase.
# It is printed only when at least ONE grid square has data:
#   complete  every square has data (mapped now or by an earlier run)
#   partial   at least one square has data, at least one was set aside
#   failed    no square has data -- the phrase is NOT printed, and the caller
#             fails the grid as All_blocks_failed without waiting for targets
# "collected" in STPSCN_RESULT counts squares WITH DATA: mapped now plus mapped
# by an earlier run. Everything is logged into the log that is open now -- the
# last square's.
n_with_data = $n_collected + $n_skipped
If $nitems <= 0 OR $n_with_data == 0
   stp_result @= failed
ElseIf $n_set_aside > 0
   stp_result @= partial
Else
   stp_result @= complete
Endif
Echo STPSCN_RESULT | result=$stp_result | collected=$n_with_data | set_aside=$n_set_aside
stpscn_collected := $n_with_data
stpscn_set_aside := $n_set_aside

If $nitems <= 0 OR $n_with_data == 0
   Echo
   Echo ############################################################
   Echo # NOT reporting completion: NO grid square has data.
   Echo #   squares=$nitems  mapped=$n_collected  skipped=$n_skipped  set_aside=$n_set_aside
   Echo # Either the navigator held no grid squares, or every square
   Echo # failed. The grid is FAILED (All_blocks_failed). Re-run to map
   Echo # them again; squares already mapped are skipped.
   Echo ############################################################
   Echo
   ScreenDown
   SetBeamBlank 1
   SetColumnOrGunValve 0
   SaveNavigator
   SaveLog
   Return
Endif

If $n_set_aside > 0
   Echo $n_set_aside of $nitems grid square(s) set aside - the grid goes on with the other $n_with_data
Endif

Echo Completed StepScan for all grid squares

ReportDateTime

ScreenDown
SetBeamBlank 1
SetColumnOrGunValve 0

SaveNavigator
SaveLog
EndMacro
Macro	33
MacroName REyes_DFMV

# =============================================================================
# REyes_DFMV -- A FAILED MOVIE LOSES ONLY ITSELF
#   Each crystal runs in its own Try. A crystal found with the valve closed, or
#   one that raised an error, is set aside (ITEM_SKIPPED | stage=DFMV | item= |
#   reason=) and the loop goes on. The end phrase "Completed acquiring tilt
#   movies for all crystals!" is ALWAYS printed once the loop has run, so REyes
#   is never left waiting; a run where NO crystal has a movie then ends with
#   Exit (grid FAILED). $dfmv_set_aside := count, for the grid summary.
#   "Movie folder:" / "Acquired movie:" are printed only after the valve check.
# =============================================================================

##############LOCATIONS AND NAMES#############

#EXTENSIONS
mrc_ext =  .mrc
nav_ext = .nav
log_ext = .log
mdoc_ext = .mdoc

#FILE NAMES
#ADDITIONS FOR DFMV SCRIPT

trgts = _targets
targets_file_name = targets\targets$nav_ext
log_dfmv_file_name = $samplename$trgts$log_ext


##################INITIATION##################

#START LOGGING

DoesFileExist $log_dfmv_file_name
log_file_exists = $reportedValue1

If $log_file_exists == 0
   SaveLogOpenNew $samplename$trgts

ElseIf $log_file_exists == 1
   Echo Log file with this name already exists and will be saved with an increment index
   SaveLogOpenNew $samplename$trgts

Endif

Echo Chosen directory: $set_directory
Echo Current sample: $samplename

# Result for the grid summary, reset before anything can return.
dfmv_set_aside := 0

ReportIfNavOpen
nav_file_open = $reportedValue1

If $nav_file_open == 0
   Exit Could not locate Navigator.

Endif

#CHECK NUMBER OF GRID SQUARES IN NAVIGATOR

# =============================================================================
# WHERE THE TARGET RANGE COMES FROM
# =============================================================================
# This macro merges targets\targets.nav itself and derives the number of
# crystal targets by differencing the navigator before and after the merge. A
# re-run against a navigator that already holds them merges nothing; the block
# recorded in targets\range_dfmv.txt then says where they are (see below).

ReportNumTableItems

nsqrs = $reportedValue1

Echo Current number of items: $nsqrs

DoesFileExist $targets_file_name
targets_file_exists = $reportedValue1

If $targets_file_exists == 1
   MergeNavFile $targets_file_name

ElseIf $targets_file_exists == 0
   Exit $targets_file_name was not found

Endif

ReportNumTableItems

all_items = $reportedValue1

Echo Total number of items with targets: $all_items

nitems := $all_items - $nsqrs

Echo Current number of crystal targets: $nitems

# --- THE TARGET BLOCK, kept on disk for a resume ---------------------------
# A fresh merge records which navigator items it added. A re-run against a
# navigator that already holds them merges nothing -- the re-merge trap
# below -- and used to be a dead end. Now the recorded block is read back,
# so resuming the movie stage needs no item numbers from the operator, and
# the per-crystal dfmv_done.flag then skips whatever is already collected.
If $nitems > 0
   dfmv_rng_first = $nsqrs + 1
   MakeDirectory targets
   OpenTextFile DFMVRNG O 0 targets\range_dfmv.txt
   WriteLineToFile DFMVRNG $dfmv_rng_first $all_items
   CloseTextFile DFMVRNG
   Echo DFMV_RANGE | first_item=$dfmv_rng_first | last_item=$all_items | source=merge
Else
   OpenTextFile DFMVRNG T 0 targets\range_dfmv.txt
   If $reportedValue1 == 1
      ReadLineToArray DFMVRNG dfmv_rng
      dfmv_rng_read = $reportedValue1
      CloseTextFile DFMVRNG
      If $dfmv_rng_read == 1
         nsqrs = $dfmv_rng[1] - 1
         nitems := $dfmv_rng[2] - $dfmv_rng[1] + 1
         Echo DFMV_RANGE | first_item=$dfmv_rng[1] | last_item=$dfmv_rng[2] | source=file
         Echo The targets were merged by an earlier run - resuming on the same block
         Echo Current number of crystal targets: $nitems
      Endif
   Endif
Endif


# =============================================================================
# RE-MERGE TRAP
# =============================================================================
# The target count above is a DIFFERENCE across the merge. Run this macro a
# second time against a navigator that already contains those targets, with no
# targets\range_dfmv.txt to fall back on, and the merge adds nothing: nitems is
# 0 and the loop below would run zero times. Falling through would print
#     "Completed acquiring tilt movies for all crystals!"
# which REyes treats as "all movies collected", reporting success having
# collected nothing. So the macro stops here instead.
If $nitems <= 0
   Echo
   Echo ############################################################
   Echo # STOPPING: NO NEW TARGETS WERE MERGED
   Echo #
   Echo # $targets_file_name added 0 items, which means this navigator
   Echo # already contains them -- almost always a re-run after a pause --
   Echo # and targets\range_dfmv.txt, which records the block an earlier
   Echo # merge added, was not found either.
   Echo #
   Echo # NOTHING HAS BEEN COLLECTED. Choose one:
   Echo #   - to resume, delete the merged target items from the navigator
   Echo #     and run again: crystals whose folder carries dfmv_done.flag
   Echo #     are skipped
   Echo #   - to start over, do the same and answer 1 to "redo all"
   Echo ############################################################
   Echo
   Exit No new targets merged - nothing collected, see the log
Endif

offset = $nsqrs

Echo Offset: $offset

xtalct := $offset


##############DIFF MOVIE SETTINGS#############

# =============================================================================
# COLUMN, SCREEN AND SPOT GUARD  -- must run BEFORE the settings below
# =============================================================================
# They run before the rotation settings so that $spsz, which is baked into the
# folder name and the movie filename, records the spot size the movies are
# actually taken at (read back by the guard after GoToLowDoseArea R).
SetColumnOrGunValve 1
SetBeamBlank 0
ScreenUp

# Movies may deliberately be collected at a different spot size from the rest
# of the session (for example a larger spot number to reduce dose), so this
# stage can declare its OWN expectation in $dfmv_spot_expected. Otherwise the
# session value from Setup_Helper is used; with neither, the guard records the
# spot size without judging it.
stage_name @= DFMV
IsVariableDefined dfmv_spot_expected
If $reportedValue1 == 1
   stage_spot_expected := $dfmv_spot_expected
Else
   IsVariableDefined spot_expected
   If $reportedValue1 == 1
      stage_spot_expected := $spot_expected
   Else
      stage_spot_expected := 0
   Endif
Endif
Call REyes_SpotGuard
If $reyes_spot_ok == 0
   Echo Movie collection abandoned by the spot guard -- see the reason above
   Return
Endif

Echo DFMV will write movie folders named $samplename-mov<n>_...

#ROTATION SETTINGS
startangle := $selected_tilt
finalangle := -$selected_tilt
range := P$startangletoN$finalangle
tiltrange := $selected_tilt * 2
nsweeps := 1
spsz := $reyes_spot_actual

# --- Rotation rate: the dose control, since no macro changes a spot size ------
# Dose per degree scales with 1/rotrate. Frame time scales with it so the
# angular sampling stays the same -- 1 deg per frame, 100 frames over a
# +/-50 deg range. Default: 1 deg/s at 1.00 s per frame (rotincr 0.033); for
# example 4 deg/s at 0.25 s per frame gives a quarter of the dose.
# Setup_Helper asks for both when run from START_REYES; run standalone, they
# are asked here.

IsVariableDefined dfmv_rotrate
If $reportedValue1 == 0
   EnterDefaultedNumber 1 2 Rotation rate in deg/s:
   dfmv_rotrate := $reportedValue1
Endif
IsVariableDefined dfmv_acqrate
If $reportedValue1 == 0
   EnterDefaultedNumber 1 2 Frame time in s (rotrate x frametime should be 1 deg):
   dfmv_acqrate := $reportedValue1
Endif

# --- Timing knobs -----------------------------------------------------------
# Settle time before each movie copy, and once at the end of the run. Named and
# adjustable (set them before running to override).
IsVariableDefined dfmv_write_settle
If $reportedValue1 == 0
   dfmv_write_settle := 5
Endif
IsVariableDefined dfmv_tail_settle
If $reportedValue1 == 0
   dfmv_tail_settle := 20
Endif

rotrate := $dfmv_rotrate
acqrate := $dfmv_acqrate
rotincr := 0.033 * $rotrate
deg_per_frame = $rotrate * $acqrate

Echo DFMV_ROTATION | rotrate=$rotrate | rotincr=$rotincr | acqrate=$acqrate | deg_per_frame=$deg_per_frame

If $deg_per_frame > 1.05 OR $deg_per_frame < 0.95
   Echo
   Echo WARNING: rotrate x frametime = $deg_per_frame deg per frame, not 1.
   Echo Angular sampling differs from the standard 1 deg per frame, which
   Echo changes how the movies integrate. Continue only if this is intended.
   Echo
Endif

ReportCameraLength
dist := $reportedValue1 * 1000
temp := c #c for cryo or rt for room temp
recordtime := $tiltrange / $rotrate


# Nominal (Talos) to calibrated Apollo camera length. Any other value falls
# back to the nominal length so $apollodist is always defined.
If $dist == 420
   apollodist := 560
ElseIf $dist == 960
   apollodist := 1280
Else
   apollodist := $dist
   Echo
   Echo WARNING: camera length $dist mm has no calibrated Apollo equivalent.
   Echo   Known pairs are 420 -> 560 and 960 -> 1280. Falling back to the
   Echo   nominal value, so movie FILENAMES will carry $dist rather than a
   Echo   calibrated length. The data are unaffected; the names are not.
   Echo   A value of 0 means the column is not in diffraction mode.
   Echo
Endif

Echo Talos camera length is $dist
Echo Apollo camera length is $apollodist


############RUN STEPSCAN IN RECORD############

# The column, screen and spot guard already ran, above the settings block, so
# that $spsz reflects the spot the movies are actually taken at.

SetContinuous R 1
SetExposure R $recordtime
SetBinning R 4
SetFrameTime R $acqrate


ReportExposure R
expsr = $acqrate

movtime = ($startangle - $finalangle) / $rotrate
nframes = ($startangle - $finalangle) / ($rotrate * $expsr)

MakeDirectory movies
Delay 2 sec


# --- Resume accounting -------------------------------------------------------
# Every iteration collects, skips (done earlier) or sets the crystal aside.
n_collected = 0
n_skipped = 0

# The done flag is written only if the column valve is still open when the
# movie finishes, so a movie taken with the valve closed is never marked done
# (a resume collects it again).
valve_check := 1
n_valve_lost = 0
n_set_aside = 0

IsVariableDefined dfmv_redo_all
If $reportedValue1 == 0
   EnterDefaultedNumber 0 -1 Resume mode: 0 = skip crystals already collected, 1 = redo all:
   dfmv_redo_all := $reportedValue1
Endif


# =============================================================================
# EVERY CRYSTAL IN ITS OWN Try -- a failed movie loses only itself
# =============================================================================
# A crystal whose valve was found closed after its movie, or whose body raised
# an error (a COM error, a stage that stays busy through every move attempt, a
# failed copy), is SET ASIDE: no done flag, no note, the camera's staging
# folder renamed -VALVECLOSED or -SKIPPED, stage tilted back to 0, valve
# re-opened, and
#     ITEM_SKIPPED | stage=DFMV | item=I | reason=valve_closed|error_<phase>
# in this log. The loop goes on with the next crystal, and the end phrase below
# is ALWAYS printed once the loop finishes -- REyes waits for it.
# The outcome is carried in $item_state (0 to do, 1 done earlier, 2 collected
# now, 3 set aside); nothing in the loop Breaks or Continues.
# "Movie folder:" / "Acquired movie:" are printed only after the valve check
# has passed, so REyes is never told about a valve-closed movie.
Loop $nitems
   xtalct := $xtalct + 1
   sweepcnt = 1
   movdir = $samplename_mov$xtalct_$distmm_$rotratedps_$acqratespf_sp$spsz_$range_sweep$sweepcnt_$temp
   movname = $samplename-mov$xtalct_$apollodist_$rotrate_$expsr_$range_g8sp$spsz_$temp$mrc_ext
   item_state = 0
   item_valve = 0
   item_phase @= resume_check
   dfmv_valve_ok = 1

   Try
      # --- RESUME: skip crystals a previous run already finished ---------------
      # The marker is written only after the copy has returned, so a crystal
      # interrupted mid-movie has no flag and is collected again. Checked on disk
      # rather than in the navigator so that it survives the navigator being
      # rebuilt, which is exactly what happens between a pause and a restart.
      DoesFileExist $set_directory\movies\$movdir\dfmv_done.flag
      If $reportedValue1 == 1 AND $dfmv_redo_all == 0
         item_state = 1
         n_skipped = $n_skipped + 1
         Echo RESUME | stage=DFMV | item=$xtalct | action=skipped | folder=$movdir
         SaveLog
      Endif

      If $item_state == 0
         # Re-open the column valve if something closed it since the last crystal.
         item_phase @= valve_reopen
         If $valve_check == 1
            ReportColumnOrGunValve
            If $reportedValue1 != 1
               Echo VALVE_REOPENED | stage=DFMV | item=$xtalct
               SetColumnOrGunValve 1
               Delay 2 sec
            Endif
         Endif

         SetFolderForFrames $movdir
         item_phase @= move
         # --- stage-safe move: wait for the stage, retry a refused move 5 times ---
         move_ok = 0
         Loop 5
            Loop 60
               ReportStageBusy
               If $reportedValue1 == 0
                  Break
               EndIf
               Delay 1 sec
            EndLoop
            Try
               MoveToNavItem $xtalct
               move_ok = 1
            Catch
               Echo STAGE_RETRY | the stage refused: MoveToNavItem $xtalct
               Echo STAGE_RETRY | waiting 20 s before trying again
               Delay 20 sec
            EndTry
            If $move_ok == 1
               Break
            EndIf
         EndLoop
         If $move_ok == 0
            Throw The stage stayed busy or not ready through 5 attempts: MoveToNavItem $xtalct
         EndIf

         Loop $nsweeps
            sweepcnt = $sweepcnt + 1
            item_phase @= tilt
            pretilt = $startangle + 3
            TiltTo $pretilt
            TiltTo $startangle
            Delay 1 sec
            ReportTiltAngle
            GoToLowDoseArea R
            UseContinuousFrames 1
            echo STARTING TILT
            item_phase @= acquire
            BackgroundTilt $finalangle $rotincr

            Record
            
            StopContinuous

            echo END TILT


            # --- VALVE CHECK, before the copy and before the done flag -----------
            # A movie taken with the valve closed is not copied and not flagged, so
            # the crystal stays NOT done and a resume collects it again. It is set
            # aside after the Try, together with the crystals that raised an error.
            item_phase @= valve_check
            If $valve_check == 1
               ReportColumnOrGunValve
               If $reportedValue1 != 1
                  dfmv_valve_ok = 0
               Endif
            Endif
            If $dfmv_valve_ok == 1
               # The movie is NAMED only now, after the valve check has passed.
               # REyes discovers movies from these two lines and then waits for
               # the folder, so a movie that will not be copied must never be
               # announced.
               echo Movie folder: $movdir
               echo Acquired movie: $movname
               # Let the detector flush the tail of the movie before the copy
               # starts (see DFMV_TIMING).
               #   PLACEMENT MATTERS: ReportClock sets reportedValue1, and the
               #   copy's error check below reads reportedValue1 from RunInShell.
               #   No clock may sit between the copy and that check.
               item_phase @= copy
               ReportClock t_ws_start
               Delay $dfmv_write_settle sec
               ReportClock t_ws_done
               TiltTo 0
               ReportClock
               ReportStageXYZ
               Delay 1 sec
               ReportClock t_copy_start
               RunInShell mkdir $set_directory\movies\$movdir
               RunInShell copy D:\DEOutput\Apollo\$movdir $set_directory\movies\$movdir
               # A failed copy is an error for THIS crystal, not the end of
               # the grid.
               If $reportedValue1 != 0
                  Throw Error creating movies directory for $movdir
               Endif
               ReportClock t_copy_done
               t_ws_actual = $t_ws_done - $t_ws_start
               t_copy_actual = $t_copy_done - $t_copy_start
               Echo DFMV_TIMING | movie=$movname | write_settle_req=$dfmv_write_settle | write_settle_act=$t_ws_actual | t_copy=$t_copy_actual | nframes=$nframes
               # NO WAIT HERE. `RunInShell` BLOCKS -- the copy has already
               # finished by the time this line is reached.

               # --- Mark this crystal done, for the resume check above ----------
               # Written only now, after the copy has returned, so an interruption
               # anywhere earlier leaves no marker and the crystal is re-collected.
               # MakeDirectory is idempotent and guarantees the folder exists.
               # The flag is written BEFORE "Completed movie acquisition", so that
               # phrase is only ever printed for a crystal that can no longer be
               # set aside: an error after this point keeps the movie (state 2).
               # nsweeps is 1 (set above), so this runs once per crystal.
               item_phase @= done_flag
               MakeDirectory $set_directory\movies\$movdir
               OpenTextFile DFMVDONE O 0 $set_directory\movies\$movdir\dfmv_done.flag
               WriteLineToFile DFMVDONE done
               CloseTextFile DFMVDONE
               ChangeItemNote $xtalct REYES_DONE_DFMV
               If $item_state != 2
                  # DONE from here on: a later error must not set it aside.
                  item_state = 2
                  n_collected = $n_collected + 1
               Endif
               Echo Completed movie acquisition
               SaveLog
            Endif
         EndLoop

         If $dfmv_valve_ok == 0
            item_valve = 1
            item_state = 3
         Else
            Echo RESUME | stage=DFMV | item=$xtalct | action=collected | folder=$movdir
         Endif
      Endif
   Catch
      If $item_state != 2 AND $item_state != 1
         item_state = 3
      Endif
   EndTry

   # --- SET ASIDE: valve closed or an error -- this crystal only ----------------
   # Every command that touches the scope or the disk has its own Try, so the
   # clean-up of one crystal can never cost the next one.
   If $item_state == 3
      n_set_aside = $n_set_aside + 1
      If $item_valve == 1
         n_valve_lost = $n_valve_lost + 1
         item_reason @= valve_closed
         item_suffix @= -VALVECLOSED
         Echo VALVE_CLOSED | stage=DFMV | item=$xtalct | folder=$movdir | action=not_copied_not_marked_done
         Echo WARNING: the column valve was closed when this movie finished.
         Echo          Its frames are set aside; it will be collected again on a resume.
      Else
         item_reason @= error_$item_phase
         item_suffix @= -SKIPPED
         Echo WARNING: crystal $xtalct failed during $item_phase and is set aside.
         Echo          Movie collection goes on; it will be collected again on a resume.
         # A failure between Record and StopContinuous would leave the camera
         # streaming into the next crystal's folder.
         Try
            StopContinuous
         Catch
            Echo WARNING: StopContinuous failed after crystal $xtalct
         EndTry
      Endif
      Echo ITEM_SKIPPED | stage=DFMV | item=$xtalct | reason=$item_reason
      Try
         TiltTo 0
      Catch
         Echo WARNING: could not tilt back to 0 after crystal $xtalct
      EndTry
      Try
         RunInShell cmd /c ren D:\DEOutput\Apollo\$movdir $movdir$item_suffix
      Catch
         Echo WARNING: could not rename the staging folder of crystal $xtalct
      EndTry
      Try
         SetColumnOrGunValve 1
         Delay 2 sec
      Catch
         Echo WARNING: could not re-open the column valve after crystal $xtalct
      EndTry
   Endif

   If $item_state >= 2
      Try
         SaveNavigator
         SaveLog
      Catch
         Echo WARNING: could not save the navigator or the log after crystal $xtalct
      EndTry
   Endif
EndLoop

Echo STOPPING

# LET THE CAMERA AND THE COPY FINISH
# Once per run, not per crystal ($dfmv_tail_settle, see Timing knobs).
Delay $dfmv_tail_settle sec
ReportClock

# set_aside is appended LAST, so field-keyed parsers of this record are unaffected.
Echo DFMV_SUMMARY | targets=$nitems | collected=$n_collected | skipped=$n_skipped | set_aside=$n_set_aside
Echo DFMV_CHECKS | valve_lost=$n_valve_lost
dfmv_set_aside := $n_set_aside

# =============================================================================
# THE END PHRASE IS ALWAYS PRINTED ONCE THE LOOP HAS RUN
# =============================================================================
# REyes treats the line below as "all movies collected" and stops watching.
# A zero target count is stopped further up (RE-MERGE TRAP), so this line can
# only be reached after the loop has walked real crystals. Every crystal is
# accounted for -- collected now, collected earlier, or set aside with an
# ITEM_SKIPPED line -- and the phrase follows, so a single failed movie never
# leaves REyes waiting.
accounted = $n_collected + $n_skipped
If $n_set_aside > 0
   Echo $n_set_aside of $nitems crystal(s) set aside - $accounted have movies
Endif

Echo Completed acquiring tilt movies for all crystals!

ReportDateTime

ScreenDown
SetBeamBlank 1
SetColumnOrGunValve 0

SaveNavigator
SaveLog

# No crystal has a movie at all: the grid has produced nothing to process, and
# the summary must not call it a success. The phrase above has already released
# the monitor, so this only decides the grid's line in the summary.
If $accounted == 0
   Exit DFMV collected nothing - all $nitems crystals were set aside
Endif
EndMacro
Macro	34
MacroName REyes_SpotGuard

# =============================================================================
# REyes_SpotGuard
#
# Shared Low Dose and spot-size check for the macros that collect diffraction:
# called by REyes_STPSCN and REyes_DFMV.
#
# IT VERIFIES, IT NEVER SETS.
#   Forcing SetSpotSize here could fight the Low Dose area setup, and the
#   failure being guarded against is an unnoticed mismatch, not an unset value.
#   No macro in this package changes the spot size.
#
# WHY IT READS AFTER GoToLowDoseArea R
#   That call is the LM -> SA transition. If the spot size is not restored by
#   the transition, diffraction is collected at the LM spot size: the dose
#   changes several-fold, and the spot size appears only in the per-frame
#   .mdoc, not in any processing log.
#
# WHY IT CHECKS LOW DOSE FIRST
#   GoToLowDoseArea does nothing when Low Dose is off, so a spot size read
#   after it would report whatever LM left behind while looking like a
#   successful check. Low Dose off is therefore reported as its own failure.
#   The area number from ReportLowDose is logged but not asserted on.
#
# EXPECTED SPOT IS PER STAGE
#   Each caller declares its own expected value in stage_spot_expected, so a
#   stage can deliberately run at a different spot size (e.g. a larger spot
#   number for rotation movies to reduce dose).
#
# CALLER CONTRACT
#   in   stage_name             short label for the log lines
#   in   stage_spot_expected    expected spot for THIS stage; 0 or undefined
#                               means "record it, do not judge it"
#   in   spot_mismatch_policy   1 = abort, 2 = warn and continue.
#                               Undefined defaults to 1 (abort).
#   out  reyes_spot_ok          1 = proceed, 0 = caller must stop
#   out  reyes_spot_actual      the spot size actually in force
#   out  reyes_lowdose_ok       1 = Low Dose on, 0 = off (already fatal below)
#
# The caller checks $reyes_spot_ok and decides. This macro never calls Exit:
# inside the pipeline's Try block an Exit is caught and reported as a generic
# "Pipeline_execution_error", which loses the reason.
# =============================================================================

reyes_spot_ok := 1
reyes_lowdose_ok := 1

IsVariableDefined stage_name
If $reportedValue1 == 0
    stage_name @= unnamed-stage
EndIf

IsVariableDefined stage_spot_expected
If $reportedValue1 == 0
    stage_spot_expected := 0
EndIf

IsVariableDefined spot_mismatch_policy
If $reportedValue1 == 0
    spot_mismatch_policy := 1
EndIf

# The screen must be up or GoToLowDoseArea silently fails. Callers raise it
# already; repeating it here is harmless and removes an ordering dependency.
ScreenUp

# --- Low Dose must be ON, or the area switch below is a no-op ---------------
ReportLowDose
lowdose_on = $reportedValue1
lowdose_area = $reportedValue2

If $lowdose_on == 0
    Echo
    Echo ############################################################
    Echo # STOPPING: LOW DOSE IS OFF  (stage $stage_name)
    Echo #
    Echo # GoToLowDoseArea does nothing while Low Dose is off, so the
    Echo # optics stay wherever the previous stage left them and the
    Echo # spot check below would pass while reporting the LM value.
    Echo # Turn Low Dose on and re-run this stage.
    Echo ############################################################
    Echo
    reyes_lowdose_ok := 0
    reyes_spot_ok := 0
    Return
EndIf

GoToLowDoseArea R

# --- Read the spot size AFTER the LM -> SA transition ----------------------
ReportSpotSize
reyes_spot_actual := $reportedValue1

Echo SPOT_GUARD | stage=$stage_name | lowdose=$lowdose_on | area=$lowdose_area | spot_expected=$stage_spot_expected | spot_actual=$reyes_spot_actual

If $stage_spot_expected == 0
    Echo Spot size $reyes_spot_actual recorded for $stage_name (no expectation declared)
    Return
EndIf

If $reyes_spot_actual == $stage_spot_expected
    Echo Spot size verified for $stage_name: $reyes_spot_actual
    Return
EndIf

Echo
Echo ############################################################
Echo # WARNING: SPOT SIZE MISMATCH  (stage $stage_name)
Echo #   expected $stage_spot_expected  but the microscope reports $reyes_spot_actual
Echo #
Echo # Collecting at an unintended spot size changes the dose several-fold.
Echo # It does not stop the run, but it breaks frame classification after
Echo # the fact, and the value is recorded only in the per-frame .mdoc.
Echo #
Echo # If this IS intended, declare it as this stage's expected value
Echo # rather than overriding the guard, so the log records the intent.
Echo ############################################################
Echo

If $spot_mismatch_policy == 1
    Echo ABORTING on spot size mismatch. To run anyway, choose "continue"
    Echo for the spot-mismatch policy at setup.
    reyes_spot_ok := 0
    Return
EndIf

Echo Continuing at spot size $reyes_spot_actual despite the mismatch (policy = continue)

EndMacro
Macro	40
MacroName REyes_postECNT

###################SETTINGS###################

#UPDATE NAVIGATOR ITEMS WITH ALL GRID SQUARES
ReportNumTableItems
Echo Number of Navigator lines: $reportedValue1

nitems := $reportedValue1 - 1
Echo Current number of items: $nitems

Loop $nitems

   DeleteNavigatorItem -1

EndLoop

all_squares = $set_directory\grid_squares\grid_squares.nav
Echo Eucentricity corrected file: $all_squares
MergeNavFile $all_squares

SaveLog
EndMacro
Macro	41
MacroName REyes_premanualECNT

###################SETTINGS###################

#REMOVE MANUALLY ADDED SQUARES GRID SQUARES
ReportNumTableItems
Echo Number of Navigator lines: $reportedValue1

nitems := $reportedValue1 - 1
Echo Current number of items: $nitems

Loop $nitems

   DeleteNavigatorItem -1

EndLoop

SaveLog
EndMacro
Macro	42
MacroName REyes_saveNAV

Echo $nav_file_name
SaveNavigator $nav_file_name
SaveLog
EndMacro
Macro	43
MacroName ECNT_nav_ready

# EVERY POLL SURVIVES A TRANSIENT MICROSCOPE ERROR.
# An asynchronous COM error from the microscope (for example "ERROR in call to
# microscope getting small screen position") can surface on any script line,
# including a plain file-wait; without a Try of its own it reaches the Catch
# in START_REYES and fails the whole grid. Each poll (the file test) and each
# Delay therefore sits in its own Try: an error is logged as
#     WAIT_POLL_ERROR | wait=<name> | errors=<n so far>
# and the wait simply goes on. Nothing inside a Try may Break out of the poll
# loop: the Try only records what it saw, and the loop decides after EndTry.
# The wait itself is unchanged: up to 20 polls of 30 s, ending as soon as
# the file appears.

nav_file_exists = 0
poll_errors = 0
poll_n = 0
Loop 20
   poll_n = $poll_n + 1
   Try
      DoesFileExist grid_squares\eucentricity.nav
      nav_file_exists = $reportedValue1
   Catch
      poll_errors = $poll_errors + 1
      Echo WAIT_POLL_ERROR | wait=ecnt_nav | errors=$poll_errors
   EndTry

   If $nav_file_exists == 1
      Try
         Delay 5 sec
      Catch
         poll_errors = $poll_errors + 1
         Echo WAIT_POLL_ERROR | wait=ecnt_nav | errors=$poll_errors
      EndTry
      Break
   EndIf

   Try
      Delay 30 sec
   Catch
      poll_errors = $poll_errors + 1
      Echo WAIT_POLL_ERROR | wait=ecnt_nav | errors=$poll_errors
   EndTry

EndLoop

If $nav_file_exists == 0
   Exit REyes got stuck and Navigator file was not generated. Please, check REyes log.
EndIf
EndMacro
Macro	44
MacroName ECNT_corrected

# EVERY POLL SURVIVES A TRANSIENT MICROSCOPE ERROR.
# An asynchronous COM error from the microscope (for example "ERROR in call to
# microscope getting small screen position") can surface on any script line,
# including a plain file-wait; without a Try of its own it reaches the Catch
# in START_REYES and fails the whole grid. Each poll (the file test) and each
# Delay therefore sits in its own Try: an error is logged as
#     WAIT_POLL_ERROR | wait=<name> | errors=<n so far>
# and the wait simply goes on. Nothing inside a Try may Break out of the poll
# loop: the Try only records what it saw, and the loop decides after EndTry.
# The wait itself is unchanged: up to 20 polls of 30 s, ending as soon as
# the file appears.
# 20 polls rather than 5: the eucentricity analysis can take two minutes or
# more, and a wait that runs out fails the grid ("REyes got stuck").

correction_file_exists = 0
poll_errors = 0
poll_n = 0
Loop 20
   poll_n = $poll_n + 1
   Try
      DoesFileExist grid_squares\eucentricity_correction.png
      correction_file_exists = $reportedValue1
   Catch
      poll_errors = $poll_errors + 1
      Echo WAIT_POLL_ERROR | wait=ecnt_corrected | errors=$poll_errors
   EndTry

   If $correction_file_exists == 1
      Try
         Delay 15 sec
      Catch
         poll_errors = $poll_errors + 1
         Echo WAIT_POLL_ERROR | wait=ecnt_corrected | errors=$poll_errors
      EndTry
      Break
   EndIf

   Try
      Delay 30 sec
   Catch
      poll_errors = $poll_errors + 1
      Echo WAIT_POLL_ERROR | wait=ecnt_corrected | errors=$poll_errors
   EndTry

EndLoop

If $correction_file_exists == 0
   Exit REyes got stuck and eucentricity was not corrected. Please, check REyes log.
EndIf
EndMacro
Macro	45
MacroName Targets_nav_ready

# EVERY POLL SURVIVES A TRANSIENT MICROSCOPE ERROR.
# An asynchronous COM error from the microscope (for example "ERROR in call to
# microscope getting small screen position") can surface on any script line,
# including a plain file-wait; without a Try of its own it reaches the Catch
# in START_REYES and fails the whole grid. Each poll (the file test) and each
# Delay therefore sits in its own Try: an error is logged as
#     WAIT_POLL_ERROR | wait=<name> | errors=<n so far>
# and the wait simply goes on. Nothing inside a Try may Break out of the poll
# loop: the Try only records what it saw, and the loop decides after EndTry.
# The wait itself is unchanged: up to 50 polls of 30 s, ending as soon as
# the file appears.

nav_file_exists = 0
poll_errors = 0
poll_n = 0
Loop 50
   poll_n = $poll_n + 1
   Try
      DoesFileExist targets\targets.nav
      nav_file_exists = $reportedValue1
   Catch
      poll_errors = $poll_errors + 1
      Echo WAIT_POLL_ERROR | wait=targets_nav | errors=$poll_errors
   EndTry

   If $nav_file_exists == 1
      Try
         Delay 5 sec
      Catch
         poll_errors = $poll_errors + 1
         Echo WAIT_POLL_ERROR | wait=targets_nav | errors=$poll_errors
      EndTry
      Break
   EndIf

   Try
      Delay 30 sec
   Catch
      poll_errors = $poll_errors + 1
      Echo WAIT_POLL_ERROR | wait=targets_nav | errors=$poll_errors
   EndTry

EndLoop

# Set persistent variable for caller to check
If $nav_file_exists == 0
   Echo WARNING: targets.nav file was not generated after 25 minutes
   Echo This usually means no viable targets were identified
   targets_found := 0
Else
   targets_found := 1
EndIf
EndMacro
Macro	46
MacroName Manual_ready

# EVERY POLL SURVIVES A TRANSIENT MICROSCOPE ERROR.
# An asynchronous COM error from the microscope (for example "ERROR in call to
# microscope getting small screen position") can surface on any script line,
# including a plain file-wait; without a Try of its own it reaches the Catch
# in START_REYES and fails the whole grid. Each poll (the file test) and each
# Delay therefore sits in its own Try: an error is logged as
#     WAIT_POLL_ERROR | wait=<name> | errors=<n so far>
# and the wait simply goes on. Nothing inside a Try may Break out of the poll
# loop: the Try only records what it saw, and the loop decides after EndTry.
# The wait itself is unchanged: up to 20 polls of 30 s, ending as soon as
# the file appears.

manual_file_exists = 0
poll_errors = 0
poll_n = 0
Loop 20
   poll_n = $poll_n + 1
   Try
      DoesFileExist grid_squares\manual_grid_overlay.png
      manual_file_exists = $reportedValue1
   Catch
      poll_errors = $poll_errors + 1
      Echo WAIT_POLL_ERROR | wait=manual_ready | errors=$poll_errors
   EndTry

   If $manual_file_exists == 1
      Try
         Delay 5 sec
      Catch
         poll_errors = $poll_errors + 1
         Echo WAIT_POLL_ERROR | wait=manual_ready | errors=$poll_errors
      EndTry
      Break
   EndIf

   Try
      Delay 30 sec
   Catch
      poll_errors = $poll_errors + 1
      Echo WAIT_POLL_ERROR | wait=manual_ready | errors=$poll_errors
   EndTry

EndLoop

If $manual_file_exists == 0
   Exit REyes got stuck and list of squares has not been updated. Please, check REyes log.
EndIf
EndMacro
