-- Sort Overlays by the Shift -- changes the order of the overlays -- as a result the chemical shift of a given peak increases from bottom to top -- the peak is identified by the region ppm_min .. ppm_max that can be defined inside or outside this file -- REQUIRES iNMR 4.1.3 -- All the open documents will be affected, so close the extraneous documents ppm_min = ppm_min or 2.91 -- **** CHANGE THESE VALUES !!!! **** <--------*** ppm_max = ppm_max or 2.97 local increment = 20 -- progressive vertical offset local savedstate = forall( true ) function pairsByKeys ( t ) -- taken from the book "Programming in Lua" by by Roberto Ierusalimschy, 1st ed., chapter 19.3 local a = {} for n in pairs(t) do table.insert(a, n) end table.sort( a ) local i = 0 -- iterator variable local iter = function () -- iterator function i = i + 1 if a[i] == nil then return nil else return a[i], t[a[i]] end end return iter end local Paths = {} -- contains the paths of the overlays and the chemical shift of the moving peak local overlays = target() -- all the documents for i = 2, 9000 do -- no. 1 is the main document, not an overlay local doc = overlays[i] if doc == NIL then print( i-2, " overlays found" ) break end target( doc ) local scale = getf() local start = scale.start + scale.width local digres = scale.width / scale.size local firsti, lasti region( start + 1, scale.start - 1 ) -- select the whole spectrum local T = copy() local max = 0 local imax = 0 firsti = math.ceil( (start - ppm_max) / digres ); -- restricting to the interesting region lasti = math.ceil( (start - ppm_min) / digres ) + 1; for i = firsti,lasti do -- looking for the maximum if T[i] > max then max = T[i] imax = i end end local h = T[imax-1] -- interpolation over 3 points local j = T[imax+1] -- we don't like having two overlays with the same shift imax = imax - 0.5 + 0.5 * (h - j) / (h + j - 2 * max ) imax = start - imax * digres while Paths[imax] do imax = imax + 1e-9 * digres end Paths[imax] = pathtofile() end local offset = 0 target( overlays[1] ) -- back to the main document (host) for ppm, path in pairsByKeys( Paths ) do offset = offset + increment ovadd( path, offset ) end amp() -- refresh the screen forall( savedstate )