
FUNCTION eis_replicate_vector, vector, dims

;+
; NAME:
;     EIS_REPLICATE_VECTOR
;
; PURPOSE:
;     Replicates a 1D vector into a larger array without using for loops.
;
; CATEGORY:
;     Arrays; replicate.
;
; CALLING SEQUENCE:
;     Result = EIS_REPLICATE_VECTOR( Vector, Dims )
;
; INPUTS:
;     Vector:  Must be a 1D array.
;     Dims:    An integer array specifying the size of the additional
;              dimensions.
;
; OUTPUTS:
;     An array of size [NVEC,DIMS[0],DIMS[1],etc.], where NVEC is the size
;     of VECTOR. Each subarray [*,i,j] will be the same as VECTOR.
;
;     Note that NVEC will always be the first dimension. To change this,
;     use the IDL REARRANGE routine.
;
; EXAMPLE:
;     IDL> a=indgen(5)
;     IDL> b=eis_replicate_vector(a,[4,6])
;     IDL> help,b
;     B               INT       = Array[5, 4, 6]
;     IDL> print,b[*,2,4]
;            0       1       2       3       4
;
; MODIFICATION HISTORY:
;     Ver.1, 06-Aug-2025, Peter Young
;       Craig Markwadt's routine CMREPLICATE does something similar, but it
;       is not in Solarsoft. The code used here was suggested by ChatGPT. I
;       wrote this routine specifically for use in the Hinode/EIS routine
;       eis_prep, although it is general purpose.
;-

  s=size(vector)
  IF s[0] NE 1 THEN BEGIN
    message,/info,/cont,'The input VECTOR must be a 1D array. Returning...'
    help,vector
    return,vector
  ENDIF

  nvec=n_elements(vector)
  
  ; dims: array of dimensions, e.g., [6, 4, 5]
  ; vary_dim: 0-based index of the dimension along which values should increase

  n_dims = N_ELEMENTS(dims)+1

  ; Build the reshape size: set 1 for all dims, except vary_dim
  reshape_dims = INTARR(n_dims) + 1
  reshape_dims[0] = nvec

  ; Reshape to insert singleton dimensions
  reshaped = REFORM(vector, reshape_dims)

  ; Rebin to final shape
  result = REBIN(reshaped, [nvec,dims])

  
  RETURN, result
END
