Index: /trunk/tools/eam/newnonlin/newnonlin.sh
===================================================================
--- /trunk/tools/eam/newnonlin/newnonlin.sh	(revision 42355)
+++ /trunk/tools/eam/newnonlin/newnonlin.sh	(revision 42355)
@@ -0,0 +1,160 @@
+
+$NKNOTS = 15
+$SHOWPLOT = 0
+
+# the MJD-OBS field means these corrections are only
+# valid from 2009/11/15
+macro make.model.chip
+  if ($0 != 2)
+    echo "USAGE: make.model.chip (chipname)"
+    break
+  end
+
+  local i chip output detname
+  $chip = $1
+
+  list name -glob models.t0/$chip.xy??.txt
+  $output = nonlin.20230126.$chip.fits
+  $detname = $detName:$chip
+
+  mcreate phu 0 0
+  keyword phu ORIGIN   -w PS1
+  keyword phu TELESCOP -w PS1
+  keyword phu INSTRUME -w gpc1
+  keyword phu CONTROLR -w STARGRASP
+  keyword phu NAMPS    -wd 64
+  keyword phu MJD-OBS  -wf 55150
+  keyword phu DETECTOR -w $detname
+  wd phu $output
+
+  for i 0 $name:n
+    strlen $name:$i Nchar
+    substr $name:$i {$Nchar - 8} 4 extname
+    #echo $extname : $name:$i
+
+    make.model $name:$i $extname $output
+  end
+end
+
+macro make.model
+  if ($0 != 4)
+    echo "make.model (file) (extname) (output)"
+    echo "generate a spline fit to the model, selecting NKNOTS uniformly-spaced knots"
+    break
+  end
+
+  local i input extname output 
+  $input = $1
+  $extname = $2
+  $output = $3
+
+  data $input
+
+  read Xg 1 Yg 2
+  set lXg = log(Xg)
+
+  # select knots from the interpolated sequence which are
+  # evenly spaced in the log(x)
+
+  $lXmin = lXg[0]
+  $lXmax = lXg[-1]
+  $lXdel = ($lXmax - $lXmin) / $NKNOTS
+
+  # creates a vector with N+1 entries
+  create lXk 0 {$NKNOTS + 1}
+  lXk[0]  = $lXmin
+  lXk[-1] = $lXmax
+  set Yk = zero(lXk) + NAN
+  Yk[0]  = Yg[0]
+  Yk[-1] = Yg[-1]
+
+  # lXk[0] and lXk[Nknots] are already set above,
+  # this fills in the knots from lXk[1] to lXk[NKNOTS-1]
+  for i 1 $NKNOTS
+    $lXval = $lXmin + $lXdel * $i
+    lXk[$i] = $lXval
+    subset tvec = Yg where (lXg >= $lXval)
+    # vstat tvec
+    
+    # this step relies on tvec having the same order as Yg (except for the excluded entries)
+    Yk[$i] = tvec[0]
+  end
+
+  # we want the factor in the IPP code to be a multiplication, not a division
+  # we need to invert the factor from Hua's analysis:
+  set rYk = 1.0 / Yk
+
+  spline create $extname lXk rYk
+  spline save $extname $output -append
+
+  if ($SHOWPLOT) 
+    lim -n 2 lXg Yg; clear; box; resize 1200 900
+    lim -n 2 lXg Yg; clear; box; resize 1200 900
+    plot -n 2 -c red lXk Yk -pt x -sz 2
+    
+    spline apply $extname lXg YgFitLog
+    plot -n 2 -c blue -x line lXg YgFitLog -lw 2
+  end
+end
+
+# detName derived from ippconfig/gpc1/format_20100723.config
+$detName:XY01 = CCID58-2-15a1  
+$detName:XY02 = CCID58-2-18a1  
+$detName:XY03 = CCID58-2-04a2  
+$detName:XY04 = CCID58-1-01b1  
+$detName:XY05 = CCID58-1-09b1  
+$detName:XY06 = CCID58-2-09b1  
+$detName:XY10 = CCID58-2-22a1  
+$detName:XY11 = CCID58-2-01a1  
+$detName:XY12 = CCID58-3-11a1  
+$detName:XY13 = CCID58-3-06a1  
+$detName:XY14 = CCID58-3-05b1  
+$detName:XY15 = CCID58-3-08b1  
+$detName:XY16 = CCID58-1-02b1  
+$detName:XY17 = CCID58-2-07b2  
+$detName:XY20 = CCID58-1-19a1  
+$detName:XY21 = CCID58-1-04a1  
+$detName:XY22 = CCID58-2-09a2  
+$detName:XY23 = CCID58-1-02a1  
+$detName:XY24 = CCID58-3-10b1  
+$detName:XY25 = CCID58-3-05b2  
+$detName:XY26 = CCID58-3-04b1  
+$detName:XY27 = CCID58-1-18b1  
+$detName:XY30 = CCID58-3-13a2  
+$detName:XY31 = CCID58-3-05a1  
+$detName:XY32 = CCID58-2-01a2  
+$detName:XY33 = CCID58-1-02a2  
+$detName:XY34 = CCID58-3-03b1  
+$detName:XY35 = CCID58-1-01b2  
+$detName:XY36 = CCID58-3-14b1  
+$detName:XY37 = CCID58-2-23b2  
+$detName:XY40 = CCID58-3-02b1  
+$detName:XY41 = CCID58-1-03b2  
+$detName:XY42 = CCID58-1-07b1  
+$detName:XY43 = CCID58-2-11b2  
+$detName:XY44 = CCID58-2-09a1  
+$detName:XY45 = CCID58-3-04a2  
+$detName:XY46 = CCID58-3-02a1  
+$detName:XY47 = CCID58-3-14a1  
+$detName:XY50 = CCID58-1-17b1  
+$detName:XY51 = CCID58-2-09b2  
+$detName:XY52 = CCID58-1-04b1  
+$detName:XY53 = CCID58-1-05b2  
+$detName:XY54 = CCID58-3-05a2  
+$detName:XY55 = CCID58-3-03a2  
+$detName:XY56 = CCID58-3-02a2  
+$detName:XY57 = CCID58-3-11a2  
+$detName:XY60 = CCID58-2-16b1  
+$detName:XY61 = CCID58-1-03b1  
+$detName:XY62 = CCID58-3-03b2  
+$detName:XY63 = CCID58-1-02b2  
+$detName:XY64 = CCID58-1-07a1  
+$detName:XY65 = CCID58-2-04a1  
+$detName:XY66 = CCID58-3-10a1  
+$detName:XY67 = CCID58-1-09a1  
+$detName:XY71 = CCID58-2-13b1  
+$detName:XY72 = CCID58-1-14b1  
+$detName:XY73 = CCID58-3-11b2  
+$detName:XY74 = CCID58-1-05a2  
+$detName:XY75 = CCID58-1-21a1  
+$detName:XY76 = CCID58-2-05a1  
