MaxMacros	60
Macro	0
MacroName START_REYES

################BEFORE STARTING###############
#  INTEGRATED MULTI-GRID PIPELINE
#  Talos Arctica + Ceta camera. SerialEM writes every image itself.
#  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
#
#  Run REyes with --microscope Arctica-CETA (serialEM_log 2).
#
#  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 every AUTOMATED mode, 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 of FSD and Screen,
# so either pipeline 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>_210LM_NxN_nav.nav back from the grid's folder;
#   - the navigator item blocks, which each merge records in
#     targets\range_*.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: it is single-grid REyes with
# someone 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)
      Echo   5  step-scan           (grid squares already mapped are skipped)
      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 yet supported.
   Echo Please use single-grid mode for manual workflows for now.
   Echo
   Echo ================================================
   Exit Multi-grid manual mode not yet 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 (--microscope Arctica-CETA).
      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 stage is busy or not ready; move
         # aborted"). Every navigator move also waits and retries.
         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 and has already
         # recorded the grid's outcome and 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 and has
         # already recorded the grid's outcome and updated the matching counter
         # (PARTIAL: no targets; FAILED: stopped by the spot-size guard).
         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_CETA

################BEFORE STARTING###############
#
#  BatchED - Unified Batch Electron Diffraction & Imaging
#  CETA-M CAMERA VERSION
#
#  Parent script that collects parameters and dispatches to:
#  - BatchED_Rotation_CETA: Continuous rotation diffraction (Record mode)
#  - BatchED_Still_CETA: Still diffraction snapshots (Record mode)
#  - BatchED_MultiAngle_CETA: Snapshots at +60, +30, 0, -30, -60 degrees (Record mode)
#  - BatchED_Imaging_CETA: Still imaging snapshots (Search mode)
#
#  Requirements:
#  - Navigator must be open with targets already added
#  - CETA-M camera configured in SerialEM
#
#  CETA-M SPECIFIC FEATURES:
#  - Uses frame-by-frame saving (not folder-based)
#  - Camera distance: 840 mm -> 790 mm calibrated
#  - Rotation increment calibrated for frame-saving overhead
#
##############################################

ClearPersistentVars

Echo
Echo ================================================
Echo ===== BATCHED - UNIFIED SETUP (CETA-M) =====
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 - CETA-M typical: 3.0 seconds (calibrated)
Echo - Range: 0.5 to 10 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 - CETA-M recommended: 2x
Echo - Options: 1, 2, 4, 8, or 16
Echo - Higher binning = faster readout, lower resolution
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 - CETA-M typical: 0.3 dps (calibrated)
   Echo - Range: 0.1 to 5.0 dps
   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)
   # CETA-M calibrated: 0.004 for 0.3 dps nominal (accounts for frame-saving overhead)
   # For other rates, scale proportionally from the calibrated value
   rotincr := 0.004 * ($rotrate / 0.3)
   Echo Rotation increment: $rotincr (calibrated for CETA-M frame overhead)

   # 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 CETA calibrated camera length
   # CETA-M calibration: 840 mm -> 790 mm
   If $dist == 840
      cetadist := 790
   ElseIf $dist == 960
      cetadist := 960
   Else
      Echo Warning: Unusual camera length detected
      cetadist := $dist
   Endif
   Echo CETA calibrated camera length: $cetadist mm

   # Temperature label
   # Note: Hardcoded to 'c' (cryo) - change to 'rt' for room temperature
   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 (CETA-M)
      Echo Tilt range: +$startangle to $finalangle degrees ($tiltrange total)
      Echo Rotation rate: $rotrate dps
      Echo Rotation increment: $rotincr (CETA-M calibrated)
      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: $cetadist mm (CETA-M calibrated)
   Echo Spot size: $spsz
   Echo Temperature: $temp
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_CETA
Echo
Echo Starting BatchED acquisition for CETA-M camera...
Echo
ReportDateTime

If $acq_mode == 1 AND $diff_type == 1
   MakeDirectory $set_directory\movies
   Echo Created subdirectory: movies
   Echo Dispatching to: BatchED_Rotation_CETA
   Call BatchED_Rotation_CETA
ElseIf $acq_mode == 1 AND $diff_type == 2
   MakeDirectory $set_directory\snapshots
   Echo Created subdirectory: snapshots
   Echo Dispatching to: BatchED_Still_CETA
   Call BatchED_Still_CETA
ElseIf $acq_mode == 1 AND $diff_type == 3
   MakeDirectory $set_directory\snapshots
   Echo Created subdirectory: snapshots
   Echo Dispatching to: BatchED_MultiAngle_CETA
   Call BatchED_MultiAngle_CETA
ElseIf $acq_mode == 2
   MakeDirectory $set_directory\imaging
   Echo Created subdirectory: imaging
   Echo Dispatching to: BatchED_Imaging_CETA
   Call BatchED_Imaging_CETA
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
#
#  The diffraction spot size is not declared here, so a standalone
#  REyes_STPSCN or REyes_DFMV records the spot size without judging it.
#
###################SETTINGS###################

# Start from a clean slate, as after reopening SerialEM. Without this, a
# standalone REyes_STPSCN / REyes_DFMV run after START_REYES in the same session
# would reuse that run's spot expectation, mismatch policy and resume answers.
ClearPersistentVars

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 210x LM magnification (CETA)
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
#
# Modes: 1 FSD, 2 Screen, 3 Montage.
# Rotation is NOT asked: on the Ceta REyes_DFMV uses a fixed nominal 0.3 deg/s
# with 3 s frames. The measured stage rate is lower (about 0.12 deg/s, 0.89 deg
# between frame starts, 0.36 deg exposed per frame); such movies index and
# solve with an XDS oscillation of 0.9.

#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 FSD, 2 Screen, 3 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 (FSD ONLY - THE ONLY MODE THAT COLLECTS ROTATION DATA)
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
   Echo Rotation: fixed in REyes_DFMV for CETA - nominal 0.3 deg/s, 3 s frames
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.
   # Default 10: the spot size used for Ceta REyes StepScans and movies
   # (1 s, bin 2).
   EnterDefaultedNumber 10 -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

   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 for folder name (critical for database)
   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 for folder name (critical for database)
         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
If $pipeline_mode == 1 or $pipeline_mode == 2
   Echo Expected diffraction spot size: $spot_expected
Endif
Echo
If $single_grid_mode == 1
   Echo Sample folder name (with -reyes suffix for database):
   Echo   $sample_names[1]
Else
   Echo Sample folder names (with -reyes suffix for database):
   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 (1), Screen (2), Montage-only (3)
# Every stage of every mode carries a resume gate -- see RESUME POINT below.
# Called for each grid in the main loop.
#
# A SET-ASIDE BLOCK OR ITEM 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 step-scan
#       left NO grid square with data ($stpscn_collected == 0) -- no 25-minute
#       wait for targets that cannot come;
#     - 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, for every mode)
# =============================================================================
# $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 user has stated they are done.
#
# This is the coarse layer. It sits on top of, and does not replace, the
# per-item resume inside each stage: STPSCN skips grid squares it has mapped
# and 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 (210x LM).
   rs_montage = _210LM_$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: Grid squares, eucentricity and step-scan (FSD and Screen)
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 step-scan goes on.
      # Only a step-scan that left NO square with data fails the grid -- and
      # then at once: there is nothing for REyes to find targets in, so waiting
      # 25 min 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) deliberately 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 2 (mysample-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 step-scan and the movies set a failed grid square / crystal aside and go
# on (REyes_STPSCN / REyes_DFMV). A step-scan 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_CETA

# Continuous rotation diffraction acquisition for CETA-M camera
# Called by BatchED_Main_CETA with persistent variables:
#   $compoundname, $xtalct, $nitems, $acqrate, $binning,
#   $startangle, $finalangle, $tiltrange, $range, $rotrate, $rotincr, $recordtime,
#   $nsweeps, $spsz, $cetadist, $temp, $set_directory
#
# KEY DIFFERENCES FROM APOLLO VERSION:
# - Uses frame-by-frame saving with Save command (not SetFolderForFrames)
# - Opens MRC file directly in movies subdirectory
# - No post-acquisition frame copying from detector
# - Uses 840 mm -> 790 mm CETA calibrated distance (not apollodist)

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

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

# Calculate number of frames needed
# Formula: nframes = tiltrange / (rotrate * acqrate)
# Example: 100 deg / (0.3 * 3) = 111.11 -> 112 frames
nframes = $tiltrange / ($rotrate * $acqrate)
Echo Calculated frames to save: $nframes
Echo (Based on rotation rate $rotrate dps, frame time $acqrate s)

# Get CETA camera length (calibrated)
ReportCameraLength
dist := $reportedValue1 * 1000
Echo Talos camera length: $dist mm

# CETA camera length calibration: 840 mm -> 790 mm
If $dist == 840
   cetadist := 790
ElseIf $dist == 960
   cetadist := 960
Else
   Echo Warning: Unusual camera length detected
   cetadist := $dist
Endif
Echo CETA calibrated camera length: $cetadist mm

# Create movies directory and get timestamp
MakeDirectory movies
ReportDateTime
yyyymmdd := $reportedValue1
movnum = 20000

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

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

   Loop $nsweeps
      sweepcount = $sweepcount + 1
      movnum = $movnum + 1

      # Create folder name for movie files
      # Format: compoundname-movN_XXXmm_X.Xdps_X.Xspf_PXXtoNXXX_spXXg8_sweepN_c
      # Note: g8 is hardcoded (gun lens position 8 - no SerialEM command to report this)
      movdir = $compoundname-mov$xtalct_$cetadistmm_$rotratedps_$acqratespf_$range_sp$spszg8_sweep$sweepcount_$temp
      MakeDirectory movies\$movdir

      # Create movie filename
      movname = $compoundname-mov$xtalct_$cetadist_$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

      # Open file for movie
      OpenNewFile movies\$movdir\$yyyymmdd_$movnum_integrated_movie.mrc
      ReportCurrentFilename

      Echo STARTING TILT
      ReportClock

      # Acquire first frame
      Record

      # Wait for first frame and save
      StartFrameWaitTimer -1
      WaitForNextFrame
      Save

      # Start background tilt
      BackgroundTilt $finalangle $rotincr

      # Save remaining frames
      Loop $nframes
         StartFrameWaitTimer -1
         WaitForNextFrame
         Save
      EndLoop

      # Stop continuous acquisition
      StopContinuous
      CloseFile

      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

      Echo Completed sweep $sweepcount for target $xtalct
      Echo

      Delay 5 sec

   EndLoop

   Echo Completed all sweeps for target $xtalct
   Echo

EndLoop
EndMacro
Macro	14
ScriptName BatchED_Still_CETA

# Still diffraction snapshot acquisition for CETA-M camera
# Called by BatchED_Main_CETA with persistent variables:
#   $compoundname, $xtalct, $nitems, $acqrate, $binning,
#   $nsweeps, $spsz, $cetadist, $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

# Get timestamp for file naming
ReportDateTime
yyyymmdd := $reportedValue1
snapnum = 30000

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

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

   Loop $nsweeps
      sweepcount = $sweepcount + 1
      snapnum = $snapnum + 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_$cetadistmm_$acqratespf_sp$spsz_$currentangle_sweep$sweepcount_$temp
      MakeDirectory snapshots\$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

      # Open file and acquire still snapshot
      OpenNewFile snapshots\$movdir\$yyyymmdd_$snapnum_snapshot.mrc
      ReportCurrentFilename
      Record
      Save
      CloseFile

      Echo Frame folder: $movdir
      ReportClock

      Echo Completed sweep $sweepcount for target $xtalct
      Echo

      Delay 2 sec

   EndLoop

   Echo Completed all sweeps for target $xtalct
   Echo

EndLoop
EndMacro
Macro	15
ScriptName BatchED_MultiAngle_CETA

# Multi-angle still diffraction snapshot acquisition for CETA-M camera
# Collects 5 still snapshots at +60, +30, 0, -30, -60 degrees per target
# Called by BatchED_Main_CETA with persistent variables:
#   $compoundname, $xtalct, $nitems, $acqrate, $binning,
#   $nsweeps, $spsz, $cetadist, $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

# Get timestamp for file naming
ReportDateTime
yyyymmdd := $reportedValue1
snapnum = 30000

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

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

   Loop $nsweeps
      sweepcount = $sweepcount + 1
      snapnum = $snapnum + 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_$cetadistmm_$acqratespf_sp$spsz_$angleslabel_sweep$sweepcount_$temp
      MakeDirectory snapshots\$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

      # Open single MRC file for all angles
      OpenNewFile snapshots\$movdir\$yyyymmdd_$snapnum_multiangle.mrc
      ReportCurrentFilename

      # 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
         Save

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

      CloseFile

      Echo Frame folder: $movdir
      ReportClock

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

      Echo Completed sweep $sweepcount for target $xtalct
      Echo

      Delay 5 sec

   EndLoop

   Echo Completed all sweeps for target $xtalct
   Echo

EndLoop
EndMacro
Macro	16
ScriptName BatchED_Imaging_CETA

# Still imaging snapshot acquisition (Search mode) for CETA-M camera
# Called by BatchED_Main_CETA 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

# Get timestamp for file naming
ReportDateTime
yyyymmdd := $reportedValue1
imgnum = 40000

# Main acquisition loop
Loop $nitems
   xtalct := $xtalct + 1
   imgnum = $imgnum + 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
   MakeDirectory imaging\$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

   # Open file and acquire Search image
   OpenNewFile imaging\$searchdir\$yyyymmdd_$imgnum_search.mrc
   ReportCurrentFilename
   Search
   Save
   CloseFile

   Echo Frame folder: $searchdir
   ReportClock

   Echo Completed target: $xtalct
   Echo

   Delay 2 sec

EndLoop
EndMacro
Macro	19
MacroName AutoloaderScreen

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

MinSlotNum    := 3
MaxSlotNum  := 6

########## 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

################BEFORE STARTING###############
#  TURN OFF LOW DOSE ON SCOPE
#  TURN ON LOW DOSE IN SERIALEM
#  LOAD VIEW PARAMETERS
#  CHECK SPOT SIZE
###################SETTINGS###################



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

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

#FILE NAMES
#FOR MNT SCRIPT
montage = _210LM_$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 210 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 210x 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 3
   SetBinning M 2

   M
   NewMap

   ScreenDown

   SetBeamBlank 1
   SetColumnOrGunValve 0

   Echo Completed 210x 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

################BEFORE STARTING###############
#  CHECK THAT FINE EUCENTRIC WILL RUN IN VIEW
#  2
#  3
#  4
###################SETTINGS###################
#
# Ceta View settings: 1.5 s, bin 2, no frame time. nitems is persistent (':='),
# as in REyes_postECNT / REyes_STPSCN / BatchED_Main_CETA, and the navigator
# move waits for the stage and retries.



##############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 while the stage is busy, retry 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

   SetContinuous V 0
   SetExposure V 1.5
   SetBinning V 2


   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  (Ceta)
# =============================================================================
# The Ceta StepScan: 1 s, bin 2, SerialEM writes one MRC per pattern.
#   - A FAILED GRID SQUARE LOSES ONLY ITSELF. Each square runs in its own Try;
#     a square that raised an error, or finished with the column valve closed,
#     is SET ASIDE: no done flag, no navigator note, its folder renamed
#     <sample>_<N>-SKIPPED or -VALVECLOSED, the valve re-opened, and its block
#     log carries
#       BLOCK_SKIPPED | stage=STPSCN | square=N | item=I | reason=R
#     instead of the per-square completion phrase. The grid goes on. There is
#     no consecutive-failure breaker: every square is tried.
#   - RESUME: a square whose folder carries stpscn_done.flag is skipped.
#   - 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 Return: the CALLER fails the
#     grid (All_blocks_failed) without waiting for targets.
#   - The spot guard (REyes_SpotGuard) replaces the bare GoToLowDoseArea R.
#
# CETA ACQUISITION:
#   SetExposure R 1 / SetBinning R 2, no frame time, and SerialEM writes the
#   file itself -- no staging folder, no copy:
#
#   FROZEN SEQUENCE -- NOTHING MAY BE INSERTED BETWEEN THESE SIX LINES:
#       OpenNewFile / ReportCurrentFilename / Record / Save / CloseFile /
#       ReportStageXYZ
#   dif-map finds the log line naming the MRC ("Current open image file is:
#   ...mrc", printed by ReportCurrentFilename) and reads "Stage x y z" EXACTLY
#   serialEM_log = 2 lines later (Arctica-CETA), the line between being
#   "Saved Z = 0". Anything that prints in between costs that frame its
#   coordinates, silently.
# =============================================================================

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

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

#FILE NAMES
#FOR STPSCN 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 (slot 34), replaces a bare GoToLowDoseArea
# =============================================================================
# GoToLowDoseArea R is a silent no-op when Low Dose is off, and SpotSize is
# recorded only in the per-frame .mdoc. Run from the orchestrator the expected
# spot (Setup_Helper) is enforced; run standalone it is recorded, not judged.
# Low Dose being off is fatal either way.
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

# CETA RECORD SETTINGS (1 s, bin 2).
SetContinuous R 0
SetExposure R 1
SetBinning R 2

# The done flag is written only if the column valve is still open when the
# square finishes; a square exposed with the valve closed holds blank frames.
# Regular (not persistent) and set on every run, so no value left over from an
# earlier run in the same SerialEM session can switch the check off.
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.
   # 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_file_open = 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 file that cannot be written --
   # jumps to the Catch below and costs THIS square only. Nothing in here may
   # Break or Continue out to the square loop (SerialEM block-scan traps); 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
         ChangeItemNote $posct REYES_DONE_STPSCN
      Endif

      If $sq_state == 0
         sq_phase @= open_log
         SaveLogOpenNew $samplename_$posname
         # CETA: SerialEM writes the patterns straight into this folder.
         MakeDirectory $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

         sq_phase @= move
         # --- stage-safe move: wait while the stage is busy, retry 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 SEQUENCE -- NOTHING MAY SIT BETWEEN THESE SIX LINES.
               # See the header: dif-map reads "Stage x y z" exactly two log
               # lines after "Current open image file is: ...mrc".
               # sq_file_open is set BEFORE and cleared AFTER the six lines, so
               # that a square failing with its file open closes it on set-aside.
               # ===============================================================
               sq_file_open = 1
               OpenNewFile $samplename_$posname\$yyyymmdd_$imgnum_integrated_movie$mrc_ext
               ReportCurrentFilename
               Record
               Save
               CloseFile
               ReportStageXYZ
               sq_file_open = 0

            EndLoop

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

         EndLoop

         # --- VALVE CHECK, before the done flag ---------------------------------
         # A square exposed with the valve closed is 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
            # Marker for the resume check above.
            sq_phase @= done_flag
            OpenTextFile STPDONE O 0 $set_directory\$samplename_$posname\stpscn_done.flag
            WriteLineToFile STPDONE done
            CloseTextFile STPDONE
            # Per-square navigator status. 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 are on disk).
            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 done flag, no note; an image file left open by the error is closed; the
   # square's folder is renamed out of the way, so a resume maps it into a clean
   # <sample>_<N> folder and REyes never mixes a half square with a full one;
   # the valve is re-opened. Every command that touches the scope or the disk
   # has its own Try, so cleaning up one square can never cost the next one.
   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_marked_done
         Echo WARNING: the column valve was closed when square $posname finished.
         Echo          Its patterns 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
      If $sq_file_open == 1
         Try
            CloseFile
         Catch
            Echo WARNING: could not close the open image file of grid square $posname
         EndTry
      Endif
      Try
         RunInShell cmd /c ren "$set_directory\$samplename_$posname" "$samplename_$posname$sq_suffix"
      Catch
         Echo WARNING: could not rename the 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 | camera=CETA | exposure=1 | binning=2 | 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, so it is printed only when at least one 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
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  (Ceta)
# =============================================================================
# The Ceta continuous-rotation movie (SerialEM saves frame by frame during the
# tilt):
#   - A FAILED MOVIE LOSES ONLY ITSELF. Each crystal runs in its own Try; a
#     crystal that raised an error, or whose movie finished with the column
#     valve closed, is set aside (ITEM_SKIPPED | stage=DFMV | item= | reason=):
#     no done flag, no note, continuous mode stopped, the open file closed, its
#     movie folder renamed -SKIPPED / -VALVECLOSED, stage tilted back to 0,
#     valve re-opened. The loop goes on.
#   - RESUME: a crystal whose movie folder carries dfmv_done.flag is skipped;
#     the merged target block is recorded in targets\range_dfmv.txt, so a
#     re-run against a navigator that already holds the targets resumes on the
#     same block instead of collecting nothing (the re-merge trap below).
#   - "Movie folder:" / "Acquired movie:" are printed only after the valve
#     check, so REyes is never told about a valve-closed movie.
#   - The end phrase "Completed acquiring tilt movies for all crystals!" is
#     ALWAYS printed once the loop has run, so the monitor is never left
#     waiting; a run where NO crystal has a movie then ends with Exit.
#   - The spot guard (REyes_SpotGuard) checks Low Dose and the spot size.
#
# THE ROTATION IS HARD-CODED, ON PURPOSE.
#   rotrate 0.3 deg/s and rotincr 0.004 are NOMINAL. With 3 s frames the Ceta
#   needs ~4.4 s to read out and save each frame while the stage keeps turning,
#   so frames start 7.4 s apart and the stage really turns at about 0.12 deg/s:
#   0.889 deg between frame starts, of which 0.36 deg is exposed. Movies taken
#   this way integrate with OSCILLATION_RANGE 0.9, and index and solve.
#   The movie folder name (..._0.3dps_0.33fps_...) and movie name
#   (..._<dist>_0.3_3_...) must keep this exact form: the processing reads the
#   rotation from them.
#   There is a 120 s settle after each movie and a 900 s wait at the end.
# =============================================================================

##############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

# =============================================================================
# THE TARGET BLOCK
# =============================================================================
# This macro merges targets\targets.nav itself and derives the count by
# differencing, as REyes always has. A fresh merge records which navigator
# items it added in targets\range_dfmv.txt; a re-run against a navigator that
# already holds them merges nothing, and then the recorded block is read back.

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

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 -- a silent no-op. With 0 targets the loop below
# would run zero times and fall through to the terminal phrase, which REyes
# reads as "all movies collected". Stop 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


# =============================================================================
# COLUMN, SCREEN AND SPOT GUARD -- before the settings, so that $spsz (baked
# into the folder and movie names) is the spot the movies are taken at.
# =============================================================================
SetColumnOrGunValve 1
SetBeamBlank 0
ScreenUp

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


##############DIFF MOVIE SETTINGS#############
# Persistent (':='), like BatchED_Main_CETA, which sets the same names: a
# regular '=' here would be refused after a BatchED run in the same session.

#ROTATION SETTINGS
startangle := $selected_tilt
finalangle := -$selected_tilt
range := P$startangletoN$finalangle
tiltrange := $selected_tilt * 2
nsweeps := 1
spsz := $reyes_spot_actual
# Nominal values, hard-coded for CETA -- see the header. The REAL stage rate
# is 0.12 deg/s; 0.33 (frames per second at 3 s per frame) only names the folder.
rotrate := 0.3
rotincr := 0.004
acqrate := 0.33
ReportCameraLength
dist := $reportedValue1 * 1000
temp := c #c for cryo or rt for room temp

Echo Talos camera length is $dist


############RUN DIFFRACTION MOVIES IN RECORD############

# CETA RECORD SETTINGS: continuous, 3 s per frame, bin 2. SerialEM saves each
# frame (no frame time).
SetContinuous R 1
SetExposure R 3
SetBinning R 2

ReportExposure R
expsr = $reportedValue1

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

Echo DFMV_ROTATION | camera=CETA | rotrate_nominal=$rotrate | rotincr=$rotincr | frame_exposure=$expsr | nframes=$nframes | real_rate=0.12 | deg_per_frame=0.89 | deg_exposed=0.36

MakeDirectory movies
movnum = 20000
Delay 2 sec

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

# The done flag is written only if the column valve is still open when the
# movie finishes. Regular and set on every run, so no value left over from an
# earlier run in the same SerialEM session can switch the check off.
valve_check = 1
n_valve_lost = 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
# =============================================================================
# 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 out of the Try.
# item_streaming / item_file_open say what the set-aside must undo.
Loop $nitems
   xtalct := $xtalct + 1
   movnum = $movnum + 1
   sweepcnt = 1
   movdir = $samplename_$distmm_sp$spsz_$rotratedps_$acqratefps_$range_mov$xtalct_sweep$sweepcnt_$temp
   movname = $samplename-mov$xtalct_$dist_$rotrate_$expsr_$range_g8sp$spsz_$temp$mrc_ext
   item_state = 0
   item_valve = 0
   item_streaming = 0
   item_file_open = 0
   item_phase @= resume_check
   dfmv_valve_ok = 1

   Try
      # --- RESUME: skip crystals a previous run already finished ---------------
      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

         MakeDirectory movies\$movdir
         item_phase @= move
         # --- stage-safe move: wait while the stage is busy, retry 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
            TiltTo $startangle
            Delay 1 sec
            ReportTiltAngle
            GoToLowDoseArea R
            UseContinuousFrames 1

            # --- THE CETA MOVIE: one MRC, written frame by frame ---------------
            # Frame 1 is saved at the start angle, then the tilt starts and
            # nframes more are saved while it runs.
            item_phase @= acquire
            item_file_open = 1
            OpenNewFile movies\$movdir\$yyyymmdd_$movnum_integrated_movie$mrc_ext
            ReportCurrentFilename
            item_streaming = 1
            Record
            StartFrameWaitTimer -1
            WaitForNextFrame
            Save
            echo STARTING TILT
            BackgroundTilt $finalangle $rotincr

            Loop $nframes
               StartFrameWaitTimer -1
               WaitForNextFrame
               Save
            EndLoop

            StopContinuous
            item_streaming = 0
            CloseFile
            item_file_open = 0

            echo END TILT

            # --- VALVE CHECK, before the movie is named and flagged -----------
            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.
               echo Movie folder: $movdir
               echo Acquired movie: $movname
               item_phase @= settle
               Delay 120 sec
               TiltTo 0
               ReportClock
               ReportStageXYZ
               Delay 2 sec

               # --- Mark this crystal done, for the resume check above --------
               # 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).
               item_phase @= done_flag
               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
                  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_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.
      Endif
      Echo ITEM_SKIPPED | stage=DFMV | item=$xtalct | reason=$item_reason
      # A failure between Record and StopContinuous would leave the camera
      # streaming, and one between OpenNewFile and CloseFile the movie open.
      If $item_streaming == 1
         Try
            StopContinuous
         Catch
            Echo WARNING: StopContinuous failed after crystal $xtalct
         EndTry
      Endif
      If $item_file_open == 1
         Try
            CloseFile
         Catch
            Echo WARNING: could not close the movie file of crystal $xtalct
         EndTry
      Endif
      Try
         TiltTo 0
      Catch
         Echo WARNING: could not tilt back to 0 after crystal $xtalct
      EndTry
      Try
         RunInShell cmd /c ren "$set_directory\movies\$movdir" "$movdir$item_suffix"
      Catch
         Echo WARNING: could not rename the movie 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 AUTOPROCESS FINISH DATA REDUCTION.

Delay 900 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. A zero target count
# is fatal further up, so it can only be reached after walking real crystals,
# and every crystal is accounted for: collected now, earlier, or set aside.
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 summary must not call the grid a success.
# The phrase above has already released the monitor.
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 inside a plain file
# wait; without a Try of its own it reaches START_REYES's Catch and fails the
# whole grid. Here each poll (the file test) and each Delay 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.
# 20 polls of 30 s.

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 inside a plain file
# wait; without a Try of its own it reaches START_REYES's Catch and fails the
# whole grid. Here each poll (the file test) and each Delay 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.
# 20 polls of 30 s. The wait ends the moment the file appears, so the
# limit costs nothing on a normal run.

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 inside a plain file
# wait; without a Try of its own it reaches START_REYES's Catch and fails the
# whole grid. Here each poll (the file test) and each Delay 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.
# 50 polls of 30 s.

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 inside a plain file
# wait; without a Try of its own it reaches START_REYES's Catch and fails the
# whole grid. Here each poll (the file test) and each Delay 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.
# 20 polls of 30 s.

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
