(************** Content-type: application/mathematica **************
                     CreatedBy='Mathematica 5.0'

                    Mathematica-Compatible Notebook

This notebook can be used with any Mathematica-compatible
application, such as Mathematica, MathReader or Publicon. The data
for the notebook starts with the line containing stars above.

To get the notebook into a Mathematica-compatible application, do
one of the following:

* Save the data starting with the line of stars above into a file
  with a name ending in .nb, then open the file inside the
  application;

* Copy the data starting with the line of stars above to the
  clipboard, then use the Paste menu command inside the application.

Data for notebooks contains only printable 7-bit ASCII and can be
sent directly in email or through ftp in text mode.  Newlines can be
CR, LF or CRLF (Unix, Macintosh or MS-DOS style).

NOTE: If you modify the data for this notebook not in a Mathematica-
compatible application, you must delete the line below containing
the word CacheID, otherwise Mathematica-compatible applications may
try to use invalid cache data.

For more information on notebooks and Mathematica-compatible 
applications, contact Wolfram Research:
  web: http://www.wolfram.com
  email: info@wolfram.com
  phone: +1-217-398-0700 (U.S.)

Notebook reader applications are available free of charge from 
Wolfram Research.
*******************************************************************)

(*CacheID: 232*)


(*NotebookFileLineBreakTest
NotebookFileLineBreakTest*)
(*NotebookOptionsPosition[     30735,        645]*)
(*NotebookOutlinePosition[     31380,        667]*)
(*  CellTagsIndexPosition[     31336,        663]*)
(*WindowFrame->Normal*)



Notebook[{
Cell[BoxData[
    \(\(\(\ \)\(\[IndentingNewLine]\)\(\[IndentingNewLine]\)\(\
\[IndentingNewLine]\)\( (*\ 
      Routine\ for\ Data\ Reduction\ of\ Seeing\ data\  - \ 
        FWHM\ *) \)\(\[IndentingNewLine]\)\(\[IndentingNewLine]\)\(\
\[IndentingNewLine]\)\)\)], "Input"],

Cell[BoxData[
    \( (*\ 
      The\ first\ part\ of\ the\ data\ analysis\ will\ look\ at\ the\ flat - 
        calibrated\ raw\ data\ coming\ from\ the\ CCD\ \((input\ of\ text\ \
file\ in\ ReadList)\); \ 
      the\ idea\ is\ to\ have\ a\ good\ 3  D\ view\ of\ it, \ 
      where\ the\ vertical\ axis\ are\ the\ ADUs\ of\ each\ pixel\ *) \)], \
"Input"],

Cell[BoxData[
    \(\(\(\[IndentingNewLine]\)\( (*\ 
      Routine\ 1\  - 
        Initial\ look\ at\ the\ data\ *) \)\(\[IndentingNewLine]\)\)\)], \
"Input"],

Cell[BoxData[
    \( (*\ The\ 1  
          st\ instruction\ is\ actually\ to\ ignore\ spell\ checking\ since\ \
it\ can\ be\ annoying\ for\ similarly - named\ variables\ *) \)], "Input"],

Cell[BoxData[
    \(\(\( (*\ 1  st, 
      we\ need\ to\ input\ the\ matrix\ \((x, 
          y)\)\ size\ of\ the\ CCD\ chip\ *) \)\(\n\)\( (*\ Here, 
      and\ elsewhere\ in\ the\ routine, \ 
      relevant\ numbers\ will\ be\ printed\ out\ so\ that\ after\ saving\ the\
\ file\ these\ can\ be\ looked\ up\ later; 
      we\ start\ doing\ so\ for\ the[x, 
          y]\ CCD\ chip\ size\ *) \)\)\)], "Input"],

Cell[BoxData[
    \(\(\(\[IndentingNewLine]\)\( (*\ Obviously, 
      change\ the\ SetDirectory\ according\ to\ the\ place\ where\ you\ have\ \
the\ CCD\ text\ file; 
      and\ place\ its\ name\ on\ "\<ReadList\>"*) \)\(\n\)\(\
\[IndentingNewLine]\)\( (*\ 
      The\ 1  st\ plot\ will\ give\ an\ idea\ of\ where\ the\ data\ lie\ in\ \
the\ \((x, y, ADU)\)\ space; 
      note\ that\ the\ initial\ ADU\ maximum\ is\ set\ at\ 60000, 
      but\ this\ might\ be\ changed\ higher\ \((if\ the\ data\ come\ out\ \
chopped)\)\ or\ lower\ \((closer\ to\ the\ maximum\ ADU\ value)\); 
      the\ maximum\ value\ of\ ADUs, 
      that\ the\ routine\ also\ gives\ as\ output, helps\ in\ this\ matter; 
      only\ change\ the\ viewpoint\ as\ a\ last\ resource\ *) \)\(\
\[IndentingNewLine]\)\)\)], "Input"],

Cell[BoxData[{
    RowBox[{
      StyleBox[\(Off[General::spell1]\),
        "MR"], 
      StyleBox[";",
        "MR"], 
      " ", \(matx = 
        Input["\<Please enter the x-direction number of pixels in the CCD \
chip:\>"]\), 
      ";", \(maty = 
        Input["\<Please enter the y-direction number of pixels in the CCD \
chip:\>"]\), ";", 
      " ", \(Print["\<CCD chip size (x,y): (\>", matx, "\<,\>", 
        maty, "\<)\>"]\), ";"}], "\[IndentingNewLine]", 
    RowBox[{\(SetDirectory["\<C:\Users\FullDirectoryPathHERE\>"];\), 
      " "}], "\[IndentingNewLine]", \(a = 
        ReadList["\<FILE_NAME_HERE.txt\>", 
          Table[Number, {matx}]];\), "\[IndentingNewLine]", \(ListPlot3D[a, 
      ColorFunction \[Rule] Hue, 
      PlotRange -> {{0, matx}, {0, maty}, {0, 60000}}, 
      ViewPoint \[Rule] {1, 1,  .2}]; mag = Max[a]; 
    Print["\<Peak value (ADU):  \>", mag]\)}], "Input"],

Cell[BoxData[
    \(\(\(\[IndentingNewLine]\)\( (*\ 
      Routine\ 2\  - \ 
        Interact\ with\ the\ \(data : 
            final\ graph\ production\ and\ view\)\ *) \)\(\[IndentingNewLine]\
\)\)\)], "Input"],

Cell[BoxData[
    \(\(\(\[IndentingNewLine]\)\( (*\ 
      The\ following\ routine\ will\ have\ to\ be\ adjusted\ until\ we\ have\ \
a\ "\<nice looking\>"\ 3  D\ plot\ of\ the\ Polaris\ path; 
      the\ data\ will\ be\ chopped\ \((at\ this\ moment\ only\ for\ \
viewing)\)\ at\ half - maximum\ \((mag/2)\); 
      we\ usually\ guess\ a\ tighter\ SQUARE\ \((x1, x2)\) - \((y1, 
            y2)\)\ matrix\ from\ the\ plot\ above, 
      eventually\ enlarging\ \(it : trial\ an\ error\); in\ the\ end, 
      changing\ the\ viewpoint\ might\ be\ necessary\ too\ *) \)\(\
\[IndentingNewLine]\)\)\)], "Input"],

Cell[BoxData[
    \(m = N[IntegerPart[mag/2]]; 
    ListPlot3D[a, ColorFunction \[Rule] Hue, 
      PlotRange -> {{375, 525}, {250, 400}, {0, m}}, 
      ViewPoint \[Rule] { .4, 4, 2}];\)], "Input"],

Cell[BoxData[
    \(\(\(\[IndentingNewLine]\)\( (*\ 
      Routine\ 3\  - \ 
        The\ main\ data\ reduction\ routine\ \
*) \)\(\[IndentingNewLine]\)\)\)], "Input"],

Cell[BoxData[
    \(\(\(\[IndentingNewLine]\)\( (*\(\(\(\(\ \)\(After\ the\ final\ values\ \
x1 - x2, \ y1 - y2\ are\ entered, \ a\ second\ viewing\ of\ the\ data, \ 
              this\ time\ from\ the\ top, \ 
              is\ \(shown : \ 
                  this\ will\ be\ the\ standard\ view\ in\ all\ that\ follows\
\); \ and\ it\ is\ also\ here\ that\ the\ user\ must\ \(indicate : \ 
                  i\)\)\()\)\)\ the\ rough\ main\ direction\ of\ Polaris; \ 
          ii\)\()\)\)\ the\ coordinates\ of\ the\ extremes\ of\ the\ path; \ 
      this\ can\ be\ just\ an\ estimate, \ 
      since\ there\ will\ be\ a\ chance\ to\ refine\ this\ later; \ 
      all\ values\ are\ printed\ out\ as\ a\ reminder\ for\ later\ *) \)\(\
\[IndentingNewLine]\)\)\)], "Input"],

Cell[BoxData[{
    \(nx1 = Input["\<x1?\>"]; nx2 = Input["\<x2?\>"]; ny1 = Input["\<y1?\>"]; 
    ny2 = Input["\<y2?\>"]; \ 
    Print["\<The chosen working square goes from \>", nx1, "\< to \>", 
      nx2, "\< in the x-axis and from \>", ny1, "\< to \>", 
      ny2, "\< in the y-axis\>"];\), "\[IndentingNewLine]", 
    \(pp2 = 
      ListPlot3D[a, ColorFunction \[Rule] GrayLevel, 
        PlotRange -> {{nx1, nx2}, {ny1, ny2}, {N[m] - 1, N[m]}}, 
        ViewPoint \[Rule] {0, 0, 2}]; 
    anspath = 
      Input["\<Judging by eye, which main direction follows the Polaris path? \
Left-right (1) or Top-down (2)?\>"]; \ 
    If\ [anspath \[Equal] 1, 
      u = Input["\<Please indicate the x-coordinate of the left extremity of \
the path\>"]; 
      t = Input["\<Please indicate the y-coordinate of the left extremity of \
the path\>"]; 
      w = Input["\<Please indicate the x-coordinate of the right extremity of \
the path\>"]; 
      v = Input["\<Please indicate the y-coordinate of the right extremity of \
the path\>"]; 
      Print["\<You have picked the path from (\>", u, "\<,\>", 
        t, "\<) to (\>", w, "\<,\>", v, "\<).\>"], 
      u = Input["\<Please indicate the x-coordinate of the bottom extremity \
of the path\>"]; 
      t = Input["\<Please indicate the y-coordinate of the bottom extremity \
of the path\>"]; 
      w = Input["\<Please indicate the x-coordinate of the top extremity of \
the path\>"]; 
      v = Input["\<Please indicate the y-coordinate of the top extremity of \
the path\>"]; 
      Print["\<You have picked the path from (\>", u, "\<,\>", 
        t, "\<) to (\>", w, "\<,\>", v, "\<).\>"]]; \ 
    pathpoints = {{u, t}, {w, v}};\)}], "Input"],

Cell[BoxData[
    \(\(\(\[IndentingNewLine]\)\( (*\ 
      The\ following\ routine\ will\ choose\ the\ best\ axis\ \((xx\ or\ \
yy)\)\ to\ define\ a\ path\ in\ our\ chopped\ \(data : \ 
          the\ one\ making\ the\ smallest\ angle\); \ 
      it\ uses\ the\ user - entered\ path\ extremes; \ 
      this\ will\ also\ be\ corrected\ later, \ 
      if/when\ the\ path\ extreme\ points\ are\ changed; \ here, \ 
      it\ calculates\ the\ angle\ with\ each\ of\ the\ xx\ and\ yy\ axis, 
      determining\ for\ which\ this\ is\ smallest\ \((no = 
            1\ is\ small\ with\ yy; \ 
          otherwise\ inver = 1\ \((small\ with\ xx)\))\); \ 
      we\ also\ make\ sure\ that\ we\ always\ have\ t \[LessEqual] 
        v\ and\ u \[LessEqual] w\ *) \)\(\[IndentingNewLine]\)\)\)], "Input"],

Cell[BoxData[{
    \(\(b = Transpose[a];\)\ \), "\[IndentingNewLine]", 
    \(angyy = 0; angxx = 0; no = 0; inver = 0; 
    angyy = ArcTan[Abs[u - w]/Abs[t - v]]; angxx = Pi/2 - angyy; 
    If[angxx > angyy, no = 1; If[t > v, alter = t; t = v; v = alter]; 
      If[u > w, alter = u; u = w; w = alter], inver = 1; 
      If[t > v, alter = t; t = v; v = alter]; 
      If[u > w, alter = u; u = w; w = alter]];\)}], "Input"],

Cell[BoxData[
    \(\(\(\[IndentingNewLine]\)\( (*\(\(\(\(\(\(\ \)\(Now\ this\ routine\ \
\((actually\ split\ into\ two, \ 
                      depending\ on\ the\ axis\ chosen)\)\ will\ i\)\()\)\)\ \
if\ the\ smallest\ angle\ is\ with\ the\ yy\ axis\ \((no = 1)\), \ 
              start\ with\ the\ top\ row\ \((of\ the\ top - 
                      left\ pixel\ identified)\)\ and\ redefine\ the\ value\ \
of\ every\ pixel\  \[GreaterEqual] m\ to\ m\ \((new\ vector\ d)\), \ 
              while\ all\ others\ will\ be\ set\ to\ zero; \ 
              at\ the\ same\ time, \ vector\ e\ is\ inititated, \ 
              having\ as\ first\ point\ the\ midpoint\ \((or\ the\ one\ on\ \
its\ left, \ if\ even\ number\ of\ pixels)\)\ of\ the\ top\ row; \ 
              note\ that\ if\ this\ is\ not\ continuous, \ 
              the\ midpoint\ is\ defined\ by\ the\ set\ of\ points\ where\ \
the\ top - left\ pixel\ is\ located; \ 
              ii\)\()\)\)\ the\ routine\ proceeds\ to\ the\ next\ row, \ 
          and\ so\ on, \ 
          stopping\ after\ concluding\ the\ row\ of\ the\ bottom - 
            right\ pixel\ above\ identified; \ at\ each\ row\ n, \ 
          the\ nth\ pixel\ of\ vector\ e\ is\ defined, \ 
          making\ sure\ it\ will\ become\ a\ continuous\ vector\ by\ the\ \
following\ \(rule : \ 
              the\ pixel\ in\ row\ n\ is\ the\ one\ closest\ to\ the\ pixel\ \
of\ row\ n - 1\ AND\ to\ the\ midpoint\ in\ row\ n\), \ 
          as\ defined\ in\ i\ \((hence, \ 
              each\ pixel\ of\ vector\ e\ is\ chosen\ among\ three\ \
possibilities)\); \ 
          iii\)\()\)\)\ if\ the\ smallest\ angle\ is\ with\ the\ xx\ axis\ \
\((inver = 1)\), \ do\ the\ same\ but, \ this\ time, \ column\ by\ column, \ 
      from\ the\ pixel\ at\ top - left\ to\ the\ pixel\ at\ bottom - 
        right\ \((vectors\ d\ and\ e\ are\ defined, \ 
            similarly)\)\ \ *) \)\)\)], "Input"],

Cell[BoxData[""], "Input"],

Cell[BoxData[
    \( (*\ 1  st\ the\ \ subroutine\ for\ the\ case\ in\ which\ the\ yy - 
        axis\ is\ chosen\ \((no = 1)\); \ 
      several\ plots\ in\ the\ end\ [graph\ plotting\ included]*) \)], "Input"],

Cell[BoxData[
    \(\(\(\[IndentingNewLine]\)\(If[no == 1, x = u; x1 = u; y = t; d = {}; 
      e = {}; midp = 0; soma = 0; midpoint = {}; i = 0; jset = 0; 
      While[y <= v, 
        While[x < matx + 1, 
          While[b[\([x, y]\)] >= m, d = Append[d, {x, y}]; soma = soma + x; 
            x = x + 1; i = i + 1]; 
          If[i \[NotEqual] 0, jset = jset + 1; \ midp = IntegerPart[soma/i]; 
            midpoint = Append[midpoint, midp]; midp = 0; i = 0; soma = 0, 
            x = x + 1]]; x2 = 0; k = 1; dist1 = 0; dist = matx*2; 
        While[k \[LessEqual] jset, dist1 = Abs[midpoint[\([k]\)] - x1]; \ 
          If[dist1 < dist, x2 = midpoint[\([k]\)]; dist = dist1]; k = k + 1]; 
        If[x1 \[NotEqual] u\  || \ y \[NotEqual] t, \ \ \ While[
            x2 < x1 - 1\  && \ b[\([x2 + 1, y]\)] \[GreaterEqual] m, 
            x2 = x2 + 1]; \ 
          While[x2 > x1 + 1\  && \ b[\([x2 - 1, y]\)] \[GreaterEqual] m, 
            x2 = x2 - 1]; \ e = Append[e, {x2, y}]; x1 = x2, 
          e = Append[e, {x2, y}]; x1 = x2]; y = y + 1; x = 1; i = 0; 
        jset = 0; midpoint = {}]]; 
    e1 = ListPlot[d, PlotRange \[Rule] All, PlotStyle -> PointSize[0.013], 
        Frame -> True, Axes -> False, AspectRatio \[Rule] Automatic]; 
    e2 = ListPlot[e, PlotRange \[Rule] All, 
        PlotStyle \[Rule] RGBColor[1, 0, 0], PlotStyle -> PointSize[0.013], 
        Frame -> True, Axes -> False, 
        AspectRatio \[Rule] Automatic];\[IndentingNewLine]
    le = Length[e]; Print\ ["\<Vector e has  \>", le, \ "\< points\>"]; 
    e\)\)\)], "Input"],

Cell[BoxData[
    \(\(\(\[IndentingNewLine]\)\( (*\ 
      2  nd\ the\ \ subroutine\ for\ the\ case\ in\ which\ the\ xx - 
        axis\ is\ chosen\ \((inver = 1)\); \ 
      several\ plots\ in\ the\ end\ [
          graph\ plotting\ not\ included]\ *) \)\(\[IndentingNewLine]\)\)\)], \
"Input"],

Cell[BoxData[
    \(\(\(\[IndentingNewLine]\)\(\(y = t; y1 = t; x = u; d = {}; e = {}; 
    midp = 0; soma = 0; midpoint = {}; i = 0; jset = 0; 
    While[x \[LessEqual] w, 
      While[y < maty + 1, 
        While[b[\([x, y]\)] >= m, d = Append[d, {x, y}]; soma = soma + y; 
          y = y + 1; i = i + 1]; 
        If[i \[NotEqual] 0, jset = jset + 1; \ midp = IntegerPart[soma/i]; 
          midpoint = Append[midpoint, midp]; midp = 0; i = 0; soma = 0, 
          y = y + 1]]; y2 = 0; k = 1; dist1 = 0; dist = maty*2; 
      While[k \[LessEqual] jset, dist1 = Abs[midpoint[\([k]\)] - y1]; \ 
        If[dist1 < dist, y2 = midpoint[\([k]\)]; dist = dist1]; k = k + 1]; 
      If[y1 \[NotEqual] t\  || \ x \[NotEqual] u, \ \ \ While[
          y2 < y1 - 1\  && \ b[\([x, y2 + 1]\)] \[GreaterEqual] m, 
          y2 = y2 + 1]; \ 
        While[y2 > y1 + 1\  && \ b[\([x, y2 - 1]\)] \[GreaterEqual] m, 
          y2 = y2 - 1]; \ e = Append[e, {x, y2}]; y1 = y2, 
        e = Append[e, {x, y2}]; y1 = y2]; x = x + 1; y = 1; i = 0; jset = 0; 
      midpoint = {}]\)\(\[IndentingNewLine]\)
    \)\)\)], "Input"],

Cell[BoxData[
    \(\(\(\[IndentingNewLine]\)\( (*\ Finally, \ the\ iteration\ routine, \ 
      showing\ more\ plots\ \((namely\ the\ Polaris\ path\ in\ red)\)\ and\ \
allowing\ to\ better\ refine\ the\ initial\ and\ final\ points\ for\ the\ \
path\ *) \)\(\[IndentingNewLine]\)\)\)], "Input"],

Cell[BoxData[
    \(pp1 = 
      ListPlot[pathpoints, PlotRange \[Rule] All, 
        PlotStyle \[Rule] RGBColor[1, 0, 0], PlotStyle -> PointSize[0.013], 
        Frame -> True, Axes -> False, AspectRatio \[Rule] Automatic]; 
    Show[e1, pp1]; Show[pp2]; Show[e1, e2]; anshere = 1; 
    anshere = 
      Input["\<Please confirm (1) that the path extremes are correct (red \
points), comparing with the original plot. If not, write 0.\>"]; \ 
    While[anshere \[Equal] 0, 
      If[anspath \[Equal] 1, 
        u = Input["\<Please indicate the x-coordinate of the left extremity \
of the path\>"]; 
        t = Input["\<Please indicate the y-coordinate of the left extremity \
of the path\>"]; 
        w = Input["\<Please indicate the x-coordinate of the right extremity \
of the path\>"]; 
        v = Input["\<Please indicate the y-coordinate of the right extremity \
of the path\>"]; 
        Print["\<You have picked the path from (\>", u, "\<,\>", 
          t, "\<) to (\>", w, "\<,\>", v, "\<).\>"], 
        u = Input["\<Please indicate the x-coordinate of the bottom extremity \
of the path\>"]; 
        t = Input["\<Please indicate the y-coordinate of the bottom extremity \
of the path\>"]; 
        w = Input["\<Please indicate the x-coordinate of the top extremity of \
the path\>"]; 
        v = Input["\<Please indicate the y-coordinate of the top extremity of \
the path\>"]; 
        Print["\<You have picked the path from (\>", u, "\<,\>", 
          t, "\<) to (\>", w, "\<,\>", v, "\<).\>"]]; \ 
      pathpoints = {{u, t}, {w, v}}; angyy = 0; angxx = 0; no = 0; inver = 0; 
      angyy = ArcTan[Abs[u - w]/Abs[t - v]]; angxx = Pi/2 - angyy; 
      If[angxx > angyy, no = 1; If[t > v, alter = t; t = v; v = alter]; 
        If[u > w, alter = u; u = w; w = alter], inver = 1; 
        If[t > v, alter = t; t = v; v = alter]; 
        If[u > w, alter = u; u = w; w = alter]]; \[IndentingNewLine]If[
        no == 1, x = u; x1 = u; y = t; d = {}; e = {}; midp = 0; soma = 0; 
        midpoint = {}; i = 0; jset = 0; 
        While[y <= v, 
          While[x < matx + 1, 
            While[b[\([x, y]\)] >= m, d = Append[d, {x, y}]; soma = soma + x; 
              x = x + 1; i = i + 1]; 
            If[i \[NotEqual] 0, jset = jset + 1; \ 
              midp = IntegerPart[soma/i]; midpoint = Append[midpoint, midp]; 
              midp = 0; i = 0; soma = 0, x = x + 1]]; x2 = 0; k = 1; 
          dist1 = 0; dist = matx*2; 
          While[k \[LessEqual] jset, dist1 = Abs[midpoint[\([k]\)] - x1]; \ 
            If[dist1 < dist, x2 = midpoint[\([k]\)]; dist = dist1]; 
            k = k + 1]; 
          If[x1 \[NotEqual] u\  || \ y \[NotEqual] t, \ \ \ While[
              x2 < x1 - 1\  && \ b[\([x2 + 1, y]\)] \[GreaterEqual] m, 
              x2 = x2 + 1]; \ 
            While[x2 > x1 + 1\  && \ b[\([x2 - 1, y]\)] \[GreaterEqual] m, 
              x2 = x2 - 1]; \ e = Append[e, {x2, y}]; x1 = x2, 
            e = Append[e, {x2, y}]; x1 = x2]; y = y + 1; x = 1; i = 0; 
          jset = 0; midpoint = {}], \[IndentingNewLine]y = t; y1 = t; x = u; 
        d = {}; e = {}; midp = 0; soma = 0; midpoint = {}; i = 0; jset = 0; 
        While[x \[LessEqual] w, 
          While[y < maty + 1, 
            While[b[\([x, y]\)] >= m, d = Append[d, {x, y}]; soma = soma + y; 
              y = y + 1; i = i + 1]; 
            If[i \[NotEqual] 0, jset = jset + 1; \ 
              midp = IntegerPart[soma/i]; midpoint = Append[midpoint, midp]; 
              midp = 0; i = 0; soma = 0, y = y + 1]]; y2 = 0; k = 1; 
          dist1 = 0; dist = maty*2; 
          While[k \[LessEqual] jset, dist1 = Abs[midpoint[\([k]\)] - y1]; \ 
            If[dist1 < dist, y2 = midpoint[\([k]\)]; dist = dist1]; 
            k = k + 1]; 
          If[y1 \[NotEqual] t\  || \ x \[NotEqual] u, \ \ \ While[
              y2 < y1 - 1\  && \ b[\([x, y2 + 1]\)] \[GreaterEqual] m, 
              y2 = y2 + 1]; \ 
            While[y2 > y1 + 1\  && \ b[\([x, y2 - 1]\)] \[GreaterEqual] m, 
              y2 = y2 - 1]; \ e = Append[e, {x, y2}]; y1 = y2, 
            e = Append[e, {x, y2}]; y1 = y2]; x = x + 1; y = 1; i = 0; 
          jset = 0; midpoint = {}]]; 
      e1 = ListPlot[d, PlotRange \[Rule] All, PlotStyle -> PointSize[0.013], 
          Frame -> True, Axes -> False, AspectRatio \[Rule] Automatic]; 
      e2 = ListPlot[e, PlotRange \[Rule] All, 
          PlotStyle \[Rule] RGBColor[1, 0, 0], PlotStyle -> PointSize[0.013], 
          Frame -> True, Axes -> False, 
          AspectRatio \[Rule] Automatic]; \[IndentingNewLine]le = Length[e]; 
      Print\ ["\<Vector e has  \>", le, \ "\< points\>"]; e; 
      pp1 = ListPlot[pathpoints, PlotRange \[Rule] All, 
          PlotStyle \[Rule] RGBColor[1, 0, 0], PlotStyle -> PointSize[0.013], 
          Frame -> True, Axes -> False, AspectRatio \[Rule] Automatic]; 
      Show[e1, pp1]; Show[pp2]; Show[e1, e2]; anshere = 1; 
      anshere = 
        Input["\<Please confirm (1) that the path extremes are correct (red \
points), comparing with the original plot. If not, write 0.\>"]]; \ 
    Print\ ["\<Vector e has  \>", le, \ "\< points\>"]; e\)], "Input"],

Cell[BoxData[
    \(\(\(\[IndentingNewLine]\)\(\[IndentingNewLine]\)\( (*\ 
      Routine\ 4\ *) \)\(\[IndentingNewLine]\)\( (*\ 
      ONLY\ if\ \(necessary : \ comment\ out\); \ 
      note\ that\ points\ of\ the\ form\ \((0, y)\)\ or\ \((x, 
          0)\)\ will\ have\ to\ be\ removed\ from\ vector\ e\ *) \)\(\
\[IndentingNewLine]\)\(\[IndentingNewLine]\)\)\)], "Input"],

Cell[BoxData[
    \( (*\ e = {}; 
      e2 = ListPlot[e, PlotRange \[Rule] All, PlotStyle -> PointSize[0.013], 
          Frame -> True, Axes -> False, AspectRatio \[Rule] Automatic]\ ; 
      le = Length[e]; Print\ ["\<Vector e has  \>", le, \ "\< points\>"]; \ 
      e\ *) \)], "Input"],

Cell[BoxData[
    \(\(\(\[IndentingNewLine]\)\( (*\ 
      Now\ begins\ the\ SEEING\ DETERMINATION\ section\ *) \)\(\
\[IndentingNewLine]\)\)\)], "Input"],

Cell[BoxData[
    \(\(\(\[IndentingNewLine]\)\(\[IndentingNewLine]\)\(\[IndentingNewLine]\)\
\( (*\ Routine\ 5\ \((seeing)\)\ *) \)\(\[IndentingNewLine]\)\(\
\[IndentingNewLine]\)\(\[IndentingNewLine]\)\)\)], "Input"],

Cell[BoxData[
    \(\(\(\[IndentingNewLine]\)\(\[IndentingNewLine]\)\( (*\ 
      Routine\ 5.1\ \((Circle\ Fitting)\)\ *) \)\(\[IndentingNewLine]\)\( (*\ 
      The\ 1  st\ attempt\ \((default)\)\ will\ be\ with\ Circle\ Fitting\ \
*) \)\( (*\(\(\ \)\(The\ first\ step\ is\ to\ use\ information\ we\ \(know : 
              the\ radius\ of\ the\ circle\); 
          this\ is\ the\ exact\ distance\ of\ Polaris\ to\ the\ NCP, 
          coming\ from\ the\ exact\ declination\ when\ we\ observed\ Polaris \
. Using\ the\ tables\ in\ cadastral . com\ we\ have\ for\ 1  
            st\ Feb\ 2008\ a\ declination\ of\ 89  \.ba18' 
            24.2 "\< and for 5th Mar 2009 89\.ba18'37.2\>" . 
              The\ difference\ of\ 13 "\< is irrelevant, giving an error of \
0.5% in the radius, so we pick the mean distance to the NCP (2489.3\>"\)\()\)\
\)\ which, 
      divided\ by\ the\ pixel\ size\ of\ 0.77 \*"\"\< gives 3233 pixels \
(rounding to units). \>"*) \)\(\[IndentingNewLine]\)\(\[IndentingNewLine]\)\( \
(*\ Recalling\ that\ vector\ e\ contains\ the\ data\ in\ the\ format\ we\ \
need, \((x, y)\)\ coordinates\ of\ the\ Polaris\ path, 
      we\ then\ fit\ these\ data\ as\ follows\ *) \)\(\[IndentingNewLine]\)\(\
\[IndentingNewLine]\)\( (*It\ is\ important\ to\ modify\ the\ values\ in\ \
UnitStep, 
      since\ these\ focus\ the\ fit\ in\ the\ arc\ section\ where\ the\ data\ \
points\ lie, which\ is\ a\ very\ small\ section\ of\ the\ full\ circle; 
      we\ use\ the\ \((x1, x2)\), \((y1, 
          y2)\)\ limits\ used\ for\ the\ graphs\ above; 
      note\ that\ there\ will\ actually\ be\ two\ fits\ \((one\ with\ the' + \
\(\('\)\(\ \)\(sign\)\); the\ other\ with\ the' - \(\('\)\(\ \)\(sign\)\))\); 
      sometimes\ one\ fails\ completely\ but, more\ typically, 
      both\ are\ usually\ similar; the\ graphs\ should\ be\ analysed, 
      to\ make\ sure\ the\ default' + \(\('\)\(\ \)\(is\)\(\ \)\(always\)\(\ \
\)\(the\)\(\ \)\(best\)\(\ \)\(fit\)\)\ *) \)\(\[IndentingNewLine]\)\(\
\[IndentingNewLine]\)\)\)], "Input"],

Cell[BoxData[
    \(circfit1 = 
      FindFit[e, 
        atr + Sqrt[
            3233^2 - \((UnitStep[r - nx1]*UnitStep[nx2 - r]*r - 
                    btr)\)^2], {atr, btr}, r, MaxIterations \[Rule] 50000, 
        Method -> LevenbergMarquardt]; atrstr = ToString[circfit1[\([1]\)]]; 
    btrstr = ToString[circfit1[\([2]\)]]; latrstr = StringLength[atrstr]; 
    lbtrstr = StringLength[btrstr]; 
    atr2 = ToExpression[StringTake[atrstr, {8, latrstr}]]; 
    btr2 = ToExpression[StringTake[btrstr, {8, lbtrstr}]]; 
    circfit2 = 
      FindFit[e, 
        atm - Sqrt[
            3233^2 - \((UnitStep[r - nx1]*UnitStep[nx2 - r]*r - 
                    btm)\)^2], {atm, btm}, r, MaxIterations \[Rule] 50000, 
        Method -> LevenbergMarquardt]; atmstr = ToString[circfit2[\([1]\)]]; 
    btmstr = ToString[circfit2[\([2]\)]]; latmstr = StringLength[atmstr]; 
    lbtmstr = StringLength[btmstr]; 
    atm2 = ToExpression[StringTake[atmstr, {8, latmstr}]]; 
    btm2 = ToExpression[StringTake[btmstr, {8, lbtmstr}]];\)], "Input"],

Cell[BoxData[
    \(\(\(\[IndentingNewLine]\)\( (*\ 
      We\ now\ build\ each\ graph\ fit\ *) \)\(\[IndentingNewLine]\)\)\)], \
"Input"],

Cell[BoxData[
    \(e10 = 
      Plot[atr2 + Sqrt[3233^2 - \((r - btr2)\)^2], {r, nx1, nx2}, 
        PlotStyle \[Rule] {RGBColor[0, 1, 0], AbsoluteThickness[2]}]; 
    e20 = Plot[atm2 - Sqrt[3233^2 - \((r - btm2)\)^2], {r, nx1, nx2}, 
        PlotStyle \[Rule] {RGBColor[0, 1, 0], AbsoluteThickness[2]}]; 
    Show[e10, e1]; Show[e10, e2]; Show[e20, e1]\ ; 
    Show[e20, e2]\ ;\)], "Input"],

Cell[BoxData[
    \(\(\(\[IndentingNewLine]\)\( (*\ Finally, 
      we\ get\ the\ seeing\ calculations\ for\ the\ circle\ fit; 
      we\ pick\ the\ best\ from\ the\ two\ above\ \
*) \)\(\[IndentingNewLine]\)\)\)], "Input"],

Cell[BoxData[
    \(ans = 
      Input["\<After careful eye inspection of both plots, including \
curvature analysis, which one is better? If the 1st one (or the two cannot be \
distinguished), please place '1' below (only the number). If the 2nd one, \
please place '2'.\>"]; 
    If[ans \[Equal] 2, at = atm2; bt = btm2; sinal = \(-1\), at = atr2; 
      bt = btr2; sinal = 1];\)], "Input"],

Cell[BoxData[
    \(\(\(\[IndentingNewLine]\)\( (*\ 
      We\ use\ the\ distance\ formula\ by\ constructing\ vector\ distancirc\ \
*) \)\(\[IndentingNewLine]\)\)\)], "Input"],

Cell[BoxData[
    \(l = 1; n2 = Length[e]; distancirc = {}; somasqr = 0; 
    While[l \[LessEqual] n2, 
      sdf = Minimize[
          Sqrt[\((ttt - e[\([l, 1]\)])\)^2 + \((at + 
                    sinal*Sqrt[Abs[3233^2 - \((ttt - bt)\)^2]] - 
                    e[\([l, 2]\)])\)^2], ttt]; 
      distancirc = Append[distancirc, sdf[\([1]\)]]; 
      somasqr = somasqr + sdf[\([1]\)]^2; l = l + 1]; 
    seeingPh = Median[distancirc]; seeingVis = 2*seeingPh;\)], "Input"],

Cell[BoxData[
    \(\(\(\[IndentingNewLine]\)\( (*\ As\ you\ can\ see\ above, 
      the\ routine\ just\ calculated\ the\ photographic\ and\ visual\ seeing\ \
values\ from\ the\ mean; however, 
      we\ will\ print\ out\ \((and\ consider)\)\ only\ the\ photographic\ R . 
          M . S . value, 
      which\ we\ calculate\ \((and\ print\ out)\)\ next\ *) \)\(\
\[IndentingNewLine]\)\)\)], "Input"],

Cell[BoxData[
    \(seeingPh2 = Sqrt[somasqr/n2]; \ seeingVis2 = 2*seeingPh2; 
    sddev = seeingVis2*0.77/
          206265; \ \ Print["\<CCD Seeing (R.M.S.) - CIRCLE FIT in arcseconds \
=  \>", seeingPh2*0.77\ ];\)], "Input"],

Cell[BoxData[
    \(\(\(\[IndentingNewLine]\)\(\[IndentingNewLine]\)\(\[IndentingNewLine]\)\
\( (*\ Routine\ 5.2\ \((Parabole\ Fitting)\)\ *) \)\(\[IndentingNewLine]\)\(\
\[IndentingNewLine]\)\)\)], "Input"],

Cell[BoxData[
    \(\(\(\[IndentingNewLine]\)\( (*\ For\ comparison, 
      we\ have\ actually\ done\ a\ Parabole\ Fit\ too\ \((when\ possible)\), 
      following\ Moroder\ and\ Righini\ 1973; of\ course, 
      such\ a\ fit\ is\ the\ only\ one\ available\ when\ Circle\ Fitting\ \
fails\ *) \)\(\n\)\(\[IndentingNewLine]\)\( (*\ 
      The\ parabole\ fit\ is\ simple\ but\ not\ too\ physical; and, 
      sometimes, \(the\ graphs\ are\ just\ stupid\ since\ we\ know\ Polaris\ \
is\ not\ following\ the\ full\ path\ that\ we\ see ... \)\ It\ is, thus, 
      a\ good\ idea\ to\ always\ get\ the\ alternative\ and\ more\ physical\ \
fit\ of\ a\ circle, 
      since\ this\ is\ the\ actual\ motion\ of\ Polaris\ *) \)\(\
\[IndentingNewLine]\)\)\)], "Input"],

Cell[BoxData[{
    \(parabfit1 = FindFit[e, a3 + a2\ xp + a1\ xp^2, \ {a3, a2, a1}, xp]; 
    a3str = ToString[parabfit1[\([1]\)]]; 
    a2str = ToString[parabfit1[\([2]\)]]; 
    a1str = ToString[parabfit1[\([3]\)]]; la3str = StringLength[a3str]; 
    la2str = StringLength[a2str]; la1str = StringLength[a1str]; 
    aa3 = ToExpression[
        StringTake[a3str, {7, la3str}]];\), "\[IndentingNewLine]", 
    \(aa2 = ToExpression[StringTake[a2str, {7, la2str}]]; 
    aa1 = ToExpression[
        StringTake[a1str, {7, la1str}]];\), "\[IndentingNewLine]", 
    \(e3 = 
      Plot[aa3 + aa2\ xrt + aa1\ xrt^2, {xrt, nx1, nx2}, 
        PlotStyle \[Rule] {RGBColor[0, 1, 0], AbsoluteThickness[2]}]; 
    Show[e3, e1]; Show[e3, e2]; l = 1; n2 = Length[e]; distancia = {}; 
    somasqr = 0; 
    While[l \[LessEqual] n2, 
      sdf = Minimize[
          Sqrt[\((tpy - e[\([l, 1]\)])\)^2 + \((aa3 + aa2\ tpy + aa1\ tpy^2 - 
                    e[\([l, 2]\)])\)^2], tpy]; 
      distancia = Append[distancia, sdf[\([1]\)]]; 
      somasqr = somasqr + sdf[\([1]\)]^2; l = l + 1]; 
    seeingPh = Median[distancia]; seeingVis = 2*seeingPh; 
    seeingPh2 = Sqrt[somasqr/n2]; \ seeingVis2 = 2*seeingPh2; 
    sddev = seeingVis2*0.77/206265; \ 
    Print["\<CCD Seeing (R.M.S.) - PARABOLE FIT in arcseconds =  \>", 
      seeingPh2*0.77\ ];\)}], "Input"],

Cell[BoxData[
    \(\(\(\[IndentingNewLine]\)\(\[IndentingNewLine]\)\( (*\ 
      Routine\ 5.3\ \(("\<Horizontal\>"\ Fitting)\)\ *) \)\(\
\[IndentingNewLine]\)\(\[IndentingNewLine]\)\)\)], "Input"],

Cell[BoxData[
    \(\(\( (*\ 
      The\ 3  rd\ attempt, \(when\ Circle &\)\ Parabole\ fitting\ both\ fail, 
      is\ the\ "\<Horizontal Method\>"\ *) \)\(\[IndentingNewLine]\)\(\
\[IndentingNewLine]\)\( (*\ 
      In\ the\ rare\ situation\ that\ the\ values\ of\ seeing\ are\ ridiculus\
\ \((usually\ because\ the\ fitted\ arc\ of\ circle\ is\ too\ vertical)\)\ \
then\ run\ the\ following\ routine\ that\ calculates\ the\ horizontal\ \
distance\ of\ each\ point\ of\ vector\ h\ to\ the\ arc\ of\ circle\ *) \)\(\
\[IndentingNewLine]\)\)\)], "Input"],

Cell[BoxData[
    \(l = 1; n2 = Length[e]; distancirc = {}; xarcocirculo = {}; valor = 0; 
    sdf = 0; somasqr = 0; 
    ArcoCirculo = at + sinal*Sqrt[3233^2 - \((ty - bt)\)^2]; 
    While[l \[LessEqual] n2, 
      xarcocirculo = Solve[ArcoCirculo \[Equal] e[\([l, 2]\)], ty]; 
      valor = ty\  /. \ xarcocirculo[\([1]\)]; 
      sdf = Abs[valor - e[\([l, 1]\)]]; distancirc = Append[distancirc, sdf]; 
      somasqr = somasqr + sdf^2; l = l + 1; xarcocirculo = {}; sdf = 0; 
      valor = 0]; seeingPh = Median[distancirc]; seeingVis = 2*seeingPh; 
    seeingPh2 = Sqrt[somasqr/n2]; \ seeingVis2 = 2*seeingPh2; 
    sddev = seeingVis2*0.77/
          206265; \ \ Print["\<CCD Seeing (R.M.S.) - HORIZONTAL FIT in \
arcseconds =  \>", seeingPh2*0.77\ ];\)], "Input"]
},
FrontEndVersion->"5.0 for Microsoft Windows",
ScreenRectangle->{{0, 1920}, {0, 982}},
WindowSize->{851, 880},
WindowMargins->{{62, Automatic}, {Automatic, 17}}
]

(*******************************************************************
Cached data follows.  If you edit this Notebook file directly, not
using Mathematica, you must remove the line containing CacheID at
the top of  the file.  The cache data will then be recreated when
you save this file from within Mathematica.
*******************************************************************)

(*CellTagsOutline
CellTagsIndex->{}
*)

(*CellTagsIndex
CellTagsIndex->{}
*)

(*NotebookFileOutline
Notebook[{
Cell[1754, 51, 273, 5, 150, "Input"],
Cell[2030, 58, 355, 7, 70, "Input"],
Cell[2388, 67, 158, 4, 70, "Input"],
Cell[2549, 73, 187, 3, 50, "Input"],
Cell[2739, 78, 409, 8, 90, "Input"],
Cell[3151, 88, 797, 14, 210, "Input"],
Cell[3951, 104, 902, 21, 150, "Input"],
Cell[4856, 127, 212, 5, 70, "Input"],
Cell[5071, 134, 605, 10, 130, "Input"],
Cell[5679, 146, 198, 4, 50, "Input"],
Cell[5880, 152, 167, 4, 70, "Input"],
Cell[6050, 158, 772, 12, 150, "Input"],
Cell[6825, 172, 1695, 34, 370, "Input"],
Cell[8523, 208, 794, 13, 170, "Input"],
Cell[9320, 223, 422, 7, 90, "Input"],
Cell[9745, 232, 1924, 31, 370, "Input"],
Cell[11672, 265, 26, 0, 30, "Input"],
Cell[11701, 267, 211, 3, 50, "Input"],
Cell[11915, 272, 1562, 26, 310, "Input"],
Cell[13480, 300, 295, 6, 90, "Input"],
Cell[13778, 308, 1109, 19, 210, "Input"],
Cell[14890, 329, 294, 4, 110, "Input"],
Cell[15187, 335, 5123, 90, 990, "Input"],
Cell[20313, 427, 375, 6, 150, "Input"],
Cell[20691, 435, 289, 5, 50, "Input"],
Cell[20983, 442, 154, 3, 70, "Input"],
Cell[21140, 447, 217, 3, 150, "Input"],
Cell[21360, 452, 2043, 31, 490, "Input"],
Cell[23406, 485, 1037, 20, 190, "Input"],
Cell[24446, 507, 138, 3, 70, "Input"],
Cell[24587, 512, 392, 7, 110, "Input"],
Cell[24982, 521, 223, 4, 70, "Input"],
Cell[25208, 527, 392, 7, 110, "Input"],
Cell[25603, 536, 175, 3, 70, "Input"],
Cell[25781, 541, 474, 9, 130, "Input"],
Cell[26258, 552, 401, 7, 110, "Input"],
Cell[26662, 561, 227, 4, 50, "Input"],
Cell[26892, 567, 207, 3, 130, "Input"],
Cell[27102, 572, 756, 12, 210, "Input"],
Cell[27861, 586, 1344, 26, 250, "Input"],
Cell[29208, 614, 197, 3, 110, "Input"],
Cell[29408, 619, 552, 9, 130, "Input"],
Cell[29963, 630, 768, 13, 150, "Input"]
}
]
*)



(*******************************************************************
End of Mathematica Notebook file.
*******************************************************************)

