Advertisement
xerpi

LOL AT THIS

Sep 1st, 2011
299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 266.28 KB | None | 0 0
  1. --SATFINDER PORTABLE Version 1.3 - Español
  2.  
  3. -- Ready to be used with LuaPlayerHM6.5
  4.  
  5. --Satfinder has been created by:
  6. --Pedro J. Fernández - [email protected]
  7. --Guillermo Fernández - [email protected]
  8.  
  9. --Special thanks to:
  10. --Razorblade - [email protected]  
  11. --Dr. T.S.Kelso - [email protected]
  12. --John Walker (Home Planet) - http://www.fourmilab.ch/
  13. --John A. Magliacane (Predict) - http://www.qsl.net/kd2db/
  14.  
  15. function screen.height()
  16.     return 272
  17. end
  18. function screen.width()
  19.     return 480
  20. end
  21.  
  22. function SF_initialize()   
  23.     -- Inicialiamos algunos colores
  24.     sf_color_white = color.new(255,255,255)
  25.     sf_color_black = color.new(0,0,0)
  26.     sf_color_red = color.new (255,0,0)
  27.     sf_palette = {}
  28.     sf_star_palette = {}
  29.     sf_palette_len = 16
  30.    
  31.     sf_color_day = {}
  32.     sf_color_night = {}
  33.  
  34.    
  35.     sf_color_day.r = 0
  36.     sf_color_day.g = 255
  37.     sf_color_day.b = 0
  38.    
  39.     sf_color_night.r = 255
  40.     sf_color_night.g = 0
  41.     sf_color_night.b = 0   
  42.    
  43.     SF_changePalette (sf_color_day.r,sf_color_day.g,sf_color_day.b)
  44.     SF_crearPaletaEstrellas()
  45.  
  46.     sf_selected_sat = 0
  47.    
  48.     -- Borramos las dos pantallas (doble buffer)
  49.     screen.clear(sf_color_black)
  50.     screen.waitvblankstart()
  51.     screen.flip()
  52.     screen.clear(sf_color_black)
  53.     screen.waitvblankstart()
  54.     screen.flip()  
  55.    
  56.    
  57.     local file = io.open("update.txt")
  58.    
  59.     local mensaje
  60.     if file then
  61.         local line = file:read("*l")
  62.         local update_jt = tonumber(line)
  63.         if update_jt ~= nil then
  64.             local ct = os.date("*t")
  65.  
  66.             -- This functions only works in LuaPlayerHM6.5
  67.             ct.year = tonumber(os.date("%Y"))
  68.             ct.month = tonumber(os.date("%m") )
  69.             ct.day = tonumber(os.date("%d"))  
  70.             ct.hour = tonumber(os.date("%I"))
  71.             if string.lower(os.date("%p")) == "pm" then
  72.                 ct.hour = ct.hour + 12.0
  73.             end
  74.             ct.min = tonumber(os.date("%M"))
  75.             ct.sec = tonumber(os.date("%S"))
  76.  
  77.            
  78.             -- current_time.year , month, day, hour, min, sec, isdst
  79.             local jt = SGP_Date_To_Julian(ct.year, ct.month, ct.day, ct.hour, ct.min, ct.sec)
  80.             local diff = jt - update_jt
  81.             local dias_julianos = diff
  82.            
  83.             screen.print(440, 261, "v1.3", sf_palette[4])
  84.             screen.waitvblankstart()
  85.             screen.flip()
  86.             screen.print(440, 261, "v1.3", sf_palette[4])
  87.             screen.waitvblankstart()
  88.             screen.flip()
  89.  
  90.            
  91.             screen.print(1, 1, "> Comprobando edad de los ficheros TLE: "..string.format("%.2f",dias_julianos).." dias" , sf_palette[4])
  92.             screen.waitvblankstart()
  93.             screen.flip()
  94.             screen.print(1, 1, "> Comprobando edad de los ficheros TLE: "..string.format("%.2f",dias_julianos).." dias" , sf_palette[4])
  95.             screen.waitvblankstart()
  96.             screen.flip()
  97.  
  98.             if dias_julianos > 3 then
  99.                 screen.print(1, 11, "  Los arhivos TLE son demasiado viejos!" , sf_color_red)
  100.                 screen.print(1, 21, "  Actualiza con SatFinder Updater o via WIFI." , sf_color_red)                    
  101.                 screen.waitvblankstart()
  102.                 screen.flip()
  103.                 screen.print(1, 11, "  Los arhivos TLE son demasiado viejos!" , sf_color_red)
  104.                 screen.print(1, 21, "  Actualiza con SatFinder Updater o via WIFI." , sf_color_red)                    
  105.                 screen.waitvblankstart()
  106.                 screen.flip()
  107.  
  108.             else
  109.                 screen.print(1, 11, "  Los arhivos TLE son suficientemente recientes.", sf_palette[1])
  110.                 screen.print(1, 21, "  No es necesario actualizar." , sf_palette[1])
  111.                 screen.waitvblankstart()
  112.                 screen.flip()
  113.                 screen.print(1, 11, "  Los arhivos TLE son suficientemente recientes.", sf_palette[1])
  114.                 screen.print(1, 21, "  No es necesario actualizar." , sf_palette[1])
  115.                 screen.waitvblankstart()
  116.                 screen.flip()
  117.             end
  118.         end
  119.     end
  120.    
  121.  
  122.  
  123.     screen.print(1, 31, "> Comprobando asistente de voz... " , sf_palette[4])
  124.     screen.waitvblankstart()
  125.     screen.flip()
  126.     screen.print(1, 31, "> Comprobando asistente de voz... " , sf_palette[4])
  127.     screen.waitvblankstart()
  128.     screen.flip()
  129.    
  130.     --System.oaenable();
  131.     local sound_welcome = sound.load("SatFinder/welcome.wav",false)
  132.     sound_welcome:play()
  133.     screen.waitvblankstart(5)
  134.     while sound_welcome:playing() do
  135.         screen.waitvblankstart(5)
  136.     end
  137.    
  138.     screen.print(274, 31, "OK!" , sf_palette[1])
  139.     screen.waitvblankstart()
  140.     screen.flip()
  141.     screen.print(274, 31, "OK!" , sf_palette[1])
  142.     screen.waitvblankstart()
  143.     screen.flip()
  144.  
  145.     screen.print(1, 41, "> Definiendo constantes... " , sf_palette[4])
  146.     screen.waitvblankstart()
  147.     screen.flip()
  148.     screen.print(1, 41, "> Definiendo constantes... " , sf_palette[4])
  149.     screen.waitvblankstart()
  150.     screen.flip()
  151.    
  152.     -- Constantes
  153.     SF_constants()
  154.     SGP_initCoefficients()
  155.  
  156.     screen.print(216, 41, "OK!" , sf_palette[1])
  157.     screen.waitvblankstart()
  158.     screen.flip()
  159.     screen.print(216, 41, "OK!" , sf_palette[1])
  160.     screen.waitvblankstart()
  161.     screen.flip()
  162.    
  163.    
  164.     screen.print(1, 51, "> Definiendo variables... " , sf_palette[4])
  165.     screen.waitvblankstart()
  166.     screen.flip()
  167.     screen.print(1, 51, "> Definiendo variables... " , sf_palette[4])
  168.     screen.waitvblankstart()
  169.     screen.flip()
  170.    
  171.     -- Sky configuration   
  172.     sf_sky_color = color.new(0,0,0)
  173.     sf_sky = {}
  174.     sf_sky.radio = 241
  175.     sf_sky.centro = {}
  176.     sf_sky.centro.x = 241
  177.     sf_sky.centro.y = 137
  178.     sf_sky.elevacion_minima = 0
  179.     sf_sky.color = sf_color_black
  180.     sf_sky.orientacion = -1.0
  181.     sf_sky.rotacion = 0.0
  182.     sf_sky.show = {}
  183.     sf_sky.show.satellites = true
  184.     sf_sky.show.satelliteNames = false
  185.     sf_sky.show.stars = true
  186.     sf_sky.show.autoMagStars = false
  187.     sf_sky.show.constellationNames = false
  188.     sf_sky.show.sunAndMoon = true
  189.     sf_sky.show.elevationLines = true
  190.    
  191.     sf_next_visible_results = {}
  192.    
  193.     sgp_sun = {}
  194.     sgp_sun.raise = {}
  195.     sgp_sun.set = {}
  196.    
  197.     sgp_location = {}
  198.     sgp_location.polar = {}
  199.     sgp_location.day = {}
  200.     sgp_location.night = {}
  201.    
  202.     sgp_moon = {}
  203.     sgp_moon.age = {}
  204.     sgp_moon.raise = {}
  205.     sgp_moon.set = {}
  206.     local i
  207.    
  208.  
  209.     sf_pass_max = 23
  210.     sf_pass_limit = 0
  211.     sf_pass_current = 0
  212.     sf_passes = {}
  213.     for i = 1,sf_pass_max do
  214.         sf_passes[i] = {}
  215.     end
  216.    
  217.     sf_pass_path = {}
  218.     sf_pass_path.longitud = 0
  219.     sf_pass_path.max = 256
  220.      
  221.     for i = 1,sf_pass_path.max do
  222.         sf_pass_path[i] = {}
  223.     end
  224.    
  225.     sf_primera_busqueda = true
  226.     sf_next_visible_show_info = false
  227.     sf_next_visible_precision = 0.4
  228.  
  229.  
  230.     sf_primer_calculo = true
  231.    
  232.     sf_vision_nocturna = true
  233.        
  234.     sf_moving_sky = false
  235.     sf_zoom_mode = false
  236.  
  237.     sf_moon_wait = 0
  238.     sf_sun_wait = 0
  239.  
  240.     -- Carga de imagenes en memoria
  241.     bitmap_sol = image.load("SatFinder/sol.png")
  242.     bitmap_cruz = image.load("SatFinder/cruz.png")
  243.     bitmap_luna = image.load("SatFinder/luna_dia.png")
  244.  
  245.  
  246.    
  247.     screen.print(208, 51, "OK!" , sf_palette[1])
  248.     screen.waitvblankstart()
  249.     screen.flip()
  250.     screen.print(208, 51, "OK!" , sf_palette[1])
  251.     screen.waitvblankstart()
  252.     screen.flip()
  253.    
  254.    
  255.     screen.print(1, 61, "> Cargando configuracion... " , sf_palette[4])
  256.     screen.waitvblankstart()
  257.     screen.flip()
  258.     screen.print(1, 61, "> Cargando configuracion... " , sf_palette[4])
  259.     screen.waitvblankstart()
  260.     screen.flip()
  261.  
  262.     if SF_loadConfigFromFile("config.txt") then
  263.         screen.print(216, 61, "OK!" , sf_palette[1])
  264.         screen.waitvblankstart()
  265.         screen.flip()
  266.         screen.print(216, 61, "OK!" , sf_palette[1])
  267.         screen.waitvblankstart()
  268.         screen.flip()  
  269.     else
  270.         screen.print(216, 61, "Usando valores por defecto." , sf_color_red)
  271.         screen.waitvblankstart()
  272.         screen.flip()
  273.         screen.print(216, 61, "Usando valores por defecto." , sf_color_red)
  274.         screen.waitvblankstart()
  275.         screen.flip()  
  276.     end
  277.  
  278.        
  279.     screen.print(1, 71, "> Cargando satelites... " , sf_palette[4])
  280.     screen.waitvblankstart()
  281.     screen.flip()
  282.     screen.print(1, 71, "> Cargando satelites... " , sf_palette[4])
  283.     screen.waitvblankstart()
  284.     screen.flip()
  285.  
  286.    
  287.     -- Satelites
  288.     sgp_sats = { }
  289.     sgp_sat_number = 0 -- esta variable tiene que ser cero y se modifica por SGP_getTLE()
  290.    
  291.     SGP_getTLE("./tle/visual.tle") 
  292.     SGP_getTLE("./tle/iridium.tle")
  293.  
  294.  
  295.  
  296.    
  297.     if sgp_sat_number == -1 then
  298.         screen.print(192, 71, "Error: Archivos TLE corruptos." , sf_color_red)
  299.         screen.waitvblankstart()
  300.         screen.flip()
  301.         screen.print(192, 71, "Error: Archivos TLE corruptos." , sf_color_red)
  302.         screen.waitvblankstart()
  303.         screen.flip()  
  304.         sgp_sat_number = 0
  305.     elseif sgp_sat_number == -2 then
  306.         screen.print(192, 71, "Error: Faltan archivos TLE." , sf_color_red)
  307.         screen.waitvblankstart()
  308.         screen.flip()
  309.         screen.print(192, 71, "Error: Faltan archivos TLE." , sf_color_red)
  310.         screen.waitvblankstart()
  311.         screen.flip()  
  312.         sgp_sat_number = 0
  313.     else
  314.         screen.print(192, 71, "OK! (".. sgp_sat_number..")" , sf_palette[1])
  315.         screen.waitvblankstart()
  316.         screen.flip()
  317.         screen.print(192, 71, "OK! (".. sgp_sat_number..")" , sf_palette[1])
  318.         screen.waitvblankstart()
  319.         screen.flip()  
  320.     end
  321.  
  322.     sf_next_max = sgp_sat_number
  323.     sf_next_limit = 0
  324.     sf_next_current = 0
  325.     sf_next_passes = {}
  326.     for i = 1,sf_next_max do
  327.         sf_next_passes[i] = {}
  328.     end
  329.    
  330.     screen.print(1, 81, "> Cargando estrellas... " , sf_palette[4])
  331.     screen.waitvblankstart()
  332.     screen.flip()
  333.     screen.print(1, 81, "> Cargando estrellas... " , sf_palette[4])
  334.     screen.waitvblankstart()
  335.     screen.flip()
  336.  
  337.     sgp_mag_limit = 3.4
  338.     sgp_mag_max = 4.2
  339.     sf_star_update_rate_limit = 10
  340.     sf_star_update_current_rate = 1
  341.     sgp_star_number = 0
  342.     sgp_constellation_number = 0
  343.    
  344.     SGP_getStars("allstars.txt", sgp_mag_max)
  345.    
  346.    
  347.    
  348.     screen.print(192, 81, "OK! (" .. sgp_star_number ..")", sf_palette[1])
  349.     screen.waitvblankstart()
  350.     screen.flip()  
  351.     screen.print(192, 81, "OK! (" .. sgp_star_number ..")", sf_palette[1])
  352.     screen.waitvblankstart()
  353.     screen.flip()
  354.    
  355.     screen.print(1, 91, "> Cargando informacion del observador... " , sf_palette[4])
  356.     screen.waitvblankstart()
  357.     screen.flip()
  358.     screen.print(1, 91, "> Cargando informacion del observador... " , sf_palette[4])
  359.     screen.waitvblankstart()
  360.     screen.flip()
  361.    
  362.    
  363.     -- Localizacion de observacion
  364.     if not SGP_getLocationFromFile("location.txt") then
  365.         SGP_setLocation(39.232, -6.536, 80, 1, "Defecto", false)   
  366.         screen.print(322, 91, "Usando informacion por defecto." , sf_color_red)
  367.         screen.waitvblankstart()
  368.         screen.flip()
  369.         screen.print(322, 91, "Usando informacion por defecto." , sf_color_red)
  370.         screen.waitvblankstart()
  371.         screen.flip()  
  372.     else
  373.         screen.print(322, 91, "OK!" , sf_palette[1])
  374.         screen.waitvblankstart()
  375.         screen.flip()
  376.         screen.print(322, 91, "OK!" , sf_palette[1])
  377.         screen.waitvblankstart()
  378.         screen.flip()
  379.        
  380.     end
  381.    
  382.     screen.print(1, 101, "> Calculando posicion de los satelites... " , sf_palette[4])
  383.     screen.waitvblankstart()
  384.     screen.flip()
  385.     screen.print(1, 101, "> Calculando posicion de los satelites... " , sf_palette[4])
  386.     screen.waitvblankstart()
  387.     screen.flip()
  388.        
  389. end
  390.  
  391. function SF_clacularOtrosDatos()
  392.     screen.print(330, 101, "OK!" , sf_palette[1])
  393.     screen.waitvblankstart()
  394.     screen.flip()
  395.     screen.print(330, 101, "OK!" , sf_palette[1])
  396.     screen.waitvblankstart()
  397.     screen.flip()
  398.    
  399.     screen.print(1, 111, "> Calculando fases lunares... " , sf_palette[4])
  400.     screen.waitvblankstart()
  401.     screen.flip()
  402.     screen.print(1, 111, "> Calculando fases lunares... " , sf_palette[4])
  403.     screen.waitvblankstart()
  404.     screen.flip()      
  405.    
  406.     SGP_calculateMoonPhases ()
  407.    
  408.     screen.print(234, 111, "OK!" , sf_palette[1])
  409.     screen.waitvblankstart()
  410.     screen.flip()
  411.     screen.print(234, 111, "OK!" , sf_palette[1])
  412.     screen.waitvblankstart()
  413.     screen.flip()
  414.  
  415.     screen.print(1, 121, "> Calculando amaneceres y ocasos... " , sf_palette[4])
  416.     screen.waitvblankstart()
  417.     screen.flip()
  418.     screen.print(1, 121, "> Calculando amaneceres y ocasos... " , sf_palette[4])
  419.     screen.waitvblankstart()
  420.     screen.flip()      
  421.    
  422.     SGP_calculateSunRaiseTime()
  423.     SGP_calculateSunSetTime()
  424.     SGP_calculateNightBegining()
  425.     SGP_calculateNightEnding()
  426.     SGP_calculateMoonRaiseTime()
  427.     SGP_calculateMoonSetTime()
  428.    
  429.     screen.print(282, 121, "OK!" , sf_palette[1])
  430.     screen.waitvblankstart()
  431.     screen.flip()
  432.     screen.print(282, 121, "OK!" , sf_palette[1])
  433.     screen.waitvblankstart()
  434.     screen.flip()
  435.    
  436.     screen.print(1, 131, "> Procesando estrellas, espere... " , sf_palette[4])
  437.     screen.waitvblankstart()
  438.     screen.flip()
  439.     screen.print(1, 131, "> Procesando estrellas, espere... " , sf_palette[4])
  440.     screen.waitvblankstart()
  441.     screen.flip()
  442.  
  443. end
  444.  
  445. function SGP_getCurrentTime()
  446.     if not sf_primer_calculo then
  447.         sgp_last_time = sgp_time
  448.     end
  449.    
  450.     sgp_time = os.date("*t")
  451.    
  452.     -- This functions only works in LuaPlayerHM6.5
  453.     sgp_time.year = System.getDate(1)
  454.     sgp_time.month = System.getDate(2)
  455.     sgp_time.day = System.getDate(3)
  456.     sgp_time.hour = System.getTime(1)
  457.     if System.getTime(5) == "pm" then
  458.         sgp_time.hour = sgp_time.hour + 12.0
  459.     end
  460.     sgp_time.min = System.getTime(2)
  461.     sgp_time.sec = System.getTime(3)
  462.     sgp_time.isdst = sgp_location.dst
  463.     sgp_time.jtime = SGP_Date_To_Julian(sgp_time.year, sgp_time.month, sgp_time.day, sgp_time.hour, sgp_time.min, sgp_time.sec)
  464.     sgp_time.ujtime = SGP_Local_to_UTC(sgp_time.jtime)
  465.     sgp_time.uyear,sgp_time.umonth,sgp_time.uday,sgp_time.uhour,sgp_time.umin,sgp_time.usec = SGP_Julian_To_Date(sgp_time.ujtime)
  466.  
  467.     if sf_primer_calculo then
  468.         sgp_last_time = sgp_time
  469.     end
  470. end
  471.  
  472.  
  473. function SF_drawTextMap(latitud, longitud, texto, color)
  474.     local px, py = SF_LatLonToXY(latitud, longitud)
  475.    
  476.     local len = texto:len()
  477.    
  478.     if latitud > 0 then
  479.         py = py-4 + 8
  480.     else
  481.         py = py-4 - 8
  482.     end
  483.     screen.print(px, py, texto, color)
  484. end
  485.  
  486. function SF_AzElevToXY( azimut, elevacion )
  487.         local distanciaCentro =   sf_sky.radio * ((90.0-elevacion)/90.0) * (90 / (90-sf_sky.elevacion_minima))
  488.    
  489.         local x, y
  490.        
  491.         local azimut_rotated = azimut + sf_sky.rotacion;
  492.        
  493.         x = sf_sky.radio + (distanciaCentro * math.sin(azimut_rotated*math.pi/180.0) * sf_sky.orientacion)
  494.         y = sf_sky.radio - distanciaCentro * math.cos(azimut_rotated*math.pi/180.0)
  495.    
  496.         x = x + (sf_sky.centro.x - sf_sky.radio)
  497.         y = y + (sf_sky.centro.y - sf_sky.radio)
  498.        
  499.         return x , y
  500. end
  501.  
  502. function SF_drawTextSky(azimut, elevacion, texto, color)
  503.     local txt
  504.     if texto == nil then
  505.         txt = "nil"
  506.     else
  507.         txt = texto
  508.     end
  509.     if elevacion > sf_sky.elevacion_minima then
  510.         local px, py = SF_AzElevToXY( azimut, elevacion )
  511.         local longitud = texto:len()
  512.        
  513.         screen.print(px-2-((longitud-1)*4), py-2, txt, color)
  514.     end
  515. end
  516.  
  517. function SF_drawPointSky( azimut , elevacion , color )
  518.     if elevacion > sf_sky.elevacion_minima then
  519.         local px, py = SF_AzElevToXY( azimut, elevacion )
  520.         if px > 0 and px < screen.width()-1 and py > 0 and py < screen.height()-1 then
  521.             image.pixel( px , py , color )
  522.             return true
  523.         end
  524.     end
  525.     return false
  526. end
  527.  
  528. function SF_LatLonToXY(latitud, longitud)
  529.   local x, y
  530.   y = math.floor(latitud * 1.51111)
  531.   x = math.floor(longitud * 1.33333)
  532.   x = 240 + x
  533.   y = 136 - y
  534.   return x, y
  535. end
  536.  
  537. function SF_drawPointMap( latitud , longitud , color )
  538.     local px, py = SF_LatLonToXY(latitud, longitud)
  539.     if px > 0 and px < screen.width() and py > 0 and py < screen.height() then
  540.         image.pixel( px , py , color )
  541.     end
  542. end
  543.  
  544. function SF_drawCircleSky(radio,azimut,elevacion,color)
  545.     local px, py = SF_AzElevToXY( azimut, elevacion )
  546.  
  547.     x0 = math.sin(math.pi-0/32*2*math.pi) * radio + px 
  548.     y0 = math.cos(math.pi-0/32*2*math.pi) * radio + py
  549.  
  550.    
  551.     for i=1,64 do
  552.         x1 = math.sin(math.pi-i/32*2*math.pi) * radio + px
  553.         y1 = math.cos(math.pi-i/32*2*math.pi) * radio + py
  554.         if x1 > 0 and x1 < screen.width() and y1 > 0 and y1 < screen.height() then
  555.             if x0 > 0 and x0 < screen.width() and y0 > 0 and y0 < screen.height() then
  556.                 draw.line(x0, y0, x1, y1, color)
  557.             end
  558.         end
  559.         x0 = x1
  560.         y0 = y1
  561.     end
  562. end
  563.  
  564. function SF_drawElevationLine (elevacion, color)
  565.     local azimut = 0
  566.     if elevacion >= 0 and 90 > elevacion then
  567.         local px, py = SF_AzElevToXY( -sf_sky.rotacion, elevacion )
  568.         SF_drawCircleSky(py - sf_sky.centro.y, 0, 90, color)
  569.     elseif elevacion == 90 then
  570.         SF_drawPointSky(azimut, elevacion, color)
  571.     end
  572. end
  573.  
  574. function SF_drawLineSky( azimut1 , elevacion1 , azimut2, elevacion2, color )
  575.     if ((elevacion1 > sf_sky.elevacion_minima) and (elevacion2 > sf_sky.elevacion_minima)) then
  576.         local x1, y1 = SF_AzElevToXY( azimut1, elevacion1 )
  577.         local x2, y2 = SF_AzElevToXY( azimut2, elevacion2 )
  578.         if x1 > 0 and x1 < screen.width() and y1 > 0 and y1 < screen.height() then
  579.             if x2 > 0 and x2 < screen.width() and y2 > 0 and y2 < screen.height() then
  580.                 draw.line( x1, y1, x2, y2, color)
  581.             end
  582.         end
  583.     end
  584. end
  585.  
  586. function SF_drawElevation()
  587.     SF_drawElevationLine (0,sf_palette[14])
  588.     SF_drawElevationLine (30,sf_palette[14])
  589.     SF_drawElevationLine (60,sf_palette[14])
  590.     SF_drawElevationLine (90,sf_palette[14])
  591.     --SF_drawText("30", 300 , 26 , sf_palette[8])
  592.     --SF_drawText("60", 300 , 56 , sf_palette[8])
  593.     SF_drawTextSky(300 , 86 , "90", sf_palette[12])
  594.     SF_drawTextSky(120 , 34 , "30", sf_palette[14])
  595.     SF_drawTextSky(120 , 64 , "60", sf_palette[14])
  596.     SF_drawTextSky(120 , 4  , "0" , sf_palette[12])
  597.  
  598. end
  599.  
  600. function SF_drawNSEW()
  601.     SF_drawTextSky( 0 , 45 , "N", sf_palette[14])
  602.     SF_drawTextSky( 90 , 45 , "E", sf_palette[14])
  603.     SF_drawTextSky( 180 , 45 , "S", sf_palette[14])
  604.     SF_drawTextSky( 270 , 45 , "W", sf_palette[14])
  605. end
  606.  
  607. function SF_drawBitmapSky( azimut , elevacion , bitmap, xini, yini, xlen, ylen )
  608.     if elevacion > 0 then
  609.         local px, py = SF_AzElevToXY( azimut, elevacion )          
  610.             image.blit(px - xlen/2, py - ylen/2, bitmap, xini, yini, xlen, ylen, true)
  611.     end
  612. end
  613.  
  614. function SF_dibuarLineasReferencia()
  615.    
  616.     if sf_r_pressed and not sf_zoom_mode then
  617.         return
  618.     elseif sf_l_pressed then
  619.         SF_drawMap()
  620.     else
  621.         -- Dibujamos los puntos cardinales N, S, E, W (O)
  622.         SF_drawNSEW()
  623.    
  624.         -- Dibujamos las lineas de elevación con los grados en texto
  625.         if sf_sky.show.elevationLines then
  626.             SF_drawElevation()
  627.         else
  628.             SF_drawElevationLine(0,sf_palette[14])
  629.             SF_drawElevationLine(90,sf_palette[14])
  630.         end
  631.     end
  632. end
  633.  
  634. function SF_procesarSol(ujtime)
  635.     SF_calcularSol(ujtime) -- Rellena los valores de sgp_sun
  636.     SF_dibujarSol()
  637. end
  638.  
  639. function SF_calcularSol(ujtime)
  640.     if sf_sun_wait <= 0 then
  641.         SGP_calculateSun (ujtime)
  642.         sf_sun_wait = 20
  643.     else
  644.         sf_sun_wait = sf_sun_wait - 1
  645.     end
  646. end
  647.  
  648. function SF_dibujarSol()
  649.     --if sf_l_pressed then
  650.     --  screen.print(100,130,"Dibujando mapa",sf_palette[8])
  651.     --end
  652.     if sf_r_pressed and not sf_zoom_mode then
  653.         SF_drawBackgroundInfo()
  654.         SF_drawSunInfo()
  655.     elseif sf_sky.show.sunAndMoon then
  656.         if sf_l_pressed then
  657.             SF_drawBitmapMap(sgp_sun.lat, sgp_sun.lon , bitmap_sol,0,0,bitmap_sol:width(),bitmap_sol:height() )
  658.         elseif sgp_sun.ele > 0 then
  659.                 SF_drawBitmapSky(sgp_sun.azi, sgp_sun.ele , bitmap_sol,0,0,bitmap_sol:width(),bitmap_sol:height() )
  660.         end
  661.     end
  662. end
  663.  
  664.  
  665.  
  666. function SF_procesarLuna(ujtime)
  667.     SF_calcularLuna(ujtime) -- Rellena los valores de sgp_moon
  668.     SF_dibujarLuna()
  669. end
  670.  
  671. function SF_calcularLuna(ujtime)
  672.     if sf_moon_wait <= 0 then
  673.         SGP_calculateMoon (ujtime)
  674.         sf_moon_wait = 200
  675.         local numero_imagen = sgp_moon.age.cicle * 16 - 0.5  
  676.         if numero_imagen < 0 then
  677.             numero_imagen = numero_imagen + 16
  678.         end
  679.         numero_imagen = math.floor(numero_imagen)
  680.         sf_moon_x_desp = numero_imagen * 16
  681.        
  682.     else
  683.         sf_moon_wait = sf_moon_wait - 1
  684.     end
  685. end
  686.  
  687. function SF_dibujarLuna()
  688.     if sf_sky.show.sunAndMoon then
  689.  
  690.         if sf_r_pressed and not sf_zoom_mode then
  691.             SF_drawMoonInfo()
  692.             image.blit(218, 172, bitmap_luna, sf_moon_x_desp, 0, 16, 16, true)
  693.         else
  694.             if sf_l_pressed then
  695.                 SF_drawBitmapMap(sgp_moon.lat, sgp_moon.lon, bitmap_luna, sf_moon_x_desp, 0, 16, 16)
  696.             else
  697.                 if sgp_moon.ele > 0 then
  698.                     SF_drawBitmapSky(sgp_moon.azi, sgp_moon.ele, bitmap_luna, sf_moon_x_desp, 0, 16, 16)
  699.                 end
  700.             end
  701.         end
  702.     end
  703. end
  704.  
  705. function SF_crearPaletaEstrellas()
  706.     for i = 1 , sf_palette_len do
  707.         local nivel = math.floor(255 * (1-((i-1)/sf_palette_len)))
  708.         sf_star_palette[i] = color.new(nivel,nivel,nivel)
  709.     end
  710. end
  711.  
  712. function SF_cambiarMagnitudAuto()
  713.     local daylight
  714.     if sgp_sun.ele < const_twilightCivil and sgp_sun.ele > const_twilightNautical then
  715.         daylight = (sgp_sun.ele-const_twilightCivil)/(const_twilightNautical-const_twilightCivil)
  716.     elseif sgp_sun.ele >= const_twilightCivil then
  717.         daylight = 0.0
  718.     else
  719.         daylight = 1.0
  720.     end
  721.     sgp_mag_limit = ((sgp_mag_max + 2) * daylight) - 2  
  722. end
  723.  
  724. function SF_procesarEstrellas(ujtime)
  725.     if sf_primer_calculo then
  726.         SF_calcularEstrellas(ujtime)
  727.     elseif sf_sky.show.stars and not sf_l_pressed  then
  728.         if sf_sky.show.autoMagStars then
  729.             SF_cambiarMagnitudAuto()
  730.         end
  731.         if not sf_moving_sky then
  732.             SF_calcularEstrellas(ujtime)
  733.         end
  734.         SF_dibujarEstrellas()
  735.     end
  736. end
  737.  
  738. function SF_calcularEstrellas(ujtime)
  739.     local star
  740.    
  741.     if sf_primer_calculo then
  742.         for star = 1,sgp_star_number do
  743.             if sgp_stars[star].no_calcular <= 0 then
  744.                 if sgp_stars[star].mag < sgp_mag_max then
  745.                     sgp_stars[star].azi, sgp_stars[star].ele = SGP_RADecToAzElev( ujtime,sgp_stars[star].ra,sgp_stars[star].dec,sgp_location.lat, sgp_location.lon)
  746.                    
  747.                     if sgp_stars[star].ele < 0 then
  748.                         sgp_stars[star].no_calcular = 10 * -sgp_stars[star].ele
  749.                     end
  750.                 end
  751.             else
  752.                 sgp_stars[star].no_calcular = sgp_stars[star].no_calcular - 1
  753.             end
  754.         end
  755.         cursor_stars = 1
  756.     else
  757.         local stars_to_calc = sf_star_update_current_rate
  758.         while stars_to_calc > 0 and cursor_stars < sgp_star_number   do    
  759.             while  ((sgp_stars[cursor_stars].no_calcular > 0) and sgp_stars[cursor_stars].mag < sgp_mag_limit and (cursor_stars < sgp_star_number)) do
  760.                 sgp_stars[cursor_stars].no_calcular = sgp_stars[cursor_stars].no_calcular - 1
  761.                 cursor_stars = cursor_stars + 1
  762.             end
  763.             if cursor_stars >= sgp_star_number then
  764.                 cursor_stars = 1
  765.                 if sf_star_update_current_rate + 1 < sf_star_update_rate_limit then
  766.                     sf_star_update_current_rate = sf_star_update_current_rate + 1
  767.                 else
  768.                     sf_star_update_current_rate = sf_star_update_rate_limit
  769.                 end
  770.             else
  771.                 if sgp_stars[cursor_stars].no_calcular <= 0 then
  772.                     if sgp_stars[cursor_stars].mag < sgp_mag_limit then
  773.                         sgp_stars[cursor_stars].azi, sgp_stars[cursor_stars].ele = SGP_RADecToAzElev( ujtime,sgp_stars[cursor_stars].ra,sgp_stars[cursor_stars].dec,sgp_location.lat, sgp_location.lon)
  774.                         stars_to_calc = stars_to_calc - 1
  775.                         if sgp_stars[cursor_stars].ele < 0 then
  776.                             sgp_stars[cursor_stars].no_calcular = 10 * -sgp_stars[cursor_stars].ele
  777.                         end
  778.                     else
  779.                         sgp_stars[cursor_stars].no_calcular = sgp_stars[cursor_stars].no_calcular - 1
  780.                     end
  781.                 else
  782.                     sgp_stars[cursor_stars].no_calcular = sgp_stars[cursor_stars].no_calcular - 1
  783.                 end
  784.                 cursor_stars = cursor_stars + 1
  785.             end
  786.            
  787.         end
  788.     end
  789. end
  790.  
  791. function SF_dibujarEstrellas()
  792.     if sf_r_pressed and not sf_zoom_mode then
  793.         SF_drawStarsInfo()
  794.     else       
  795.         local star, color
  796.         for star = 1,sgp_star_number do
  797.             if sgp_stars[star].mag < sgp_mag_limit then
  798.                 if sgp_stars[star].ele > 0 then                  
  799.                     color = math.floor((sgp_stars[star].mag - sf_sky.radio/241*0.2 + 1)/4*16 )
  800.                     if color < 1 then
  801.                         color = 1
  802.                     elseif color > 16 then
  803.                         color = 16
  804.                     end
  805.                     if not SF_drawPointSky(sgp_stars[star].azi, sgp_stars[star].ele , sf_star_palette[color]) then
  806.                         sgp_stars[star].no_calcular = 10
  807.                     end
  808.                 end
  809.             end
  810.         end
  811.     end
  812. end
  813.  
  814. function SF_procesarSatelites(ujtime)
  815.     if sf_primer_calculo then
  816.         SF_calcularSatelites(ujtime)
  817.         SF_dibujarSatelites()
  818.     elseif sf_sky.show.satellites then
  819.         if not sf_moving_sky then
  820.             SF_calcularSatelites(ujtime)
  821.         end
  822.         SF_dibujarSatelites()
  823.     end
  824. end
  825.  
  826. function SF_calcularSatelites(ujtime)
  827.     if sf_primer_calculo then
  828.         --Para cada satélite obtener sus datos para el tiempo actual, incluido el sol
  829.         sf_elevacion_max = 0
  830.         for s = 1,sgp_sat_number do
  831.             --Calcular solo si no está suspendido temporalmente para optimizar recursos
  832.             if sgp_sats[s].no_calcular <= 0 then   
  833.                 SGP_calculateSatellite (s, ujtime)
  834.                 -- Nos vamos quedando con el satélite con elevación más alta
  835.                 -- Necesario para los avisos por voz.
  836.                 if sgp_sats[s].elevacion > sf_elevacion_max and sgp_sats[s].visible then
  837.                     sf_elevacion_max = sgp_sats[s].elevacion
  838.                     sf_azimut_max = sgp_sats[s].azimut
  839.                 end
  840.                 --Optimizacion
  841.                
  842.                 if sgp_sats[s].elevacion < sf_sky.elevacion_minima then
  843.                     if sgp_sats[s].elevacion_ant > sgp_sats[s].elevacion then -- bajando por debajo del horizonte
  844.                         sgp_sats[s].no_calcular = math.random (30,60)
  845.                     else -- subiendo por debajo del horizonte
  846.                         sgp_sats[s].no_calcular = math.random (10,30)
  847.                     end
  848.                    
  849.                     if sgp_sats[s].numero == sf_iss then
  850.                         sgp_sats[s].no_calcular = 10
  851.                     end
  852.                 end        
  853.                                
  854.             else
  855.                 --Descontar un calculo suspendido
  856.                 sgp_sats[s].no_calcular = sgp_sats[s].no_calcular - 1
  857.             end
  858.         end
  859.         cursor_satelite = 1
  860.     else
  861.    
  862.         while ((sgp_sats[cursor_satelite].no_calcular > 0) and (cursor_satelite < sgp_sat_number)) do
  863.             sgp_sats[cursor_satelite].no_calcular = sgp_sats[cursor_satelite].no_calcular - 1
  864.             cursor_satelite = cursor_satelite + 1
  865.         end
  866.         if cursor_satelite >= sgp_sat_number then
  867.             cursor_satelite = 1
  868.         else
  869.             SGP_calculateSatellite (cursor_satelite, ujtime)
  870.             -- Nos vamos quedando con el satélite con elevación más alta
  871.             -- Necesario para los avisos por voz.
  872.             if sgp_sats[cursor_satelite].elevacion > sf_elevacion_max and sgp_sats[cursor_satelite].visible then
  873.                 sf_elevacion_max = sgp_sats[cursor_satelite].elevacion
  874.                 sf_azimut_max = sgp_sats[cursor_satelite].azimut
  875.             end
  876.            
  877.             --Optimizacion
  878.  
  879.             if sgp_sats[cursor_satelite].elevacion < sf_sky.elevacion_minima then
  880.                 if sgp_sats[cursor_satelite].elevacion_ant > sgp_sats[cursor_satelite].elevacion then -- bajando por debajo del horizonte
  881.                     sgp_sats[cursor_satelite].no_calcular = math.random (30,60)
  882.                 else -- subiendo por debajo del horizonte
  883.                     sgp_sats[cursor_satelite].no_calcular = math.random (10,30)
  884.                 end
  885.                 if sgp_sats[cursor_satelite].numero == sf_iss then
  886.                     sgp_sats[cursor_satelite].no_calcular = 10
  887.                 end
  888.             end        
  889.             cursor_satelite = cursor_satelite + 1      
  890.         end
  891.  
  892.     end
  893. end
  894.  
  895.  
  896. function SF_dibujarSatelites()
  897.     if sf_primer_calculo then
  898.         sf_vision_nocturna = sgp_location.noche
  899.  
  900.         sf_tail_length = 16
  901.         sf_step_length = 20
  902.         SF_initTails()
  903.     end
  904.  
  905.     local s
  906.     if sf_r_pressed and not sf_zoom_mode then
  907.         SF_drawSatsInfo()
  908.     elseif sf_l_pressed then
  909.         for s = 1,sgp_sat_number do
  910.             SF_drawSatMap( s )
  911.         end    
  912.         SF_drawSatPathMap()
  913.     else
  914.         for s = 1,sgp_sat_number do
  915.             if sgp_sats[s].elevacion > 0  and  sgp_sats[s].elevacion_ant < 0 then
  916.                 SF_initTail(s)
  917.             end
  918.             SF_drawSatSky( s )
  919.         end
  920.         SF_drawSatPathSky()
  921.     end
  922.  
  923. end
  924.  
  925. function SF_procesarConstelaciones(jt)
  926.     if sf_r_pressed and not sf_zoom_mode then
  927.         return
  928.     end
  929.     if sf_sky.show.constellationNames and not sf_l_pressed then
  930.         if not sf_moving_sky then  
  931.             SF_calcularConstelaciones(jt)
  932.         end
  933.         SF_dibujarConstelaciones()
  934.     end
  935. end
  936.  
  937. function SF_calcularConstelaciones(jt)
  938.     local c
  939.     for c=1,sgp_constellation_number do
  940.         if sgp_constellations[c].no_calcular > 0 then
  941.             sgp_constellations[c].no_calcular = sgp_constellations[c].no_calcular - 1
  942.         else
  943.             sgp_constellations[c].az, sgp_constellations[c].el =  SGP_RADecToAzElev( jt,sgp_constellations[c].label_ra,sgp_constellations[c].label_dec,sgp_location.lat, sgp_location.lon)
  944.             if el < 0 then
  945.                 sgp_constellations[c].no_calcular = math.random(50,100)
  946.             end
  947.         end
  948.     end
  949. end
  950.  
  951. function SF_dibujarConstelaciones()
  952.     local c
  953.     for c=1,sgp_constellation_number do
  954.         if sgp_constellations[c].el > 0 then
  955.             SF_drawTextSky(sgp_constellations[c].az , sgp_constellations[c].el ,sgp_constellations[c].nombre, sf_palette[14])
  956.         end
  957.     end
  958. end
  959.  
  960. function SGP_Local_to_UTC(jtime)
  961.   local jt = jtime
  962.   jt = jt - sgp_location.UTC_offset/24.0
  963.   if sgp_time.isdst then  --si estamos en horario de verano
  964.     jt = jt - 1.0/24.0
  965.   end
  966.   return jt
  967. end
  968.  
  969.  
  970. function SGP_UTC_to_Local(jtime)
  971.   local jt = jtime
  972.   jt = jt + (sgp_location.UTC_offset/24.0)
  973.   if sgp_time.isdst then
  974.     jt = jt + (1.0/24.0)
  975.   end
  976.   return jt
  977. end  
  978.  
  979. function SF_drawLabelSatsSky(azimut, elevacion, texto, color)
  980.     if elevacion > sf_sky.elevacion_minima then
  981.         local px, py = SF_AzElevToXY( azimut, elevacion )
  982.         if azimut > 90 and azimut < 270 then
  983.             sky:print(px+ 5, py-10, texto, color)
  984.         else
  985.             sky:print(px+ 5, py+2, texto, color)
  986.         end
  987.     end
  988. end
  989.  
  990. function SGP_setLocation(lat, lon, alt, offset, name, dst)
  991.     sgp_location.lat = lat
  992.     sgp_location.lon = lon
  993.     sgp_location.alt = alt
  994.     sgp_location.UTC_offset = offset
  995.     sgp_location.name = name
  996.     sgp_location.dst = dst
  997.     sgp_location.polar.lat = math.rad(sgp_location.lat)
  998.     sgp_location.polar.lon = math.rad(SGP_modulus(sgp_location.lon,360))
  999.     sgp_location.polar.alt = sgp_location.alt * 0.001
  1000. end
  1001.  
  1002. function SGP_initCoefficients()
  1003.  
  1004.     sgp_lrCoeff = {}
  1005.  
  1006.     sgp_lrCoeff[1] = {0, 0, 1, 0}
  1007.     sgp_lrCoeff[2] = {2, 0,-1, 0}
  1008.     sgp_lrCoeff[3] = {2, 0, 0, 0}
  1009.     sgp_lrCoeff[4] = {0, 0, 2, 0}
  1010.     sgp_lrCoeff[5] = {0, 1, 0, 0}
  1011.     sgp_lrCoeff[6] = {0, 0, 0, 2}
  1012.     sgp_lrCoeff[7] = {2, 0,-2, 0}
  1013.     sgp_lrCoeff[8] = {2,-1,-1, 0}
  1014.     sgp_lrCoeff[9] = {2, 0, 1, 0}
  1015.     sgp_lrCoeff[10] = {2,-1, 0, 0}
  1016.     sgp_lrCoeff[11] = {0, 1,-1, 0}
  1017.     sgp_lrCoeff[12] = {1, 0, 0, 0}
  1018.     sgp_lrCoeff[13] = {0, 1, 1, 0}
  1019.     sgp_lrCoeff[14] = {2, 0, 0,-2}
  1020.     sgp_lrCoeff[15] = {0, 0, 1, 2}
  1021.     sgp_lrCoeff[16] = {0, 0, 1,-2}
  1022.     sgp_lrCoeff[17] = {4, 0,-1, 0}
  1023.     sgp_lrCoeff[18] = {0, 0, 3, 0}
  1024.     sgp_lrCoeff[19] = {4, 0,-2, 0}
  1025.     sgp_lrCoeff[20] = {2, 1,-1, 0}
  1026.     sgp_lrCoeff[21] = {2, 1, 0, 0}
  1027.     sgp_lrCoeff[22] = {1, 0,-1, 0}
  1028.     sgp_lrCoeff[23] = {1, 1, 0, 0}
  1029.     sgp_lrCoeff[24] = {2,-1, 1, 0}
  1030.     sgp_lrCoeff[25] = {2, 0, 2, 0}
  1031.     sgp_lrCoeff[26] = {4, 0, 0, 0}
  1032.     sgp_lrCoeff[27] = {2, 0,-3, 0}
  1033.     sgp_lrCoeff[28] = {0, 1,-2, 0}
  1034.     sgp_lrCoeff[29] = {2, 0,-1, 2}
  1035.     sgp_lrCoeff[30] = {2,-1,-2, 0}
  1036.     sgp_lrCoeff[31] = {1, 0, 1, 0}
  1037.     sgp_lrCoeff[32] = {2,-2, 0, 0}
  1038.     sgp_lrCoeff[33] = {0, 1, 2, 0}
  1039.     sgp_lrCoeff[34] = {0, 2, 0, 0}
  1040.     sgp_lrCoeff[35] = {2,-2,-1, 0}
  1041.     sgp_lrCoeff[36] = {2, 0, 1,-2}
  1042.     sgp_lrCoeff[37] = {2, 0, 0, 2}
  1043.     sgp_lrCoeff[38] = {4,-1,-1, 0}
  1044.     sgp_lrCoeff[39] = {0, 0, 2, 2}
  1045.     sgp_lrCoeff[40] = {3, 0,-1, 0}
  1046.     sgp_lrCoeff[41] = {2, 1, 1, 0}
  1047.     sgp_lrCoeff[42] = {4,-1,-2, 0}
  1048.     sgp_lrCoeff[43] = {0, 2,-1, 0}
  1049.     sgp_lrCoeff[44] = {2, 2,-1, 0}
  1050.     sgp_lrCoeff[45] = {2, 1,-2, 0}
  1051.     sgp_lrCoeff[46] = {2,-1, 0,-2}
  1052.     sgp_lrCoeff[47] = {4, 0, 1, 0}
  1053.     sgp_lrCoeff[48] = {0, 0, 4, 0}
  1054.     sgp_lrCoeff[49] = {4,-1, 0, 0}
  1055.     sgp_lrCoeff[50] = {1, 0,-2, 0}
  1056.     sgp_lrCoeff[51] = {2, 1, 0,-2}
  1057.     sgp_lrCoeff[52] = {0, 0, 2,-2}
  1058.     sgp_lrCoeff[53] = {1, 1, 1, 0}
  1059.     sgp_lrCoeff[54] = {3, 0,-2, 0}
  1060.     sgp_lrCoeff[55] = {4, 0,-3, 0}
  1061.     sgp_lrCoeff[56] = {2,-1, 2, 0}
  1062.     sgp_lrCoeff[57] = {0, 2, 1, 0}
  1063.     sgp_lrCoeff[58] = {1, 1,-1, 0}
  1064.     sgp_lrCoeff[59] = {2, 0, 3, 0}
  1065.     sgp_lrCoeff[60] = {2, 0,-1,-2}
  1066.  
  1067.  
  1068.     sgp_bCoeff = {}
  1069.      
  1070.     sgp_bCoeff[1] = {0, 0, 0, 1}
  1071.     sgp_bCoeff[2] = {0, 0, 1, 1}
  1072.     sgp_bCoeff[3] = {0, 0, 1,-1}
  1073.     sgp_bCoeff[4] = {2, 0, 0,-1}
  1074.     sgp_bCoeff[5] = {2, 0,-1, 1}
  1075.     sgp_bCoeff[6] = {2, 0,-1,-1}
  1076.     sgp_bCoeff[7] = {2, 0, 0, 1}
  1077.     sgp_bCoeff[8] = {0, 0, 2, 1}
  1078.     sgp_bCoeff[9] = {2, 0, 1,-1}
  1079.     sgp_bCoeff[10] = {0, 0, 2,-1}
  1080.     sgp_bCoeff[11] = {2,-1, 0,-1}
  1081.     sgp_bCoeff[12] = {2, 0,-2,-1}
  1082.     sgp_bCoeff[13] = {2, 0, 1, 1}
  1083.     sgp_bCoeff[14] = {2, 1, 0,-1}
  1084.     sgp_bCoeff[15] = {2,-1,-1, 1}
  1085.     sgp_bCoeff[16] = {2,-1, 0, 1}
  1086.     sgp_bCoeff[17] = {2,-1,-1,-1}
  1087.     sgp_bCoeff[18] = {0, 1,-1,-1}
  1088.     sgp_bCoeff[19] = {4, 0,-1,-1}
  1089.     sgp_bCoeff[20] = {0, 1, 0, 1}
  1090.     sgp_bCoeff[21] = {0, 0, 0, 3}
  1091.     sgp_bCoeff[22] = {0, 1,-1, 1}
  1092.     sgp_bCoeff[23] = {1, 0, 0, 1}
  1093.     sgp_bCoeff[24] = {0, 1, 1, 1}
  1094.     sgp_bCoeff[25] = {0, 1, 1,-1}
  1095.     sgp_bCoeff[26] = {0, 1, 0,-1}
  1096.     sgp_bCoeff[27] = {1, 0, 0,-1}
  1097.     sgp_bCoeff[28] = {0, 0, 3, 1}
  1098.     sgp_bCoeff[29] = {4, 0, 0,-1}
  1099.     sgp_bCoeff[30] = {4, 0,-1, 1}
  1100.     sgp_bCoeff[31] = {0, 0, 1,-3}
  1101.     sgp_bCoeff[32] = {4, 0,-2, 1}
  1102.     sgp_bCoeff[33] = {2, 0, 0,-3}
  1103.     sgp_bCoeff[34] = {2, 0, 2,-1}
  1104.     sgp_bCoeff[35] = {2,-1, 1,-1}
  1105.     sgp_bCoeff[36] = {2, 0,-2, 1}
  1106.     sgp_bCoeff[37] = {0, 0, 3,-1}
  1107.     sgp_bCoeff[38] = {2, 0, 2, 1}
  1108.     sgp_bCoeff[39] = {2, 0,-3,-1}
  1109.     sgp_bCoeff[40] = {2, 1,-1, 1}
  1110.     sgp_bCoeff[41] = {2, 1, 0, 1}
  1111.     sgp_bCoeff[42] = {4, 0, 0, 1}
  1112.     sgp_bCoeff[43] = {2,-1, 1, 1}
  1113.     sgp_bCoeff[44] = {2,-2, 0,-1}
  1114.     sgp_bCoeff[45] = {0, 0, 1, 3}
  1115.     sgp_bCoeff[46] = {2, 1, 1,-1}
  1116.     sgp_bCoeff[47] = {1, 1, 0,-1}
  1117.     sgp_bCoeff[48] = {1, 1, 0, 1}
  1118.     sgp_bCoeff[49] = {0, 1,-2,-1}
  1119.     sgp_bCoeff[50] = {2, 1,-1,-1}
  1120.     sgp_bCoeff[51] = {1, 0, 1, 1}
  1121.     sgp_bCoeff[52] = {2,-1,-2,-1}
  1122.     sgp_bCoeff[53] = {0, 1, 2, 1}
  1123.     sgp_bCoeff[54] = {4, 0,-2,-1}
  1124.     sgp_bCoeff[55] = {4,-1,-1,-1}
  1125.     sgp_bCoeff[56] = {1, 0, 1,-1}
  1126.     sgp_bCoeff[57] = {4, 0, 1,-1}
  1127.     sgp_bCoeff[58] = {1, 0,-1,-1}
  1128.     sgp_bCoeff[59] = {4,-1, 0,-1}
  1129.     sgp_bCoeff[60] = {2,-2, 0, 1}
  1130.  
  1131.  
  1132.     sgp_lTerms = {
  1133.          6288774,
  1134.          1274027,
  1135.           658314,
  1136.           213618,
  1137.          -185116,
  1138.          -114332,
  1139.            58793,
  1140.            57066,
  1141.            53322,
  1142.            45758,
  1143.           -40923,
  1144.           -34720,
  1145.           -30383,
  1146.            15327,
  1147.           -12528,
  1148.            10980,
  1149.            10675,
  1150.            10034,
  1151.             8548,
  1152.            -7888,
  1153.            -6766,
  1154.            -5163,
  1155.             4987,
  1156.             4036,
  1157.             3994,
  1158.             3861,
  1159.             3665,
  1160.            -2689,
  1161.            -2602,
  1162.             2390,
  1163.            -2348,
  1164.             2236,
  1165.            -2120,
  1166.            -2069,
  1167.             2048,
  1168.            -1773,
  1169.            -1595,
  1170.             1215,
  1171.            -1110,
  1172.             -892,
  1173.             -810,
  1174.              759,
  1175.             -713,
  1176.             -700,
  1177.              691,
  1178.              596,
  1179.              549,
  1180.              537,
  1181.              520,
  1182.             -487,
  1183.             -399,
  1184.             -381,
  1185.              351,
  1186.             -340,
  1187.              330,
  1188.              327,
  1189.             -323,
  1190.              299,
  1191.              294,
  1192.                0
  1193.     }
  1194.  
  1195.     sgp_rTerms = {
  1196.        -20905355,
  1197.         -3699111,
  1198.         -2955968,
  1199.          -569925,
  1200.            48888,
  1201.            -3149,
  1202.           246158,
  1203.          -152138,
  1204.          -170733,
  1205.          -204586,
  1206.          -129620,
  1207.           108743,
  1208.           104755,
  1209.            10321,
  1210.                0,
  1211.            79661,
  1212.           -34782,
  1213.           -23210,
  1214.           -21636,
  1215.            24208,
  1216.            30824,
  1217.            -8379,
  1218.           -16675,
  1219.           -12831,
  1220.           -10445,
  1221.           -11650,
  1222.            14403,
  1223.            -7003,
  1224.                0,
  1225.            10056,
  1226.             6322,
  1227.            -9884,
  1228.             5751,
  1229.                0,
  1230.            -4950,
  1231.             4130,
  1232.                0,
  1233.            -3958,
  1234.                0,
  1235.             3258,
  1236.             2616,
  1237.            -1897,
  1238.            -2117,
  1239.             2354,
  1240.                0,
  1241.                0,
  1242.            -1423,
  1243.            -1117,
  1244.            -1571,
  1245.            -1739,
  1246.                0,
  1247.            -4421,
  1248.                0,
  1249.                0,
  1250.                0,
  1251.                0,
  1252.             1165,
  1253.                0,
  1254.                0,
  1255.             8752
  1256.     }
  1257.  
  1258.     sgp_bTerms = {
  1259.          5128122,
  1260.           280602,
  1261.           277693,
  1262.           173237,
  1263.            55413,
  1264.            46271,
  1265.            32573,
  1266.            17198,
  1267.             9266,
  1268.             8822,
  1269.             8216,
  1270.             4324,
  1271.             4200,
  1272.            -3359,
  1273.             2463,
  1274.             2211,
  1275.             2065,
  1276.            -1870,
  1277.             1828,
  1278.            -1794,
  1279.            -1749,
  1280.            -1565,
  1281.            -1491,
  1282.            -1475,
  1283.            -1410,
  1284.            -1344,
  1285.            -1335,
  1286.             1107,
  1287.             1021,
  1288.              833,
  1289.              777,
  1290.              671,
  1291.              607,
  1292.              596,
  1293.              491,
  1294.             -451,
  1295.              439,
  1296.              422,
  1297.              421,
  1298.             -366,
  1299.             -351,
  1300.              331,
  1301.              315,
  1302.              302,
  1303.             -283,
  1304.             -229,
  1305.              223,
  1306.              223,
  1307.             -220,
  1308.             -220,
  1309.             -185,
  1310.              181,
  1311.             -177,
  1312.              176,
  1313.              166,
  1314.             -164,
  1315.              132,
  1316.             -119,
  1317.              115,
  1318.              107
  1319.     }
  1320.  
  1321. end
  1322.  
  1323.  
  1324.  
  1325.  
  1326. function SF_readInput()
  1327.     if sf_primer_calculo then
  1328.         sf_entrada = nil
  1329.         sf_entrada_ant = Controls.read()
  1330.         sf_acel_x = 0
  1331.         sf_acel_y = 0
  1332.         sf_moving_sky = false
  1333.     else
  1334.         sf_entrada = Controls.read()
  1335.        
  1336.         sf_analog_x = sf_entrada:analogX() 
  1337.         sf_analog_y = sf_entrada:analogY()
  1338.            
  1339.         if sf_analog_x > 30 or sf_analog_x < -30 or sf_analog_y > 30 or sf_analog_y < -30 then
  1340.             sf_acel_x = sf_acel_x + sf_analog_x * 0.01
  1341.             sf_acel_y = sf_acel_y - sf_analog_y * 0.01
  1342.             sf_star_update_current_rate = 1
  1343.             sf_moving_sky = true
  1344.         else
  1345.             sf_acel_x = sf_acel_x * 0.5
  1346.             sf_acel_y = sf_acel_y * 0.5
  1347.             if math.abs(sf_acel_x) < 0.1 and math.abs(sf_acel_y) < 0.1 then
  1348.                 sf_acel_x = 0
  1349.                 sf_acel_y = 0
  1350.                 sf_moving_sky = false
  1351.             end
  1352.         end
  1353.         if sf_r_pressed and sf_zoom_mode then
  1354.             sf_sky.rotacion = sf_sky.rotacion - sf_acel_x * sf_sky.orientacion
  1355.             if sf_sky.rotacion > 360 then
  1356.                 sf_sky.rotacion = sf_sky.rotacion - 360
  1357.             end
  1358.             if sf_sky.rotacion < 0 then
  1359.                 sf_sky.rotacion = sf_sky.rotacion + 360
  1360.             end
  1361.         elseif sf_zoom_mode then
  1362.             local total = sf_acel_x + sf_acel_y
  1363.             sf_sky.radio = sf_sky.radio + total
  1364.             if sf_sky.radio < 130 then
  1365.                 sf_sky.radio = 130
  1366.             end
  1367.            
  1368.             sf_sky.centro.x = sf_sky.centro.x + ((sf_sky.centro.x - 240)*((sf_sky.radio/(sf_sky.radio - total))-1))
  1369.             sf_sky.centro.y = sf_sky.centro.y + ((sf_sky.centro.y - 136)*((sf_sky.radio/(sf_sky.radio - total))-1))
  1370.  
  1371.             local distancia = math.sqrt( ((sf_sky.centro.x - 240)*(sf_sky.centro.x - 240)) + ((sf_sky.centro.y - 136)*(sf_sky.centro.y - 136)) )
  1372.            
  1373.             if distancia > sf_sky.radio then
  1374.                 sf_sky.centro.x = sf_sky.centro.x - ((sf_sky.centro.x - 240)*((sf_sky.radio/(sf_sky.radio - total))-1))
  1375.                 sf_sky.centro.y = sf_sky.centro.y - ((sf_sky.centro.y - 136)*((sf_sky.radio/(sf_sky.radio - total))-1))
  1376.             end
  1377.         else
  1378.             sf_sky.centro.x = sf_sky.centro.x - sf_acel_x
  1379.             sf_sky.centro.y = sf_sky.centro.y + sf_acel_y
  1380.             local distancia = math.sqrt( ((sf_sky.centro.x - 240)*(sf_sky.centro.x - 240)) + ((sf_sky.centro.y - 136)*(sf_sky.centro.y - 136)) )
  1381.            
  1382.             if distancia > sf_sky.radio then
  1383.                 sf_acel_x = -sf_acel_x
  1384.                 sf_acel_y = -sf_acel_y
  1385.             end
  1386.         end
  1387.        
  1388.         if sf_entrada == sf_entrada_ant then
  1389.             return
  1390.         end
  1391.  
  1392.  
  1393.  
  1394.         sf_zoom_mode = false
  1395.        
  1396.         --L y R
  1397.         sf_r_pressed = sf_entrada:r()  
  1398.         sf_l_pressed = sf_entrada:l()
  1399.  
  1400.        
  1401.         -- CRUZ
  1402.         if sf_entrada:cross() then
  1403.             sf_entrada_ant = sf_entrada    
  1404.             SF_drawAllNextVisiblePassList()
  1405.                
  1406.         -- CIRCULO
  1407.         elseif sf_entrada:circle() then
  1408.             sf_entrada_ant = sf_entrada    
  1409.             if sf_vision_nocturna then
  1410.                 sf_vision_nocturna = false
  1411.                 SF_changePalette (sf_color_day.r,sf_color_day.g,sf_color_day.b)
  1412.             else
  1413.                 sf_vision_nocturna = true
  1414.                 SF_changePalette (sf_color_night.r,sf_color_night.g,sf_color_night.b)
  1415.             end
  1416.  
  1417.         --TRIANGULO
  1418.         elseif sf_entrada:triangle() then
  1419.             sf_entrada_ant = sf_entrada
  1420.             if sf_iss ~= nil then
  1421.                 SF_drawSatNextVisiblePassList(sf_iss)
  1422.             end
  1423.         --CUADRADO
  1424.         elseif sf_entrada:square() then
  1425.             sf_entrada_ant = sf_entrada
  1426.             sf_zoom_mode = true
  1427.            
  1428.         elseif sf_entrada:up() then
  1429.             if sgp_mag_limit + 0.2 <= sgp_mag_max + 0.1 then
  1430.                 sgp_mag_limit = sgp_mag_limit + 0.2
  1431.             end
  1432.         elseif sf_entrada:down() then
  1433.             if sgp_mag_limit - 0.2 >= -2.1 then
  1434.                 sgp_mag_limit = sgp_mag_limit - 0.2
  1435.             end
  1436.         elseif sf_entrada:left() then
  1437.             if sf_sky.orientacion < 0 then
  1438.                 sf_sky.orientacion = 1.0
  1439.             else
  1440.                 sf_sky.orientacion = -1.0
  1441.             end    
  1442.         elseif sf_entrada:right() then
  1443.             if sf_sky.orientacion < 0 then
  1444.                 sf_sky.orientacion = 1.0
  1445.             else
  1446.                 sf_sky.orientacion = -1.0
  1447.             end
  1448.  
  1449.  
  1450.            
  1451. --      elseif sf_entrada:hold() then
  1452. --      elseif sf_entrada:note() then
  1453.  
  1454.         elseif sf_entrada:select() then
  1455.             SF_downloadTLEs()
  1456.             sgp_sat_number = 0 -- esta variable tiene que ser cero y se modifica por SGP_getTLE()
  1457.             SGP_getTLE("./tle/visual.tle")
  1458.             SGP_getTLE("./tle/iridium.tle")
  1459.             SF_initTails()
  1460.        
  1461.             if sgp_sat_number == -1 then
  1462.                 screen.print(30, 150, "Error: Archivos TLE corruptos." , sf_palette[1])
  1463.                 screen.waitvblankstart()
  1464.                 screen.flip()
  1465.                 screen.print(30, 150, "Error: Archivos TLE corruptos." , sf_palette[1])
  1466.                 screen.waitvblankstart(300)
  1467.                 screen.flip()  
  1468.                 sgp_sat_number = 0
  1469.             else
  1470.                 screen.print(30, 150, "OK! (".. sgp_sat_number..")" , sf_palette[1])
  1471.                 screen.waitvblankstart()
  1472.                 screen.flip()
  1473.                 screen.print(30, 150, "OK! (".. sgp_sat_number.." satelites)" , sf_palette[1])
  1474.                 screen.waitvblankstart(200)
  1475.                 screen.flip()  
  1476.             end    
  1477.            
  1478.            
  1479. --      elseif entrada:home() then
  1480.         elseif sf_entrada:start() then
  1481.             SF_cambiarConfiguracion()
  1482.        
  1483.         elseif sf_l_pressed and sf_r_pressed then
  1484.             SF_screenshot()    
  1485.            
  1486.         end
  1487.  
  1488.         sf_entrada_ant = sf_entrada
  1489.     end
  1490. end
  1491.  
  1492. function SF_screenshot()
  1493.     screen.toimage():save("../../../../PICTURE/captura_"..sgp_time.year.."_"..sgp_time.month.."_".. sgp_time.day.."_".. sgp_time.hour.."_".. sgp_time.min.."_".. sgp_time.sec..".png")
  1494.     SF_erase (0,262,80)
  1495.     screen.print(0, 262, "Screenshot saved in a PNG file.",  sf_color_white)
  1496.     screen.waitvblankstart()
  1497.     screen.flip()          
  1498.     screen.waitvblankstart(200)
  1499. end
  1500.  
  1501. function SF_constants()
  1502.  
  1503.     const_xmnpda = 1440.0
  1504.     -- Constante gravitacional de la tierra
  1505.     const_ge = 398600.8
  1506.     const_ae = 1.0
  1507.     const_tothrd = 2.0/3.0
  1508.     -- Radio ecuatorial de la tierra en kilómetros
  1509.     const_xkmper = 6378.135
  1510.     -- Achatamiento de la tierra
  1511.     const_f = 1.0/298.26
  1512.     -- Armónico J2
  1513.     const_J2 = 1.0826158e-3
  1514.     -- Armónico J3
  1515.     const_J3 = -2.53881e-6
  1516.     -- Armónico J4
  1517.     const_J4 = -1.65597e-6
  1518.     const_ck2 = const_J2/2.0
  1519.     const_ck4 = -3.0*const_J4/8.0
  1520.     const_xj3 = const_J3
  1521.     const_qo = const_ae + (120.0/const_xkmper)
  1522.     const_s = const_ae + (78/const_xkmper)
  1523.     const_e6a = 0.000001
  1524.     -- Código de inialización del modelo Deep-Space
  1525.     const_dpinit = 1
  1526.     -- Código secular del modelo Deep-Space
  1527.     const_dpsec = 2
  1528.     -- Código periódico del modelo Deep-Space
  1529.     const_dpper = 3
  1530.     -- Radio solar en kilómetros
  1531.     const_sr = 696000.0
  1532.     -- Unidad Astronómica (distancia de la Tierra al Sol en kilómetros)
  1533.     const_AU = 1.49597870e8
  1534.     -- Minutos por dia
  1535.     const_xmnpda = 1440.0
  1536.     -- Segundos por dia
  1537.     const_secday = 86400.0  
  1538.     -- Rotaciones de la tierra por día sideral (no-constante)
  1539.     const_omega_E = 1.00273790934
  1540.     -- Rotacion de la tierra, radianes por dia sideral
  1541.     const_omega_ER = const_omega_E*(math.pi*2.0)
  1542.     -- Sqrt(ge) ER^3/min^2
  1543.     const_xke = math.sqrt(3600.0*const_ge/(const_xkmper^3))
  1544.     -- (qo-s)^4 ER^4  
  1545.     const_qoms2t = (const_qo-const_s)^4        
  1546.     -- Twilight elevations
  1547.     const_twilightCivil = -6
  1548.     const_twilightNautical = -12
  1549.     const_twilightAstronomical = -18
  1550.     const_mfactor = (math.pi*2.0)*const_omega_E/const_secday
  1551.    
  1552.     const_julian_minute = 0.000694446
  1553.    
  1554.    
  1555.     --Constantes para calcular la posicion de la luna
  1556.    
  1557.     const_epoch =   2444238.5      --1980 January 0.0
  1558.  
  1559.     --Constants defining the Sun's apparent orbit
  1560.  
  1561.     const_elonge    =   278.833540     --Ecliptic longitude of the Sun at epoch 1980.0
  1562.     const_elongp    =   282.596403     --Ecliptic longitude of the Sun at perigee
  1563.     const_eccent    =   0.016718       --Eccentricity of Earth's orbit
  1564.     const_sunangsiz =   0.533128       --Sun's angular size, degrees, at semi-major axis distance
  1565.     const_AstronomicalUnit =  149597870.0       -- Astronomical unit in kilometres
  1566.     const_SunSMAX   =  (const_AstronomicalUnit * 1.000001018) -- Semi-major axis of Earth's orbit
  1567.    
  1568.     --Elements of the Moon's orbit, epoch 1980.0
  1569.     const_mmlong    =   64.975464      --Moon's mean longitude at the epoch
  1570.     const_mmlongp   =   349.383063     --Mean longitude of the perigee at the epoch
  1571.     const_mlnode    =   151.950429     --Mean longitude of the node at the epoch
  1572.     const_minc      =   5.145396       --Inclination of the Moon's orbit
  1573.     const_mecc      =   0.054900       --Eccentricity of the Moon's orbit
  1574.     const_mangsiz   =   0.5181         --Moon's angular size at distance a from Earth
  1575.     const_msmax     =   384401.0       --Semi-major axis of Moon's orbit in km
  1576.     const_mparallax =   0.9507         --Parallax at distance a from Earth
  1577.  
  1578.     const_SynMonth  =   29.53058868
  1579.  
  1580.     const_JulianCentury =   36525.0 -- Days in Julian century
  1581.     const_J2000 =   2451545.0
  1582.    
  1583.     const_NTERMS =  60
  1584.  
  1585. end
  1586.  
  1587.  
  1588. function SF_loadConfigFromFile(filename)   
  1589.     -- Miramos primero que el fichero exista
  1590.     local file = io.open(filename)
  1591.     if file then
  1592.         io.close(file)
  1593.     else
  1594.         return false
  1595.     end
  1596.  
  1597.     for line in io.lines(filename) do  
  1598.         local c = string.find(line," ",1,true)
  1599.         local str = string.sub(line,1,c-1)
  1600.         if str == "sun_and_moon" then
  1601.             sf_sky.show.sunAndMoon = (string.sub(line,c+1,string.len(line)) == "true")     
  1602.         elseif str == "satellites" then
  1603.             sf_sky.show.satellites = (string.sub(line,c+1,string.len(line)) == "true")
  1604.         elseif str == "stars" then
  1605.             sf_sky.show.stars = (string.sub(line,c+1,string.len(line)) == "true")
  1606.         elseif str == "auto_magnitude" then
  1607.             sf_sky.show.autoMagStars = (string.sub(line,c+1,string.len(line)) == "true")
  1608.         elseif str == "satellite_names" then
  1609.             sf_sky.show.satelliteNames = (string.sub(line,c+1,string.len(line)) == "true")
  1610.         elseif str == "constellation_names" then
  1611.             sf_sky.show.constellationNames = (string.sub(line,c+1,string.len(line)) == "true")
  1612.         elseif str == "elevation_lines" then
  1613.             sf_sky.show.elevationLines = (string.sub(line,c+1,string.len(line)) == "true")
  1614.         elseif str == "color_day_r" then
  1615.             sf_color_day.r = tonumber(string.sub(line,c+1,string.len(line)))
  1616.         elseif str == "color_day_g" then
  1617.             sf_color_day.g = tonumber(string.sub(line,c+1,string.len(line)))
  1618.         elseif str == "color_day_b" then
  1619.             sf_color_day.b = tonumber(string.sub(line,c+1,string.len(line)))
  1620.         elseif str == "color_night_r" then
  1621.             sf_color_night.r = tonumber(string.sub(line,c+1,string.len(line)))
  1622.         elseif str == "color_night_g" then
  1623.             sf_color_night.g = tonumber(string.sub(line,c+1,string.len(line)))
  1624.         elseif str == "color_night_b" then
  1625.             sf_color_night.b = tonumber(string.sub(line,c+1,string.len(line)))
  1626.         end
  1627.     end
  1628.    
  1629.     return true
  1630.    
  1631. end
  1632.  
  1633. function SF_saveConfigToFile ( filename )
  1634.     local f = io.open (filename, "w+")
  1635.    
  1636.     if sf_sky.show.sunAndMoon then
  1637.         f:write("sun_and_moon true\n")
  1638.     else
  1639.         f:write("sun_and_moon false\n")
  1640.     end
  1641.    
  1642.     if sf_sky.show.satellites then
  1643.         f:write("satellites true\n" )
  1644.     else
  1645.         f:write("satellites false\n")
  1646.     end
  1647.  
  1648.     if sf_sky.show.stars then
  1649.         f:write("stars true\n" )
  1650.     else
  1651.         f:write("stars false\n")
  1652.     end
  1653.  
  1654.     if sf_sky.show.autoMagStars then
  1655.         f:write("auto_magnitude true\n")
  1656.     else
  1657.         f:write("auto_magnitude false\n")
  1658.     end
  1659.    
  1660.     if sf_sky.show.satelliteNames then
  1661.         f:write("satellite_names true\n" )
  1662.     else
  1663.         f:write("satellite_names false\n")
  1664.     end
  1665.  
  1666.     if sf_sky.show.constellationNames then
  1667.         f:write("constellation_names true\n" )
  1668.     else
  1669.         f:write("constellation_names false\n")
  1670.     end
  1671.    
  1672.     if sf_sky.show.elevationLines then
  1673.         f:write("elevation_lines true\n" )
  1674.     else
  1675.         f:write("elevation_lines false\n")
  1676.     end
  1677.    
  1678.     f:write("color_day_r "..sf_color_day.r .. "\n")
  1679.     f:write("color_day_g "..sf_color_day.g .. "\n")
  1680.     f:write("color_day_b "..sf_color_day.b .. "\n")
  1681.     f:write("color_night_r "..sf_color_night.r .. "\n")
  1682.     f:write("color_night_g "..sf_color_night.g .. "\n")
  1683.     f:write("color_night_b "..sf_color_night.b .. "\n")
  1684.    
  1685.     f:flush()
  1686.     io.close(f)
  1687.    
  1688. end
  1689.  
  1690. function SF_cambiarConfiguracion()
  1691.     local linea = 1
  1692.     local opciones = 15
  1693.    
  1694.     local changed = false
  1695.  
  1696.     local current_lat, current_lon, current_alt
  1697.     local NS, EW
  1698.    
  1699.     if sgp_location.lat >= 0 then
  1700.         current_lat = string.format ("%.2f",sgp_location.lat)
  1701.         NS = "N"
  1702.     else
  1703.         current_lat = string.format ("%.2f",sgp_location.lat*-1)
  1704.         NS = "S"   
  1705.     end
  1706.     if sgp_location.lat < 10.0 and sgp_location.lat > -10.0 then
  1707.         current_lat = "0" .. current_lat
  1708.     end
  1709.    
  1710.     if sgp_location.lon >= 0 then
  1711.         current_lon = string.format ("%.2f",sgp_location.lon)
  1712.         EW = "E"
  1713.     else
  1714.         current_lon = string.format ("%.2f",sgp_location.lon*-1)
  1715.         EW = "W"
  1716.     end
  1717.    
  1718.     if sgp_location.lon < 100.0 and sgp_location.lon > -100.0 then
  1719.         current_lon = "0" .. current_lon
  1720.     end
  1721.     if sgp_location.lon < 10.0 and sgp_location.lon > -10.0 then
  1722.         current_lon = "0" .. current_lon
  1723.     end
  1724.    
  1725.     current_alt = string.format ("%04d",sgp_location.alt)
  1726.    
  1727.    
  1728.     current_UTC_offset = "GMT" .. string.format ("%+03d",sgp_location.UTC_offset)
  1729.     current_name = sgp_location.name
  1730.  
  1731.    
  1732.     local pintar_cursor = true
  1733.     local cursor_x = 428
  1734.     repeat
  1735.         screen.clear(sf_sky_color)
  1736.         screen.print(1,1,"----- OPCIONES ------------------------------------------------------", sf_palette[1])
  1737.         SF_drawInfoBar(false)
  1738.         screen.print(1,11, "Elementos del cielo disponibles:" , sf_palette[8])
  1739.  
  1740.         screen.print(1, 21, "    - Sol y Luna" , sf_palette[4])
  1741.         if sf_sky.show.sunAndMoon then
  1742.             screen.print(420, 21, "[x]" , sf_palette[4])
  1743.         else
  1744.             screen.print(420, 21, "[ ]" , sf_palette[4])
  1745.         end
  1746.        
  1747.         screen.print(1,31, "    - Satelites" , sf_palette[4])
  1748.         if sf_sky.show.satellites then
  1749.             screen.print(420, 31, "[x]" , sf_palette[4])
  1750.         else
  1751.             screen.print(420, 31, "[ ]" , sf_palette[4])
  1752.         end
  1753.        
  1754.  
  1755.         screen.print(1,41, "    - Estrellas" , sf_palette[4])
  1756.         if sf_sky.show.stars then
  1757.             screen.print(420, 41, "[x]" , sf_palette[4])
  1758.         else
  1759.             screen.print(420, 41, "[ ]" , sf_palette[4])
  1760.         end
  1761.  
  1762.         screen.print(1,51, "    - Magnitud de estrellas automatico" , sf_palette[4])
  1763.         if sf_sky.show.autoMagStars then
  1764.             screen.print(420, 51, "[x]" , sf_palette[4])
  1765.         else
  1766.             screen.print(420, 51, "[ ]" , sf_palette[4])
  1767.         end
  1768.  
  1769.         screen.print(1,61, "    - Nombre de los satelites" , sf_palette[4])
  1770.         if sf_sky.show.satelliteNames then
  1771.             screen.print(420, 61, "[x]" , sf_palette[4])
  1772.         else
  1773.             screen.print(420, 61, "[ ]" , sf_palette[4])
  1774.         end
  1775.        
  1776.        
  1777.         screen.print(1, 71, "    - Nombre de las constelaciones" , sf_palette[4])
  1778.         if sf_sky.show.constellationNames then
  1779.             screen.print(420, 71, "[x]" , sf_palette[4])
  1780.         else
  1781.             screen.print(420, 71, "[ ]" , sf_palette[4])
  1782.         end
  1783.    
  1784.         screen.print(1, 81, "    - Lineas de referencia en 30, 60 y 90 grados" , sf_palette[4])
  1785.         if sf_sky.show.elevationLines then
  1786.             screen.print(420, 81, "[x]" , sf_palette[4])
  1787.         else
  1788.             screen.print(420, 81, "[ ]" , sf_palette[4])
  1789.         end
  1790.  
  1791.         screen.print(1, 91,  "Coordenadas de localizacion:" , sf_palette[8])
  1792.         screen.print(1, 101, "    - Latitud", sf_palette[4])
  1793.         screen.print(380, 101, "[" .. current_lat .. NS .. "]", sf_palette[4])
  1794.         screen.print(1, 111, "    - Longitud", sf_palette[4])
  1795.         screen.print(372, 111, "[" .. current_lon .. EW .. "]", sf_palette[4])
  1796.         screen.print(1, 121, "    - Altitud (en metros)", sf_palette[4])
  1797.         screen.print(396, 121, "[" .. current_alt .. "]" , sf_palette[4])
  1798.         screen.print(1, 131, "    - Zona horaria", sf_palette[4])
  1799.         screen.print(380, 131, "[" .. current_UTC_offset .. "]" , sf_palette[4])
  1800.         screen.print(1, 141,  "Colores de dia y noche:" , sf_palette[8])       
  1801.         screen.print(1, 151, "    - Dia:",sf_palette[4])
  1802.         screen.print(124, 151, "Rojo ["..string.format ("%03d",sf_color_day.r).."]    Verde ["..string.format ("%03d",sf_color_day.g).."]     Azul ["..string.format ("%03d",sf_color_day.b).."]", sf_palette[4])
  1803.         screen.print(1, 161, "    - Noche:",sf_palette[4])  
  1804.         screen.print(124, 161, "Rojo ["..string.format ("%03d",sf_color_night.r).."]    Verde ["..string.format ("%03d",sf_color_night.g).."]     Azul ["..string.format ("%03d",sf_color_night.b).."]", sf_palette[4])
  1805.         screen.print(1, 251, "Pulsa X para modificar la opcion seleccionada.", sf_palette[8])      
  1806.         screen.print(1, 261, "Pulsa O para salir de las opciones.", sf_palette[8])
  1807.        
  1808.         if pintar_cursor then
  1809.             screen.print(cursor_x,13 + 10 * linea, "_", sf_palette[1])
  1810.             pintar_cursor = false
  1811.         else
  1812.             pintar_cursor = true
  1813.         end
  1814.  
  1815.         screen.waitvblankstart()
  1816.         screen.flip()      
  1817.    
  1818.        
  1819.         sf_entrada = Controls.read()
  1820.         if sf_entrada:up() then
  1821.             if linea > 1 then
  1822.                 linea = linea - 1
  1823.                 if linea == 8 then
  1824.                     linea = linea - 1
  1825.                 end
  1826.             end
  1827.             pintar_cursor = true
  1828.         elseif sf_entrada:down() then
  1829.             if linea < opciones then
  1830.                 linea = linea + 1
  1831.                 if linea == 8 then
  1832.                     linea = linea + 1
  1833.                 elseif linea == 13 then
  1834.                     linea = linea + 1
  1835.                 end
  1836.            
  1837.             end
  1838.             pintar_cursor = true
  1839.         elseif sf_entrada ~= sf_entrada_ant then
  1840.             sf_entrada_ant = sf_entrada
  1841.             if sf_entrada:cross() then
  1842.                 if linea == 1 then
  1843.                     sf_sky.show.sunAndMoon = not sf_sky.show.sunAndMoon
  1844.                 elseif linea == 2 then
  1845.                     sf_sky.show.satellites = not sf_sky.show.satellites    
  1846.                 elseif linea == 3 then
  1847.                     sf_sky.show.stars = not sf_sky.show.stars
  1848.                 elseif linea == 4 then
  1849.                     sf_sky.show.autoMagStars = not sf_sky.show.autoMagStars
  1850.                 elseif linea == 5 then
  1851.                     sf_sky.show.satelliteNames = not sf_sky.show.satelliteNames                
  1852.                 elseif linea == 6 then
  1853.                     sf_sky.show.constellationNames = not sf_sky.show.constellationNames
  1854.                 elseif linea == 7 then
  1855.                     sf_sky.show.elevationLines = not sf_sky.show.elevationLines
  1856.                 elseif linea == 9 then
  1857.                     SF_editLatitud()
  1858.                     if sgp_location.lat >= 0 then
  1859.                         current_lat = string.format ("%.2f",sgp_location.lat)
  1860.                         NS = "N"
  1861.                     else
  1862.                         current_lat = string.format ("%.2f",sgp_location.lat*-1)
  1863.                         NS = "S"   
  1864.                     end
  1865.                     if sgp_location.lat < 10.0 and sgp_location.lat > -10.0 then
  1866.                         current_lat = "0" .. current_lat
  1867.                     end
  1868.                     changed = true
  1869.                 elseif linea == 10 then
  1870.                     SF_editLongitud()
  1871.                     if sgp_location.lon >= 0 then
  1872.                         current_lon = string.format ("%.2f",sgp_location.lon)
  1873.                         EW = "E"
  1874.                     else
  1875.                         current_lon = string.format ("%.2f",sgp_location.lon*-1)
  1876.                         EW = "W"
  1877.                     end
  1878.                    
  1879.                     if sgp_location.lon < 100.0 and sgp_location.lon > -100.0 then
  1880.                         current_lon = "0" .. current_lon
  1881.                     end
  1882.                     if sgp_location.lon < 10.0 and sgp_location.lon > -10.0 then
  1883.                         current_lon = "0" .. current_lon
  1884.                     end
  1885.                     changed = true
  1886.                 elseif linea == 11 then
  1887.                     SF_editAltitud()
  1888.                     current_alt = string.format ("%04d",sgp_location.alt)
  1889.                     changed = true
  1890.                 elseif linea == 12 then
  1891.                     SF_editUtcOffset()
  1892.                     current_UTC_offset = "GMT" .. string.format ("%+03d",sgp_location.UTC_offset)
  1893.                     changed = true
  1894.                 elseif linea == 14 then
  1895.                     SF_editColorDay()
  1896.                     if sf_vision_nocturna then
  1897.                         SF_changePalette (sf_color_night.r,sf_color_night.g,sf_color_night.b)
  1898.                     else
  1899.                         SF_changePalette (sf_color_day.r,sf_color_day.g,sf_color_day.b)
  1900.                     end
  1901.                 elseif linea == 15 then
  1902.                     SF_editColorNight()
  1903.                     if sf_vision_nocturna then
  1904.                         SF_changePalette (sf_color_night.r,sf_color_night.g,sf_color_night.b)
  1905.                     else
  1906.                         SF_changePalette (sf_color_day.r,sf_color_day.g,sf_color_day.b)
  1907.                     end
  1908.                 end
  1909.                
  1910.             end
  1911.         end
  1912.        
  1913.         sf_entrada_ant = sf_entrada
  1914.         screen.waitvblankstart(10) 
  1915.     until sf_entrada:circle()
  1916.  
  1917.     if changed then
  1918.         SF_initTails()
  1919.         SF_enableAllSatellites()
  1920.         SF_enableAllStars()
  1921.         sgp_location.name = "Personalizado"
  1922.         sgp_location.polar.lat = math.rad(sgp_location.lat)
  1923.         sgp_location.polar.lon = math.rad(SGP_modulus(sgp_location.lon,360))
  1924.         sgp_location.polar.alt = sgp_location.alt * 0.001
  1925.     end
  1926.     SF_saveConfigToFile ("config.txt")
  1927.  
  1928. end
  1929.  
  1930. function SF_editLatitud()
  1931.     local current_lat
  1932.     local NS
  1933.    
  1934.     if sgp_location.lat >= 0 then
  1935.         current_lat = string.format ("%.2f",sgp_location.lat)
  1936.         NS = "N"
  1937.     else
  1938.         current_lat = string.format ("%.2f",sgp_location.lat*-1)
  1939.         NS = "S"   
  1940.     end
  1941.     if sgp_location.lat < 10.0 and sgp_location.lat > -10.0 then
  1942.         current_lat = "0" .. current_lat
  1943.     end
  1944.    
  1945.     --Edicion latitud
  1946.     local edit_x, edit_y, edit_pos, edit_len
  1947.     edit_x = 388
  1948.     edit_y = 101
  1949.     edit_pos = 1
  1950.     edit_len = 6
  1951.     local lat = { }
  1952.     lat[1] = string.sub(current_lat,1,1)
  1953.     lat[2] = string.sub(current_lat,2,2)
  1954.     lat[3] = string.sub(current_lat,3,3)
  1955.     lat[4] = string.sub(current_lat,4,4)
  1956.     lat[5] = string.sub(current_lat,5,5)
  1957.     lat[6] = NS
  1958.    
  1959.     local pintar_cursor = true
  1960.     while true do
  1961.         draw.fillrect(edit_x, edit_y, edit_len * 8 , 9, sf_color_black)
  1962.         screen.print(edit_x, edit_y, lat[1] .. lat[2] .. lat[3] .. lat[4] .. lat[5] .. lat[6] .. "]",sf_palette[1])
  1963.         if pintar_cursor then
  1964.             screen.print(edit_x + (edit_pos-1) * 8,edit_y + 2, "_", sf_palette[1])
  1965.             pintar_cursor = false
  1966.         else
  1967.             pintar_cursor = true
  1968.         end
  1969.  
  1970.         screen.waitvblankstart()
  1971.         screen.flip()  
  1972.        
  1973.         sf_entrada = Controls.read()
  1974.        
  1975.        
  1976.         if sf_entrada:right() then
  1977.             if edit_pos < edit_len then
  1978.                 edit_pos = edit_pos + 1
  1979.             end
  1980.             pintar_cursor = true
  1981.         elseif sf_entrada:left() then
  1982.             if edit_pos > 1 then
  1983.                 edit_pos = edit_pos - 1
  1984.             end
  1985.             pintar_cursor = true
  1986.         elseif sf_entrada:up() then
  1987.             if edit_pos == 1 or edit_pos == 2 or edit_pos == 4 or edit_pos == 5 then
  1988.                 if tonumber(lat[edit_pos]) < 9 then
  1989.                     lat[edit_pos] = lat[edit_pos] + 1
  1990.                 end
  1991.             elseif edit_pos == 6 then
  1992.                 lat[edit_pos] = "N"
  1993.             end
  1994.             pintar_cursor = true
  1995.         elseif sf_entrada:down() then
  1996.             if edit_pos == 1 or edit_pos == 2 or edit_pos == 4 or edit_pos == 5 then
  1997.                 if tonumber(lat[edit_pos]) > 0 then
  1998.                     lat[edit_pos] = lat[edit_pos] - 1
  1999.                 end
  2000.             elseif edit_pos == 6 then
  2001.                 lat[edit_pos] = "S"
  2002.             end
  2003.             pintar_cursor = true
  2004.         elseif sf_entrada ~= sf_entrada_ant then
  2005.             sf_entrada_ant = sf_entrada
  2006.             if sf_entrada:cross() then
  2007.                 if lat[6] == "N" then
  2008.                     sgp_location.lat = tonumber(lat[1]..lat[2]..lat[3]..lat[4]..lat[5])
  2009.                 else
  2010.                     sgp_location.lat = -tonumber(lat[1]..lat[2]..lat[3]..lat[4]..lat[5])
  2011.                 end
  2012.                 return
  2013.             end
  2014.         end
  2015.        
  2016.         if lat[1] == 9 then
  2017.             lat[2] = 0
  2018.             lat[4] = 0
  2019.             lat[5] = 0
  2020.         end
  2021.        
  2022.         sf_entrada_ant = sf_entrada
  2023.        
  2024.         screen.waitvblankstart(10) 
  2025.     end
  2026.    
  2027.  
  2028.  
  2029. end
  2030.    
  2031. function SF_editLongitud() 
  2032.     if sgp_location.lon >= 0 then
  2033.         current_lon = string.format ("%.2f",sgp_location.lon)
  2034.         EW = "E"
  2035.     else
  2036.         current_lon = string.format ("%.2f",sgp_location.lon*-1)
  2037.         EW = "W"
  2038.     end
  2039.    
  2040.     if sgp_location.lon < 100.0 and sgp_location.lon > -100.0 then
  2041.         current_lon = "0" .. current_lon
  2042.     end
  2043.     if sgp_location.lon < 10.0 and sgp_location.lon > -10.0 then
  2044.         current_lon = "0" .. current_lon
  2045.     end
  2046.     --Edicion longitud
  2047.    
  2048.     edit_x = 380
  2049.     edit_y = 111
  2050.     edit_pos = 1
  2051.     edit_len = 7
  2052.     local lon = { }
  2053.     lon[1] = string.sub(current_lon,1,1)
  2054.     lon[2] = string.sub(current_lon,2,2)
  2055.     lon[3] = string.sub(current_lon,3,3)
  2056.     lon[4] = string.sub(current_lon,4,4)
  2057.     lon[5] = string.sub(current_lon,5,5)
  2058.     lon[6] = string.sub(current_lon,6,6)
  2059.     lon[7] = EW
  2060.    
  2061.     pintar_cursor = true
  2062.     while true do
  2063.         draw.fillrect(edit_x, edit_y, edit_len * 8 , 9, sf_color_black)
  2064.         screen.print(edit_x, edit_y, lon[1] .. lon[2] .. lon[3] .. lon[4] .. lon[5] .. lon[6] .. lon[7] .. "]",sf_palette[1])
  2065.         if pintar_cursor then
  2066.             screen.print(edit_x + (edit_pos-1) * 8,edit_y + 2, "_", sf_palette[1])
  2067.             pintar_cursor = false
  2068.         else
  2069.             pintar_cursor = true
  2070.         end
  2071.  
  2072.         screen.waitvblankstart()
  2073.         screen.flip()  
  2074.        
  2075.         local sf_entrada = Controls.read()
  2076.        
  2077.         if sf_entrada:right() then
  2078.             if edit_pos < edit_len then
  2079.                 edit_pos = edit_pos + 1
  2080.             end
  2081.             pintar_cursor = true
  2082.         elseif sf_entrada:left() then
  2083.             if edit_pos > 1 then
  2084.                 edit_pos = edit_pos - 1
  2085.             end
  2086.             pintar_cursor = true
  2087.         elseif sf_entrada:up() then
  2088.             if edit_pos == 1 or edit_pos == 2 or edit_pos == 3 or edit_pos == 5 or edit_pos == 6 then
  2089.                 if tonumber(lon[edit_pos]) < 9 then
  2090.                     lon[edit_pos] = lon[edit_pos] + 1
  2091.                 end
  2092.             elseif edit_pos == 7 then
  2093.                 lon[edit_pos] = "E"
  2094.             end
  2095.             pintar_cursor = true
  2096.         elseif sf_entrada:down() then
  2097.             if edit_pos == 1 or edit_pos == 2 or edit_pos == 3 or edit_pos == 5 or edit_pos == 6 then
  2098.                 if tonumber(lon[edit_pos]) > 0 then
  2099.                     lon[edit_pos] = lon[edit_pos] - 1
  2100.                 end
  2101.             elseif edit_pos == 7 then
  2102.                 lon[edit_pos] = "W"
  2103.             end
  2104.             pintar_cursor = true
  2105.         elseif sf_entrada ~= sf_entrada_ant then
  2106.             sf_entrada_ant = sf_entrada
  2107.             if sf_entrada:cross() then
  2108.                 if lon[7] == "E" then
  2109.                     sgp_location.lon = tonumber(lon[1]..lon[2]..lon[3]..lon[4]..lon[5]..lon[6])
  2110.                 else
  2111.                     sgp_location.lon = tonumber(lon[1]..lon[2]..lon[3]..lon[4]..lon[5]..lon[6])*-1
  2112.                 end
  2113.                 return
  2114.             end
  2115.         end
  2116.        
  2117.         if tonumber(lon[1]) > 1 then
  2118.             lon[1] = 1
  2119.             lon[2] = 8
  2120.             lon[3] = 0
  2121.             lon[5] = 0
  2122.             lon[6] = 0
  2123.         elseif tonumber(lon[1]) == 1 and tonumber(lon[2]) > 8 then
  2124.             lon[2] = 8
  2125.             lon[3] = 0     
  2126.             lon[5] = 0
  2127.             lon[6] = 0
  2128.         elseif tonumber(lon[1]) == 1 and tonumber(lon[2]) == 8 then
  2129.             lon[3] = 0     
  2130.             lon[5] = 0
  2131.             lon[6] = 0     
  2132.         end
  2133.        
  2134.         sf_entrada_ant = sf_entrada
  2135.         screen.waitvblankstart(10) 
  2136.     end
  2137.    
  2138. end
  2139.  
  2140. function SF_editAltitud ()
  2141.     current_alt = string.format ("%04d",sgp_location.alt)
  2142.     --Edicion altitud
  2143.     edit_x = 404
  2144.     edit_y = 121
  2145.     edit_pos = 1
  2146.     edit_len = 4
  2147.     local alt = { }
  2148.     alt[1] = string.sub(current_alt,1,1)
  2149.     alt[2] = string.sub(current_alt,2,2)
  2150.     alt[3] = string.sub(current_alt,3,3)
  2151.     alt[4] = string.sub(current_alt,4,4)
  2152.  
  2153.     pintar_cursor = true
  2154.     while true do
  2155.         draw.fillrect(edit_x, edit_y, edit_len * 8 , 9, sf_color_black)
  2156.         screen.print(edit_x, edit_y, alt[1] .. alt[2] .. alt[3] .. alt[4],sf_palette[1])
  2157.         if pintar_cursor then
  2158.             screen.print(edit_x + (edit_pos-1) * 8,edit_y + 2, "_", sf_palette[1])
  2159.             pintar_cursor = false
  2160.         else
  2161.             pintar_cursor = true
  2162.         end
  2163.  
  2164.         screen.waitvblankstart()
  2165.         screen.flip()  
  2166.        
  2167.         sf_entrada = Controls.read()
  2168.        
  2169.         if sf_entrada:right() then
  2170.             if edit_pos < edit_len then
  2171.                 edit_pos = edit_pos + 1
  2172.             end
  2173.             pintar_cursor = true
  2174.         elseif sf_entrada:left() then
  2175.             if edit_pos > 1 then
  2176.                 edit_pos = edit_pos - 1
  2177.             end
  2178.             pintar_cursor = true
  2179.         elseif sf_entrada:up() then
  2180.             if tonumber(alt[edit_pos]) < 9 then
  2181.                 alt[edit_pos] = alt[edit_pos] + 1
  2182.             end
  2183.             pintar_cursor = true
  2184.         elseif sf_entrada:down() then
  2185.             if tonumber(alt[edit_pos]) > 0 then
  2186.                 alt[edit_pos] = alt[edit_pos] - 1
  2187.             end
  2188.             pintar_cursor = true
  2189.         elseif sf_entrada ~= sf_entrada_ant then
  2190.             sf_entrada_ant = sf_entrada
  2191.             if sf_entrada:cross() then
  2192.                 sgp_location.alt = tonumber(alt[1]..alt[2]..alt[3]..alt[4])
  2193.                 return
  2194.             end
  2195.         end
  2196.        
  2197.         sf_entrada_ant = sf_entrada
  2198.         screen.waitvblankstart(10) 
  2199.     end
  2200.    
  2201. end
  2202.  
  2203.    
  2204. function SF_editUtcOffset()
  2205.    
  2206.     current_UTC_offset = "GMT" .. string.format ("%+03d",sgp_location.UTC_offset)
  2207.    
  2208.     --Edicion UTC Offset
  2209.    
  2210.     edit_x = 388
  2211.     edit_y = 131
  2212.     edit_pos = 4
  2213.     edit_len = 6
  2214.     local utc_off = sgp_location.UTC_offset
  2215.    
  2216.     pintar_cursor = true
  2217.     while true do
  2218.         draw.fillrect(edit_x, edit_y, edit_len * 8 , 9, sf_color_black)
  2219.         screen.print(edit_x, edit_y, "GMT" .. string.format ("%+03d",utc_off),sf_palette[1])
  2220.         if pintar_cursor then
  2221.             screen.print(edit_x + (edit_pos-1) * 8,edit_y + 2, "___", sf_palette[1])
  2222.             pintar_cursor = false
  2223.         else
  2224.             pintar_cursor = true
  2225.         end
  2226.  
  2227.         screen.waitvblankstart()
  2228.         screen.flip()  
  2229.        
  2230.         local sf_entrada = Controls.read()
  2231.        
  2232.  
  2233.         if sf_entrada:up() then
  2234.             if utc_off < 12 then
  2235.                 utc_off = utc_off + 1
  2236.             end
  2237.         elseif sf_entrada:down() then
  2238.             if utc_off > -12 then
  2239.                 utc_off = utc_off - 1
  2240.             end
  2241.         elseif sf_entrada ~= sf_entrada_ant then
  2242.             sf_entrada_ant = sf_entrada
  2243.             if sf_entrada:cross() then
  2244.                 sgp_location.UTC_offset = utc_off
  2245.                 return
  2246.             end
  2247.         end
  2248.        
  2249.         sf_entrada_ant = sf_entrada
  2250.         screen.waitvblankstart(10) 
  2251.     end
  2252. end
  2253.  
  2254. function SF_editColorDay()
  2255.     --Edicion UTC Offset
  2256.    
  2257.     edit_x = 172
  2258.     edit_y = 151
  2259.     edit_pos = 2
  2260.     edit_len = 3
  2261.     edit_rgb = 1
  2262.  
  2263.     pintar_cursor = true
  2264.     while true do
  2265.         local edit_color = color.new(sf_color_day.r,sf_color_day.g,sf_color_day.b)
  2266.         draw.fillrect(edit_x, edit_y, edit_len * 8 , 9, sf_color_black)
  2267.         screen.print(edit_x, edit_y, string.format ("%03d",sf_color_day.r),edit_color)
  2268.         draw.fillrect(edit_x + 120, edit_y, edit_len * 8 , 9, sf_color_black)
  2269.         screen.print(edit_x+ 120, edit_y, string.format ("%03d",sf_color_day.g),edit_color)
  2270.         draw.fillrect(edit_x+ 240, edit_y, edit_len * 8 , 9, sf_color_black)
  2271.         screen.print(edit_x+ 240, edit_y, string.format ("%03d",sf_color_day.b),edit_color)
  2272.         if pintar_cursor then
  2273.             screen.print(edit_x + (edit_pos-2) * 8 + (edit_rgb-1)*120 ,edit_y + 2, "___", sf_palette[1])
  2274.             pintar_cursor = false
  2275.         else
  2276.             pintar_cursor = true
  2277.         end
  2278.  
  2279.         screen.waitvblankstart()
  2280.         screen.flip()  
  2281.        
  2282.         local sf_entrada = Controls.read()
  2283.        
  2284.  
  2285.         if sf_entrada:up() then
  2286.             if edit_rgb == 1 then
  2287.                 if sf_color_day.r + 5 <= 255 then
  2288.                     sf_color_day.r = sf_color_day.r + 5
  2289.                 end
  2290.             elseif edit_rgb == 2 then
  2291.                 if sf_color_day.g + 5 <= 255 then
  2292.                     sf_color_day.g = sf_color_day.g + 5
  2293.                 end
  2294.             elseif edit_rgb == 3 then
  2295.                 if sf_color_day.b + 5 <= 255 then
  2296.                     sf_color_day.b = sf_color_day.b + 5
  2297.                 end            
  2298.             end
  2299.         elseif sf_entrada:down() then
  2300.             if edit_rgb == 1 then
  2301.                 if sf_color_day.r - 5 >= 0 then
  2302.                     sf_color_day.r = sf_color_day.r - 5
  2303.                 end
  2304.             elseif edit_rgb == 2 then
  2305.                 if sf_color_day.g - 5 >= 0 then
  2306.                     sf_color_day.g = sf_color_day.g - 5
  2307.                 end
  2308.             elseif edit_rgb == 3 then
  2309.                 if sf_color_day.b - 5 >= 0 then
  2310.                     sf_color_day.b = sf_color_day.b - 5
  2311.                 end            
  2312.             end
  2313.         elseif sf_entrada ~= sf_entrada_ant then
  2314.             sf_entrada_ant = sf_entrada
  2315.             if sf_entrada:left() then
  2316.                 if edit_rgb > 1 then
  2317.                     edit_rgb = edit_rgb - 1
  2318.                 end
  2319.             elseif sf_entrada:right() then
  2320.                 if edit_rgb < 3 then
  2321.                     edit_rgb = edit_rgb + 1
  2322.                 end
  2323.             elseif sf_entrada:cross() then
  2324.                 return
  2325.             end
  2326.         end
  2327.        
  2328.         sf_entrada_ant = sf_entrada
  2329.         screen.waitvblankstart(10) 
  2330.     end
  2331. end
  2332.  
  2333. function SF_editColorNight()
  2334.     --Edicion UTC Offset
  2335.    
  2336.     edit_x = 172
  2337.     edit_y = 161
  2338.     edit_pos = 2
  2339.     edit_len = 3
  2340.     edit_rgb = 1
  2341.  
  2342.     pintar_cursor = true
  2343.     while true do
  2344.         local edit_color = color.new(sf_color_night.r,sf_color_night.g,sf_color_night.b)
  2345.         draw.fillrect(edit_x, edit_y, edit_len * 8 , 9, sf_color_black)
  2346.         screen.print(edit_x, edit_y, string.format ("%03d",sf_color_night.r),edit_color)
  2347.         draw.fillrect(edit_x + 120, edit_y, edit_len * 8 , 9, sf_color_black)
  2348.         screen.print(edit_x+ 120, edit_y, string.format ("%03d",sf_color_night.g),edit_color)
  2349.         draw.fillrect(edit_x+ 240, edit_y, edit_len * 8 , 9, sf_color_black)
  2350.         screen.print(edit_x+ 240, edit_y, string.format ("%03d",sf_color_night.b),edit_color)
  2351.         if pintar_cursor then
  2352.             screen.print(edit_x + (edit_pos-2) * 8 + (edit_rgb-1)*120 ,edit_y + 2, "___", sf_palette[1])
  2353.             pintar_cursor = false
  2354.         else
  2355.             pintar_cursor = true
  2356.         end
  2357.  
  2358.         screen.waitvblankstart()
  2359.         screen.flip()  
  2360.        
  2361.         local sf_entrada = Controls.read()
  2362.        
  2363.  
  2364.         if sf_entrada:up() then
  2365.             if edit_rgb == 1 then
  2366.                 if sf_color_night.r + 5 <= 255 then
  2367.                     sf_color_night.r = sf_color_night.r + 5
  2368.                 end
  2369.             elseif edit_rgb == 2 then
  2370.                 if sf_color_night.g + 5 <= 255 then
  2371.                     sf_color_night.g = sf_color_night.g + 5
  2372.                 end
  2373.             elseif edit_rgb == 3 then
  2374.                 if sf_color_night.b + 5 <= 255 then
  2375.                     sf_color_night.b = sf_color_night.b + 5
  2376.                 end            
  2377.             end
  2378.         elseif sf_entrada:down() then
  2379.             if edit_rgb == 1 then
  2380.                 if sf_color_night.r - 5 >= 0 then
  2381.                     sf_color_night.r = sf_color_night.r - 5
  2382.                 end
  2383.             elseif edit_rgb == 2 then
  2384.                 if sf_color_night.g - 5 >= 0 then
  2385.                     sf_color_night.g = sf_color_night.g - 5
  2386.                 end
  2387.             elseif edit_rgb == 3 then
  2388.                 if sf_color_night.b - 5 >= 0 then
  2389.                     sf_color_night.b = sf_color_night.b - 5
  2390.                 end            
  2391.             end
  2392.         elseif sf_entrada ~= sf_entrada_ant then
  2393.             sf_entrada_ant = sf_entrada
  2394.             if sf_entrada:left() then
  2395.                 if edit_rgb > 1 then
  2396.                     edit_rgb = edit_rgb - 1
  2397.                 end
  2398.             elseif sf_entrada:right() then
  2399.                 if edit_rgb < 3 then
  2400.                     edit_rgb = edit_rgb + 1
  2401.                 end
  2402.             elseif sf_entrada:cross() then
  2403.                 return
  2404.             end
  2405.         end
  2406.        
  2407.         sf_entrada_ant = sf_entrada
  2408.         screen.waitvblankstart(10) 
  2409.     end
  2410. end
  2411.  
  2412. function SF_enableAllSatellites()
  2413.     local s
  2414.     for s = 1,sgp_sat_number do
  2415.         sgp_sats[s].no_calcular = 0
  2416.     end
  2417. end
  2418.  
  2419.  
  2420. function SF_enableAllStars()
  2421.     local s
  2422.     for s = 1,sgp_star_number do
  2423.         sgp_stars[s].no_calcular    = 0
  2424.     end
  2425. end
  2426.  
  2427. function SF_sendHTTPheader(sock, filepath)
  2428.   sock:send("GET " .. filepath .. " HTTP/1.1\r\n")
  2429.   sock:send("Host: celestrak.com\r\n")
  2430.   sock:send("User-Agent: Satfinder 1.3\r\n")
  2431.   sock:send("Accept: */*;q=0.01\r\n")
  2432.   sock:send("Keep-Alive: 300\r\n")
  2433.   sock:send("Connection: keep-alive\r\n")
  2434.   sock:send("\r\n")  
  2435. end
  2436.  
  2437. function SF_downloadTLEs()
  2438.     local tle_filename = "visual"
  2439.     sf_iss = nil
  2440.    
  2441.     screen.clear(sf_sky_color)
  2442.     screen.print(30, 0,"> Inicializando conexion WIFI...", sf_palette[4])
  2443.     screen.waitvblankstart()
  2444.     screen.flip()
  2445.     screen.clear(sf_sky_color)
  2446.     screen.print(30, 0,"> Inicializando conexion WIFI...", sf_palette[4])
  2447.     screen.waitvblankstart()
  2448.     screen.flip()
  2449.     screen.waitvblankstart(200)
  2450.  
  2451.     -- Inicializar modulo WIFI
  2452.     while not ipAddress do
  2453.         os.sleep(0.01*100)
  2454.         wlan.init()
  2455.         wifi, ipAddress = pcall(function() return wlan.getIP() end)
  2456.         if wifi == false then
  2457.             ipAddress = false
  2458.             os.message("No fue posible el acceso a Internet. Por favor, conectese a un punto de acceso.",0)
  2459.             wlan.init()
  2460.         end
  2461.     end
  2462.  
  2463.     screen.print(30, 0,"> Inicializando conexion WIFI...", sf_palette[4])  
  2464.     screen.print(30, 10, "> Obteniendo direccion IP... ".. ipAddress , sf_palette[4])
  2465.     screen.waitvblankstart()
  2466.     screen.flip()
  2467.    
  2468.     screen.print(30, 0,"> Inicializando conexion WIFI...", sf_palette[4])  
  2469.     screen.print(30, 10, "> Obteniendo direccion IP... ".. ipAddress , sf_palette[4])
  2470.     screen.waitvblankstart()
  2471.     screen.flip()
  2472.  
  2473.     screen.print(30, 20, "> Conectando con Celestrak.com..." , sf_palette[1])
  2474.     screen.waitvblankstart()
  2475.     screen.flip()
  2476.  
  2477.     screen.print(30, 20, "> Conectando con Celestrak.com..." , sf_palette[1])
  2478.     screen.waitvblankstart()
  2479.     screen.flip()
  2480.    
  2481.     socket,error = Socket.connect("celestrak.com", 80)
  2482.     while not socket:isConnected() do
  2483.         os.sleep(0.01*100)
  2484.     end
  2485.  
  2486.     screen.print(300, 20, "conectado!. ", sf_palette[1])
  2487.     screen.waitvblankstart()
  2488.     screen.flip()
  2489.    
  2490.     screen.print(300, 20, "conectado!. ", sf_palette[1])
  2491.     screen.waitvblankstart()
  2492.     screen.flip()
  2493.  
  2494.     SF_sendHTTPheader(socket, "/NORAD/elements/".. tle_filename ..".txt")
  2495.  
  2496.     screen.print(30, 30, "> Recibiendo archivo " .. tle_filename .. ".tle", sf_palette[4])
  2497.     screen.waitvblankstart()
  2498.     screen.flip()
  2499.     screen.print(30, 30, "> Recibiendo archivo " .. tle_filename .. ".tle ...", sf_palette[4])
  2500.     screen.waitvblankstart()
  2501.     screen.flip()  
  2502.    
  2503.    
  2504.     -- Obtenemos el fichero TLE
  2505.     local f = io.open ("./tle/"..tle_filename .. ".tle", "w+")
  2506.     local buffer_len = 1
  2507.     reintentos = 20
  2508.     while (buffer_len > 0) do
  2509.         screen.waitvblankstart(5)
  2510.         local buffer = socket:recv(500)
  2511.         if string.len(buffer) > 0 then
  2512.             f:write (buffer)   
  2513.             screen.waitvblankstart(5)
  2514.             screen.flip()
  2515.             screen.waitvblankstart(5)
  2516.             screen.flip()
  2517.             buffer_len = string.len(buffer)
  2518.             reintentos = 20        
  2519.         else
  2520.             if reintentos > 0 then
  2521.                 reintentos = reintentos - 1
  2522.                 screen.waitvblankstart(10)
  2523.                 screen.flip()
  2524.                 screen.waitvblankstart(10)
  2525.                 screen.flip()
  2526.                 buffer_len = 1
  2527.             else
  2528.                 buffer_len = string.len(buffer)
  2529.             end
  2530.         end
  2531.     end
  2532.     f:flush ()
  2533.     io.close (f)
  2534.     socket:close()
  2535.     screen.print(30, 30, "> Recibiendo archivo " .. tle_filename .. ".tle ... OK!", sf_palette[4])
  2536.     screen.waitvblankstart()
  2537.     screen.flip()  
  2538.     screen.print(30, 30, "> Recibiendo archivo " .. tle_filename .. ".tle ... OK!", sf_palette[4])
  2539.     screen.waitvblankstart(300)
  2540.     screen.flip()
  2541.  
  2542.     tle_filename = "iridium"
  2543.  
  2544.  
  2545.     screen.print(30, 40, "> Conectando con Celestrak.com..." , sf_palette[1])
  2546.     screen.waitvblankstart()
  2547.     screen.flip()
  2548.  
  2549.     screen.print(30, 40, "> Conectando con Celestrak.com..." , sf_palette[1])
  2550.     screen.waitvblankstart()
  2551.     screen.flip()
  2552.  
  2553.    
  2554.     socket,error = Socket.connect("celestrak.com", 80)
  2555.     while not socket:isConnected() do
  2556.         os.sleep(0.01*100)
  2557.     end
  2558.  
  2559.  
  2560.     screen.print(300, 40, "conectado!. ", sf_palette[1])
  2561.     screen.waitvblankstart()
  2562.     screen.flip()
  2563.    
  2564.     screen.print(300, 40, "conectado!. ", sf_palette[1])
  2565.     screen.waitvblankstart()
  2566.     screen.flip()
  2567.    
  2568.     SF_sendHTTPheader(socket, "/NORAD/elements/".. tle_filename ..".txt")
  2569.  
  2570.  
  2571.     screen.print(30, 50, "> Recibiendo archivo " .. tle_filename .. ".tle", sf_palette[4])
  2572.     screen.waitvblankstart()
  2573.     screen.flip()
  2574.     screen.print(30, 50, "> Recibiendo archivo " .. tle_filename .. ".tle ...", sf_palette[4])
  2575.     screen.waitvblankstart()
  2576.     screen.flip()  
  2577.    
  2578.    
  2579.     -- Obtenemos el fichero TLE
  2580.     local f = io.open ("./tle/"..tle_filename .. ".tle", "w+")
  2581.     local buffer_len = 1
  2582.     reintentos = 20
  2583.     while (buffer_len > 0) do
  2584.         screen.waitvblankstart(5)
  2585.         local buffer = socket:recv(500)
  2586.         if string.len(buffer) > 0 then
  2587.             f:write (buffer)   
  2588.             screen.waitvblankstart(5)
  2589.             screen.flip()
  2590.             screen.waitvblankstart(5)
  2591.             screen.flip()
  2592.             buffer_len = string.len(buffer)
  2593.             reintentos = 20        
  2594.         else
  2595.             if reintentos > 0 then
  2596.                 reintentos = reintentos - 1
  2597.                 screen.waitvblankstart(10)
  2598.                 screen.flip()
  2599.                 screen.waitvblankstart(10)
  2600.                 screen.flip()
  2601.                 buffer_len = 1
  2602.             else
  2603.                 buffer_len = string.len(buffer)
  2604.             end
  2605.         end
  2606.  
  2607.     end
  2608.     f:flush ()
  2609.     io.close (f)
  2610.     socket:close()
  2611.     screen.print(30, 50, "> Recibiendo archivo " .. tle_filename .. ".tle ... OK!", sf_palette[4])
  2612.     screen.waitvblankstart()
  2613.     screen.flip()  
  2614.     screen.print(30, 50, "> Recibiendo archivo " .. tle_filename .. ".tle ... OK!", sf_palette[4])
  2615.     screen.waitvblankstart(100)
  2616.     screen.flip()
  2617.  
  2618.     screen.print(30, 60, "> Informacion de los satelites actualizada con exito!", sf_palette[4])
  2619.     screen.print(30, 70, "> Procesando informacion, espere por favor...", sf_palette[8])
  2620.     screen.waitvblankstart()
  2621.     screen.flip()  
  2622.     screen.print(30, 60, "> Informacion de los satelites actualizada con exito!", sf_palette[4])
  2623.     screen.print(30, 70, "> Procesando informacion, espere por favor...", sf_palette[8])
  2624.    
  2625.     local file = io.open("update.txt", "w+")
  2626.     if file ~= nil then
  2627.         file:write(tostring(sgp_time.jtime).."\n")
  2628.         file:flush(file)
  2629.         io.close(file)
  2630.     end
  2631.    
  2632.     screen.waitvblankstart(300)
  2633.     screen.flip()
  2634.    
  2635.    
  2636.     wlan.term()
  2637. end
  2638.  
  2639. function SF_drawLineMap( lat1 , lon1 , lat2, lon2, color )
  2640.     local px1, py1 = SF_LatLonToXY(lat1, lon1)
  2641.     local px2, py2 = SF_LatLonToXY(lat2, lon2)
  2642.  
  2643.     draw.line( px1, py1, px2, py2, color)
  2644. end
  2645.  
  2646.  
  2647. function SF_drawBitmapMap( lat , lon , bitmap, xini, yini, xlen, ylen )
  2648.  
  2649.     local px, py = SF_LatLonToXY(lat, lon)         
  2650.     image.blit(px - xlen/2, py - ylen/2, bitmap, xini, yini, xlen, ylen, true)
  2651. end
  2652.  
  2653.  
  2654.  
  2655. function SF_drawSuperPointSky( azimut , elevacion , color )
  2656.     if elevacion > sf_sky.elevacion_minima then
  2657.         local px, py = SF_AzElevToXY( azimut, elevacion )
  2658.         if px > 0 and px < screen.width()-1 and py > 0 and py < screen.height()-1 then
  2659.             image.pixel( px , py , color )
  2660.             px = px-2
  2661.             py = py-4
  2662.             screen.print(px, py, "o", color)
  2663.         end
  2664.     end
  2665. end
  2666.  
  2667. function SF_drawSuperCrossSky( azimut , elevacion , color )
  2668.     if elevacion > sf_sky.elevacion_minima then
  2669.         local px, py = SF_AzElevToXY( azimut, elevacion )
  2670.         if px > 0 and px < screen.width()-1 and py > 0 and py < screen.height()-1 then
  2671.             image.pixel( px , py , color )
  2672.             px = px-2
  2673.             py = py-4
  2674.             screen.print(px, py, "x", color)
  2675.         end
  2676.     end
  2677. end
  2678.  
  2679. function SF_drawSuperPointMap( latitud , longitud , color )
  2680.     local px, py = SF_LatLonToXY(latitud, longitud)
  2681.     image.pixel( px , py , color )
  2682.     px = px-2
  2683.     py = py-4
  2684.     screen.print(px, py, "o", color)
  2685. end
  2686.  
  2687. function SF_drawSuperCrossMap( latitud , longitud , color )
  2688.     local px, py = SF_LatLonToXY(latitud, longitud)
  2689.     px = px-2
  2690.     py = py-4
  2691.     screen.print(px, py, "x", color)
  2692. end
  2693.  
  2694. function SF_drawLabelSatsSky(azimut, elevacion, texto, color)
  2695.     if elevacion > sf_sky.elevacion_minima then
  2696.         local px, py = SF_AzElevToXY( azimut, elevacion )
  2697.         if azimut > 90 and azimut < 270 then
  2698.             screen.print(px+ 5, py-10, texto, color)
  2699.         else
  2700.             screen.print(px+ 5, py+2, texto, color)
  2701.         end
  2702.     end
  2703. end
  2704.  
  2705.  
  2706. function SF_drawSatellitesSky()
  2707.     local s
  2708.     for s = 1,sgp_sat_number do
  2709.             SF_drawSatSky( s )
  2710.     end
  2711. end
  2712.  
  2713. function SF_drawSatellitesMap()
  2714.     local s
  2715.     for s = 1,sgp_sat_number do
  2716.             SF_drawSatMap( s )
  2717.     end
  2718. end
  2719.  
  2720. function SF_decimales( valor, precision )
  2721.     local absoluto = math.abs (valor)
  2722.    
  2723.     return (absoluto - math.floor(absoluto))*10*precision
  2724. end
  2725.  
  2726. function SF_drawBackgroundInfo ()
  2727.  
  2728. --Sol
  2729.     draw.fillrect(0,0,236,9,sf_palette[6])
  2730.     draw.line(0,0,239,0,sf_palette[1])
  2731.     draw.line(0,9,235,9,sf_palette[10])
  2732.     draw.fillrect(0,10,236,10,sf_palette[12])
  2733.     draw.line(0,19,235,19,sf_palette[14])
  2734.     draw.line(0,39,235,39,sf_palette[14])
  2735.     draw.fillrect(0,40,236,10,sf_palette[12])
  2736.     draw.line(0,49,235,49,sf_palette[14])
  2737.     draw.line(0,59,235,59,sf_palette[14])
  2738.     draw.fillrect(0,60,236,10,sf_palette[12])
  2739.     draw.line(0,69,235,69,sf_palette[14])
  2740. --Luna
  2741.     draw.fillrect(0,100,236,10,sf_palette[6])
  2742.     draw.line(0,99,235,99,sf_palette[1])
  2743.     draw.line(0,109,235,109,sf_palette[10])
  2744.     draw.fillrect(0,110,236,10,sf_palette[12])
  2745.     draw.line(0,119,235,119,sf_palette[14])
  2746.     draw.line(0,139,235,139,sf_palette[14])
  2747.     draw.fillrect(0,140,236,10,sf_palette[12])
  2748.     draw.line(0,149,235,149,sf_palette[14])
  2749.     draw.line(0,159,235,159,sf_palette[14])
  2750.     draw.fillrect(0,160,236,10,sf_palette[12])
  2751.     draw.line(0,169,235,169,sf_palette[14])
  2752.     draw.line(0,189,215,189,sf_palette[14])
  2753.     draw.line(215,170,215,189,sf_palette[14])
  2754.     draw.fillrect(0,190,190,10,sf_palette[12])
  2755.     draw.line(190,190,190,199,sf_palette[14])  
  2756.     draw.line(0,199,235,199,sf_palette[14])
  2757.     draw.fillrect(0,200,236,10,sf_palette[12])
  2758.     draw.line(0,209,235,209,sf_palette[14])
  2759.     draw.line(0,219,235,219,sf_palette[14])
  2760.     draw.fillrect(0,220,236,10,sf_palette[12])
  2761.     draw.line(0,229,235,229,sf_palette[14])
  2762.  
  2763. -- Separador intermedio
  2764.     draw.fillrect(236,1,3,272,sf_palette[6])
  2765.  
  2766. -- Situación
  2767.     draw.fillrect(239,0,241,9,sf_palette[6])
  2768.     draw.line(239,0,480,0,sf_palette[1])
  2769.     draw.line(239,9,480,9,sf_palette[10])  
  2770.     draw.line(239,19,480,19,sf_palette[14])
  2771.     draw.fillrect(239,10,50,9,sf_palette[12])
  2772.     draw.line(289,10,289,19,sf_palette[14])
  2773.     draw.line(239,39,480,39,sf_palette[14])
  2774.     draw.line(239,29,307,29,sf_palette[14])
  2775.     draw.fillrect(239,20,68,9,sf_palette[12])  
  2776.     draw.line(307,20,307,29,sf_palette[14])
  2777.     draw.line(239,49,480,49,sf_palette[14])
  2778.     draw.fillrect(239,40,241,9,sf_palette[12])
  2779.  
  2780. -- Hora y fecha
  2781.     draw.fillrect(239,80,241,9,sf_palette[6])
  2782.     draw.line(239,79,480,79,sf_palette[1])
  2783.     draw.line(239,89,480,89,sf_palette[10])
  2784.  
  2785. -- Satelites   
  2786.     draw.fillrect(239,140,241,9,sf_palette[6])
  2787.     draw.line(239,139,480,139,sf_palette[1])
  2788.     draw.line(239,149,480,149,sf_palette[10])  
  2789.    
  2790. -- Estrellas   
  2791.     draw.fillrect(239,190,241,9,sf_palette[6])
  2792.     draw.line(239,189,480,189,sf_palette[1])
  2793.     draw.line(239,199,480,199,sf_palette[10])  
  2794.        
  2795. -- PSP 
  2796.     draw.fillrect(239,230,241,9,sf_palette[6])
  2797.     draw.line(239,229,480,229,sf_palette[1])
  2798.     draw.line(239,239,480,239,sf_palette[10])  
  2799.        
  2800.        
  2801. end
  2802.  
  2803. function SF_drawSunInfo ()
  2804.  
  2805.     screen.print(1, 1,  "    SOL" , sf_palette[14])
  2806.     screen.print(1, 11, "Posicion", sf_palette[8])
  2807.     screen.print(1, 21, " Azim " .. string.format("%.3d.%.2d",sgp_sun.azi, SF_decimales( sgp_sun.azi,2)), sf_palette[4])
  2808.     screen.print(1, 31, " Elev " .. string.format ("%+.2d.%.2d",sgp_sun.ele, SF_decimales( sgp_sun.ele,2)), sf_palette[4])
  2809.     if sgp_sun.lat >= 0 then
  2810.         screen.print(113, 21, "Lat  " .. string.format ("%.2d.%.2d",sgp_sun.lat,SF_decimales(sgp_sun.lat,2)) .. " N" , sf_palette[4])
  2811.     else
  2812.         screen.print(113, 21, "Lat  " .. string.format ("%.2d.%.2d",-sgp_sun.lat,SF_decimales(sgp_sun.lat,2)) .. " S" , sf_palette[4])     
  2813.     end
  2814.     if sgp_sun.lon >= 0 then
  2815.         screen.print(113, 31, "Lon " .. string.format ("%.3d.%.2d",sgp_sun.lon,SF_decimales(sgp_sun.lon,2)) .. " E", sf_palette[4])
  2816.     else
  2817.         screen.print(113, 31, "Lon " .. string.format ("%.3d.%.2d",-sgp_sun.lon,SF_decimales(sgp_sun.lon,2)) .. " W", sf_palette[4])
  2818.     end
  2819.     screen.print(1, 41, "Distancia en millones de KMs", sf_palette[8])
  2820.     screen.print(1, 51, " 142.7 < ", sf_palette[8])
  2821.     screen.print(73, 51, string.format ("%.6f",sgp_sun.alt/1000000) , sf_palette[4])
  2822.     screen.print(153, 51, " < 151.8", sf_palette[8])
  2823.     screen.print(1, 61, "Salidas y puestas de Sol" , sf_palette[8])
  2824.     if sgp_sun.ele > 0 then
  2825.         screen.print(1, 71, " Ha salido a las " .. sgp_sun.raise.hour .. ":" .. string.format ("%.2d",sgp_sun.raise.min) , sf_palette[4])
  2826.         screen.print(1, 81, " Se va poner a las " .. sgp_sun.set.hour .. ":" .. string.format ("%.2d",sgp_sun.set.min) , sf_palette[4])
  2827.     else
  2828.         screen.print(1, 71, " Se ha puesto a las " .. sgp_sun.set.hour .. ":" .. string.format ("%.2d",sgp_sun.set.min) , sf_palette[4])
  2829.         screen.print(1, 81, " Va a salir a las " .. sgp_sun.raise.hour .. ":" .. string.format ("%.2d",sgp_sun.raise.min)  , sf_palette[4])
  2830.     end
  2831. end
  2832.  
  2833. function SF_drawMoonInfo()
  2834.     screen.print(1, 101, "    LUNA" , sf_palette[14])
  2835.     screen.print(1, 111, "Posicion", sf_palette[8])
  2836.     screen.print(1, 121, " Azim " .. string.format("%.3d.%.2d",sgp_moon.azi, SF_decimales( sgp_moon.azi,2)), sf_palette[4])
  2837.     screen.print(1, 131, " Elev " .. string.format ("%+.2d.%.2d",sgp_moon.ele, SF_decimales( sgp_moon.ele,2)), sf_palette[4])
  2838.     if sgp_moon.lat >= 0 then
  2839.         screen.print(113, 121, "Lat  " .. string.format ("%.2d.%.2d",sgp_moon.lat,SF_decimales(sgp_moon.lat,2)) .. " N" , sf_palette[4])
  2840.     else
  2841.         screen.print(113, 121, "Lat  " .. string.format ("%.2d.%.2d",-sgp_moon.lat,SF_decimales(sgp_moon.lat,2)) .. " S" , sf_palette[4])      
  2842.     end
  2843.     if sgp_moon.lon >= 0 then
  2844.         screen.print(113, 131, "Lon " .. string.format ("%.3d.%.2d",sgp_moon.lon,SF_decimales(sgp_moon.lon,2)) .. " E", sf_palette[4])
  2845.     else
  2846.         screen.print(113, 131, "Lon " .. string.format ("%.3d.%.2d",-sgp_moon.lon,SF_decimales(sgp_moon.lon,2)) .. " W", sf_palette[4])
  2847.     end
  2848.     screen.print(1, 141, "Distancia en miles de KMs", sf_palette[8])
  2849.     screen.print(1, 151, " 363.0 < ", sf_palette[8])
  2850.     screen.print(73, 151, string.format ("%.6f",sgp_moon.dist/1000) , sf_palette[4])
  2851.     screen.print(153, 151, " < 405.0", sf_palette[8])
  2852.     screen.print(1, 161, "Salidas y puestas de Luna" , sf_palette[8])
  2853.     if sgp_moon.ele > 0 then
  2854.         screen.print(1, 171, " Ha salido a las " .. sgp_moon.raise.hour.. ":" .. string.format ("%.2d",sgp_moon.raise.min) , sf_palette[4])
  2855.         screen.print(1, 181," Se va poner a las " .. sgp_moon.set.hour.. ":" .. string.format ("%.2d",sgp_moon.set.min) , sf_palette[4])   
  2856.     else
  2857.         screen.print(1, 171, " Se ha puesto a las " .. sgp_moon.set.hour.. ":" .. string.format ("%.2d",sgp_moon.set.min) , sf_palette[4])
  2858.         screen.print(1, 181," Va a salir a las " .. sgp_moon.raise.hour.. ":" .. string.format ("%.2d",sgp_moon.raise.min)  , sf_palette[4])
  2859.     end
  2860.     screen.print(1, 191, "Iluminacion disco lunar", sf_palette[8])
  2861.     screen.print(185, 191, " " .. string.format ("%d", sgp_moon.phase * 100) .. "%" , sf_palette[4])       
  2862.     screen.print(1, 201, "Progreso del ciclo lunar", sf_palette[8])
  2863.     screen.print(1, 211, " " .. sgp_moon.age.d .. "dias " .. sgp_moon.age.h .. "horas " .. sgp_moon.age.m .. "min (" .. string.format ("%d", sgp_moon.age.cicle * 100) .. "%)" , sf_palette[4])    
  2864.     screen.print(1, 221, "Proximas fases lunares", sf_palette[8])
  2865.     local ye, mo, da, ho, mi, se = SGP_Julian_To_Date(sgp_moon.first_quarter_moon)
  2866.     screen.print(1, 231, " Creciente " .. string.format ("%.2d",da) .. "/" .. string.format ("%.2d",mo) .."/" .. ye .. " " .. string.format ("%.2d",ho) .. ":" .. string.format ("%.2d",mi), sf_palette[4])
  2867.     ye, mo, da, ho, mi, se = SGP_Julian_To_Date(sgp_moon.full_moon)
  2868.     screen.print(1, 241, " Llena     " .. string.format ("%.2d",da) .. "/" .. string.format ("%.2d",mo) .."/" .. ye .. " " .. string.format ("%.2d",ho) .. ":" .. string.format ("%.2d",mi), sf_palette[4])
  2869.     ye, mo, da, ho, mi, se = SGP_Julian_To_Date(sgp_moon.third_quarter_moon)
  2870.     screen.print(1, 251, " Menguante " .. string.format ("%.2d",da) .. "/" .. string.format ("%.2d",mo) .."/" .. ye .. " " .. string.format ("%.2d",ho) .. ":" .. string.format ("%.2d",mi), sf_palette[4])
  2871.     ye, mo, da, ho, mi, se = SGP_Julian_To_Date(sgp_moon.new_moon)
  2872.     screen.print(1, 261, " Nueva     " .. string.format ("%.2d",da) .. "/" .. string.format ("%.2d",mo) .."/" .. ye .. " " .. string.format ("%.2d",ho) .. ":" .. string.format ("%.2d",mi), sf_palette[4])
  2873. end
  2874.  
  2875. function SF_drawStarsInfo()
  2876.     screen.print(240, 191, "    ESTRELLAS" , sf_palette[14])
  2877.     screen.print(240, 201, "Estrellas cargadas: " , sf_palette[8])
  2878.     screen.print(400, 201, sgp_star_number , sf_palette[4])
  2879.     screen.print(240, 211, "Magnitud limite actual: " , sf_palette[8]) 
  2880.     screen.print(432, 211, string.format ("%.2f",sgp_mag_limit), sf_palette[4])
  2881. end
  2882.  
  2883. function SF_drawSatsInfo()
  2884.     screen.print(240, 141, "    SATELITES" , sf_palette[14])
  2885.     screen.print(240, 151, "Satelites cargados: " , sf_palette[8])
  2886.     screen.print(400, 151, sgp_sat_number , sf_palette[4])
  2887.     if sf_iss ~= nil then
  2888.         screen.print(240, 161, "ISS Azimut: ", sf_palette[8])
  2889.         screen.print(360, 161, string.format ("%.2f",sgp_sats[sf_iss].azimut) , sf_palette[4])
  2890.         screen.print(240, 171, "ISS Elevacion: ", sf_palette[8])
  2891.         screen.print(360, 171, string.format ("%.2f",sgp_sats[sf_iss].elevacion) , sf_palette[4])      
  2892.     end
  2893. end
  2894.  
  2895. function SF_drawTimeInfo()
  2896.     screen.print(240, 81,  "    FECHA Y HORA" , sf_palette[14])
  2897.     screen.print(240, 91, "Local: ",sf_palette[8])
  2898.     screen.print(304, 91, sgp_time.day ..
  2899.         "/" .. sgp_time.month ..
  2900.         "/" .. sgp_time.year,sf_palette[4])
  2901.     screen.print(400, 91, sgp_time.hour ..
  2902.         ":" .. string.format("%.2d",sgp_time.min) ..
  2903.         ":" .. string.format("%.2d",sgp_time.sec), sf_palette[4])
  2904.     screen.print(240, 101, "UTC: ",sf_palette[8])
  2905.     screen.print(304, 101, sgp_time.uday ..
  2906.         "/" .. sgp_time.umonth ..
  2907.         "/" .. sgp_time.uyear,sf_palette[4])
  2908.     screen.print(400, 101, sgp_time.uhour ..
  2909.         ":" .. string.format("%.2d",sgp_time.umin) ..
  2910.         ":" .. string.format("%.2d",sgp_time.usec), sf_palette[4])
  2911.     screen.print(240, 111, "Zona horaria:" , sf_palette[8])
  2912.     screen.print(344, 111, "GMT" .. string.format ("%+03d",sgp_location.UTC_offset), sf_palette[4])
  2913.     screen.print(416, 111, "DST:" , sf_palette[8])
  2914.     if sgp_location.dst then
  2915.         screen.print(448, 111, "SI", sf_palette[4])
  2916.     else
  2917.         screen.print(448, 111, "NO", sf_palette[4])
  2918.     end
  2919.  
  2920. end
  2921.  
  2922. function SF_drawInfoBar(completa)
  2923.     if completa then
  2924.         -- Fondo de la barra (borra lo anterior)
  2925.         draw.fillrect(0, 0, 480, 9, sf_palette[14])
  2926.         draw.line(0,9, 479,9,sf_palette[10])
  2927.        
  2928.         -- Bateria
  2929.         local level = batt.percent()
  2930.         if level < 0 or level > 100 then
  2931.             level = "--"
  2932.         end
  2933.         screen.print(1, 1, "Bat:"..level .. "%" , sf_palette[8])
  2934.        
  2935.         -- Magnitud umbral
  2936.         screen.print(80, 1, "Mag:"..string.format ("%.1f",sgp_mag_limit), sf_palette[8])
  2937.        
  2938.         -- Fremes per second
  2939.         screen.print(160, 1, "Fps:"..string.format ("%.1f",sf_fps), sf_palette[8])
  2940.        
  2941.         -- Zoom
  2942.         screen.print(240, 1, "Zoom:"..string.format ("%.1fx",sf_sky.radio/241), sf_palette[8])
  2943.        
  2944.         -- Rotación
  2945.         screen.print(336, 1, "Rot:"..string.format ("%.1f",sf_sky.rotacion), sf_palette[8])
  2946.        
  2947.     else
  2948.         draw.fillrect(414, 0, 479, 9, sf_palette[14])
  2949.         draw.line(414,9, 479,9,sf_palette[10])
  2950.         draw.line(414,9, 414,0,sf_palette[10])
  2951.     end
  2952.  
  2953.     -- Hora actual
  2954.     SGP_getCurrentTime()
  2955.     if sgp_time.hour >= 10 then
  2956.         screen.print(416, 1, sgp_time.hour ..
  2957.             ":" .. string.format("%.2d",sgp_time.min) ..
  2958.             ":" .. string.format("%.2d",sgp_time.sec) , sf_palette[10])
  2959.     else
  2960.         screen.print(424, 1,sgp_time.hour ..
  2961.             ":" .. string.format("%.2d",sgp_time.min) ..
  2962.             ":" .. string.format("%.2d",sgp_time.sec), sf_palette[10])
  2963.     end
  2964. end
  2965.  
  2966. function SF_drawLocationInfo ()
  2967.     screen.print(240, 1,  "    SITUACION OBSERVADOR" , sf_palette[14])
  2968.    
  2969.     screen.print(240, 11, "Nombre" , sf_palette[8])
  2970.     screen.print(304, 11, sgp_location.name, sf_palette[4])
  2971.     screen.print(240, 21, "Posicion", sf_palette[8])
  2972.     if sgp_location.lat >= 0 then
  2973.         screen.print(368, 21, "Lat  " .. string.format ("%.2d.%.2d",sgp_location.lat,SF_decimales(sgp_location.lat,2)) .. " N" , sf_palette[4])
  2974.     else
  2975.         screen.print(368, 21, "Lat  " .. string.format ("%.2d.%.2d",-sgp_location.lat,SF_decimales(sgp_location.lat,2)) .. " S" , sf_palette[4])       
  2976.     end
  2977.     if sgp_location.lon >= 0 then
  2978.         screen.print(368, 31, "Lon " .. string.format ("%.3d.%.2d",sgp_location.lon,SF_decimales(sgp_location.lon,2)) .. " E", sf_palette[4])
  2979.     else
  2980.         screen.print(368, 31, "Lon " .. string.format ("%.3d.%.2d",-sgp_location.lon,SF_decimales(sgp_location.lon,2)) .. " W", sf_palette[4])
  2981.     end
  2982.     screen.print(248, 31, "Alt " .. string.format("%.4d m",sgp_location.alt), sf_palette[4])
  2983.     screen.print(240, 41, "La Noche" , sf_palette[8])
  2984.     if sgp_sun.ele > const_twilightCivil then
  2985.         screen.print(240, 51, " Ha terminado a las " .. sgp_location.day.hour.. ":" .. string.format ("%.2d",sgp_location.day.min) , sf_palette[4])
  2986.         screen.print(240, 61, " Va a empezar a las " .. sgp_location.night.hour.. ":" .. string.format ("%.2d",sgp_location.night.min) , sf_palette[4])
  2987.     else
  2988.         screen.print(240, 51, " Ha empezado a las " .. sgp_location.night.hour.. ":" .. string.format ("%.2d",sgp_location.night.min) , sf_palette[4])
  2989.         screen.print(240, 61, " Va a terminar a las " .. sgp_location.day.hour.. ":" .. string.format ("%.2d",sgp_location.day.min)  , sf_palette[4])
  2990.     end
  2991. end
  2992.  
  2993. function SF_drawPSPInfo()
  2994.         screen.print(240, 231,  "    PSP" , sf_palette[14])
  2995.         SF_drawMemLevel(240, 241, sf_palette[4])
  2996.         SF_drawBatLevel(240, 251, sf_palette[4])
  2997.         SF_drawTempLevel(240, 261, sf_palette[4])
  2998. end
  2999.  
  3000.  
  3001. function SF_drawAllNextVisiblePassList()
  3002.     local continua_buscando = true
  3003.     local i
  3004.  
  3005.  
  3006.     if sf_next_limit == 0 then 
  3007.         i = 1
  3008.         while i <= sgp_sat_number and (sf_next_limit < sf_next_max) and continua_buscando do
  3009.             screen.clear(sf_sky_color)         
  3010.             draw.fillrect(0, 0, 480, 10, sf_palette[14])
  3011.             SF_drawInfoBar(false)
  3012.             screen.print( 0, 1,  " Proximos avistamientos en 30 minutos (".. string.format("%d",i/sgp_sat_number * 100) .. "%)", sf_palette[1])
  3013.             draw.line(0,9, 479,9,sf_palette[10])
  3014.             draw.fillrect(0, 10, 480, 19, sf_palette[14])
  3015.             screen.print( 0, 11, " FECHA     |INICIO     |MAXIMO     |FIN        |NOMBRE     ", sf_palette[8])
  3016.             screen.print( 0, 21, " DD/MM/AAAA|HH:MM EL AZ|HH:MM EL AZ|HH:MM EL AZ|           ", sf_palette[8])     
  3017.             draw.line(0,29, 479,29,sf_palette[10])
  3018.             local j
  3019.             local limite
  3020.             if sf_next_limit > 23 then
  3021.                 limite = 23
  3022.             else
  3023.                 limite = sf_next_limit
  3024.             end
  3025.             for j = 1,limite do
  3026.                 local c = math.floor ((90- sf_next_passes[j].max_elevacion)/90*12)
  3027.                 screen.print( 8, 21 + 10*j, sf_next_passes[j].linea , sf_palette[4 + c])
  3028.             end
  3029.             draw.line(0,259, 479,259,sf_palette[10])
  3030.             draw.fillrect(0, 260, 480, 272, sf_palette[14])
  3031.             screen.print(0, 261,  " Pulsa CUADRADO para parar." , sf_palette[1])
  3032.             screen.waitvblankstart()
  3033.             screen.flip()
  3034.             screen.clear(sf_sky_color)
  3035.             draw.fillrect(0, 0, 480, 10, sf_palette[14])
  3036.             SF_drawInfoBar(false)
  3037.             screen.print( 0, 1,  " Proximos avistamientos en 30 minutos (".. string.format("%d",i/sgp_sat_number * 100) .. "%)", sf_palette[1])
  3038.             draw.line(0,9, 479,9,sf_palette[10])
  3039.             draw.fillrect(0, 10, 480, 20, sf_palette[14])
  3040.             screen.print( 0, 11, " FECHA     |INICIO     |MAXIMO     |FIN        |NOMBRE     ", sf_palette[8])
  3041.             screen.print( 0, 21, " DD/MM/AAAA|HH:MM EL AZ|HH:MM EL AZ|HH:MM EL AZ|           ", sf_palette[8])     
  3042.             draw.line(0,29, 479,29,sf_palette[10])
  3043.             for j = 1,limite do
  3044.                 local c = math.floor ((90- sf_next_passes[j].max_elevacion)/90*12)
  3045.                 screen.print( 8, 21 + 10*j, sf_next_passes[j].linea , sf_palette[4 + c])
  3046.             end
  3047.             draw.line(0,259, 479,259,sf_palette[10])
  3048.             draw.fillrect(0, 260, 480, 272, sf_palette[14])
  3049.             screen.print(0, 261,  " Pulsa CUADRADO para parar." , sf_palette[1])
  3050.             screen.waitvblankstart()
  3051.             screen.flip()
  3052.             continua_buscando = SGP_calculateAllNextVisiblePass(i)
  3053.  
  3054.             i = i + 1
  3055.         end
  3056.         if sf_next_current > 23 then
  3057.             sf_next_current = 23
  3058.         end
  3059.     end
  3060.  
  3061.     while true do
  3062.         screen.clear(sf_sky_color)
  3063.         draw.fillrect(0, 0, 480, 10, sf_palette[14])
  3064.         SF_drawInfoBar(false)
  3065.         screen.print( 0, 1,  " Proximos avistamientos en 30 minutos", sf_palette[1])
  3066.         draw.line(0,9, 479,9,sf_palette[10])
  3067.         draw.fillrect(0, 10, 480, 20, sf_palette[14])
  3068.         screen.print( 0, 11, " FECHA     |INICIO     |MAXIMO     |FIN        |NOMBRE     ", sf_palette[8])
  3069.         screen.print( 0, 21, " DD/MM/AAAA|HH:MM EL AZ|HH:MM EL AZ|HH:MM EL AZ|           ", sf_palette[8])     
  3070.         draw.line(0,29, 479,29,sf_palette[10])
  3071.  
  3072.         local i
  3073.         local limite
  3074.         if sf_next_limit > 23 then
  3075.             limite = 23
  3076.         else
  3077.             limite = sf_next_limit
  3078.         end
  3079.         if limite == 0 then
  3080.             screen.print( 8, 21 + 10*1, "Ningun satelite va a ser visible en 30 minutos." , sf_palette[8])
  3081.             screen.print( 8, 21 + 10*2, "Intentelo otra vez mas tarde." , sf_palette[8])
  3082.  
  3083.         else
  3084.             for i = 1,limite do
  3085.                 if sf_next_current == i then
  3086.                     screen.print( 0, 21 + 10*i, ">"..sf_next_passes[i].linea , sf_palette[1])
  3087.                 else
  3088.                     local c = math.floor ((90- sf_next_passes[i].max_elevacion)/90*12)
  3089.                     screen.print( 8, 21 + 10*i, sf_next_passes[i].linea , sf_palette[4 + c])
  3090.                 end
  3091.             end
  3092.         end
  3093.         draw.line(0,259, 479,259,sf_palette[10])
  3094.         draw.fillrect(0, 260, 480, 272, sf_palette[14])
  3095.         screen.print(0, 261, " Pulsa X para seleccionar. Pulsa O para buscar otra vez." , sf_palette[1])
  3096.         screen.waitvblankstart()
  3097.         screen.flip()
  3098.  
  3099.        
  3100.         sf_entrada = Controls.read()
  3101.        
  3102.  
  3103.         if sf_entrada:down() then
  3104.             if sf_next_current < limite then
  3105.                 sf_next_current = sf_next_current + 1
  3106.             end
  3107.         elseif sf_entrada:up() then
  3108.             if sf_next_current > 1 then
  3109.                 sf_next_current = sf_next_current - 1
  3110.             end
  3111.         elseif sf_entrada ~= sf_entrada_ant then
  3112.             sf_entrada_ant = sf_entrada
  3113.             if sf_entrada:cross() then
  3114.                 if sf_next_current > 0 then
  3115.                     sf_selected_sat = sf_next_passes[sf_next_current].sat
  3116.                     SF_generateNextPassPathSky(sf_selected_sat)
  3117.                    
  3118.                 end
  3119.                 return
  3120.             end
  3121.             if sf_entrada:circle() then
  3122.                 sf_next_limit = 0
  3123.                 sf_next_current = 0
  3124.                 SF_drawAllNextVisiblePassList()            
  3125.                 return
  3126.             end
  3127.             if sf_entrada:r() and sf_entrada:l() then
  3128.                 SF_screenshot()
  3129.             end
  3130.         end
  3131.        
  3132.         sf_entrada_ant = sf_entrada
  3133.         screen.waitvblankstart(5)
  3134.     end
  3135. end
  3136.  
  3137. function SF_drawSatNextVisiblePassList(sat)
  3138.     local continua_buscando = true 
  3139.     screen.clear(sf_sky_color)         
  3140.     draw.fillrect(0, 0, 480, 10, sf_palette[14])
  3141.     SF_drawInfoBar(false)
  3142.     screen.waitvblankstart()
  3143.     screen.flip()  
  3144.     screen.clear(sf_sky_color)                 
  3145.     draw.fillrect(0, 0, 480, 10, sf_palette[14])
  3146.     SF_drawInfoBar(false)
  3147.     screen.waitvblankstart()
  3148.     screen.flip()
  3149.     SF_drawInfoBar(false)  
  3150.     screen.print( 0, 1,  " Proximos avistamientos del satelite "..sgp_sats[sat].nombre, sf_palette[1])
  3151.     draw.line(0,9, 479,9,sf_palette[10])
  3152.     draw.fillrect(0, 10, 480, 19, sf_palette[14])
  3153.     screen.print( 0, 11, " FECHA     | INICIO     | MAXIMO     | FIN                 ", sf_palette[1])
  3154.     screen.print( 0, 21, " DD/MM/AAAA| HH:MM EL AZ| HH:MM EL AZ| HH:MM EL AZ         ", sf_palette[1])     
  3155.     draw.line(0,29, 479,29,sf_palette[10])
  3156.     local i
  3157.     for i = 1,sf_pass_limit do
  3158.         if sf_pass_current == i then
  3159.             screen.print( 8, 21 + 10*i, sf_passes[i].linea , sf_palette[4])
  3160.         else
  3161.             local c = math.floor ((90- sf_passes[i].max_elevacion)/90*12)
  3162.             screen.print( 8, 21 + 10*i, sf_passes[i].linea , sf_palette[4 + c])
  3163.         end
  3164.     end
  3165.     draw.line(0,259, 479,259,sf_palette[10])
  3166.     draw.fillrect(0, 260, 480, 272, sf_palette[14])
  3167.     screen.waitvblankstart()
  3168.     screen.flip()
  3169.     SF_drawInfoBar(false)
  3170.     screen.print( 0, 1,  " Proximos avistamientos del satelite "..sgp_sats[sat].nombre, sf_palette[1])
  3171.     draw.line(0,9, 479,9,sf_palette[10])
  3172.     draw.fillrect(0, 10, 480, 19, sf_palette[14])
  3173.     screen.print( 0, 11, " FECHA     | INICIO     | MAXIMO     | FIN                 ", sf_palette[1])
  3174.     screen.print( 0, 21, " DD/MM/AAAA| HH:MM EL AZ| HH:MM EL AZ| HH:MM EL AZ         ", sf_palette[1])     
  3175.     draw.line(0,29, 479,29,sf_palette[10])
  3176.     for i = 1,sf_pass_limit do
  3177.         if sf_pass_current == i then
  3178.             screen.print( 8, 21 + 10*i, sf_passes[i].linea , sf_palette[4])
  3179.         else
  3180.             local c = math.floor ((90- sf_passes[i].max_elevacion)/90*12)
  3181.             screen.print( 8, 21 + 10*i, sf_passes[i].linea , sf_palette[4 + c])
  3182.         end
  3183.     end
  3184.     draw.line(0,259, 479,259,sf_palette[10])
  3185.     draw.fillrect(0, 260, 480, 272, sf_palette[14])
  3186.     screen.waitvblankstart()
  3187.     screen.flip()
  3188.     screen.print(0, 261,  " Pulsa CUADRADO para parar." , sf_palette[1])
  3189.     screen.waitvblankstart()
  3190.     screen.flip()
  3191.     screen.print(0, 261,  " Pulsa CUADRADO para parar." , sf_palette[1])
  3192.     screen.waitvblankstart()
  3193.     screen.flip()
  3194.     while (sf_pass_limit < sf_pass_max) and continua_buscando do
  3195.         continua_buscando = SGP_calculateSatNextVisiblePass(sat)
  3196.  
  3197.     end
  3198.    
  3199.     while true do
  3200.         screen.clear(sf_sky_color)         
  3201.         draw.fillrect(0, 0, 480, 10, sf_palette[14])
  3202.         SF_drawInfoBar(false)  
  3203.         screen.print( 0, 1,  " Proximos avistamientos del satelite "..sgp_sats[sat].nombre, sf_palette[1])
  3204.         draw.line(0,9, 479,9,sf_palette[10])
  3205.         draw.fillrect(0, 10, 480, 19, sf_palette[14])
  3206.         screen.print( 0, 11, " FECHA     | INICIO     | MAXIMO     | FIN                 ", sf_palette[1])
  3207.         screen.print( 0, 21, " DD/MM/AAAA| HH:MM EL AZ| HH:MM EL AZ| HH:MM EL AZ         ", sf_palette[1])     
  3208.         draw.line(0,29, 479,29,sf_palette[10])
  3209.  
  3210.         local i
  3211.         for i = 1,sf_pass_limit do
  3212.             if sf_pass_current == i then
  3213.                 screen.print( 0, 21 + 10*i, ">" .. sf_passes[i].linea , sf_palette[1])
  3214.             else
  3215.                 local c = math.floor ((90- sf_passes[i].max_elevacion)/90*12)
  3216.                 screen.print( 8, 21 + 10*i, sf_passes[i].linea , sf_palette[4 + c])
  3217.             end
  3218.         end
  3219.         draw.line(0,259, 479,259,sf_palette[10])
  3220.         draw.fillrect(0, 260, 480, 272, sf_palette[14])
  3221.         screen.print(0, 261,  " Pulsa X para seleccionar la que quiera ver marcada." , sf_palette[1])
  3222.         screen.waitvblankstart()
  3223.         screen.flip()
  3224.  
  3225.        
  3226.         sf_entrada = Controls.read()
  3227.        
  3228.  
  3229.         if sf_entrada:down() then
  3230.             if sf_pass_current < sf_pass_limit then
  3231.                 sf_pass_current = sf_pass_current + 1
  3232.             end
  3233.         elseif sf_entrada:up() then
  3234.             if sf_pass_current > 1 then
  3235.                 sf_pass_current = sf_pass_current - 1
  3236.             end
  3237.         elseif sf_entrada ~= sf_entrada_ant then
  3238.             sf_entrada_ant = sf_entrada
  3239.             if sf_entrada:cross() then
  3240.                 sf_selected_sat = sat
  3241.                 SF_generatePassPathSky(sat)
  3242.                 return
  3243.             end
  3244.             if sf_entrada:r() and sf_entrada:l() then
  3245.                 SF_screenshot()
  3246.             end
  3247.         end
  3248.        
  3249.         sf_entrada_ant = sf_entrada
  3250.            
  3251.         screen.waitvblankstart(10)     
  3252.     end
  3253.  
  3254. end
  3255.  
  3256. function SGP_calculateAllNextVisiblePass(s)
  3257.     local jtime
  3258.    
  3259.  
  3260.     SGP_getCurrentTime()
  3261.     jtime = sgp_time.ujtime
  3262.  
  3263.    
  3264.     sf_next_limit = sf_next_limit + 1
  3265.     sf_next_current = sf_next_limit
  3266.  
  3267.    
  3268.     SGP_calculateSun (jtime)
  3269.     SGP_calculateSatellite (s , jtime)
  3270.  
  3271.     -- buscamos la proxima vez que estará visible y por encima del horizonte en un plazo de 1 hora
  3272.     -- incrementamos mucho cuando su elevacion baja y poco cuando sube
  3273.     local sf_mostrar_progreso = 0
  3274.     local one_hour_after = jtime + const_julian_minute * 30
  3275.     while not ( sgp_sats[s].elevacion > sf_sky.elevacion_minima and sgp_sats[s].visible) do
  3276.         if sgp_sats[s].elevacion > sgp_sats[s].elevacion_ant and sgp_sun.ele < 15 then
  3277.             jtime = jtime + const_julian_minute * sf_next_visible_precision
  3278.         else
  3279.             jtime = jtime + const_julian_minute * sf_next_visible_precision * 50
  3280.         end
  3281.  
  3282.  
  3283.         if jtime >= one_hour_after then
  3284.             --lo dejamos todo como estaba (en el tiempo real), incluso la dirección, llamando dos veces
  3285.             SGP_calculateSun (sgp_time.ujtime - const_julian_minute)
  3286.             SGP_calculateSatellite (s , sgp_time.ujtime - const_julian_minute)
  3287.             SGP_calculateSun (sgp_time.ujtime)
  3288.             SGP_calculateSatellite (s , sgp_time.ujtime)
  3289.             SF_erase(1, 21+sf_next_limit*10, 60)
  3290.             sf_next_limit = sf_next_limit - 1
  3291.             sf_next_current = sf_next_limit
  3292.             screen.waitvblankstart()
  3293.             screen.flip()
  3294.             return true
  3295.         end
  3296.        
  3297.        
  3298.         SGP_calculateSun (jtime)
  3299.         SGP_calculateSatellite (s , jtime)
  3300.         if sf_mostrar_progreso <= 0 then
  3301.             sf_mostrar_progreso = 15
  3302.  
  3303.             local jt_temp = SGP_UTC_to_Local(jtime)
  3304.             local year , month, day, hour, minute, second = SGP_Julian_To_Date(jt_temp)
  3305.            
  3306.             SF_drawInfoBar(false)
  3307.            
  3308.             if sf_next_current <= 22 then
  3309.                 SF_erase(8, 21+sf_next_limit*10, 60)
  3310.                 screen.print(8, 21+sf_next_limit*10, string.format ("%02d",day) .. "/" .. string.format ("%02d",month)
  3311.                 .. "/" .. year .. " " .. string.format ("%02d",hour) .. ":" .. string.format("%02d",minute)..
  3312.                 "                               " .. sgp_sats[s].nombre, sf_palette[12])
  3313.                 screen.waitvblankstart()
  3314.                 screen.flip()
  3315.             end
  3316.            
  3317.             sf_entrada = Controls.read()
  3318.            
  3319.             if sf_entrada ~= sf_entrada_ant then
  3320.                 sf_entrada_ant = sf_entrada
  3321.                 if sf_entrada:square() then
  3322.                     --lo dejamos todo como estaba (en el tiempo real), incluso la dirección, llamando dos veces
  3323.                     SGP_calculateSun (sgp_time.ujtime - const_julian_minute)
  3324.                     SGP_calculateSatellite (s , sgp_time.ujtime - const_julian_minute)
  3325.                     SGP_calculateSun (sgp_time.ujtime)
  3326.                     SGP_calculateSatellite (s , sgp_time.ujtime)
  3327.                     SF_erase(1, 21+sf_next_limit*10, 60)
  3328.                     sf_next_limit = sf_next_limit - 1
  3329.                     sf_next_current = sf_next_limit
  3330.                     screen.waitvblankstart()
  3331.                     screen.flip()
  3332.                    
  3333.                     return false           
  3334.                 end
  3335.             end
  3336.             sf_entrada_ant = sf_entrada
  3337.         else
  3338.             sf_mostrar_progreso = sf_mostrar_progreso - 1
  3339.         end
  3340.     end
  3341.  
  3342.  
  3343.     -- una vez encontrado un punto donde el satelite es visible por encima de la elevación minima establecida
  3344.     -- buscamos el inicio de este hecho retrocediendo en saltitos pequeños
  3345.  
  3346.     repeat 
  3347.         jtime = jtime - const_julian_minute * sf_next_visible_precision * 0.2
  3348.         SGP_calculateSun (jtime)
  3349.         SGP_calculateSatellite (s , jtime)
  3350.        
  3351.     until ( not sgp_sats[s].visible or sgp_sats[s].elevacion <= sf_sky.elevacion_minima)
  3352.  
  3353.     --hemos encontrado cuando empieza a ser visible. Lo apuntamos.
  3354.     jtime = jtime + const_julian_minute * sf_next_visible_precision * 0.2
  3355.     SGP_calculateSun (jtime)
  3356.     SGP_calculateSatellite (s , jtime)
  3357.  
  3358.  
  3359.     sgp_sats[s].no_calcular = 0
  3360.    
  3361.     local temp_jt =  SGP_UTC_to_Local(jtime)
  3362.     local temp_ele = sgp_sats[s].elevacion
  3363.     local temp_azi = sgp_sats[s].azimut
  3364.    
  3365.     --Ahora vamos a ver la elevación maxima visible que va a tener avanzando en saltos pequeños
  3366.    
  3367.     repeat 
  3368.         jtime = jtime + const_julian_minute * sf_next_visible_precision * 0.2
  3369.         SGP_calculateSun (jtime)
  3370.         SGP_calculateSatellite (s , jtime)
  3371.     until ( (sgp_sats[s].elevacion < sgp_sats[s].elevacion_ant) or (not sgp_sats[s].visible) or (sgp_sats[s].elevacion < sf_sky.elevacion_minima)) 
  3372.    
  3373.     jtime = jtime - const_julian_minute * sf_next_visible_precision * 0.2
  3374.     SGP_calculateSun (jtime)
  3375.     SGP_calculateSatellite (s , jtime)
  3376.  
  3377.  
  3378.     local j = 1
  3379.     local momento = SGP_UTC_to_Local(jtime)
  3380.     while j < sf_next_limit and sf_next_passes[j].max_jt < momento do
  3381.         j = j + 1
  3382.     end
  3383.    
  3384.     if (j < sf_next_limit) then
  3385.         local k = sf_next_limit-1
  3386.        
  3387.         while k >= j do
  3388.             sf_next_passes[k+1].ini_jt = sf_next_passes[k].ini_jt
  3389.             sf_next_passes[k+1].max_jt = sf_next_passes[k].max_jt
  3390.             sf_next_passes[k+1].fin_jt = sf_next_passes[k].fin_jt
  3391.             sf_next_passes[k+1].ini_azimut = sf_next_passes[k].ini_azimut
  3392.             sf_next_passes[k+1].max_azimut = sf_next_passes[k].max_azimut
  3393.             sf_next_passes[k+1].fin_azimut = sf_next_passes[k].fin_azimut
  3394.             sf_next_passes[k+1].ini_elevacion = sf_next_passes[k].ini_elevacion
  3395.             sf_next_passes[k+1].max_elevacion = sf_next_passes[k].max_elevacion
  3396.             sf_next_passes[k+1].fin_elevacion = sf_next_passes[k].fin_elevacion
  3397.             sf_next_passes[k+1].linea = sf_next_passes[k].linea
  3398.             sf_next_passes[k+1].sat = sf_next_passes[k].sat
  3399.             k = k - 1
  3400.         end
  3401.  
  3402.  
  3403.        
  3404.         sf_next_current = j
  3405.        
  3406.  
  3407.     end
  3408.     sf_next_passes[sf_next_current].ini_jt = temp_jt
  3409.     sf_next_passes[sf_next_current].ini_elevacion = temp_ele
  3410.     sf_next_passes[sf_next_current].ini_azimut = temp_azi
  3411.    
  3412.     --apuntamos la elevacion maxima visible y ya que estamos el azimut
  3413.     sf_next_passes[sf_next_current].max_jt = SGP_UTC_to_Local(jtime)
  3414.     sf_next_passes[sf_next_current].max_elevacion = sgp_sats[s].elevacion
  3415.     sf_next_passes[sf_next_current].max_azimut = sgp_sats[s].azimut
  3416.  
  3417.     --Ahora vamos a ver donde termina de ser visible
  3418.    
  3419.     repeat 
  3420.         jtime = jtime + const_julian_minute * sf_next_visible_precision * 0.2
  3421.         SGP_calculateSun (jtime)
  3422.         SGP_calculateSatellite (s , jtime)
  3423.     until ( (not sgp_sats[s].visible) or (sgp_sats[s].elevacion < sf_sky.elevacion_minima))
  3424.    
  3425.     jtime = jtime - const_julian_minute * sf_next_visible_precision * 0.2
  3426.     SGP_calculateSun (jtime)
  3427.     SGP_calculateSatellite (s , jtime)
  3428.    
  3429.     --apuntamos donde termina de verse
  3430.     sf_next_passes[sf_next_current].fin_jt = SGP_UTC_to_Local(jtime)
  3431.     sf_next_passes[sf_next_current].fin_elevacion = sgp_sats[s].elevacion
  3432.     sf_next_passes[sf_next_current].fin_azimut = sgp_sats[s].azimut
  3433.  
  3434.     sf_next_passes[sf_next_current].sat = s
  3435.    
  3436.     --lo dejamos todo como estaba (en el tiempo real), incluso la dirección, llamando dos veces
  3437.     SGP_calculateSun (sgp_time.ujtime - const_julian_minute)
  3438.     SGP_calculateSatellite (s , sgp_time.ujtime - const_julian_minute)
  3439.     SGP_calculateSun (sgp_time.ujtime)
  3440.     SGP_calculateSatellite (s , sgp_time.ujtime)
  3441.    
  3442.     local year, month, day, hour, minute, second = SGP_Julian_To_Date(sf_next_passes[sf_next_current].ini_jt)
  3443.     sf_next_passes[sf_next_current].linea = string.format ("%02d",day) .. "/" .. string.format ("%02d",month)
  3444.     .. "/" .. year .. " " .. string.format ("%02d",hour) .. ":" .. string.format("%02d",minute)
  3445.     .. " " .. string.format("%02d", sf_next_passes[sf_next_current].ini_elevacion) .. " "
  3446.     .. SF_azimutToString(sf_next_passes[sf_next_current].ini_azimut) .. " "
  3447.  
  3448.     year, month, day, hour, minute, second = SGP_Julian_To_Date(sf_next_passes[sf_next_current].max_jt)
  3449.     sf_next_passes[sf_next_current].linea = sf_next_passes[sf_next_current].linea
  3450.     .. string.format ("%02d",hour) .. ":" .. string.format("%02d",minute)
  3451.     .. " " .. string.format("%02d", sf_next_passes[sf_next_current].max_elevacion) .. " "
  3452.     .. SF_azimutToString(sf_next_passes[sf_next_current].max_azimut) .. " "
  3453.  
  3454.     year, month, day, hour, minute, second = SGP_Julian_To_Date(sf_next_passes[sf_next_current].fin_jt)
  3455.     sf_next_passes[sf_next_current].linea = sf_next_passes[sf_next_current].linea
  3456.     .. string.format ("%02d",hour) .. ":" .. string.format("%02d",minute)
  3457.     .. " " .. string.format("%02d", sf_next_passes[sf_next_current].fin_elevacion) .. " "
  3458.     .. SF_azimutToString(sf_next_passes[sf_next_current].fin_azimut) .. " " .. sgp_sats[s].nombre
  3459.    
  3460.     return true
  3461.  
  3462. end
  3463.  
  3464. function SGP_calculateSatNextVisiblePass(s)
  3465.     local entrada
  3466.  
  3467.     local jtime
  3468.    
  3469.     if sf_pass_limit == 0 then
  3470.         SGP_getCurrentTime()
  3471.         jtime = sgp_time.ujtime
  3472.     else
  3473.         jtime = sf_passes[sf_pass_limit].fin_jt + const_julian_minute * 20
  3474.     end
  3475.    
  3476.     sf_pass_limit = sf_pass_limit + 1
  3477.     sf_pass_current = sf_pass_limit
  3478.     -- no sabemos donde se encuentra ahora mismo el satélite, pero no queremos
  3479.     -- que se encuentre a la vista, asi que si es así, lo metemos en el horizonte
  3480.     -- con saltos grandes de 15 minutos
  3481.     SGP_calculateSun (jtime)
  3482.     SGP_calculateSatellite (s , jtime)
  3483.  
  3484.     -- buscamos la proxima vez que estará visible y por encima del horizonte.
  3485.     -- incrementamos mucho cuando su elevacion baja y poco cuando sube
  3486.     local sf_mostrar_progreso = 0
  3487.     while not ( sgp_sats[s].elevacion > sf_sky.elevacion_minima and sgp_sats[s].visible ) do
  3488.    
  3489.         if sgp_sats[s].elevacion > sgp_sats[s].elevacion_ant and sgp_sun.ele < 15 then
  3490.             jtime = jtime + const_julian_minute * sf_next_visible_precision
  3491.         else
  3492.             jtime = jtime + const_julian_minute * sf_next_visible_precision * 50
  3493.         end
  3494.         SGP_calculateSun (jtime)
  3495.         SGP_calculateSatellite (s , jtime)
  3496.         if sf_mostrar_progreso <= 0 then
  3497.             sf_mostrar_progreso = 15
  3498.  
  3499.             local jt_temp = SGP_UTC_to_Local(jtime)
  3500.             local year , month, day, hour, minute, second = SGP_Julian_To_Date(jt_temp)
  3501.  
  3502.             SF_drawInfoBar(false)
  3503.            
  3504.             SF_erase(8, 21+sf_pass_limit*10, 60)
  3505.             screen.print(8, 21+sf_pass_limit*10, string.format ("%02d",day) .. "/" .. string.format ("%02d",month)
  3506.             .. "/" .. year .. "  " .. string.format ("%02d",hour) .. ":" .. string.format("%02d",minute), sf_palette[12])
  3507.             screen.waitvblankstart()
  3508.             screen.flip()
  3509.             entrada = Controls.read()
  3510.            
  3511.             if entrada:square() then
  3512.                 --lo dejamos todo como estaba (en el tiempo real), incluso la dirección, llamando dos veces
  3513.                 SGP_calculateSun (sgp_time.ujtime - const_julian_minute)
  3514.                 SGP_calculateSatellite (s , sgp_time.ujtime - const_julian_minute)
  3515.                 SGP_calculateSun (sgp_time.ujtime)
  3516.                 SGP_calculateSatellite (s , sgp_time.ujtime)
  3517.                 sf_pass_limit = sf_pass_limit - 1
  3518.                 sf_pass_current = sf_pass_limit
  3519.                 SF_erase(1, 20+sf_pass_limit*10, 60)
  3520.                 screen.waitvblankstart()
  3521.                 screen.flip()
  3522.                
  3523.                 return false           
  3524.             end        
  3525.         else
  3526.             sf_mostrar_progreso = sf_mostrar_progreso - 1
  3527.         end
  3528.     end
  3529.  
  3530.     -- una vez encontrado un punto donde el satelite es visible por encima de la elevación minima establecida
  3531.     -- buscamos el inicio de este hecho retrocediendo en saltitos pequeños
  3532.  
  3533.     repeat 
  3534.         jtime = jtime - const_julian_minute * sf_next_visible_precision * 0.2
  3535.         SGP_calculateSun (jtime)
  3536.         SGP_calculateSatellite (s , jtime)
  3537.        
  3538.     until ( not sgp_sats[s].visible or sgp_sats[s].elevacion <= sf_sky.elevacion_minima)
  3539.  
  3540.     --hemos encontrado cuando empieza a ser visible. Lo apuntamos.
  3541.     jtime = jtime + const_julian_minute * sf_next_visible_precision * 0.2
  3542.     SGP_calculateSun (jtime)
  3543.     SGP_calculateSatellite (s , jtime)
  3544.  
  3545.     sf_passes[sf_pass_current].ini_jt = SGP_UTC_to_Local(jtime)
  3546.     sf_passes[sf_pass_current].ini_elevacion = sgp_sats[s].elevacion
  3547.     sf_passes[sf_pass_current].ini_azimut = sgp_sats[s].azimut
  3548.    
  3549.     --Ahora vamos a ver la elevación maxima visible que va a tener avanzando en saltos pequeños
  3550.    
  3551.     repeat 
  3552.         jtime = jtime + const_julian_minute * sf_next_visible_precision * 0.2
  3553.         SGP_calculateSun (jtime)
  3554.         SGP_calculateSatellite (s , jtime)
  3555.     until ( (sgp_sats[s].elevacion < sgp_sats[s].elevacion_ant) or (not sgp_sats[s].visible) or (sgp_sats[s].elevacion < sf_sky.elevacion_minima)) 
  3556.    
  3557.     jtime = jtime - const_julian_minute * sf_next_visible_precision * 0.2
  3558.     SGP_calculateSun (jtime)
  3559.     SGP_calculateSatellite (s , jtime)
  3560.    
  3561.     --apuntamos la elevacion maxima visible y ya que estamos el azimut
  3562.     sf_passes[sf_pass_current].max_jt = SGP_UTC_to_Local(jtime)
  3563.     sf_passes[sf_pass_current].max_elevacion = sgp_sats[s].elevacion
  3564.     sf_passes[sf_pass_current].max_azimut = sgp_sats[s].azimut
  3565.  
  3566.     --Ahora vamos a ver donde termina de ser visible
  3567.    
  3568.     repeat 
  3569.         jtime = jtime + const_julian_minute * sf_next_visible_precision * 0.2
  3570.         SGP_calculateSun (jtime)
  3571.         SGP_calculateSatellite (s , jtime)
  3572.     until ( (not sgp_sats[s].visible) or (sgp_sats[s].elevacion < sf_sky.elevacion_minima))
  3573.    
  3574.     jtime = jtime - const_julian_minute * sf_next_visible_precision * 0.2
  3575.     SGP_calculateSun (jtime)
  3576.     SGP_calculateSatellite (s , jtime)
  3577.    
  3578.     --apuntamos donde termina de verse
  3579.     sf_passes[sf_pass_current].fin_jt = SGP_UTC_to_Local(jtime)
  3580.     sf_passes[sf_pass_current].fin_elevacion = sgp_sats[s].elevacion
  3581.     sf_passes[sf_pass_current].fin_azimut = sgp_sats[s].azimut
  3582.  
  3583.    
  3584.    
  3585.     --lo dejamos todo como estaba (en el tiempo real), incluso la dirección, llamando dos veces
  3586.     SGP_calculateSun (sgp_time.ujtime - const_julian_minute)
  3587.     SGP_calculateSatellite (s , sgp_time.ujtime - const_julian_minute)
  3588.     SGP_calculateSun (sgp_time.ujtime)
  3589.     SGP_calculateSatellite (s , sgp_time.ujtime)
  3590.    
  3591.     local year, month, day, hour, minute, second = SGP_Julian_To_Date(sf_passes[sf_pass_current].ini_jt)
  3592.     sf_passes[sf_pass_current].linea = string.format ("%02d",day) .. "/" .. string.format ("%02d",month)
  3593.     .. "/" .. year .. "  " .. string.format ("%02d",hour) .. ":" .. string.format("%02d",minute)
  3594.     .. " " .. string.format("%02d", sf_passes[sf_pass_current].ini_elevacion) .. " "
  3595.     .. SF_azimutToString(sf_passes[sf_pass_current].ini_azimut) .. "  "
  3596.  
  3597.     year, month, day, hour, minute, second = SGP_Julian_To_Date(sf_passes[sf_pass_current].max_jt)
  3598.     sf_passes[sf_pass_current].linea = sf_passes[sf_pass_current].linea
  3599.     .. string.format ("%02d",hour) .. ":" .. string.format("%02d",minute)
  3600.     .. " " .. string.format("%02d", sf_passes[sf_pass_current].max_elevacion) .. " "
  3601.     .. SF_azimutToString(sf_passes[sf_pass_current].max_azimut) .. "  "
  3602.  
  3603.     year, month, day, hour, minute, second = SGP_Julian_To_Date(sf_passes[sf_pass_current].fin_jt)
  3604.     sf_passes[sf_pass_current].linea = sf_passes[sf_pass_current].linea
  3605.     .. string.format ("%02d",hour) .. ":" .. string.format("%02d",minute)
  3606.     .. " " .. string.format("%02d", sf_passes[sf_pass_current].fin_elevacion) .. " "
  3607.     .. SF_azimutToString(sf_passes[sf_pass_current].fin_azimut)
  3608.    
  3609.     local c = math.floor ((90- sf_passes[sf_pass_current].max_elevacion)/90*12)
  3610.     SF_erase(8, 21+sf_pass_limit*10, 60)
  3611.     screen.print(8, 21+sf_pass_limit*10, sf_passes[sf_pass_current].linea , sf_palette[4+c])           
  3612.     screen.waitvblankstart()
  3613.     screen.flip()
  3614.     SF_erase(8, 21+sf_pass_limit*10, 60)
  3615.     screen.print(8, 21+sf_pass_limit*10, sf_passes[sf_pass_current].linea , sf_palette[4+c])           
  3616.     screen.waitvblankstart()
  3617.     screen.flip()
  3618.    
  3619.     return true
  3620. end
  3621.  
  3622.  
  3623. function SF_generatePassPathSky(num_sat)   
  3624.     if sf_pass_current < 1 or sf_pass_current > sf_pass_limit then
  3625.         return
  3626.     end
  3627.    
  3628.     local jtime = SGP_Local_to_UTC(sf_passes[sf_pass_current].ini_jt)
  3629.     local jtime_fin = SGP_Local_to_UTC(sf_passes[sf_pass_current].fin_jt)
  3630.     local i = 0
  3631.     while jtime < jtime_fin and i < sf_pass_path.max do
  3632.         i = i + 1
  3633.         SGP_calculateSun (jtime)
  3634.         SGP_calculateSatellite (num_sat , jtime)
  3635.        
  3636.         sf_pass_path[i].azimut = sgp_sats[num_sat].azimut
  3637.         sf_pass_path[i].elevacion = sgp_sats[num_sat].elevacion
  3638.        
  3639.         sf_pass_path[i].latitud = sgp_sats[num_sat].latitud
  3640.         sf_pass_path[i].longitud = sgp_sats[num_sat].longitud
  3641.        
  3642.         jtime = jtime + const_julian_minute * 0.5
  3643.     end
  3644.     sf_pass_path.longitud = i
  3645.    
  3646.    
  3647.     SGP_getCurrentTime()
  3648.     jtime = sgp_time.ujtime
  3649.     SGP_calculateSun (jtime-const_julian_minute)
  3650.     SGP_calculateSatellite (num_sat , jtime-const_julian_minute)
  3651.     SGP_calculateSun (jtime)
  3652.     SGP_calculateSatellite (num_sat , jtime)
  3653. end
  3654.  
  3655. function SF_generateNextPassPathSky(num_sat)   
  3656.     if sf_next_current < 1 or sf_next_current > sf_next_limit then
  3657.         return
  3658.     end
  3659.    
  3660.     local jtime = SGP_Local_to_UTC(sf_next_passes[sf_next_current].ini_jt)
  3661.     local jtime_fin = SGP_Local_to_UTC(sf_next_passes[sf_next_current].fin_jt)
  3662.     local i = 0
  3663.     while jtime < jtime_fin and i < sf_pass_path.max do
  3664.         i = i + 1
  3665.         SGP_calculateSun (jtime)
  3666.         SGP_calculateSatellite (num_sat , jtime)
  3667.        
  3668.         sf_pass_path[i].azimut = sgp_sats[num_sat].azimut
  3669.         sf_pass_path[i].elevacion = sgp_sats[num_sat].elevacion
  3670.        
  3671.         sf_pass_path[i].latitud = sgp_sats[num_sat].latitud
  3672.         sf_pass_path[i].longitud = sgp_sats[num_sat].longitud
  3673.        
  3674.    
  3675.         jtime = jtime + const_julian_minute * 0.5
  3676.     end
  3677.     sf_pass_path.longitud = i
  3678.    
  3679.    
  3680.     SGP_getCurrentTime()
  3681.     jtime = sgp_time.ujtime
  3682.     SGP_calculateSun (jtime-const_julian_minute)
  3683.     SGP_calculateSatellite (num_sat , jtime-const_julian_minute)
  3684.     SGP_calculateSun (jtime)
  3685.     SGP_calculateSatellite (num_sat , jtime)
  3686. end
  3687.  
  3688. function SF_drawSatPathSky()
  3689.     local i
  3690.     if sf_pass_path.longitud <= 0 then
  3691.         return
  3692.     end
  3693.    
  3694.     for i = 1, sf_pass_path.longitud do
  3695.         SF_drawPointSky (sf_pass_path[i].azimut,sf_pass_path[i].elevacion,sf_palette[8])
  3696.     end
  3697. end
  3698.  
  3699. function SF_drawSatPathMap()
  3700.     local i
  3701.     if sf_pass_path.longitud <= 0 then
  3702.         return
  3703.     end
  3704.    
  3705.     for i = 1, sf_pass_path.longitud do
  3706.         SF_drawPointMap (sf_pass_path[i].latitud,sf_pass_path[i].longitud,sf_color_white)
  3707.     end
  3708. end
  3709.  
  3710.  
  3711. function SF_drawBatLevel(x, y, color_fuente)
  3712.     local level = batt.percent()
  3713.     local life = batt.time()
  3714.     local horas, minutos
  3715.     if level < 0 or level > 100 then
  3716.         level = "--"
  3717.     end
  3718.    
  3719.     if life < 0 or (life/60) > 20 then
  3720.         horas = "--"
  3721.         minutos = "--"
  3722.    
  3723.         color = color_fuente
  3724.     else
  3725.        
  3726.         horas = string.format ("%.0f",life / 60)
  3727.         minutos = string.format ("%.2d",life % 60)
  3728.        
  3729.         if sf_vision_nocturna then
  3730.             color = color_fuente
  3731.         else
  3732.             if level < 50 then
  3733.                 color = color.new(255,255 * level/50,0)
  3734.             else
  3735.                 color = color.new(255*(1-((level-50)/50)),255,0)
  3736.             end
  3737.         end
  3738.  
  3739.     end
  3740.     screen.print(x, y, "Autonomia: " .. horas.. "h " .. minutos .. "' ".. " Carga: " .. level .. "%" , color)
  3741.  
  3742.    
  3743. end
  3744.  
  3745. function SF_drawTempLevel(x, y,color_fuente)
  3746.     local level = batt.temp()
  3747.     local color
  3748.     if level > -20 and level < 90 then
  3749.         color = color_fuente
  3750.         if not sf_vision_nocturna then
  3751.             if level < 30 then
  3752.                 color = color.new(0,255,0)
  3753.             elseif level < 40 then
  3754.                 color = color.new(255*((level-30)/10),255,0)       
  3755.             elseif level < 50 then
  3756.                 color = color.new(255,255*(1-((level-40)/10)),0)
  3757.             else
  3758.                 color = color.new(255,0,0)
  3759.             end
  3760.         end
  3761.     else
  3762.         color = color_fuente
  3763.         level = "--"
  3764.     end
  3765.  
  3766.     screen.print(x, y, "Temperatura bateria: " .. level .. " C"  , color)
  3767. end
  3768.  
  3769. function SF_drawMemLevel(x, y,color_fuente)
  3770.     --local kbytes = System.getFreeMemory()/1024
  3771.     local kbytes = gcinfo ()
  3772.     local color = color_fuente
  3773.     if not sf_vision_nocturna then
  3774.         if kbytes < 3000 then
  3775.             color = color.new(0,255,0)
  3776.         elseif kbytes < 4000 then
  3777.             color = color.new(255,255,0)       
  3778.         else
  3779.             color = color.new(255,0,0)
  3780.         end
  3781.     end
  3782.  
  3783.     screen.print(x, y, "Memoria ocupada: " .. tostring(kbytes) .. " KB", color)
  3784. end
  3785.  
  3786.  
  3787. function SF_drawCrossSky(azimut, elevacion)
  3788.     if elevacion > 0 then  
  3789.         screen.waitvblankstart()
  3790.         screen.flip()
  3791.         SF_drawBitmapSky(azimut, elevacion , bitmap_cruz,0,0,bitmap_cruz:width(),bitmap_cruz:height())
  3792.         screen.waitvblankstart()
  3793.         screen.flip()
  3794.     end
  3795. end
  3796.  
  3797. function SF_drawSatSky( num_sat )
  3798.     if sgp_sats[num_sat].no_calcular > 0 then
  3799.         return
  3800.     end
  3801.  
  3802.     if (sgp_sats[num_sat].elevacion > sf_sky.elevacion_minima) then    
  3803.         if sgp_sats[num_sat].visible then
  3804.             SF_drawLineSky(sgp_sats[num_sat].azimut, sgp_sats[num_sat].elevacion, sf_sat_tails[num_sat][1].azimut, sf_sat_tails[num_sat][1].elevacion, sf_palette[1])      
  3805.             if string.sub(sgp_sats[num_sat].nombre,1,7) == "IRIDIUM" then  
  3806.                 SF_drawSuperCrossSky(sgp_sats[num_sat].azimut, sgp_sats[num_sat].elevacion,sf_palette[1])
  3807.             else
  3808.                 SF_drawSuperPointSky(sgp_sats[num_sat].azimut, sgp_sats[num_sat].elevacion,sf_palette[1])
  3809.             end
  3810.             if sf_sky.show.satelliteNames then
  3811.                 SF_drawLabelSatsSky(sgp_sats[num_sat].azimut , sgp_sats[num_sat].elevacion , sgp_sats[num_sat].nombre, sf_palette[1])
  3812.             end
  3813.         else
  3814.             SF_drawLineSky(sgp_sats[num_sat].azimut, sgp_sats[num_sat].elevacion, sf_sat_tails[num_sat][1].azimut, sf_sat_tails[num_sat][1].elevacion, sf_palette[12])     
  3815.             if string.sub(sgp_sats[num_sat].nombre,1,7) == "IRIDIUM" then  
  3816.                 SF_drawSuperCrossSky(sgp_sats[num_sat].azimut, sgp_sats[num_sat].elevacion,sf_palette[12])
  3817.             else
  3818.                 SF_drawSuperPointSky(sgp_sats[num_sat].azimut, sgp_sats[num_sat].elevacion,sf_palette[12])
  3819.             end
  3820.             if sf_sky.show.satelliteNames then
  3821.                 SF_drawLabelSatsSky(sgp_sats[num_sat].azimut , sgp_sats[num_sat].elevacion , sgp_sats[num_sat].nombre, sf_palette[12])
  3822.             end            
  3823.         end
  3824.         if not sf_moving_sky then
  3825.             for i = 1, sf_tail_length-1 do
  3826.                 if sf_sat_tails[num_sat][i].eclipsado then
  3827.                     SF_drawLineSky(sf_sat_tails[num_sat][i].azimut, sf_sat_tails[num_sat][i].elevacion, sf_sat_tails[num_sat][i+1].azimut, sf_sat_tails[num_sat][i+1].elevacion, sf_palette[math.floor(12+(i/4))]) 
  3828.                 else
  3829.                     SF_drawLineSky(sf_sat_tails[num_sat][i].azimut, sf_sat_tails[num_sat][i].elevacion, sf_sat_tails[num_sat][i+1].azimut, sf_sat_tails[num_sat][i+1].elevacion, sf_palette[i])
  3830.                 end
  3831.             end
  3832.         end
  3833.         sf_sat_tails[num_sat].step_current = sf_sat_tails[num_sat].step_current + 1
  3834.         if sf_sat_tails[num_sat].step_current >= sf_step_length then
  3835.             sf_sat_tails[num_sat].step_current = 0
  3836.             SF_AddPointToTail( num_sat, sgp_sats[num_sat].azimut, sgp_sats[num_sat].elevacion,sgp_sats[num_sat].eclipsado)
  3837.         end
  3838.     else
  3839.         if sgp_sats[num_sat].elevacion < 0 and sgp_sats[num_sat].elevacion_ant > 0 then
  3840.             SF_resetTail(num_sat)
  3841.         end
  3842.     end
  3843. end
  3844.  
  3845.  
  3846.  
  3847. function SF_drawMap ()
  3848.     --Polo Norte
  3849.     local fronterasX = {}
  3850.     local fronterasY = {}
  3851.     local color_fronteras
  3852.     if sf_vision_nocturna then
  3853.         color_fronteras = sf_palette[8]
  3854.     else
  3855.         color_fronteras = sf_palette[8]
  3856.     end
  3857.  
  3858.     fronterasX[1] = 19  fronterasY[1] = 38
  3859.     fronterasX[2] = 30  fronterasY[2] = 37
  3860.     fronterasX[3] = 41  fronterasY[3] = 38
  3861.     fronterasX[4] = 43  fronterasY[4] = 39
  3862.     fronterasX[5] = 49  fronterasY[5] = 36
  3863.     fronterasX[6] = 63  fronterasY[6] = 36
  3864.     fronterasX[7] = 80  fronterasY[7] = 35
  3865.     fronterasX[8] = 91  fronterasY[8] = 34
  3866.     fronterasX[9] = 102  fronterasY[9] = 31
  3867.     fronterasX[10] = 105  fronterasY[10] = 27
  3868.     fronterasX[11] = 100  fronterasY[11] = 27
  3869.     fronterasX[12] = 99  fronterasY[12] = 26
  3870.     fronterasX[13] = 107  fronterasY[13] = 25
  3871.     fronterasX[14] = 125  fronterasY[14] = 25
  3872.     fronterasX[15] = 138  fronterasY[15] = 23
  3873.     fronterasX[16] = 154  fronterasY[16] = 21
  3874.     fronterasX[17] = 171  fronterasY[17] = 19
  3875.     fronterasX[18] = 190  fronterasY[18] = 17
  3876.     fronterasX[19] = 201  fronterasY[19] = 16
  3877.     fronterasX[20] = 231  fronterasY[20] = 15
  3878.     fronterasX[21] = 235  fronterasY[21] = 17
  3879.     fronterasX[22] = 244  fronterasY[22] = 17
  3880.     fronterasX[23] = 256  fronterasY[23] = 17
  3881.     fronterasX[24] = 270  fronterasY[24] = 16
  3882.     fronterasX[25] = 280  fronterasY[25] = 15
  3883.     fronterasX[26] = 295  fronterasY[26] = 14
  3884.     fronterasX[27] = 316  fronterasY[27] = 14
  3885.     fronterasX[28] = 324  fronterasY[28] = 16
  3886.     fronterasX[29] = 333  fronterasY[29] = 17
  3887.     fronterasX[30] = 337  fronterasY[30] = 17
  3888.     fronterasX[31] = 342  fronterasY[31] = 16
  3889.     fronterasX[32] = 344  fronterasY[32] = 18
  3890.     fronterasX[33] = 364  fronterasY[33] = 19
  3891.     fronterasX[34] = 375  fronterasY[34] = 19
  3892.     fronterasX[35] = 383  fronterasY[35] = 18
  3893.     fronterasX[36] = 395  fronterasY[36] = 18
  3894.     fronterasX[37] = 399  fronterasY[37] = 19
  3895.     fronterasX[38] = 407  fronterasY[38] = 18
  3896.     fronterasX[39] = 413  fronterasY[39] = 19
  3897.     fronterasX[40] = 417  fronterasY[40] = 20
  3898.     fronterasX[41] = 424  fronterasY[41] = 19
  3899.     fronterasX[42] = 433  fronterasY[42] = 20
  3900.     fronterasX[43] = 444  fronterasY[43] = 20
  3901.     fronterasX[44] = 451  fronterasY[44] = 19
  3902.     fronterasX[45] = 462  fronterasY[45] = 19
  3903.     fronterasX[46] = 471  fronterasY[46] = 20
  3904.     fronterasX[47] = 474  fronterasY[47] = 20
  3905.     fronterasX[48] = 476  fronterasY[48] = 19
  3906.     fronterasX[49] = 479  fronterasY[49] = 19
  3907.     fronterasX[50] = 481  fronterasY[50] = 20
  3908.     fronterasX[51] = 497  fronterasY[51] = 20
  3909.     fronterasX[52] = 499  fronterasY[52] = 22
  3910.     fronterasX[53] = 501  fronterasY[53] = 24
  3911.     fronterasX[54] = 505  fronterasY[54] = 23
  3912.     fronterasX[55] = 507  fronterasY[55] = 21
  3913.     fronterasX[56] = 510  fronterasY[56] = 19
  3914.     fronterasX[57] = 511  fronterasY[57] = 21
  3915.     fronterasX[58] = 515  fronterasY[58] = 21
  3916.     fronterasX[59] = 523  fronterasY[59] = 20
  3917.     fronterasX[60] = 531  fronterasY[60] = 21
  3918.     fronterasX[61] = 536  fronterasY[61] = 21
  3919.     fronterasX[62] = 542  fronterasY[62] = 21
  3920.     fronterasX[63] = 546  fronterasY[63] = 21
  3921.     fronterasX[64] = 553  fronterasY[64] = 21
  3922.     fronterasX[65] = 561  fronterasY[65] = 21
  3923.     fronterasX[66] = 569  fronterasY[66] = 21
  3924.     fronterasX[67] = 577  fronterasY[67] = 22
  3925.     fronterasX[68] = 573  fronterasY[68] = 23
  3926.     fronterasX[69] = 577  fronterasY[69] = 25
  3927.     fronterasX[70] = 580  fronterasY[70] = 26
  3928.     fronterasX[71] = 585  fronterasY[71] = 27
  3929.     fronterasX[72] = 593  fronterasY[72] = 28
  3930.     fronterasX[73] = 593  fronterasY[73] = 31
  3931.     fronterasX[74] = 592  fronterasY[74] = 32
  3932.     fronterasX[75] = 597  fronterasY[75] = 30
  3933.     fronterasX[76] = 605  fronterasY[76] = 30
  3934.     fronterasX[77] = 613  fronterasY[77] = 29
  3935.     fronterasX[78] = 622  fronterasY[78] = 30
  3936.     fronterasX[79] = 626  fronterasY[79] = 32
  3937.     fronterasX[80] = 625  fronterasY[80] = 35
  3938.     fronterasX[81] = 630  fronterasY[81] = 36
  3939.     fronterasX[82] = 635  fronterasY[82] = 36
  3940.     fronterasX[83] = 639  fronterasY[83] = 37
  3941.     fronterasX[84] = 644  fronterasY[84] = 35
  3942.     fronterasX[85] = 651  fronterasY[85] = 34
  3943.     fronterasX[86] = 659  fronterasY[86] = 35
  3944.     fronterasX[87] = 668  fronterasY[87] = 34
  3945.     fronterasX[88] = 678  fronterasY[88] = 35
  3946.     fronterasX[89] = 686  fronterasY[89] = 36
  3947.     fronterasX[90] = 696  fronterasY[90] = 36
  3948.     fronterasX[91] = 707  fronterasY[91] = 37
  3949.     fronterasX[92] = 717  fronterasY[92] = 37
  3950.     fronterasX[93] = 719  fronterasY[93] = 37
  3951.  
  3952.     SF_drawPoliLine(fronterasX, fronterasY, 93, color_fronteras)
  3953.  
  3954.     fronterasX[1] = 2  fronterasY[1] = 36
  3955.     fronterasX[2] = 19  fronterasY[2] = 38
  3956.  
  3957.     SF_drawPoliLine(fronterasX, fronterasY, 2, color_fronteras)
  3958.    
  3959.     -- Islandia
  3960.    
  3961.     fronterasX[1] = 220  fronterasY[1] = 28
  3962.     fronterasX[2] = 226  fronterasY[2] = 29
  3963.     fronterasX[3] = 234  fronterasY[3] = 29
  3964.     fronterasX[4] = 239  fronterasY[4] = 29
  3965.     fronterasX[5] = 244  fronterasY[5] = 31
  3966.     fronterasX[6] = 249  fronterasY[6] = 36
  3967.     fronterasX[7] = 251  fronterasY[7] = 37
  3968.     fronterasX[8] = 248  fronterasY[8] = 38
  3969.     fronterasX[9] = 250  fronterasY[9] = 40
  3970.     fronterasX[10] = 251  fronterasY[10] = 38
  3971.     fronterasX[11] = 253  fronterasY[11] = 38
  3972.     fronterasX[12] = 255  fronterasY[12] = 39
  3973.     fronterasX[13] = 255  fronterasY[13] = 40
  3974.     fronterasX[14] = 250  fronterasY[14] = 42
  3975.     fronterasX[15] = 253  fronterasY[15] = 44
  3976.     fronterasX[16] = 254  fronterasY[16] = 43
  3977.     fronterasX[17] = 258  fronterasY[17] = 43
  3978.     fronterasX[18] = 257  fronterasY[18] = 45
  3979.     fronterasX[19] = 258  fronterasY[19] = 47
  3980.     fronterasX[20] = 256  fronterasY[20] = 49
  3981.     fronterasX[21] = 254  fronterasY[21] = 50
  3982.     fronterasX[22] = 255  fronterasY[22] = 51
  3983.     fronterasX[23] = 257  fronterasY[23] = 51
  3984.     fronterasX[24] = 260  fronterasY[24] = 53
  3985.     fronterasX[25] = 259  fronterasY[25] = 56
  3986.     fronterasX[26] = 261  fronterasY[26] = 58
  3987.     fronterasX[27] = 264  fronterasY[27] = 60
  3988.     fronterasX[28] = 266  fronterasY[28] = 60
  3989.     fronterasX[29] = 269  fronterasY[29] = 60
  3990.     fronterasX[30] = 271  fronterasY[30] = 62
  3991.     fronterasX[31] = 273  fronterasY[31] = 62
  3992.     fronterasX[32] = 276  fronterasY[32] = 58
  3993.     fronterasX[33] = 275  fronterasY[33] = 56
  3994.     fronterasX[34] = 277  fronterasY[34] = 55
  3995.     fronterasX[35] = 279  fronterasY[35] = 54
  3996.     fronterasX[36] = 281  fronterasY[36] = 51
  3997.     fronterasX[37] = 284  fronterasY[37] = 50
  3998.     fronterasX[38] = 290  fronterasY[38] = 49
  3999.     fronterasX[39] = 295  fronterasY[39] = 46
  4000.     fronterasX[40] = 300  fronterasY[40] = 45
  4001.     fronterasX[41] = 307  fronterasY[41] = 44
  4002.     fronterasX[42] = 315  fronterasY[42] = 42
  4003.     fronterasX[43] = 311  fronterasY[43] = 41
  4004.     fronterasX[44] = 309  fronterasY[44] = 40
  4005.     fronterasX[45] = 305  fronterasY[45] = 39
  4006.     fronterasX[46] = 308  fronterasY[46] = 38
  4007.     fronterasX[47] = 311  fronterasY[47] = 39
  4008.     fronterasX[48] = 313  fronterasY[48] = 40
  4009.     fronterasX[49] = 316  fronterasY[49] = 40
  4010.     fronterasX[50] = 311  fronterasY[50] = 38
  4011.     fronterasX[51] = 307  fronterasY[51] = 36
  4012.     fronterasX[52] = 310  fronterasY[52] = 35
  4013.     fronterasX[53] = 314  fronterasY[53] = 34
  4014.     fronterasX[54] = 322  fronterasY[54] = 33
  4015.     fronterasX[55] = 318  fronterasY[55] = 32
  4016.     fronterasX[56] = 321  fronterasY[56] = 31
  4017.     fronterasX[57] = 321  fronterasY[57] = 30
  4018.     fronterasX[58] = 318  fronterasY[58] = 29
  4019.     fronterasX[59] = 322  fronterasY[59] = 26
  4020.     fronterasX[60] = 322  fronterasY[60] = 25
  4021.     fronterasX[61] = 327  fronterasY[61] = 24
  4022.     fronterasX[62] = 331  fronterasY[62] = 22
  4023.     fronterasX[63] = 334  fronterasY[63] = 21
  4024.     fronterasX[64] = 325  fronterasY[64] = 21
  4025.     fronterasX[65] = 321  fronterasY[65] = 21
  4026.     fronterasX[66] = 318  fronterasY[66] = 20
  4027.     fronterasX[67] = 312  fronterasY[67] = 19
  4028.     fronterasX[68] = 308  fronterasY[68] = 18
  4029.     fronterasX[69] = 304  fronterasY[69] = 17
  4030.     fronterasX[70] = 300  fronterasY[70] = 18
  4031.     fronterasX[71] = 299  fronterasY[71] = 17
  4032.     fronterasX[72] = 294  fronterasY[72] = 17
  4033.     fronterasX[73] = 293  fronterasY[73] = 18
  4034.     fronterasX[74] = 291  fronterasY[74] = 18
  4035.     fronterasX[75] = 289  fronterasY[75] = 17
  4036.     fronterasX[76] = 287  fronterasY[76] = 18
  4037.     fronterasX[77] = 285  fronterasY[77] = 18
  4038.     fronterasX[78] = 283  fronterasY[78] = 19
  4039.     fronterasX[79] = 280  fronterasY[79] = 18
  4040.     fronterasX[80] = 279  fronterasY[80] = 18
  4041.     fronterasX[81] = 275  fronterasY[81] = 18
  4042.     fronterasX[82] = 271  fronterasY[82] = 18
  4043.     fronterasX[83] = 269  fronterasY[83] = 19
  4044.     fronterasX[84] = 266  fronterasY[84] = 19
  4045.     fronterasX[85] = 264  fronterasY[85] = 20
  4046.     fronterasX[86] = 261  fronterasY[86] = 20
  4047.     fronterasX[87] = 258  fronterasY[87] = 19
  4048.     fronterasX[88] = 260  fronterasY[88] = 22
  4049.     fronterasX[89] = 254  fronterasY[89] = 20
  4050.     fronterasX[90] = 252  fronterasY[90] = 21
  4051.     fronterasX[91] = 250  fronterasY[91] = 20
  4052.     fronterasX[92] = 245  fronterasY[92] = 20
  4053.     fronterasX[93] = 239  fronterasY[93] = 21
  4054.     fronterasX[94] = 233  fronterasY[94] = 22
  4055.     fronterasX[95] = 229  fronterasY[95] = 23
  4056.     fronterasX[96] = 224  fronterasY[96] = 24
  4057.     fronterasX[97] = 229  fronterasY[97] = 25
  4058.     fronterasX[98] = 224  fronterasY[98] = 26
  4059.     fronterasX[99] = 221  fronterasY[99] = 27  
  4060.     fronterasX[100] = 214 fronterasY[100] = 27
  4061.     fronterasX[101] = 220  fronterasY[101] = 28
  4062.     SF_drawPoliLine(fronterasX, fronterasY, 101, color_fronteras)
  4063.            
  4064.     --America
  4065. -- Africa, Arabia saudi e India
  4066. -- China y Japon
  4067.     fronterasX[1] = 606  fronterasY[1] = 107
  4068.     fronterasX[2] = 602  fronterasY[2] = 109
  4069.     fronterasX[3] = 601  fronterasY[3] = 107
  4070.     fronterasX[4] = 598  fronterasY[4] = 107
  4071.     fronterasX[5] = 602  fronterasY[5] = 105
  4072.     fronterasX[6] = 608  fronterasY[6] = 100
  4073.     fronterasX[7] = 606  fronterasY[7] = 104
  4074.     fronterasX[8] = 607  fronterasY[8] = 105
  4075.     fronterasX[9] = 612  fronterasY[9] = 104
  4076.     fronterasX[10] = 615  fronterasY[10] = 104
  4077.     fronterasX[11] = 613  fronterasY[11] = 107
  4078.     fronterasX[12] = 615  fronterasY[12] = 108
  4079.     fronterasX[13] = 617  fronterasY[13] = 108
  4080.     fronterasX[14] = 617  fronterasY[14] = 111
  4081.     fronterasX[15] = 616  fronterasY[15] = 113
  4082.     fronterasX[16] = 618  fronterasY[16] = 114
  4083.     fronterasX[17] = 619  fronterasY[17] = 114
  4084.     fronterasX[18] = 624  fronterasY[18] = 111
  4085.     fronterasX[19] = 622  fronterasY[19] = 107
  4086.     fronterasX[20] = 620  fronterasY[20] = 104
  4087.     fronterasX[21] = 623  fronterasY[21] = 101
  4088.     fronterasX[22] = 628  fronterasY[22] = 96
  4089.     fronterasX[23] = 633  fronterasY[23] = 97
  4090.     fronterasX[24] = 640  fronterasY[24] = 92
  4091.     fronterasX[25] = 646  fronterasY[25] = 85
  4092.     fronterasX[26] = 647  fronterasY[26] = 79
  4093.     fronterasX[27] = 649  fronterasY[27] = 80
  4094.     fronterasX[28] = 649  fronterasY[28] = 85
  4095.     fronterasX[29] = 649  fronterasY[29] = 91
  4096.     fronterasX[30] = 648  fronterasY[30] = 95
  4097.     fronterasX[31] = 645  fronterasY[31] = 98
  4098.     fronterasX[32] = 646  fronterasY[32] = 101
  4099.     fronterasX[33] = 645  fronterasY[33] = 102
  4100.     fronterasX[34] = 645  fronterasY[34] = 105
  4101.     fronterasX[35] = 642  fronterasY[35] = 108
  4102.     fronterasX[36] = 638  fronterasY[36] = 108
  4103.     fronterasX[37] = 638  fronterasY[37] = 110
  4104.     fronterasX[38] = 635  fronterasY[38] = 112
  4105.     fronterasX[39] = 631  fronterasY[39] = 112
  4106.     fronterasX[40] = 628  fronterasY[40] = 113
  4107.     fronterasX[41] = 624  fronterasY[41] = 116
  4108.     fronterasX[42] = 624  fronterasY[42] = 118
  4109.     fronterasX[43] = 626  fronterasY[43] = 122
  4110.     fronterasX[44] = 628  fronterasY[44] = 118
  4111.     fronterasX[45] = 629  fronterasY[45] = 116
  4112.     fronterasX[46] = 630  fronterasY[46] = 118
  4113.     fronterasX[47] = 631  fronterasY[47] = 116
  4114.     fronterasX[48] = 633  fronterasY[48] = 116
  4115.     fronterasX[49] = 634  fronterasY[49] = 115
  4116.     fronterasX[50] = 636  fronterasY[50] = 116
  4117.     fronterasX[51] = 639  fronterasY[51] = 114
  4118.     fronterasX[52] = 641  fronterasY[52] = 114
  4119.     fronterasX[53] = 644  fronterasY[53] = 112
  4120.     fronterasX[54] = 647  fronterasY[54] = 111
  4121.     fronterasX[55] = 647  fronterasY[55] = 108
  4122.     fronterasX[56] = 649  fronterasY[56] = 105
  4123.     fronterasX[57] = 649  fronterasY[57] = 103
  4124.     fronterasX[58] = 647  fronterasY[58] = 101
  4125.     fronterasX[59] = 647  fronterasY[59] = 99
  4126.     fronterasX[60] = 646  fronterasY[60] = 98
  4127.     fronterasX[61] = 649  fronterasY[61] = 98
  4128.     fronterasX[62] = 652  fronterasY[62] = 100
  4129.     fronterasX[63] = 653  fronterasY[63] = 97
  4130.     fronterasX[64] = 657  fronterasY[64] = 96
  4131.     fronterasX[65] = 652  fronterasY[65] = 94
  4132.     fronterasX[66] = 649  fronterasY[66] = 91
  4133.     fronterasX[67] = 652  fronterasY[67] = 89
  4134.     fronterasX[68] = 651  fronterasY[68] = 87
  4135.     fronterasX[69] = 652  fronterasY[69] = 84
  4136.     fronterasX[70] = 654  fronterasY[70] = 84
  4137.     fronterasX[71] = 653  fronterasY[71] = 81
  4138.     fronterasX[72] = 652  fronterasY[72] = 79
  4139.     fronterasX[73] = 653  fronterasY[73] = 76
  4140.     fronterasX[74] = 651  fronterasY[74] = 75
  4141.     fronterasX[75] = 648  fronterasY[75] = 76
  4142.     fronterasX[76] = 645  fronterasY[76] = 74
  4143.     fronterasX[77] = 642  fronterasY[77] = 75
  4144.     fronterasX[78] = 640  fronterasY[78] = 72
  4145.     fronterasX[79] = 641  fronterasY[79] = 69
  4146.     fronterasX[80] = 646  fronterasY[80] = 66
  4147.     fronterasX[81] = 653  fronterasY[81] = 64
  4148.     fronterasX[82] = 662  fronterasY[82] = 63
  4149.     fronterasX[83] = 670  fronterasY[83] = 63
  4150.     fronterasX[84] = 676  fronterasY[84] = 62
  4151.     fronterasX[85] = 682  fronterasY[85] = 59
  4152.     fronterasX[86] = 686  fronterasY[86] = 59
  4153.     fronterasX[87] = 688  fronterasY[87] = 61
  4154.     fronterasX[88] = 686  fronterasY[88] = 64
  4155.     fronterasX[89] = 681  fronterasY[89] = 67
  4156.     fronterasX[90] = 678  fronterasY[90] = 70
  4157.     fronterasX[91] = 677  fronterasY[91] = 72
  4158.     fronterasX[92] = 678  fronterasY[92] = 75
  4159.     fronterasX[93] = 680  fronterasY[93] = 79
  4160.     fronterasX[94] = 683  fronterasY[94] = 79
  4161.     fronterasX[95] = 683  fronterasY[95] = 77
  4162.     fronterasX[96] = 686  fronterasY[96] = 77
  4163.     fronterasX[97] = 687  fronterasY[97] = 74
  4164.     fronterasX[98] = 690  fronterasY[98] = 74
  4165.     fronterasX[99] = 692  fronterasY[99] = 72
  4166.     fronterasX[100] = 693  fronterasY[100] = 70
  4167.     fronterasX[101] = 693  fronterasY[101] = 68
  4168.     fronterasX[102] = 692  fronterasY[102] = 66
  4169.     fronterasX[103] = 694  fronterasY[103] = 65
  4170.     fronterasX[104] = 695  fronterasY[104] = 63
  4171.     fronterasX[105] = 698  fronterasY[105] = 63
  4172.     fronterasX[106] = 701  fronterasY[106] = 61
  4173.     fronterasX[107] = 705  fronterasY[107] = 62
  4174.     fronterasX[108] = 712  fronterasY[108] = 61
  4175.     fronterasX[109] = 717  fronterasY[109] = 59
  4176.     fronterasX[110] = 719  fronterasY[110] = 59
  4177.     SF_drawPoliLine(fronterasX, fronterasY, 110, color_fronteras)      
  4178.     fronterasX[1] = 609  fronterasY[1] = 109
  4179.     fronterasX[2] = 606  fronterasY[2] = 107
  4180.     SF_drawPoliLine(fronterasX, fronterasY, 2, color_fronteras)
  4181.    
  4182.     -- Europa
  4183. -- Mediterraneo
  4184.     fronterasX[1] = 432  fronterasY[1] = 92
  4185.     fronterasX[2] = 429  fronterasY[2] = 91
  4186.     fronterasX[3] = 433  fronterasY[3] = 89
  4187.     fronterasX[4] = 437  fronterasY[4] = 88
  4188.     fronterasX[5] = 436  fronterasY[5] = 90
  4189.     fronterasX[6] = 437  fronterasY[6] = 92
  4190.     fronterasX[7] = 434  fronterasY[7] = 92
  4191.     fronterasX[8] = 437  fronterasY[8] = 94
  4192.     fronterasX[9] = 441  fronterasY[9] = 97
  4193.     fronterasX[10] = 443  fronterasY[10] = 99
  4194.     fronterasX[11] = 440  fronterasY[11] = 101
  4195.     fronterasX[12] = 433  fronterasY[12] = 101
  4196.     fronterasX[13] = 429  fronterasY[13] = 99
  4197.     fronterasX[14] = 425  fronterasY[14] = 100
  4198.     fronterasX[15] = 420  fronterasY[15] = 100
  4199.     fronterasX[16] = 418  fronterasY[16] = 102
  4200.     fronterasX[17] = 413  fronterasY[17] = 103
  4201.     fronterasX[18] = 412  fronterasY[18] = 104
  4202.     fronterasX[19] = 412  fronterasY[19] = 106
  4203.     fronterasX[20] = 411  fronterasY[20] = 108
  4204.     fronterasX[21] = 413  fronterasY[21] = 109
  4205.     fronterasX[22] = 414  fronterasY[22] = 110
  4206.     fronterasX[23] = 417  fronterasY[23] = 110
  4207.     fronterasX[24] = 419  fronterasY[24] = 110
  4208.     fronterasX[25] = 422  fronterasY[25] = 110
  4209.     fronterasX[26] = 424  fronterasY[26] = 109
  4210.     fronterasX[27] = 426  fronterasY[27] = 110
  4211.     fronterasX[28] = 428  fronterasY[28] = 109
  4212.     fronterasX[29] = 432  fronterasY[29] = 111
  4213.     fronterasX[30] = 431  fronterasY[30] = 113
  4214.     fronterasX[31] = 430  fronterasY[31] = 116
  4215.     fronterasX[32] = 429  fronterasY[32] = 118
  4216.     fronterasX[33] = 428  fronterasY[33] = 120
  4217.     fronterasX[34] = 424  fronterasY[34] = 121
  4218.     fronterasX[35] = 422  fronterasY[35] = 123
  4219.     fronterasX[36] = 421  fronterasY[36] = 121
  4220.     fronterasX[37] = 419  fronterasY[37] = 121
  4221.     fronterasX[38] = 415  fronterasY[38] = 120
  4222.     fronterasX[39] = 410  fronterasY[39] = 119
  4223.     fronterasX[40] = 404  fronterasY[40] = 117
  4224.     fronterasX[41] = 402  fronterasY[41] = 117
  4225.     fronterasX[42] = 400  fronterasY[42] = 118
  4226.     fronterasX[43] = 399  fronterasY[43] = 120
  4227.     fronterasX[44] = 399  fronterasY[44] = 122
  4228.     fronterasX[45] = 397  fronterasY[45] = 122
  4229.     fronterasX[46] = 390  fronterasY[46] = 120
  4230.     fronterasX[47] = 388  fronterasY[47] = 118
  4231.     fronterasX[48] = 385  fronterasY[48] = 117
  4232.     fronterasX[49] = 380  fronterasY[49] = 116
  4233.     fronterasX[50] = 379  fronterasY[50] = 115
  4234.     fronterasX[51] = 381  fronterasY[51] = 113
  4235.     fronterasX[52] = 380  fronterasY[52] = 111
  4236.     fronterasX[53] = 380  fronterasY[53] = 109
  4237.     fronterasX[54] = 378  fronterasY[54] = 108
  4238.     fronterasX[55] = 376  fronterasY[55] = 109
  4239.     fronterasX[56] = 373  fronterasY[56] = 109
  4240.     fronterasX[57] = 371  fronterasY[57] = 109
  4241.     fronterasX[58] = 367  fronterasY[58] = 109
  4242.     fronterasX[59] = 365  fronterasY[59] = 109
  4243.     fronterasX[60] = 362  fronterasY[60] = 111
  4244.     fronterasX[61] = 359  fronterasY[61] = 112
  4245.     fronterasX[62] = 356  fronterasY[62] = 112
  4246.     fronterasX[63] = 353  fronterasY[63] = 113
  4247.     fronterasX[64] = 352  fronterasY[64] = 112
  4248.     fronterasX[65] = 350  fronterasY[65] = 111
  4249.     SF_drawPoliLine(fronterasX, fronterasY, 65, color_fronteras)       
  4250.     fronterasX[1] = 428  fronterasY[1] = 94
  4251.     fronterasX[2] = 432  fronterasY[2] = 92
  4252.     SF_drawPoliLine(fronterasX, fronterasY, 2, color_fronteras)
  4253.    
  4254.     -- Australia
  4255.     fronterasX[1] = 649  fronterasY[1] = 208
  4256.     fronterasX[2] = 648  fronterasY[2] = 212
  4257.     fronterasX[3] = 648  fronterasY[3] = 217
  4258.     fronterasX[4] = 645  fronterasY[4] = 220
  4259.     fronterasX[5] = 642  fronterasY[5] = 218
  4260.     fronterasX[6] = 639  fronterasY[6] = 216
  4261.     fronterasX[7] = 636  fronterasY[7] = 214
  4262.     fronterasX[8] = 637  fronterasY[8] = 213
  4263.     fronterasX[9] = 639  fronterasY[9] = 213
  4264.     fronterasX[10] = 639  fronterasY[10] = 212
  4265.     fronterasX[11] = 637  fronterasY[11] = 211
  4266.     fronterasX[12] = 638  fronterasY[12] = 209
  4267.     fronterasX[13] = 638  fronterasY[13] = 207
  4268.     fronterasX[14] = 633  fronterasY[14] = 208
  4269.     fronterasX[15] = 630  fronterasY[15] = 207
  4270.     fronterasX[16] = 628  fronterasY[16] = 207
  4271.     fronterasX[17] = 629  fronterasY[17] = 209
  4272.     fronterasX[18] = 625  fronterasY[18] = 207
  4273.     fronterasX[19] = 625  fronterasY[19] = 210
  4274.     fronterasX[20] = 623  fronterasY[20] = 213
  4275.     fronterasX[21] = 620  fronterasY[21] = 213
  4276.     fronterasX[22] = 618  fronterasY[22] = 212
  4277.     fronterasX[23] = 615  fronterasY[23] = 213
  4278.     fronterasX[24] = 612  fronterasY[24] = 216
  4279.     fronterasX[25] = 611  fronterasY[25] = 218
  4280.     fronterasX[26] = 609  fronterasY[26] = 217
  4281.     fronterasX[27] = 607  fronterasY[27] = 219
  4282.     fronterasX[28] = 608  fronterasY[28] = 221
  4283.     fronterasX[29] = 605  fronterasY[29] = 224
  4284.     fronterasX[30] = 600  fronterasY[30] = 225
  4285.     fronterasX[31] = 594  fronterasY[31] = 227
  4286.     fronterasX[32] = 591  fronterasY[32] = 228
  4287.     fronterasX[33] = 590  fronterasY[33] = 233
  4288.     fronterasX[34] = 590  fronterasY[34] = 237
  4289.     fronterasX[35] = 593  fronterasY[35] = 243
  4290.     fronterasX[36] = 594  fronterasY[36] = 247
  4291.     fronterasX[37] = 595  fronterasY[37] = 251
  4292.     fronterasX[38] = 593  fronterasY[38] = 254
  4293.     fronterasX[39] = 597  fronterasY[39] = 255
  4294.     fronterasX[40] = 601  fronterasY[40] = 253
  4295.     fronterasX[41] = 607  fronterasY[41] = 252
  4296.     fronterasX[42] = 612  fronterasY[42] = 251
  4297.     fronterasX[43] = 618  fronterasY[43] = 249
  4298.     fronterasX[44] = 623  fronterasY[44] = 248
  4299.     fronterasX[45] = 629  fronterasY[45] = 248
  4300.     fronterasX[46] = 632  fronterasY[46] = 250
  4301.     fronterasX[47] = 634  fronterasY[47] = 252
  4302.     fronterasX[48] = 636  fronterasY[48] = 254
  4303.     fronterasX[49] = 638  fronterasY[49] = 252
  4304.     fronterasX[50] = 640  fronterasY[50] = 251
  4305.     fronterasX[51] = 639  fronterasY[51] = 254
  4306.     fronterasX[52] = 638  fronterasY[52] = 257
  4307.     fronterasX[53] = 641  fronterasY[53] = 258
  4308.     fronterasX[54] = 642  fronterasY[54] = 257
  4309.     fronterasX[55] = 643  fronterasY[55] = 258
  4310.     fronterasX[56] = 645  fronterasY[56] = 261
  4311.     fronterasX[57] = 647  fronterasY[57] = 262
  4312.     fronterasX[58] = 650  fronterasY[58] = 262
  4313.     fronterasX[59] = 652  fronterasY[59] = 262
  4314.     fronterasX[60] = 654  fronterasY[60] = 261
  4315.     fronterasX[61] = 656  fronterasY[61] = 262
  4316.     fronterasX[62] = 660  fronterasY[62] = 261
  4317.     fronterasX[63] = 662  fronterasY[63] = 261
  4318.     fronterasX[64] = 664  fronterasY[64] = 260
  4319.     fronterasX[65] = 665  fronterasY[65] = 258
  4320.     fronterasX[66] = 666  fronterasY[66] = 255
  4321.     fronterasX[67] = 668  fronterasY[67] = 251
  4322.     fronterasX[68] = 671  fronterasY[68] = 247
  4323.     fronterasX[69] = 672  fronterasY[69] = 241
  4324.     fronterasX[70] = 671  fronterasY[70] = 236
  4325.     fronterasX[71] = 667  fronterasY[71] = 232
  4326.     fronterasX[72] = 665  fronterasY[72] = 229
  4327.     fronterasX[73] = 662  fronterasY[73] = 226
  4328.     fronterasX[74] = 658  fronterasY[74] = 223
  4329.     fronterasX[75] = 657  fronterasY[75] = 219
  4330.     fronterasX[76] = 656  fronterasY[76] = 215
  4331.     fronterasX[77] = 655  fronterasY[77] = 213
  4332.     fronterasX[78] = 652  fronterasY[78] = 212
  4333.     fronterasX[79] = 652  fronterasY[79] = 209
  4334.     fronterasX[80] = 651  fronterasY[80] = 208
  4335.     fronterasX[81] = 650  fronterasY[81] = 206
  4336.     fronterasX[82] = 649  fronterasY[82] = 208
  4337.     SF_drawPoliLine(fronterasX, fronterasY, 82, color_fronteras)       
  4338.  
  4339.     -- Antartida
  4340.    
  4341.     fronterasX[1] = 13  fronterasY[1] = 341
  4342.     fronterasX[2] = 23  fronterasY[2] = 341
  4343.     fronterasX[3] = 36  fronterasY[3] = 340
  4344.     fronterasX[4] = 38  fronterasY[4] = 338
  4345.     fronterasX[5] = 46  fronterasY[5] = 340
  4346.     fronterasX[6] = 53  fronterasY[6] = 338
  4347.     fronterasX[7] = 62  fronterasY[7] = 336
  4348.     fronterasX[8] = 77  fronterasY[8] = 335
  4349.     fronterasX[9] = 93  fronterasY[9] = 334
  4350.     fronterasX[10] = 97  fronterasY[10] = 333
  4351.     fronterasX[11] = 101  fronterasY[11] = 331
  4352.     fronterasX[12] = 108  fronterasY[12] = 333
  4353.     fronterasX[13] = 112  fronterasY[13] = 331
  4354.     fronterasX[14] = 118  fronterasY[14] = 333
  4355.     fronterasX[15] = 128  fronterasY[15] = 333
  4356.     fronterasX[16] = 135  fronterasY[16] = 333
  4357.     fronterasX[17] = 137  fronterasY[17] = 331
  4358.     fronterasX[18] = 142  fronterasY[18] = 333
  4359.     fronterasX[19] = 149  fronterasY[19] = 333
  4360.     fronterasX[20] = 150  fronterasY[20] = 331
  4361.     fronterasX[21] = 163  fronterasY[21] = 329
  4362.     fronterasX[22] = 165  fronterasY[22] = 332
  4363.     fronterasX[23] = 178  fronterasY[23] = 331
  4364.     fronterasX[24] = 187  fronterasY[24] = 332
  4365.     fronterasX[25] = 195  fronterasY[25] = 332
  4366.     fronterasX[26] = 200  fronterasY[26] = 330
  4367.     fronterasX[27] = 202  fronterasY[27] = 330
  4368.     fronterasX[28] = 203  fronterasY[28] = 332
  4369.     fronterasX[29] = 207  fronterasY[29] = 332
  4370.     fronterasX[30] = 204  fronterasY[30] = 330
  4371.     fronterasX[31] = 211  fronterasY[31] = 330
  4372.     fronterasX[32] = 212  fronterasY[32] = 330
  4373.     fronterasX[33] = 207  fronterasY[33] = 328
  4374.     fronterasX[34] = 205  fronterasY[34] = 326
  4375.     fronterasX[35] = 208  fronterasY[35] = 323
  4376.     fronterasX[36] = 213  fronterasY[36] = 327
  4377.     fronterasX[37] = 215  fronterasY[37] = 327
  4378.     fronterasX[38] = 213  fronterasY[38] = 325
  4379.     fronterasX[39] = 213  fronterasY[39] = 322
  4380.     fronterasX[40] = 219  fronterasY[40] = 323
  4381.     fronterasX[41] = 222  fronterasY[41] = 328
  4382.     fronterasX[42] = 220  fronterasY[42] = 330
  4383.     fronterasX[43] = 214  fronterasY[43] = 331
  4384.     fronterasX[44] = 221  fronterasY[44] = 331
  4385.     fronterasX[45] = 224  fronterasY[45] = 330
  4386.     fronterasX[46] = 224  fronterasY[46] = 327
  4387.     fronterasX[47] = 222  fronterasY[47] = 324
  4388.     fronterasX[48] = 224  fronterasY[48] = 323
  4389.     fronterasX[49] = 223  fronterasY[49] = 321
  4390.     fronterasX[50] = 224  fronterasY[50] = 320
  4391.     fronterasX[51] = 222  fronterasY[51] = 319
  4392.     fronterasX[52] = 229  fronterasY[52] = 315
  4393.     fronterasX[53] = 234  fronterasY[53] = 313
  4394.     fronterasX[54] = 240  fronterasY[54] = 311
  4395.     fronterasX[55] = 247  fronterasY[55] = 312
  4396.     fronterasX[56] = 242  fronterasY[56] = 314
  4397.     fronterasX[57] = 237  fronterasY[57] = 316
  4398.     fronterasX[58] = 235  fronterasY[58] = 318
  4399.     fronterasX[59] = 232  fronterasY[59] = 318
  4400.     fronterasX[60] = 229  fronterasY[60] = 319
  4401.     fronterasX[61] = 229  fronterasY[61] = 321
  4402.     fronterasX[62] = 231  fronterasY[62] = 323
  4403.     fronterasX[63] = 235  fronterasY[63] = 325
  4404.     fronterasX[64] = 238  fronterasY[64] = 327
  4405.     fronterasX[65] = 239  fronterasY[65] = 329
  4406.     fronterasX[66] = 237  fronterasY[66] = 333
  4407.     fronterasX[67] = 235  fronterasY[67] = 335
  4408.     fronterasX[68] = 264  fronterasY[68] = 340
  4409.     fronterasX[69] = 288  fronterasY[69] = 341
  4410.     fronterasX[70] = 303  fronterasY[70] = 338
  4411.     fronterasX[71] = 312  fronterasY[71] = 335
  4412.     fronterasX[72] = 313  fronterasY[72] = 334
  4413.     fronterasX[73] = 316  fronterasY[73] = 334
  4414.     fronterasX[74] = 325  fronterasY[74] = 331
  4415.     fronterasX[75] = 331  fronterasY[75] = 330
  4416.     fronterasX[76] = 335  fronterasY[76] = 329
  4417.     fronterasX[77] = 337  fronterasY[77] = 328
  4418.     fronterasX[78] = 341  fronterasY[78] = 327
  4419.     fronterasX[79] = 345  fronterasY[79] = 329
  4420.     fronterasX[80] = 348  fronterasY[80] = 326
  4421.     fronterasX[81] = 349  fronterasY[81] = 326
  4422.     fronterasX[82] = 351  fronterasY[82] = 328
  4423.     fronterasX[83] = 355  fronterasY[83] = 328
  4424.     fronterasX[84] = 355  fronterasY[84] = 326
  4425.     fronterasX[85] = 356  fronterasY[85] = 326
  4426.     fronterasX[86] = 359  fronterasY[86] = 329
  4427.     fronterasX[87] = 362  fronterasY[87] = 327
  4428.     fronterasX[88] = 364  fronterasY[88] = 326
  4429.     fronterasX[89] = 370  fronterasY[89] = 326
  4430.     fronterasX[90] = 385  fronterasY[90] = 325
  4431.     fronterasX[91] = 403  fronterasY[91] = 326
  4432.     fronterasX[92] = 421  fronterasY[92] = 323
  4433.     fronterasX[93] = 427  fronterasY[93] = 321
  4434.     fronterasX[94] = 432  fronterasY[94] = 324
  4435.     fronterasX[95] = 438  fronterasY[95] = 325
  4436.     fronterasX[96] = 440  fronterasY[96] = 322
  4437.     fronterasX[97] = 451  fronterasY[97] = 321
  4438.     fronterasX[98] = 461  fronterasY[98] = 319
  4439.     fronterasX[99] = 465  fronterasY[99] = 317
  4440.     fronterasX[100] = 471  fronterasY[100] = 317
  4441.     fronterasX[101] = 478  fronterasY[101] = 320
  4442.     fronterasX[102] = 494  fronterasY[102] = 321
  4443.     fronterasX[103] = 499  fronterasY[103] = 321
  4444.     fronterasX[104] = 500  fronterasY[104] = 327
  4445.     fronterasX[105] = 499  fronterasY[105] = 329
  4446.     fronterasX[106] = 503  fronterasY[106] = 329
  4447.     fronterasX[107] = 508  fronterasY[107] = 325
  4448.     fronterasX[108] = 516  fronterasY[108] = 322
  4449.     fronterasX[109] = 531  fronterasY[109] = 319
  4450.     fronterasX[110] = 543  fronterasY[110] = 319
  4451.     fronterasX[111] = 553  fronterasY[111] = 317
  4452.     fronterasX[112] = 567  fronterasY[112] = 316
  4453.     fronterasX[113] = 575  fronterasY[113] = 317
  4454.     fronterasX[114] = 582  fronterasY[114] = 319
  4455.     fronterasX[115] = 588  fronterasY[115] = 316
  4456.     fronterasX[116] = 593  fronterasY[116] = 316
  4457.     fronterasX[117] = 597  fronterasY[117] = 318
  4458.     fronterasX[118] = 603  fronterasY[118] = 319
  4459.     fronterasX[119] = 616  fronterasY[119] = 318
  4460.     fronterasX[120] = 623  fronterasY[120] = 319
  4461.     fronterasX[121] = 633  fronterasY[121] = 317
  4462.     fronterasX[122] = 634  fronterasY[122] = 314
  4463.     fronterasX[123] = 637  fronterasY[123] = 317
  4464.     fronterasX[124] = 648  fronterasY[124] = 319
  4465.     fronterasX[125] = 664  fronterasY[125] = 323
  4466.     fronterasX[126] = 679  fronterasY[126] = 325
  4467.     fronterasX[127] = 701  fronterasY[127] = 328
  4468.     fronterasX[128] = 704  fronterasY[128] = 329
  4469.     fronterasX[129] = 707  fronterasY[129] = 329
  4470.     fronterasX[130] = 708  fronterasY[130] = 330
  4471.     fronterasX[131] = 705  fronterasY[131] = 332
  4472.     fronterasX[132] = 700  fronterasY[132] = 334
  4473.     fronterasX[133] = 695  fronterasY[133] = 335
  4474.     fronterasX[134] = 692  fronterasY[134] = 338
  4475.     fronterasX[135] = 694  fronterasY[135] = 341
  4476.     fronterasX[136] = 699  fronterasY[136] = 342
  4477.     fronterasX[137] = 700  fronterasY[137] = 340
  4478.     fronterasX[138] = 706  fronterasY[138] = 342
  4479.     fronterasX[139] = 713  fronterasY[139] = 342
  4480.     fronterasX[140] = 719  fronterasY[140] = 342       
  4481.     SF_drawPoliLine(fronterasX, fronterasY, 140, color_fronteras)      
  4482.     fronterasX[1] = 2  fronterasY[1] = 341
  4483.     fronterasX[2] = 13  fronterasY[2] = 341    
  4484.     SF_drawPoliLine(fronterasX, fronterasY, 2, color_fronteras)
  4485.    
  4486.     -- Inglaterra
  4487.  
  4488.     fronterasX[1] = 363  fronterasY[1] = 81
  4489.     fronterasX[2] = 364  fronterasY[2] = 79
  4490.     fronterasX[3] = 364  fronterasY[3] = 77
  4491.     fronterasX[4] = 362  fronterasY[4] = 77
  4492.     fronterasX[5] = 361  fronterasY[5] = 75
  4493.     fronterasX[6] = 359  fronterasY[6] = 74
  4494.     fronterasX[7] = 358  fronterasY[7] = 72
  4495.     fronterasX[8] = 355  fronterasY[8] = 70
  4496.     fronterasX[9] = 357  fronterasY[9] = 68
  4497.     fronterasX[10] = 356  fronterasY[10] = 67
  4498.     fronterasX[11] = 353  fronterasY[11] = 68
  4499.     fronterasX[12] = 354  fronterasY[12] = 66
  4500.     fronterasX[13] = 355  fronterasY[13] = 65
  4501.     fronterasX[14] = 352  fronterasY[14] = 65
  4502.     fronterasX[15] = 350  fronterasY[15] = 67
  4503.     fronterasX[16] = 349  fronterasY[16] = 68
  4504.     fronterasX[17] = 349  fronterasY[17] = 70
  4505.     fronterasX[18] = 352  fronterasY[18] = 72
  4506.     fronterasX[19] = 354  fronterasY[19] = 73
  4507.     fronterasX[20] = 355  fronterasY[20] = 75
  4508.     fronterasX[21] = 353  fronterasY[21] = 77
  4509.     fronterasX[22] = 351  fronterasY[22] = 80
  4510.     fronterasX[23] = 355  fronterasY[23] = 79
  4511.     fronterasX[24] = 351  fronterasY[24] = 82
  4512.     fronterasX[25] = 358  fronterasY[25] = 81
  4513.     fronterasX[26] = 363  fronterasY[26] = 81      
  4514.     SF_drawPoliLine(fronterasX, fronterasY, 26, color_fronteras)       
  4515.  
  4516.     fronterasX[1] = 348  fronterasY[1] = 77
  4517.     fronterasX[2] = 349  fronterasY[2] = 73
  4518.     fronterasX[3] = 347  fronterasY[3] = 72
  4519.     fronterasX[4] = 346  fronterasY[4] = 73
  4520.     fronterasX[5] = 344  fronterasY[5] = 74
  4521.     fronterasX[6] = 342  fronterasY[6] = 74
  4522.     fronterasX[7] = 340  fronterasY[7] = 75
  4523.     fronterasX[8] = 342  fronterasY[8] = 77
  4524.     fronterasX[9] = 341  fronterasY[9] = 78
  4525.     fronterasX[10] = 342  fronterasY[10] = 79      
  4526.     fronterasX[11] = 348  fronterasY[11] = 77
  4527.     SF_drawPoliLine(fronterasX, fronterasY, 11, color_fronteras)       
  4528.  
  4529.     -- Madagascar
  4530.     fronterasX[1] = 455  fronterasY[1] = 213
  4531.     fronterasX[2] = 450  fronterasY[2] = 216
  4532.     fronterasX[3] = 448  fronterasY[3] = 220
  4533.     fronterasX[4] = 447  fronterasY[4] = 226
  4534.     fronterasX[5] = 447  fronterasY[5] = 232
  4535.     fronterasX[6] = 449  fronterasY[6] = 235
  4536.     fronterasX[7] = 454  fronterasY[7] = 234
  4537.     fronterasX[8] = 457  fronterasY[8] = 226
  4538.     fronterasX[9] = 460  fronterasY[9] = 217
  4539.     fronterasX[10] = 460  fronterasY[10] = 211
  4540.     fronterasX[11] = 459  fronterasY[11] = 209
  4541.     fronterasX[12] = 455  fronterasY[12] = 213
  4542.     SF_drawPoliLine(fronterasX, fronterasY, 12, color_fronteras)       
  4543.  
  4544.     --Islandia
  4545.     fronterasX[1] = 316  fronterasY[1] = 53
  4546.     fronterasX[2] = 321  fronterasY[2] = 55
  4547.     fronterasX[3] = 327  fronterasY[3] = 53
  4548.     fronterasX[4] = 331  fronterasY[4] = 53
  4549.     fronterasX[5] = 333  fronterasY[5] = 51
  4550.     fronterasX[6] = 328  fronterasY[6] = 49
  4551.     fronterasX[7] = 323  fronterasY[7] = 50
  4552.     fronterasX[8] = 319  fronterasY[8] = 51
  4553.     fronterasX[9] = 316  fronterasY[9] = 49
  4554.     fronterasX[10] = 313  fronterasY[10] = 51
  4555.     fronterasX[11] = 316  fronterasY[11] = 53
  4556.     SF_drawPoliLine(fronterasX, fronterasY, 11, color_fronteras)       
  4557.  
  4558.  
  4559.     --Indonesia
  4560. -- Nueva Zelanda
  4561.     fronterasX[1] = 703  fronterasY[1] = 274
  4562.     fronterasX[2] = 708  fronterasY[2] = 270
  4563.     fronterasX[3] = 713  fronterasY[3] = 267
  4564.     fronterasX[4] = 715  fronterasY[4] = 266
  4565.     fronterasX[5] = 714  fronterasY[5] = 270
  4566.     fronterasX[6] = 712  fronterasY[6] = 271
  4567.     fronterasX[7] = 710  fronterasY[7] = 274
  4568.     fronterasX[8] = 708  fronterasY[8] = 274
  4569.     fronterasX[9] = 708  fronterasY[9] = 277
  4570.     fronterasX[10] = 706  fronterasY[10] = 278
  4571.     fronterasX[11] = 704  fronterasY[11] = 279
  4572.     fronterasX[12] = 702  fronterasY[12] = 279
  4573.     fronterasX[13] = 700  fronterasY[13] = 278
  4574.     fronterasX[14] = 698  fronterasY[14] = 277
  4575.     fronterasX[15] = 703  fronterasY[15] = 274
  4576.     SF_drawPoliLine(fronterasX, fronterasY, 15, color_fronteras)       
  4577.  
  4578.     fronterasX[1] = 717  fronterasY[1] = 266
  4579.     fronterasX[2] = 717  fronterasY[2] = 265
  4580.     fronterasX[3] = 715  fronterasY[3] = 264
  4581.     fronterasX[4] = 713  fronterasY[4] = 264
  4582.     fronterasX[5] = 715  fronterasY[5] = 262
  4583.     fronterasX[6] = 716  fronterasY[6] = 261
  4584.     fronterasX[7] = 716  fronterasY[7] = 258
  4585.     fronterasX[8] = 713  fronterasY[8] = 256
  4586.     fronterasX[9] = 712  fronterasY[9] = 255
  4587.     fronterasX[10] = 715  fronterasY[10] = 255
  4588.     fronterasX[11] = 717  fronterasY[11] = 256
  4589.     fronterasX[12] = 718  fronterasY[12] = 259
  4590.     fronterasX[13] = 719  fronterasY[13] = 261
  4591.     fronterasX[14] = 719  fronterasY[14] = 261
  4592.     fronterasX[15] = 719  fronterasY[15] = 266
  4593.     fronterasX[16] = 717  fronterasY[16] = 269
  4594.     fronterasX[17] = 717  fronterasY[17] = 266
  4595.     SF_drawPoliLine(fronterasX, fronterasY, 17, color_fronteras)       
  4596.  
  4597.     fronterasX[1] = 5  fronterasY[1] = 53
  4598.     fronterasX[2] = 8  fronterasY[2] = 51
  4599.     fronterasX[3] = 11  fronterasY[3] = 50
  4600.     fronterasX[4] = 12  fronterasY[4] = 49
  4601.     fronterasX[5] = 10  fronterasY[5] = 48
  4602.     fronterasX[6] = 7  fronterasY[6] = 48
  4603.     fronterasX[7] = 5  fronterasY[7] = 47
  4604.     fronterasX[8] = 3  fronterasY[8] = 47
  4605.     fronterasX[9] = 2  fronterasY[9] = 45
  4606.     SF_drawPoliLine(fronterasX, fronterasY, 9, color_fronteras)    
  4607.     fronterasX[1] = 2  fronterasY[1] = 54
  4608.     fronterasX[2] = 5  fronterasY[2] = 53
  4609.     SF_drawPoliLine(fronterasX, fronterasY, 2, color_fronteras)
  4610.    
  4611.     fronterasX[1] = 245  fronterasY[1] = 81
  4612.     fronterasX[2] = 242  fronterasY[2] = 84
  4613.     fronterasX[3] = 241  fronterasY[3] = 86
  4614.     fronterasX[4] = 245  fronterasY[4] = 87
  4615.     fronterasX[5] = 248  fronterasY[5] = 86
  4616.     fronterasX[6] = 249  fronterasY[6] = 88
  4617.     fronterasX[7] = 251  fronterasY[7] = 87
  4618.     fronterasX[8] = 252  fronterasY[8] = 90
  4619.     fronterasX[9] = 254  fronterasY[9] = 89
  4620.     fronterasX[10] = 253  fronterasY[10] = 87
  4621.     fronterasX[11] = 251  fronterasY[11] = 84
  4622.     fronterasX[12] = 249  fronterasY[12] = 84
  4623.     fronterasX[13] = 246  fronterasY[13] = 82
  4624.     fronterasX[14] = 248  fronterasY[14] = 80
  4625.     fronterasX[15] = 246  fronterasY[15] = 80
  4626.     fronterasX[16] = 245  fronterasY[16] = 81
  4627.     SF_drawPoliLine(fronterasX, fronterasY, 16, color_fronteras)       
  4628.  
  4629.     fronterasX[1] = 190  fronterasY[1] = 139
  4630.     fronterasX[2] = 192  fronterasY[2] = 138
  4631.     fronterasX[3] = 194  fronterasY[3] = 137
  4632.     fronterasX[4] = 197  fronterasY[4] = 139
  4633.     fronterasX[5] = 199  fronterasY[5] = 139
  4634.     fronterasX[6] = 201  fronterasY[6] = 141
  4635.     fronterasX[7] = 203  fronterasY[7] = 142
  4636.     fronterasX[8] = 205  fronterasY[8] = 143
  4637.     fronterasX[9] = 209  fronterasY[9] = 143
  4638.     fronterasX[10] = 206  fronterasY[10] = 141
  4639.     fronterasX[11] = 204  fronterasY[11] = 140
  4640.     fronterasX[12] = 201  fronterasY[12] = 140
  4641.     fronterasX[13] = 199  fronterasY[13] = 138
  4642.     fronterasX[14] = 196  fronterasY[14] = 137
  4643.     fronterasX[15] = 193  fronterasY[15] = 137
  4644.     fronterasX[16] = 192  fronterasY[16] = 138
  4645.     fronterasX[17] = 190  fronterasY[17] = 137
  4646.     fronterasX[18] = 189  fronterasY[18] = 138
  4647.     fronterasX[19] = 190  fronterasY[19] = 139
  4648.     SF_drawPoliLine(fronterasX, fronterasY, 19, color_fronteras)       
  4649.  
  4650.     fronterasX[1] = 212  fronterasY[1] = 145
  4651.     fronterasX[2] = 214  fronterasY[2] = 143
  4652.     fronterasX[3] = 216  fronterasY[3] = 143
  4653.     fronterasX[4] = 218  fronterasY[4] = 145
  4654.     fronterasX[5] = 220  fronterasY[5] = 145
  4655.     fronterasX[6] = 222  fronterasY[6] = 146
  4656.     fronterasX[7] = 224  fronterasY[7] = 146
  4657.     fronterasX[8] = 227  fronterasY[8] = 147
  4658.     fronterasX[9] = 223  fronterasY[9] = 146
  4659.     fronterasX[10] = 218  fronterasY[10] = 146
  4660.     fronterasX[11] = 215  fronterasY[11] = 147
  4661.     fronterasX[12] = 212  fronterasY[12] = 147
  4662.     fronterasX[13] = 210  fronterasY[13] = 145
  4663.     fronterasX[14] = 212  fronterasY[14] = 145
  4664.     SF_drawPoliLine(fronterasX, fronterasY, 14, color_fronteras)
  4665.  
  4666.     local px, py = SF_LatLonToXY(sgp_location.lat, sgp_location.lon)
  4667.     draw.line(px-3,py, px+3, py, sf_palette[1])
  4668.     draw.line(px, py-3, px, py+3, sf_palette[1])
  4669. end
  4670.  
  4671. function SF_drawSatMap( num_sat )      
  4672.     if string.sub(sgp_sats[num_sat].nombre,1,7) == "IRIDIUM" then
  4673.         if sf_vision_nocturna then
  4674.             if sgp_sats[num_sat].visible then
  4675.                 SF_drawSuperCrossMap(sgp_sats[num_sat].latitud,sgp_sats[num_sat].longitud, sf_color_white )
  4676.             elseif sgp_sats[num_sat].eclipsado then
  4677.                 SF_drawSuperCrossMap(sgp_sats[num_sat].latitud,sgp_sats[num_sat].longitud, sf_palette[12] )
  4678.             else
  4679.                 SF_drawSuperCrossMap(sgp_sats[num_sat].latitud,sgp_sats[num_sat].longitud, sf_palette[4] )
  4680.             end
  4681.         else
  4682.             if sgp_sats[num_sat].visible then
  4683.                 SF_drawSuperCrossMap(sgp_sats[num_sat].latitud,sgp_sats[num_sat].longitud, sf_color_white )
  4684.             elseif sgp_sats[num_sat].eclipsado then
  4685.                 SF_drawSuperCrossMap(sgp_sats[num_sat].latitud,sgp_sats[num_sat].longitud, sf_palette[12] )
  4686.             else
  4687.                 SF_drawSuperCrossMap(sgp_sats[num_sat].latitud,sgp_sats[num_sat].longitud, sf_palette[4] )
  4688.             end
  4689.         end
  4690.     else
  4691.         if sf_vision_nocturna then
  4692.             if sgp_sats[num_sat].visible then
  4693.                 SF_drawSuperPointMap(sgp_sats[num_sat].latitud,sgp_sats[num_sat].longitud, sf_color_white )
  4694.             elseif sgp_sats[num_sat].eclipsado then
  4695.                 SF_drawSuperPointMap(sgp_sats[num_sat].latitud,sgp_sats[num_sat].longitud, sf_palette[12] )
  4696.             else
  4697.                 SF_drawSuperPointMap(sgp_sats[num_sat].latitud,sgp_sats[num_sat].longitud, sf_palette[1] )
  4698.             end
  4699.         else
  4700.             if sgp_sats[num_sat].visible then
  4701.                 SF_drawSuperPointMap(sgp_sats[num_sat].latitud,sgp_sats[num_sat].longitud, sf_color_white )
  4702.             elseif sgp_sats[num_sat].eclipsado then
  4703.                 SF_drawSuperPointMap(sgp_sats[num_sat].latitud,sgp_sats[num_sat].longitud, sf_palette[12] )
  4704.             else
  4705.                 SF_drawSuperPointMap(sgp_sats[num_sat].latitud,sgp_sats[num_sat].longitud, sf_palette[1] )
  4706.             end
  4707.         end
  4708.     end
  4709.     if num_sat == sf_selected_sat then
  4710.         SF_drawSatPath (num_sat, 30)
  4711.         SF_drawTextMap(sgp_sats[num_sat].latitud,sgp_sats[num_sat].longitud, sgp_sats[num_sat].nombre, sf_palette[1] ) 
  4712.     end
  4713. end
  4714.  
  4715. function SF_drawSatPath (num_sat, minutos)
  4716.     if sgp_sat_number <= 0 then
  4717.         return
  4718.     end
  4719.    
  4720.     SGP_getCurrentTime()
  4721.     local jtime = sgp_time.ujtime
  4722.     local i
  4723.     local lat_ant, lon_ant
  4724.     SGP_calculateSun (jtime)
  4725.     SGP_calculateSatellite (num_sat , jtime)
  4726.     lat_ant = sgp_sats[num_sat].latitud
  4727.     lon_ant = sgp_sats[num_sat].longitud
  4728.     for i = 1,minutos do
  4729.         SGP_calculateSun (jtime + const_julian_minute * i)
  4730.         SGP_calculateSatellite (num_sat , jtime + const_julian_minute * i)
  4731.         if math.abs (lon_ant-sgp_sats[num_sat].longitud) < 20 then
  4732.             if sf_vision_nocturna then
  4733.                 if sgp_sats[num_sat].visible then
  4734.                     SF_drawLineMap(lat_ant, lon_ant, sgp_sats[num_sat].latitud,sgp_sats[num_sat].longitud, sf_color_white )
  4735.                 elseif sgp_sats[num_sat].eclipsado then
  4736.                     SF_drawLineMap(lat_ant, lon_ant, sgp_sats[num_sat].latitud,sgp_sats[num_sat].longitud, sf_palette[12] )
  4737.                 else
  4738.                     SF_drawLineMap(lat_ant, lon_ant, sgp_sats[num_sat].latitud,sgp_sats[num_sat].longitud, sf_palette[4] )
  4739.                 end
  4740.             else
  4741.                 if sgp_sats[num_sat].visible then
  4742.                     SF_drawLineMap(lat_ant, lon_ant, sgp_sats[num_sat].latitud,sgp_sats[num_sat].longitud, sf_color_white )
  4743.                 elseif sgp_sats[num_sat].eclipsado then
  4744.                     SF_drawLineMap(lat_ant, lon_ant, sgp_sats[num_sat].latitud,sgp_sats[num_sat].longitud, sf_palette[12] )
  4745.                 else
  4746.                     SF_drawLineMap(lat_ant, lon_ant, sgp_sats[num_sat].latitud,sgp_sats[num_sat].longitud, sf_palette[4] )
  4747.                 end        
  4748.             end
  4749.         end
  4750.         lat_ant = sgp_sats[num_sat].latitud
  4751.         lon_ant = sgp_sats[num_sat].longitud
  4752.     end
  4753.    
  4754.     SGP_calculateSun (jtime-const_julian_minute)
  4755.     SGP_calculateSatellite (num_sat , jtime-const_julian_minute)
  4756.     SGP_calculateSun (jtime)
  4757.     SGP_calculateSatellite (num_sat , jtime)
  4758. end
  4759.  
  4760.  
  4761.  
  4762.  
  4763. function SF_drawPoliLine (x, y, points, color)
  4764.     local i
  4765.     for i = 1,points-1 do  
  4766.         draw.line( x[i]*480/720, y[i]*272/360, x[i+1]*480/720, y[i+1]*272/360, color)  
  4767.     end
  4768. end
  4769.  
  4770. function SF_erase(x, y, caracteres)
  4771.     draw.fillrect(x, y, 8*caracteres, 8, sf_color_black)
  4772. end
  4773.  
  4774. function SF_initTails()
  4775.     -- Estelas satelites
  4776.     sf_sat_tails = {}
  4777.     local i
  4778.     for i = 1, sgp_sat_number do       
  4779.         sf_sat_tails[i] = {}
  4780.         sf_sat_tails[i].step_current = 0
  4781.         sf_sat_tails[i].marca_tiempo = 0.0
  4782.         for j = 1, sf_palette_len do   
  4783.             sf_sat_tails[i][j] = {}
  4784.             sf_sat_tails[i][j].azimut = 0
  4785.             sf_sat_tails[i][j].elevacion = -1
  4786.             sf_sat_tails[i][j].eclipsado = false
  4787.         end
  4788.         SF_initTail(i)
  4789.     end
  4790. end
  4791.  
  4792. function SF_changePalette(red, green, blue)
  4793.     local i
  4794.     for i = 1 , sf_palette_len do
  4795.         sf_palette[i] = color.new(red * (1-((i-1)/sf_palette_len)), green * (1-((i-1)/sf_palette_len)), blue * (1-((i-1)/sf_palette_len)))
  4796.     end
  4797. end
  4798.  
  4799. function SF_initTail(sat)
  4800.     SGP_getCurrentTime()
  4801.     jtime = sgp_time.ujtime
  4802.  
  4803.     SGP_calculateSun (jtime)
  4804.     SGP_calculateSatellite (sat , jtime)
  4805.    
  4806.     if sgp_sats[sat].elevacion < sf_sky.elevacion_minima then
  4807.         return
  4808.     end
  4809.    
  4810.     local step = const_julian_minute / sf_tail_length
  4811.     local jt = jtime
  4812.     local j
  4813.     for j = 1, sf_tail_length do
  4814.         SGP_calculateSun (jt)
  4815.         SGP_calculateSatellite (sat , jt)
  4816.  
  4817.         sf_sat_tails[sat][j].azimut = sgp_sats[sat].azimut
  4818.         sf_sat_tails[sat][j].elevacion = sgp_sats[sat].elevacion
  4819.         sf_sat_tails[sat][j].eclipsado = sgp_sats[sat].eclipsado
  4820.         jt = jt - step
  4821.     end
  4822.  
  4823.     SGP_calculateSun (jtime-const_julian_minute)
  4824.     SGP_calculateSatellite (sat , jtime-const_julian_minute)   
  4825.     SGP_calculateSun (jtime)
  4826.     SGP_calculateSatellite (sat , jtime)
  4827.     sf_sat_tails[sat].marca_tiempo = jtime 
  4828.  
  4829. end
  4830.  
  4831. function SF_redrawTails()
  4832.     local i
  4833.     for i = 1, sgp_sat_number do
  4834.         SF_initTail(i) 
  4835.     end
  4836. end
  4837.  
  4838.  
  4839. function SF_resetTails()
  4840.     local i
  4841.     for i = 1, sgp_sat_number do       
  4842.         SF_resetTail(i)
  4843.     end
  4844. end
  4845.  
  4846. function SF_resetTail(sat)
  4847.     if sf_sat_tails[sat][1].elevacion == -1 then
  4848.         return
  4849.     end
  4850.    
  4851.     sf_sat_tails[sat].step_current = 0
  4852.     sf_sat_tails[sat].marca_tiempo = 0.0
  4853.     local j
  4854.     for j = 1, sf_tail_length do   
  4855.         sf_sat_tails[sat][j].azimut = 0
  4856.         sf_sat_tails[sat][j].elevacion = -1
  4857.         sf_sat_tails[sat][j].eclipsado = false     
  4858.     end
  4859. end
  4860.  
  4861. function SF_AddPointToTail( num_sat, azimut, elevacion, eclipsado)
  4862.     local i = sf_tail_length - 1;
  4863.    
  4864.     if sgp_time.ujtime - sf_sat_tails[num_sat].marca_tiempo > const_julian_minute then
  4865.         screen.print(140,135,"Actualizando satelites...",sf_palette[8])
  4866.         screen.waitvblankstart()
  4867.         screen.flip()
  4868.         screen.print(140,135,"Actualizando satelites...",sf_palette[8])
  4869.         screen.waitvblankstart()
  4870.         screen.flip()
  4871.         SF_redrawTails()
  4872.     else
  4873.         while i > 0 do
  4874.             sf_sat_tails[num_sat][i+1].azimut = sf_sat_tails[num_sat][i].azimut
  4875.             sf_sat_tails[num_sat][i+1].elevacion = sf_sat_tails[num_sat][i].elevacion
  4876.             sf_sat_tails[num_sat][i+1].eclipsado = sf_sat_tails[num_sat][i].eclipsado      
  4877.             i = i - 1
  4878.         end
  4879.         sf_sat_tails[num_sat][1].azimut = azimut
  4880.         sf_sat_tails[num_sat][1].elevacion = elevacion
  4881.         sf_sat_tails[num_sat][1].eclipsado = eclipsado
  4882.         sf_sat_tails[num_sat].marca_tiempo = sgp_time.ujtime
  4883.     end
  4884. end
  4885.  
  4886. function SND_azimut(azimut, elevacion)
  4887.     if elevacion > 80 then
  4888.         SND_wait("UP.wav",azimut, elevacion)
  4889.     else
  4890.         if ((0 <= azimut) and (azimut < 22.5 )) then  
  4891.             SND_wait("N.wav",azimut, elevacion)
  4892.         elseif ((22.5 <= azimut) and (azimut < 67.5)) then  
  4893.             SND_wait("NE.wav",azimut, elevacion)
  4894.         elseif ((67.5 <= azimut) and (azimut < 112.5)) then
  4895.             SND_wait("E.wav",azimut, elevacion)
  4896.         elseif ((112.5 <= azimut) and (azimut < 157.5)) then
  4897.             SND_wait("SE.wav",azimut, elevacion)
  4898.         elseif ((157.5 <= azimut) and (azimut < 202.5)) then
  4899.             SND_wait("S.wav",azimut, elevacion)
  4900.         elseif ((202.5 <= azimut) and (azimut < 247.5)) then
  4901.             SND_wait("SW.wav",azimut, elevacion)
  4902.         elseif ((247.5 <= azimut) and (azimut < 292.5)) then
  4903.             SND_wait("W.wav",azimut, elevacion)
  4904.         elseif ((292.5 <= azimut) and (azimut < 337.5)) then
  4905.             SND_wait("NW.wav",azimut, elevacion)
  4906.         elseif ((337.5 <= azimut) and (azimut < 360)) then
  4907.             SND_wait("N.wav",azimut, elevacion)
  4908.         end
  4909.     end
  4910. end
  4911.  
  4912. function SF_azimutToString(azimut)
  4913.     if ((0 <= azimut) and (azimut < 22.5 )) then  
  4914.         return "N "
  4915.     elseif ((22.5 <= azimut) and (azimut < 67.5)) then  
  4916.         return "NE"
  4917.     elseif ((67.5 <= azimut) and (azimut < 112.5)) then
  4918.         return "E "
  4919.     elseif ((112.5 <= azimut) and (azimut < 157.5)) then
  4920.         return "SE"
  4921.     elseif ((157.5 <= azimut) and (azimut < 202.5)) then
  4922.         return "S "
  4923.     elseif ((202.5 <= azimut) and (azimut < 247.5)) then
  4924.         return "SW"
  4925.     elseif ((247.5 <= azimut) and (azimut < 292.5)) then
  4926.         return "W "
  4927.     elseif ((292.5 <= azimut) and (azimut < 337.5)) then
  4928.         return "NW"
  4929.     elseif ((337.5 <= azimut) and (azimut < 360)) then
  4930.         return "N "
  4931.     end
  4932. end
  4933.  
  4934. function SND_wait( wave_file, azimut, elevacion)
  4935.     local snd = sound.load (wave_file,false)
  4936.     local voice = snd:play()
  4937.     voice:volume(255)
  4938.     local timer = timer.new()  
  4939.  
  4940.     timer:reset(0)
  4941.     timer:start()
  4942.  
  4943.     if azimut ~= nil and elevacion ~= nil then
  4944.         SF_drawCrossSky(azimut, elevacion)
  4945.     end
  4946.     while timer:time() < 200 or voice:playing() do
  4947.         screen.waitvblankstart(5)
  4948.         screen.flip()
  4949.     end
  4950.     timer:stop()
  4951.  
  4952. end
  4953.  
  4954. function SND_playSoundsIfNeeded()
  4955.     if sf_primer_calculo then
  4956.         sf_elevacion_max_ant = sf_elevacion_max
  4957.         sf_noche_ant = sgp_location.noche
  4958.         sf_sun_elev_ant = sgp_sun.ele
  4959.         sf_moon_elev_ant = sgp_moon.ele
  4960.     else
  4961.         if sf_sun_elev_ant < -3 and sgp_sun.ele > -3 then
  4962.             SND_wait("sunrise.wav", nil, nil)
  4963.         end
  4964.         if sf_sun_elev_ant > 3 and sgp_sun.ele < 3 then
  4965.             SND_wait("sunset.wav", nil, nil)
  4966.         end
  4967.         if sf_sun_elev_ant < 0 and sgp_sun.ele > 0 then
  4968.             SND_wait("sunrise.wav", nil, nil)
  4969.             SGP_calculateSunRaiseTime()
  4970.             SGP_calculateSunSetTime()
  4971.         end
  4972.         if sf_sun_elev_ant > 0 and sgp_sun.ele < 0 then
  4973.             SND_wait("sunset.wav", nil, nil)
  4974.             SGP_calculateSunRaiseTime()
  4975.             SGP_calculateSunSetTime()
  4976.         end
  4977.         if sf_moon_elev_ant < -3 and sgp_moon.ele > -3 then
  4978.             SND_wait("moonrise.wav", nil, nil)
  4979.         end
  4980.         if sf_moon_elev_ant > 3 and sgp_moon.ele < 3 then
  4981.             SND_wait("moonset.wav", nil, nil)
  4982.         end
  4983.         if sf_moon_elev_ant < 0 and sgp_moon.ele > 0 then
  4984.             SND_wait("moonrise.wav", nil, nil)
  4985.             SGP_calculateMoonRaiseTime()
  4986.             SGP_calculateMoonSetTime()
  4987.         end
  4988.         if sf_moon_elev_ant > 0 and sgp_moon.ele < 0 then
  4989.             SND_wait("moonset.wav", nil, nil)
  4990.             SGP_calculateMoonRaiseTime()
  4991.             SGP_calculateMoonSetTime()
  4992.         end
  4993.         sf_sun_elev_ant = sgp_sun.ele
  4994.         sf_moon_elev_ant = sgp_moon.ele    
  4995.         if sgp_location.noche and not sf_noche_ant then
  4996.             SND_wait("night_start.wav", nil, nil)              
  4997.             SGP_calculateNightBegining()
  4998.             SGP_calculateNightEnding()     
  4999.             sf_vision_nocturna = true
  5000.             SF_changePalette (sf_color_night.r,sf_color_night.g,sf_color_night.b)
  5001.         elseif not sgp_location.noche and sf_noche_ant then
  5002.             SND_wait("night_end.wav", nil, nil)
  5003.             SGP_calculateNightBegining()
  5004.             SGP_calculateNightEnding()
  5005.             sf_vision_nocturna = false
  5006.             SF_changePalette (sf_color_day.r,sf_color_day.g,sf_color_day.b)
  5007.         end
  5008.         sf_noche_ant = sgp_location.noche
  5009.         if sf_elevacion_max_ant < 80 and sf_elevacion_max > 80 then            
  5010.             SND_wait("90.wav", sf_azimut_max, sf_elevacion_max)
  5011.             SND_azimut(sf_azimut_max, sf_elevacion_max)
  5012.         elseif sf_elevacion_max_ant < 60 and sf_elevacion_max > 60 then            
  5013.             SND_wait("60.wav", sf_azimut_max, sf_elevacion_max)
  5014.             SND_azimut(sf_azimut_max, sf_elevacion_max)
  5015.         elseif sf_elevacion_max_ant < 30 and sf_elevacion_max > 30 then            
  5016.             SND_wait("30.wav", sf_azimut_max, sf_elevacion_max)
  5017.             SND_azimut(sf_azimut_max, sf_elevacion_max)
  5018.         elseif sf_elevacion_max_ant < sf_sky.elevacion_minima and sf_elevacion_max > sf_sky.elevacion_minima then          
  5019.             SND_wait("00.wav", sf_azimut_max, sf_elevacion_max)
  5020.             SND_azimut(sf_azimut_max, sf_elevacion_max)
  5021.         end
  5022.         sf_elevacion_max_ant = sf_elevacion_max
  5023.  
  5024.     end
  5025.    
  5026.     if sf_pass_limit > 0 and sf_pass_current > 0 and sf_pass_current <= sf_pass_limit then
  5027.         if sf_current_jtime_ant < sf_passes[sf_pass_current].ini_jt and sf_current_jtime > sf_passes[sf_pass_current].ini_jt then
  5028.             SND_wait("iss_start.wav",sf_passes[sf_pass_current].ini_azimut, sf_passes[sf_pass_current].ini_elevacion)
  5029.             SND_azimut(sf_passes[sf_pass_current].ini_azimut, sf_passes[sf_pass_current].ini_elevacion)
  5030.         elseif sf_current_jtime_ant < sf_passes[sf_pass_current].max_jt and sf_current_jtime > sf_passes[sf_pass_current].max_jt then      
  5031.             SND_wait("iss_max.wav",sf_passes[sf_pass_current].max_azimut, sf_passes[sf_pass_current].max_elevacion)
  5032.             SND_azimut(sf_passes[sf_pass_current].max_azimut, sf_passes[sf_pass_current].max_elevacion)
  5033.         elseif sf_current_jtime_ant < sf_passes[sf_pass_current].fin_jt and sf_current_jtime > sf_passes[sf_pass_current].fin_jt then          
  5034.             SND_wait("iss_end.wav",sf_passes[sf_pass_current].fin_azimut, sf_passes[sf_pass_current].fin_elevacion)
  5035.             SND_azimut(sf_passes[sf_pass_current].fin_azimut, sf_passes[sf_pass_current].fin_elevacion)
  5036.         end        
  5037.     end
  5038. end
  5039.  
  5040. -- AQUI EMPIEZAN LOS CALCULOS SGP
  5041.  
  5042.  
  5043.  
  5044.  
  5045.  
  5046. function SGP_Date_To_Julian(yr, mo, dy, hr, mi, se)
  5047.     return SGP_Julian_Date_of_Year(yr) + SGP_DOY(yr,mo,dy) + SGP_Fraction_of_Day(hr,mi,se)
  5048. end
  5049.  
  5050. function SGP_Julian_To_Date(jt)
  5051.         local day, month, year = SGP_calendarDate(jt,3)
  5052.         local hour, minutes, seconds, hundred_sec = SGP_timeOfDay(jt,3)
  5053.         return year, month, day, hour, minutes, seconds
  5054. end
  5055.  
  5056. function SGP_calendarDate(jd , res)
  5057.     local Z
  5058.     local month, day, year
  5059.     local A,B,C,D,E,F,alpha,factor
  5060.    
  5061.    
  5062.     factor = 0.5/const_secday/math.pow(10.0,res)
  5063.     F = (jd + 0.5) - math.floor(jd + 0.5)
  5064.     if (F + factor >= 1.0) then
  5065.         jd = jd + factor
  5066.         F = 0.0
  5067.     end
  5068.    
  5069.     Z = math.floor(jd + 0.5)
  5070.     if (Z < 2299161) then
  5071.         A = Z
  5072.     else       
  5073.         alpha = math.floor((Z - 1867216.25)/36524.25)
  5074.         A = (Z + 1.0 + alpha) - math.floor(alpha/4.0)
  5075.     end
  5076.    
  5077.     B = A + 1524.0
  5078.     C = math.floor((B - 122.1)/365.25)
  5079.     D = math.floor(365.25 * C)
  5080.     E = math.floor((B - D)/30.6001)
  5081.     day = math.floor(B - D - math.floor(30.6001 * E) + F)
  5082.     if (E < 13.5) then
  5083.         month = math.floor((E - 1.0)+0.5)
  5084.     else
  5085.         month = math.floor((E - 13.0)+0.5)
  5086.     end
  5087.     if (month > 2.5) then
  5088.         year = math.floor((C - 4716.0)+0.5)
  5089.     else
  5090.         year = math.floor((C - 4715.0)+0.5)
  5091.     end
  5092.    
  5093.     return day, month , year
  5094. end
  5095.  
  5096. function SGP_timeOfDay(jd, res)
  5097.     local hr,mn,sec,hu
  5098.     local factor,time_,sc
  5099.  
  5100.     res = math.min(math.max(0.0,res),3.0)
  5101.     factor = math.pow(10.0,res)
  5102.     time_ = ((jd - 0.5)-math.floor(jd - 0.5))*const_secday
  5103.     time_ = math.floor(time_*factor+0.5)/factor
  5104.     hr = math.floor(time_/3600.0)
  5105.     time_ = time_ - (3600.0*hr)
  5106.     if (hr == 24) then
  5107.         hr = 0
  5108.     end
  5109.     mn = math.floor(time_/60.0)
  5110.     sc = time_ - (60.0*mn)
  5111.    
  5112.     hu = math.floor(((sc - math.floor(sc))*1000)+0.5)
  5113.     sec = math.floor(sc)
  5114.  
  5115.     return hr, mn, sec, hu
  5116. end
  5117.  
  5118. -- Calcula la fecha Juliana de Enero del año 0.0
  5119. function SGP_Julian_Date_of_Year(year)
  5120.   local Y = year - 1
  5121.   local A = math.floor(year/100)
  5122.   local B = 2 - A + math.floor(A/4)
  5123.   return math.floor(365.25 * Y) + math.floor(30.6001 * 14) + 1720994.5 + B
  5124. end
  5125.  
  5126. function SGP_Julian_Date_of_Epoch(epoch)
  5127.         local year
  5128.         local day
  5129.        
  5130.         year = math.floor(epoch*0.001)
  5131.         if year < 57 then
  5132.             year = year + 2000
  5133.         else
  5134.             year = year + 1900
  5135.         end
  5136.         day = ((epoch*0.001) - math.floor(epoch*0.001))*1000
  5137.        
  5138.         return  SGP_Julian_Date_of_Year(year) + day
  5139. end
  5140.  
  5141. function SGP_DOY(yr,mo,dy)
  5142.   local days = { 31,28,31,30,31,30,31,31,30,31,30,31 }
  5143.   local day = 0
  5144.   for i = 1,mo-1 do
  5145.     day = day + days[i]
  5146.   end
  5147.   day = day + dy
  5148.   if ((yr % 4) == 0) and (((yr % 100) ~= 0) or ((yr % 400) == 0)) and (mo > 2) then
  5149.     day = day + 1
  5150.   end
  5151.   return day
  5152. end
  5153.  
  5154. function SGP_Fraction_of_Day(hr,mi,se)
  5155.   return (hr + (mi + se/60)/60)/24
  5156. end
  5157.  
  5158.  
  5159.  
  5160. function SGP_isRepeated(s,catalog_num)
  5161.     local i = 1
  5162.     while i < s do
  5163.         if sgp_sats[i].catnr == catalog_num then
  5164.             --Actualizamos el satelite con los nuevos datos
  5165.             sgp_sats[i].catnr = sgp_sats[s].catnr  
  5166.             sgp_sats[i].epoch = sgp_sats[s].epoch      
  5167.             sgp_sats[i].julian_epoch = sgp_sats[s].julian_epoch
  5168.             sgp_sats[i].xndt2o = sgp_sats[s].xndt2o
  5169.             sgp_sats[i].xndd6o = sgp_sats[s].xndd6o
  5170.             sgp_sats[i].iexp = sgp_sats[s].iexp
  5171.             sgp_sats[i].bstar = sgp_sats[s].bstar      
  5172.             sgp_sats[i].ibexp = sgp_sats[s].ibexp  
  5173.             sgp_sats[i].elset = sgp_sats[s].elset      
  5174.            
  5175.             sgp_sats[i].xincl = sgp_sats[s].xincl      
  5176.             sgp_sats[i].xnodeo = sgp_sats[s].xnodeo
  5177.             sgp_sats[i].eo = sgp_sats[s].eo
  5178.             sgp_sats[i].omegao = sgp_sats[s].omegao
  5179.             sgp_sats[i].xmo = sgp_sats[s].xmo  
  5180.             sgp_sats[i].xno = sgp_sats[s].xno  
  5181.        
  5182.             sgp_sats[i].xndd6o = sgp_sats[s].xndd6o
  5183.             sgp_sats[i].bstar = sgp_sats[s].bstar
  5184.             sgp_sats[i].xnodeo = sgp_sats[s].xnodeo
  5185.             sgp_sats[i].omegao = sgp_sats[s].omegao
  5186.             sgp_sats[i].xmo = sgp_sats[s].xmo
  5187.             sgp_sats[i].xincl = sgp_sats[s].xincl
  5188.             sgp_sats[i].xno = sgp_sats[s].xno
  5189.             sgp_sats[i].xndt2o = sgp_sats[s].xndt2o
  5190.             sgp_sats[i].xndd6o = sgp_sats[s].xndd6o
  5191.            
  5192.             --Inicializa algunas variables
  5193.             sgp_sats[i].iflag = sgp_sats[s].iflag
  5194.             sgp_sats[i].primera_vez = sgp_sats[s].primera_vez
  5195.             sgp_sats[i].elevacion_ant = sgp_sats[s].elevacion_ant
  5196.             sgp_sats[i].no_calcular = sgp_sats[s].no_calcular
  5197.  
  5198.             return true
  5199.         else
  5200.             i = i + 1
  5201.         end
  5202.     end
  5203.  
  5204.     return false
  5205. end
  5206.  
  5207. function SGP_getLocationFromFile(filename) 
  5208.     -- Miramos primero que el fichero exista
  5209.     local file = io.open(filename)
  5210.     if file then
  5211.         io.close(file)
  5212.     else
  5213.         return false
  5214.     end
  5215.  
  5216.  
  5217.  
  5218.     for line in io.lines(filename) do  
  5219.         local str = string.sub(line,1,3)
  5220.         if str == "lat" then
  5221.             sgp_location.lat = tonumber(string.sub(line,5,string.len(line)))       
  5222.         elseif str == "lon" then
  5223.             sgp_location.lon = tonumber(string.sub(line,5,string.len(line)))
  5224.         elseif str == "alt" then
  5225.             sgp_location.alt = tonumber(string.sub(line,5,string.len(line)))
  5226.         elseif str == "off" then
  5227.             sgp_location.UTC_offset = tonumber(string.sub(line,5,string.len(line)))
  5228.         elseif str == "nam" then
  5229.             sgp_location.name = string.sub(line,5,string.len(line))
  5230.         elseif str == "dst" then
  5231.             if ( string.sub(line,5,8) == "true" ) then
  5232.                 sgp_location.dst = true
  5233.             else
  5234.                 sgp_location.dst = false
  5235.             end
  5236.         end
  5237.     end
  5238.    
  5239.     sgp_location.polar.lat = math.rad(sgp_location.lat)
  5240.     sgp_location.polar.lon = math.rad(SGP_modulus(sgp_location.lon,360))
  5241.     sgp_location.polar.alt = sgp_location.alt * 0.001
  5242.     return true
  5243.    
  5244. end
  5245.  
  5246. function SGP_getStars(filename, mag)   
  5247.     -- Miramos primero que el fichero exista
  5248.     local file = io.open(filename)
  5249.     if not file then
  5250.         return false
  5251.     end
  5252.  
  5253.     sgp_stars = {}
  5254.     sgp_stars2 = {}
  5255.     sgp_constellations = {}
  5256.     local i = 1
  5257.     local j = 1
  5258.     local sum_ra = 0
  5259.     local sum_dec = 0
  5260.     local line = file:read("*l")
  5261.     local primera_estrella = true
  5262.     while line do
  5263.         local str = string.sub(line,1,3)
  5264.         if tonumber(str) ~= nil then
  5265.             sgp_stars[i] = {}
  5266.             sgp_stars[i].id = tonumber(string.sub(line,1,4))
  5267.             sgp_stars[i].ra = tonumber(string.sub(line,6,13))*15
  5268.             sgp_stars[i].dec = tonumber(string.sub(line,15,23))
  5269.             sgp_stars[i].mag = tonumber(string.sub(line,25,29))
  5270.             sgp_stars[i].no_calcular = 0
  5271.             if sgp_stars[i].mag < mag then         
  5272.                 if primera_estrella then
  5273.                     primera_estrella = false
  5274.                 else
  5275.                     local media = (sum_ra / (i - sgp_constellations[j-1].primero + 1))
  5276.                     if math.abs(media - sgp_stars[i].ra) > 250 then
  5277.                         if media > sgp_stars[i].ra then
  5278.                             sgp_stars[i].ra = sgp_stars[i].ra + 360.0
  5279.                         else
  5280.                             sgp_stars[i].ra = sgp_stars[i].ra - 360.0
  5281.                         end
  5282.                     end
  5283.                
  5284.                 end
  5285.                
  5286.                 sum_ra = sum_ra + sgp_stars[i].ra
  5287.                 sum_dec = sum_dec + sgp_stars[i].dec + 90.0
  5288.            
  5289.                 i = i + 1
  5290.             end
  5291.         else
  5292.             if j > 1 then
  5293.                 sgp_constellations[j-1].ultimo = i-1
  5294.                 sgp_constellations[j-1].label_ra = (sum_ra / (sgp_constellations[j-1].ultimo - sgp_constellations[j-1].primero+1))
  5295.                 sgp_constellations[j-1].label_dec = (sum_dec / (sgp_constellations[j-1].ultimo - sgp_constellations[j-1].primero+1)) - 90.0
  5296.                 sum_ra = 0
  5297.                 sum_dec = 0
  5298.                 primera_estrella = true
  5299.             end
  5300.             sgp_constellations[j] = {}
  5301.             sgp_constellations[j].nombre = string.sub(line,1,string.len(line)-1)
  5302.             sgp_constellations[j].no_calcular = 0
  5303.             sgp_constellations[j].primero = i
  5304.             j = j + 1
  5305.         end
  5306.         line = file:read("*l")
  5307.     end
  5308.     file:close()
  5309.     sgp_star_number = i - 1
  5310.     sgp_constellation_number = j-1
  5311.     sgp_constellations[sgp_constellation_number].ultimo = sgp_star_number
  5312.     sgp_constellations[sgp_constellation_number].label_ra = sum_ra / (sgp_constellations[sgp_constellation_number].ultimo - sgp_constellations[sgp_constellation_number].primero + 1)
  5313.     sgp_constellations[sgp_constellation_number].label_dec = sum_dec / (sgp_constellations[sgp_constellation_number].ultimo - sgp_constellations[sgp_constellation_number].primero + 1)
  5314.    
  5315.     return true
  5316. end
  5317.  
  5318.    
  5319.  
  5320. function SGP_getTLE(filename)  
  5321.     if sgp_sat_number < 0 then
  5322.         return
  5323.     end
  5324.    
  5325.     -- Miramos primero que el fichero exista
  5326.     local file = io.open(filename)
  5327.     if file then
  5328.         io.close(file)
  5329.     else
  5330.         sgp_sat_number = -2
  5331.         return
  5332.     end
  5333.    
  5334.     local sat_number = sgp_sat_number + 1
  5335.     local line_number = 1
  5336.     for line in io.lines(filename) do  
  5337.         if line_number % 3 == 1 then
  5338.             -- Obtiene nombre y guarda los numeros de la estacion espacial y el shuttle
  5339.             --SL-16 R/B
  5340.  
  5341.             sgp_sats[sat_number] = { }
  5342.             sgp_sats[sat_number].numero = sat_number
  5343.             sgp_sats[sat_number].nombre = string.sub(line,1,24)
  5344.  
  5345.         elseif line_number % 3 == 2 then
  5346.             -- Obtiene primera linea              
  5347.             --1 23405U 94077B   07216.52870987 -.00000029  00000-0  10000-4 0  7954
  5348.             if string.sub(line,1,2) == "1 " then
  5349.                 if line:len() >= 69 then
  5350.                     sgp_sats[sat_number].catnr = string.sub(line,3,7)  
  5351.                     sgp_sats[sat_number].epoch = tonumber(string.sub(line,19,32))      
  5352.                     sgp_sats[sat_number].julian_epoch = SGP_Julian_Date_of_Epoch(sgp_sats[sat_number].epoch)
  5353.                     sgp_sats[sat_number].xndt2o = tonumber(string.sub(line,34,43)) 
  5354.                     sgp_sats[sat_number].xndd6o = tonumber(string.sub(line,45,50)) * 0.00001   
  5355.                     sgp_sats[sat_number].iexp = tonumber(string.sub(line,51,52))   
  5356.                     sgp_sats[sat_number].bstar = tonumber(string.sub(line,54,59)) * 0.00001    
  5357.                     sgp_sats[sat_number].ibexp = tonumber(string.sub(line,60,61))  
  5358.                     sgp_sats[sat_number].elset = string.sub(line,66,69)    
  5359.                 else
  5360.                     sgp_sat_number = -1
  5361.                     return         
  5362.                 end
  5363.             else
  5364.                 -- Suponer que se trata de la linea de nombre
  5365.  
  5366.                 sgp_sats[sat_number] = { }
  5367.                 sgp_sats[sat_number].numero = sat_number
  5368.                 sgp_sats[sat_number].nombre = string.sub(line,1,24)
  5369.                 line_number = line_number - 1
  5370.        
  5371.             end
  5372.         elseif line_number % 3 == 0 then
  5373.             -- Obtiene seguntda linea
  5374.             --2 23405  70.9690 232.0909 0004094 343.0355  17.0620 14.14891027655607
  5375.  
  5376.             if string.sub(line,1,2) == "2 " then
  5377.                 if line:len() >= 69 then
  5378.                     sgp_sats[sat_number].xincl = tonumber(string.sub(line,9,16))       
  5379.                     sgp_sats[sat_number].xnodeo = tonumber(string.sub(line,18,25)) 
  5380.                     sgp_sats[sat_number].eo = tonumber(string.sub(line,27,33)) * 0.0000001 
  5381.                     sgp_sats[sat_number].omegao = tonumber(string.sub(line,35,40)) 
  5382.                     sgp_sats[sat_number].xmo = tonumber(string.sub(line,44,51))
  5383.                     sgp_sats[sat_number].xno = tonumber(string.sub(line,53,63))
  5384.  
  5385.                    
  5386.                     sgp_sats[sat_number].xndd6o = sgp_sats[sat_number].xndd6o * math.pow(10.0,sgp_sats[sat_number].iexp)
  5387.                     sgp_sats[sat_number].bstar = (sgp_sats[sat_number].bstar * math.pow(10.0,sgp_sats[sat_number].ibexp)) / const_ae
  5388.                     sgp_sats[sat_number].xnodeo = math.rad(sgp_sats[sat_number].xnodeo)
  5389.                     sgp_sats[sat_number].omegao = math.rad(sgp_sats[sat_number].omegao)
  5390.                     sgp_sats[sat_number].xmo = math.rad(sgp_sats[sat_number].xmo)
  5391.                     sgp_sats[sat_number].xincl = math.rad(sgp_sats[sat_number].xincl)
  5392.                     sgp_sats[sat_number].xno = (sgp_sats[sat_number].xno * (math.pi*2.0))/const_xmnpda
  5393.                     sgp_sats[sat_number].xndt2o = (sgp_sats[sat_number].xndt2o * (math.pi*2.0))/const_xmnpda^2
  5394.                     sgp_sats[sat_number].xndd6o = (sgp_sats[sat_number].xndd6o * (math.pi*2.0))/const_xmnpda^3
  5395.                    
  5396.                     --Inicializa algunas variables
  5397.                     sgp_sats[sat_number].iflag = true
  5398.                     sgp_sats[sat_number].primera_vez = true
  5399.                     sgp_sats[sat_number].elevacion_ant = 0
  5400.                     sgp_sats[sat_number].no_calcular = 0
  5401.                    
  5402.                     if not SGP_isRepeated(sat_number, sgp_sats[sat_number].catnr) then         
  5403.                         local cadena = string.sub(sgp_sats[sat_number].nombre,1,3)
  5404.                         if cadena == "ISS" then
  5405.                             sf_iss = sat_number
  5406.                             sf_selected_sat = sf_iss
  5407.                         end
  5408.                         sat_number = sat_number + 1
  5409.                     end
  5410.                 else
  5411.                     sgp_sat_number = -1
  5412.                     return         
  5413.                 end
  5414.             else
  5415.                 -- Suponer que se trata de la linea de nombre
  5416.  
  5417.                 sgp_sats[sat_number] = { }
  5418.                 sgp_sats[sat_number].numero = sat_number
  5419.                 sgp_sats[sat_number].nombre = string.sub(line,1,24)
  5420.                 line_number = line_number - 2  
  5421.  
  5422.             end
  5423.         end
  5424.    
  5425.         line_number = line_number + 1
  5426.     end
  5427.     sgp_sat_number = sat_number - 1
  5428. end
  5429.  
  5430. function SGP_modulus( d1, d2)
  5431.     local modu
  5432.    
  5433.     modu = d1 - (math.floor(d1/d2) * d2)
  5434.     if modu >= 0 then
  5435.         return modu
  5436.     else
  5437.         return modu + d2
  5438.     end
  5439. end
  5440.  
  5441.  
  5442. function SGP_fmod2p( d )
  5443.     return SGP_modulus(d,(math.pi * 2.0))
  5444. end
  5445.  
  5446.  
  5447. function SGP_acTan(sinx,cosx)
  5448.     if cosx == 0 then
  5449.         if sinx > 0 then
  5450.             return (math.pi / 2.0)
  5451.         else
  5452.             return (3.0 * math.pi / 2.0)
  5453.         end
  5454.     else
  5455.         if cosx > 0 then
  5456.             return math.atan( sinx / cosx )
  5457.         else
  5458.             return math.pi + math.atan( sinx / cosx )
  5459.         end
  5460.     end
  5461. end
  5462.  
  5463.  
  5464.  
  5465. function SGP_deltaET (year)
  5466.     return (26.465 + 0.747622 * (year - 1950) + 1.886913 * math.sin((math.pi * 2.0) * (year - 1975.0) / 33.0))
  5467. end
  5468.  
  5469. function SGP_thetaG_JD( jd )
  5470.  
  5471.     local  UT,TU,GMST
  5472.    
  5473.     UT = (jd + 0.5) - math.floor(jd + 0.5)
  5474.     jd = jd - UT
  5475.     TU = (jd - 2451545.0)/36525
  5476.     GMST = 24110.54841 + TU * (8640184.812866 + TU * (0.093104 - TU * 6.2 * 0.000001))
  5477.     GMST = SGP_modulus(GMST + const_secday * const_omega_E * UT,const_secday)
  5478.     return ((math.pi*2.0) * GMST/const_secday)
  5479. end
  5480.    
  5481.  
  5482.  
  5483. function SGP_magnitud( x,y,z )
  5484.         return math.sqrt(x^2 + y^2 + z^2)
  5485. end
  5486.  
  5487. function SGP_subVectores(v1, v2)
  5488.     local v3 = { } 
  5489.     v3.x = v1.x - v2.x
  5490.     v3.y = v1.y - v2.y
  5491.     v3.z = v1.z - v2.z
  5492.     v3.m = SGP_magnitud(v3.x,v3.y,v3.z)
  5493.     return v3
  5494. end
  5495.  
  5496. -- Calcula el angulo comprendido entre los vectores v1 y v2
  5497. function SGP_angulo(v1, v2)
  5498.     v1.m = SGP_magnitud(v1.x,v1.y,v1.z)
  5499.     v2.m = SGP_magnitud(v2.x,v2.y,v2.z)
  5500.     return math.acos(SGP_dot(v1,v2)/(v1.m * v2.m))
  5501. end
  5502.  
  5503. function SGP_dot(v1, v2)   
  5504.     return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z
  5505. end
  5506.  
  5507.  
  5508. function SGP_calculateSatPosVel( sat , jtime )
  5509.     local i
  5510.     local cosuk,sinuk,rfdotk,vx,vy,vz,ux,uy,uz,xmy,xmx
  5511.     local cosnok,sinnok,cosik,sinik,rdotk,xinck,xnodek,uk,rk
  5512.     local cos2u,sin2u,u,sinu,cosu,betal,rfdot,rdot,r,pl,elsq
  5513.     local esine,ecose,epw,temp6,temp5,temp4,temp3,cosepw,sinepw
  5514.     local capu,ayn,xlt,aynl,xll,axn,xn,beta,xl,e,a,tfour,temp2
  5515.     local tcube,delm,delomg,templ,tempe,tempa,xnode,tsq,xmp,temp
  5516.     local omega,xnoddf,omgadf,xmdf,x,y,z,xdot,ydot,zdot,temp1
  5517.    
  5518.     local tsince = (jtime - sat.julian_epoch) * const_xmnpda
  5519.    
  5520.     -- Recover original mean motion (xnodp) and semimajor axis (aodp)
  5521.     -- from input elements.
  5522.     if sat.iflag then
  5523.         -- Initialization
  5524.         sat.a1 = math.pow(const_xke/sat.xno,const_tothrd)
  5525.         sat.cosio = math.cos(sat.xincl)
  5526.         sat.theta2 = sat.cosio^2
  5527.         sat.x3thm1 = (3.0 * sat.theta2) - 1.0
  5528.         sat.eosq = sat.eo^2
  5529.         sat.betao2 = 1.0 - sat.eosq
  5530.         sat.betao = math.sqrt(sat.betao2)
  5531.         sat.del1 = 1.5 * const_ck2 * sat.x3thm1 / (sat.a1 * sat.a1 * sat.betao * sat.betao2)
  5532.         sat.ao = sat.a1 * (1.0 - sat.del1 * (0.5 * const_tothrd + sat.del1 * (1.0 + 134.0 / 81.0 * sat.del1)))
  5533.         sat.delo = 1.5 * const_ck2 * sat.x3thm1 / (sat.ao * sat.ao * sat.betao * sat.betao2)
  5534.         sat.xnodp = sat.xno / (1.0 + sat.delo)
  5535.         sat.aodp = sat.ao / (1.0 - sat.delo)
  5536.  
  5537.         -- For perigee less than 220 kilometers, the isimp flag is set and
  5538.         -- the equations are truncated to linear variation in sqrt a and
  5539.         -- quadratic variation in mean anomaly.  Also, the c3 term, the
  5540.         -- delta omega term, and the delta m term are dropped.
  5541.  
  5542.         sat.isimp = 0
  5543.         if ((sat.aodp*(1.0 - sat.eo)/const_ae) < (220.0/const_xkmper + const_ae)) then
  5544.             sat.isimp = 1
  5545.         end
  5546.         -- For perigee below 156 km, the values of s and qoms2t are altered.
  5547.            
  5548.         sat.s4 = const_s
  5549.         sat.qoms24 = const_qoms2t
  5550.         sat.perige = (sat.aodp * (1.0 - sat.eo) - const_ae)*const_xkmper
  5551.         if sat.perige < 156.0 then
  5552.             sat.s4 = sat.perige - 78.0
  5553.             if sat.perige <= 98.0 then
  5554.                 sat.s4 = 20.0
  5555.             end
  5556.             sat.qoms24 = math.pow((120.0 - sat.s4) * const_ae / const_xkmper , 4.0)
  5557.             sat.s4 = sat.s4 / const_xkmper + const_ae
  5558.         end
  5559.  
  5560.         sat.pinvsq = 1.0 / (sat.aodp * sat.aodp * sat.betao2 * sat.betao2)
  5561.         sat.tsi = 1.0/(sat.aodp - sat.s4)
  5562.         sat.eta = sat.aodp * sat.eo * sat.tsi
  5563.         sat.etasq = sat.eta * sat.eta
  5564.         sat.eeta = sat.eo * sat.eta
  5565.         sat.psisq = math.abs(1.0 - sat.etasq)
  5566.         sat.coef = sat.qoms24 * math.pow(sat.tsi,4.0)
  5567.         sat.coef1 = sat.coef / math.pow(sat.psisq,3.5)
  5568.         sat.c2 = sat.coef1 * sat.xnodp * (sat.aodp * (1.0 + 1.5 * sat.etasq + sat.eeta * (4.0 + sat.etasq))
  5569.             + 0.75 * const_ck2 * sat.tsi/sat.psisq * sat.x3thm1 * (8.0 + 3.0 * sat.etasq * (8.0 + sat.etasq)))
  5570.         sat.c1 = sat.bstar * sat.c2
  5571.         sat.sinio = math.sin(sat.xincl)
  5572.         sat.a3ovk2 = (-const_xj3) / const_ck2 * math.pow(const_ae,3.0)
  5573.         sat.c3 = sat.coef * sat.tsi * sat.a3ovk2 * sat.xnodp * const_ae * sat.sinio / sat.eo
  5574.         sat.x1mth2 = 1.0 - sat.theta2
  5575.         sat.c4 = 2.0 * sat.xnodp * sat.coef1 * sat.aodp * sat.betao2 * (sat.eta * (2.0 + 0.5 * sat.etasq)
  5576.             + sat.eo * (0.5 + 2.0 * sat.etasq) - 2.0 * const_ck2 * sat.tsi/(sat.aodp * sat.psisq)
  5577.              * (-3.0 * sat.x3thm1 * (1.0 - 2.0 * sat.eeta + sat.etasq * (1.5 - 0.5 * sat.eeta))
  5578.             + 0.75 * sat.x1mth2 * (2.0 * sat.etasq - sat.eeta * (1.0 + sat.etasq)) * math.cos(2.0 * sat.omegao)))
  5579.         sat.c5 = 2.0 * sat.coef1 * sat.aodp * sat.betao2 * (1.0 + 2.75 * (sat.etasq + sat.eeta) + sat.eeta * sat.etasq)
  5580.         sat.theta4 = sat.theta2 * sat.theta2
  5581.         local temp1 = 3.0 * const_ck2 * sat.pinvsq * sat.xnodp
  5582.         local temp2 = temp1 * const_ck2 * sat.pinvsq
  5583.         local temp3 = 1.25 * const_ck4 * sat.pinvsq * sat.pinvsq * sat.xnodp
  5584.         sat.xmdot = sat.xnodp + 0.5 * temp1 * sat.betao * sat.x3thm1
  5585.             + 0.0625 * temp2 * sat.betao * (13.0 - 78.0 * sat.theta2 + 137.0 * sat.theta4)
  5586.         sat.x1m5th = 1.0 - 5.0 * sat.theta2
  5587.         sat.omgdot = -0.5 * temp1 * sat.x1m5th + 0.0625 * temp2 * (7.0 - 114.0 * sat.theta2 +395.0 * sat.theta4)
  5588.             + temp3 * (3.0 - 36.0 * sat.theta2 + 49.0 * sat.theta4)
  5589.         sat.xhdot1 = -temp1 * sat.cosio
  5590.         sat.xnodot = sat.xhdot1 + (0.5 * temp2 * (4.0 - 19.0 * sat.theta2)
  5591.             + 2.0 * temp3 * (3.0 - 7.0 * sat.theta2)) * sat.cosio
  5592.         sat.omgcof = sat.bstar * sat.c3 * math.cos(sat.omegao)
  5593.         sat.xmcof = -const_tothrd * sat.coef * sat.bstar * const_ae/sat.eeta
  5594.         sat.xnodcf = 3.5 * sat.betao2 * sat.xhdot1 * sat.c1
  5595.         sat.t2cof = 1.5 * sat.c1
  5596.         sat.xlcof = 0.125 * sat.a3ovk2 * sat.sinio * (3.0 + 5.0 * sat.cosio)/(1.0 + sat.cosio)
  5597.         sat.aycof = 0.25 * sat.a3ovk2 * sat.sinio
  5598.         sat.delmo = math.pow(1.0 + sat.eta * math.cos(sat.xmo),3.0)
  5599.         sat.sinmo = math.sin(sat.xmo)
  5600.         sat.x7thm1 = 7.0 * sat.theta2 - 1.0
  5601.         if sat.isimp ~= 1 then
  5602.             sat.c1sq = sat.c1 * sat.c1
  5603.             sat.d2 = 4.0 * sat.aodp * sat.tsi * sat.c1sq
  5604.             local temp = sat.d2 * sat.tsi * sat.c1/3.0
  5605.             sat.d3 = (17.0 * sat.aodp + sat.s4) * temp
  5606.             sat.d4 = 0.5 * temp * sat.aodp * sat.tsi * (221.0 * sat.aodp + 31.0 * sat.s4) * sat.c1
  5607.             sat.t3cof = sat.d2 + 2.0 * sat.c1sq
  5608.             sat.t4cof = 0.25 * (3.0 * sat.d3 + sat.c1 * (12.0 * sat.d2 + 10.0 * sat.c1sq))
  5609.             sat.t5cof = 0.2 * (3.0 * sat.d4 + 12.0 * sat.c1 * sat.d3 + 6.0 * sat.d2 * sat.d2 + 15.0 * sat.c1sq * (2.0 * sat.d2 + sat.c1sq))
  5610.         end
  5611.         sat.iflag = 0
  5612.     end
  5613.     --Update for secular gravity and atmospheric drag.
  5614.  
  5615.     xmdf = sat.xmo + sat.xmdot * tsince
  5616.     omgadf = sat.omegao + sat.omgdot * tsince
  5617.     xnoddf = sat.xnodeo + sat.xnodot * tsince
  5618.     omega = omgadf
  5619.     xmp = xmdf
  5620.     tsq = tsince * tsince
  5621.     xnode = xnoddf + sat.xnodcf * tsq
  5622.     tempa = 1.0 - sat.c1 * tsince
  5623.     tempe = sat.bstar * sat.c4 * tsince
  5624.     templ = sat.t2cof * tsq
  5625.     if not sat.isimp == 1 then
  5626.         delomg = sat.omgcof * tsince
  5627.         delm = sat.xmcof * (math.pow(1.0 + sat.eta * math.cos(xmdf),3.0) - sat.delmo)
  5628.         temp = delomg + delm
  5629.         xmp = xmdf + temp
  5630.         omega = omgadf - temp
  5631.         tcube = tsq * tsince
  5632.         tfour = tsince * tcube
  5633.         tempa = tempa - sat.d2 * tsq - sat.d3 * tcube - sat.d4 * tfour
  5634.         tempe = tempe + sat.bstar * sat.c5 * (math.sin(xmp) - sat.sinmo)
  5635.         templ = templ + sat.t3cof * tcube + tfour * (sat.t4cof + tsince * sat.t5cof)
  5636.     end
  5637.     a = sat.aodp * tempa * tempa
  5638.     e = sat.eo - tempe
  5639.     xl = xmp + omega + xnode + sat.xnodp * templ
  5640.     beta = math.sqrt(1.0 - e * e)
  5641.     xn = const_xke/math.pow(a,1.5)
  5642.  
  5643.     --Long period periodics  
  5644.     axn = e * math.cos(omega)
  5645.     temp = 1.0/(a * beta * beta)
  5646.     xll = temp * sat.xlcof * axn
  5647.     aynl = temp * sat.aycof
  5648.     xlt = xl + xll
  5649.     ayn = e * math.sin(omega) + aynl
  5650.  
  5651.     -- Solve Kepler's Equation
  5652.     capu = SGP_fmod2p(xlt - xnode)
  5653.     i = 1
  5654.     epw = 0
  5655.     repeat
  5656.         i = i + 1
  5657.         if i == 2 then -- Primera iteración
  5658.             temp2 = capu
  5659.         else
  5660.             temp2 = epw
  5661.         end
  5662.         sinepw = math.sin(temp2)
  5663.         cosepw = math.cos(temp2)
  5664.         temp3 = axn * sinepw
  5665.         temp4 = ayn * cosepw
  5666.         temp5 = axn * cosepw
  5667.         temp6 = ayn * sinepw
  5668.         epw = (capu - temp4 + temp3 - temp2)/(1.0 - temp5 - temp6) + temp2
  5669.     until ((math.abs(epw - temp2) <= const_e6a) or (i>10))
  5670.    
  5671.     --Short period preliminary quantities
  5672.  
  5673.     ecose = temp5 + temp6
  5674.     esine = temp3 - temp4
  5675.     elsq = axn * axn + ayn * ayn
  5676.     temp = 1.0 - elsq
  5677.     pl = a * temp
  5678.     r = a * (1.0 - ecose)
  5679.     temp1 = 1.0/r
  5680.     rdot = const_xke * math.sqrt(a) * esine * temp1
  5681.     rfdot = const_xke * math.sqrt(pl) * temp1
  5682.     temp2 = a * temp1
  5683.     betal = math.sqrt(temp)
  5684.     temp3 = 1.0/(1.0 + betal)
  5685.     cosu = temp2 * (cosepw - axn + ayn * esine * temp3)
  5686.     sinu = temp2 * (sinepw - ayn - axn * esine * temp3)
  5687.     u = SGP_acTan(sinu,cosu)
  5688.     sin2u = 2.0 * sinu * cosu
  5689.     cos2u = 2.0 * cosu * cosu - 1.0
  5690.     temp = 1.0/pl
  5691.     temp1 = const_ck2 * temp
  5692.     temp2 = temp1 * temp
  5693.     --Update for short periodics
  5694.     rk = r * (1.0 - 1.5 * temp2 * betal * sat.x3thm1) + 0.5 * temp1 * sat.x1mth2 * cos2u
  5695.     uk = u - 0.25 * temp2 * sat.x7thm1 * sin2u
  5696.     xnodek = xnode + 1.5 * temp2 * sat.cosio * sin2u
  5697.     xinck = sat.xincl + 1.5 * temp2 * sat.cosio * sat.sinio * cos2u
  5698.     rdotk = rdot - xn * temp1 * sat.x1mth2 * sin2u
  5699.     rfdotk = rfdot + xn * temp1 * (sat.x1mth2 * cos2u + 1.5 * sat.x3thm1)
  5700.     --Orientation vectors
  5701.     sinuk = math.sin(uk)
  5702.     cosuk = math.cos(uk)
  5703.     sinik = math.sin(xinck)
  5704.     cosik = math.cos(xinck)
  5705.     sinnok = math.sin(xnodek)
  5706.     cosnok = math.cos(xnodek)
  5707.     xmx = -sinnok * cosik
  5708.     xmy = cosnok * cosik
  5709.     ux = xmx * sinuk + cosnok * cosuk
  5710.     uy = xmy * sinuk + sinnok * cosuk
  5711.     uz = sinik * sinuk
  5712.     vx = xmx * cosuk - cosnok * sinuk
  5713.     vy = xmy * cosuk - sinnok * sinuk
  5714.     vz = sinik * cosuk
  5715.    
  5716.     --Position and velocity
  5717.     x = rk * ux  
  5718.     sat.x = x
  5719.     y = rk * uy
  5720.     sat.y = y
  5721.     z = rk * uz
  5722.     sat.z = z
  5723.     xdot = rdotk * ux + rfdotk * vx  
  5724.     sat.vx = xdot
  5725.     ydot = rdotk * uy + rfdotk * vy  
  5726.     sat.vy = ydot
  5727.     zdot = rdotk * uz + rfdotk * vz  
  5728.     sat.vz = zdot
  5729.    
  5730.     --Convert sat state
  5731.    
  5732.     sat.x = sat.x * const_xkmper
  5733.     sat.vx = sat.vx * const_xkmper/60
  5734.     sat.y = sat.y * const_xkmper
  5735.     sat.vy = sat.vy * const_xkmper/60
  5736.     sat.z = sat.z * const_xkmper
  5737.     sat.vz = sat.vz * const_xkmper/60
  5738.    
  5739.     sat.m = math.sqrt(sat.x^2+sat.y^2+sat.z^2)
  5740.     sat.vm = math.sqrt(sat.vx^2+sat.vy^2+sat.vz^2)
  5741.        
  5742. end
  5743.  
  5744.  
  5745. function SGP_calculateSatIllum(sat)
  5746.     local sd_sun , sd_earth
  5747.     local delta
  5748.     local rho
  5749.     local earth
  5750.  
  5751.     -- Determina eclipse parcial
  5752.     sd_earth = math.asin(const_xkmper/sat.m)
  5753.     rho = SGP_subVectores(sgp_sun,sat)
  5754.     sd_sun = math.asin(const_sr/rho.m)
  5755.     earth = { }
  5756.     earth.x = -sat.x
  5757.     earth.y = -sat.y
  5758.     earth.z = -sat.z
  5759.     earth.m = sat.m
  5760.     delta = SGP_angulo(sgp_sun,earth)
  5761.     sat.depth = sd_earth - sd_sun - delta
  5762.     if sd_earth < sd_sun then
  5763.         sat.eclipsado = false
  5764.     else
  5765.         if sat.depth >= 0 then
  5766.           sat.eclipsado = true
  5767.         else
  5768.           sat.eclipsado = false
  5769.         end
  5770.     end  
  5771. end
  5772.  
  5773.  
  5774. function SGP_calculateSatLatLonAlt (sat, jtime)
  5775.     local alt, lon, lat
  5776.     local theta, r, e2, phi, c
  5777.    
  5778.     theta = SGP_acTan(sat.y , sat.x)
  5779.     lon = SGP_modulus(theta - SGP_thetaG_JD(jtime),math.pi*2.0)
  5780.     r = math.sqrt(sat.x^2 + sat.y^2)
  5781.     e2 = const_f*(2.0 - const_f)
  5782.     lat = SGP_acTan(sat.z,r)
  5783.     repeat
  5784.         phi = lat
  5785.         c = 1.0/math.sqrt(1.0 - e2* math.pow(math.sin(phi),2.0))
  5786.         lat = SGP_acTan(sat.z + const_xkmper * c * e2 * math.sin(phi),r)
  5787.     until (math.abs(lat-phi) < 0.0000000001)
  5788.    
  5789.     alt = r/math.cos(lat) - const_xkmper * c
  5790.    
  5791.     sat.latitud = math.deg(lat)
  5792.     sat.longitud = math.deg(lon)
  5793.     sat.altitud = alt
  5794.     if (sat.longitud >= 180) then
  5795.         sat.longitud = sat.longitud - 360
  5796.     end
  5797. end
  5798.  
  5799. function SGP_calculateSun (jtime)
  5800.     SGP_calculateSunPosition( jtime )
  5801.     SGP_calculateLocation( jtime )
  5802.     SGP_calculateSunAzElev( jtime )
  5803. end
  5804.  
  5805. function SGP_calculateSunPosition(jtime)
  5806.  
  5807.     local mjd,year,T,M,L,e,C,O,Lsa,nu,R,eps
  5808.     local alt, lon, lat
  5809.     local thetaAux, r, e2, phi, c2
  5810.    
  5811.     mjd = jtime - 2415020.0
  5812.     year = 1900.0 + mjd/365.25
  5813.     T = (mjd + SGP_deltaET(year)/const_secday)/36525.0
  5814.     M = math.rad(SGP_modulus(358.47583 + SGP_modulus(35999.04975 * T,360.0) - (0.000150 + 0.0000033*T)*(T*T),360.0))
  5815.     L = math.rad(SGP_modulus(279.69668 + SGP_modulus(36000.76892*T,360.0) + 0.0003025*(T*T),360.0))
  5816.     e = 0.01675104 - (0.0000418 + 0.000000126*T)*T
  5817.     C = math.rad((1.919460 - (0.004789 + 0.000014*T)*T)*math.sin(M) + (0.020094 - 0.000100*T)*math.sin(2.0*M) + 0.000293*math.sin(3.0*M))
  5818.     O = math.rad(SGP_modulus(259.18 - 1934.142*T,360.0))
  5819.     Lsa = SGP_modulus(L + C - math.rad(0.00569 - 0.00479*math.sin(O)),(math.pi*2.0))
  5820.     nu = SGP_modulus(M + C,(math.pi*2.0))
  5821.     R = 1.0000002*(1.0 - (e*e))/(1.0 + e*math.cos(nu))
  5822.     eps = math.rad(23.452294 - (0.0130125 + (0.00000164 - 0.000000503*T)*T)*T + 0.00256*math.cos(O))
  5823.     R = const_AU*R
  5824.    
  5825.    
  5826.     sgp_sun.x = R*math.cos(Lsa)
  5827.     sgp_sun.y = R*math.sin(Lsa)*math.cos(eps)
  5828.     sgp_sun.z = R*math.sin(Lsa)*math.sin(eps)
  5829.     sgp_sun.m = R
  5830.    
  5831.        
  5832.     thetaAux = SGP_acTan(sgp_sun.y,sgp_sun.x)
  5833.     lon = SGP_modulus(thetaAux - SGP_thetaG_JD(jtime),math.pi*2.0)
  5834.     r = math.sqrt(sgp_sun.x^2 + sgp_sun.y^2)
  5835.     e2 = const_f*(2.0 - const_f)
  5836.     lat = SGP_acTan(sgp_sun.z,r)
  5837.     repeat
  5838.         phi = lat
  5839.         c2 = 1.0 / math.sqrt (1.0 - e2 * math.pow(math.sin(phi),2.0))
  5840.         lat = SGP_acTan(sgp_sun.z + const_xkmper*c2*e2*math.sin(phi),r)
  5841.     until (math.abs(lat-phi) < 0.0000000001)
  5842.    
  5843.     alt = (r / math.cos(lat)) - (const_xkmper * c2)
  5844.    
  5845.     sgp_sun.lat = math.deg(lat)
  5846.     sgp_sun.lon = math.deg(lon)
  5847.     sgp_sun.alt = alt
  5848.     theta = thetaAux
  5849.     if sgp_sun.lon >= 180 then
  5850.         sgp_sun.lon = sgp_sun.lon - 360
  5851.     end
  5852. end
  5853.  
  5854.  
  5855. function SGP_calculateSunAzElev( jt )
  5856.     local i    
  5857.     local sin_lat,cos_lat, sin_theta,cos_theta
  5858.     local el,azim,lat,theta
  5859.     local top_s,top_e,top_z
  5860.  
  5861.      --antiguamente llamado calcular variables
  5862.    
  5863.     local range = {}
  5864.     local rgvel = {}
  5865.     range.x = sgp_sun.x - sgp_location.x
  5866.     rgvel.x = 0.0 - sgp_location.vx
  5867.     range.y = sgp_sun.y - sgp_location.y
  5868.     rgvel.y = 0.0 - sgp_location.vy
  5869.     range.z = sgp_sun.z - sgp_location.z
  5870.     rgvel.z = 0.0 - sgp_location.vz
  5871.    
  5872.     range.m = SGP_magnitud(range.x,range.y,range.z)
  5873.    
  5874.     lat = sgp_location.polar.lat
  5875.     theta = sgp_location.polar.m   
  5876.     sin_lat = math.sin(lat)
  5877.     cos_lat = math.cos(lat)
  5878.     sin_theta = math.sin(theta)
  5879.     cos_theta = math.cos(theta)
  5880.     top_s = sin_lat * cos_theta * range.x + sin_lat * sin_theta * range.y - cos_lat * range.z
  5881.     top_e = -sin_theta * range.x + cos_theta * range.y
  5882.     top_z = cos_lat * cos_theta * range.x + cos_lat * sin_theta * range.y + sin_lat * range.z
  5883.     azim = math.atan(-top_e/top_s) --Azimuth
  5884.     if (top_s > 0) then
  5885.         azim = azim + math.pi
  5886.     end
  5887.     if (azim < 0) then
  5888.         azim = azim + (math.pi * 2.0)
  5889.     end
  5890.     el = math.asin(top_z/range.m)
  5891.     sgp_sun.azi = math.deg(azim)  --Azimuth (radians to degrees)
  5892.     --sgp_sol.elevacion = el --Elevation (radians)
  5893.     sgp_sun.rango = range.m  --Range (kilometers)
  5894.     sgp_sun.ratioRango = SGP_dot(range,rgvel)/range.m --Range Rate (kilometers/second)
  5895.     -- Corrections for atmospheric refraction }
  5896.     -- Reference:  Astronomical Algorithms by Jean Meeus, pp. 101-104 }
  5897.     -- Note:  Correction is meaningless when apparent elevation is below horizon }
  5898.     sgp_sun.ele = el + math.rad((1.02/math.tan(math.rad(math.deg(el)+10.3/(math.deg(el)+5.11))))/60.0)
  5899.     --if (sgp_sun.ele < -3) then
  5900.     --  sgp_sun.ele = el  --reset to true elevation
  5901.     --end
  5902.     sgp_sun.ele = math.deg(sgp_sun.ele)
  5903.     sgp_location.noche = (sgp_sun.ele < const_twilightCivil)
  5904. end
  5905.  
  5906. function SGP_calculateSatAzElev (sat, jt)
  5907.     local i    
  5908.     local sin_lat,cos_lat, sin_theta,cos_theta
  5909.     local el,azim,lat,theta
  5910.     local top_s,top_e,top_z
  5911.  
  5912.     SGP_calculateLocation(jt);
  5913.    
  5914.     local range = {}
  5915.     local rgvel = {}
  5916.    
  5917.     range.x = sat.x - sgp_location.x
  5918.     range.y = sat.y - sgp_location.y
  5919.     range.z = sat.z - sgp_location.z
  5920.  
  5921.     rgvel.x = sat.vx - sgp_location.vx
  5922.     rgvel.y = sat.vy - sgp_location.vy
  5923.     rgvel.z = sat.vz - sgp_location.vz
  5924.  
  5925.     range.m = SGP_magnitud(range.x,range.y,range.z)
  5926.    
  5927.     lat = sgp_location.polar.lat
  5928.     theta = sgp_location.polar.m
  5929.     sin_lat = math.sin(lat)
  5930.     cos_lat = math.cos(lat)
  5931.     sin_theta = math.sin(theta)
  5932.     cos_theta = math.cos(theta)
  5933.     top_s = sin_lat*cos_theta*range.x + sin_lat*sin_theta*range.y - cos_lat*range.z
  5934.     top_e = -sin_theta*range.x + cos_theta*range.y
  5935.     top_z = cos_lat*cos_theta*range.x + cos_lat*sin_theta*range.y + sin_lat*range.z
  5936.     azim = math.atan(-top_e/top_s) --Azimuth
  5937.     if (top_s > 0) then
  5938.         azim = azim + math.pi
  5939.     end
  5940.     if (azim < 0) then
  5941.         azim = azim + (math.pi*2.0)
  5942.     end
  5943.     el = math.asin(top_z/range.m)
  5944.     sat.azimut = math.deg(azim)  --Azimuth (radians to degrees)
  5945.     --sat.elevacion = el  --Elevation (radians)
  5946.     sat.rango = range.m  --Range (kilometers)
  5947.     sat.ratioRango = SGP_dot(range,rgvel)/range.m --Range Rate (kilometers/second)
  5948.     -- Corrections for atmospheric refraction }
  5949.     -- Reference:  Astronomical Algorithms by Jean Meeus, pp. 101-104 }
  5950.     -- Note:  Correction is meaningless when apparent elevation is below horizon }
  5951.     if not sat.primera_vez then
  5952.         sat.elevacion_ant = sat.elevacion
  5953.     else
  5954.         sat.primera_vez = false
  5955.     end
  5956.    
  5957.     sat.elevacion = el + math.rad((1.02/math.tan(math.rad(math.deg(el)+10.3/(math.deg(el)+5.11))))/60.0)
  5958.     if (sat.elevacion < 0) then
  5959.         sat.elevacion = el  --Reset to true elevation
  5960.     end
  5961.     sat.elevacion = math.deg(sat.elevacion)
  5962.        
  5963. end
  5964.  
  5965.  
  5966. function SGP_calculateSatVisibility(sat)
  5967.     sat.visible = ( (not sat.eclipsado) and (sgp_location.noche) and (sat.elevacion > 0))
  5968. end
  5969.  
  5970.  
  5971. function SGP_calculateLocation(jt)
  5972.     local c,s,achcp
  5973.     local lat, lon, alt, theta
  5974.    
  5975.     lat = sgp_location.polar.lat
  5976.     lon = sgp_location.polar.lon
  5977.     alt = sgp_location.polar.alt
  5978.    
  5979.     --LMST
  5980.     theta = SGP_modulus(SGP_thetaG_JD(jt) + lon,(math.pi * 2))
  5981.     sgp_location.polar.m=theta
  5982.     c = 1.0/math.sqrt(1.0 + const_f*(const_f - 2.0)*math.pow(math.sin(lat),2.0))
  5983.     s = ((1 - const_f)*(1 - const_f))*c
  5984.     achcp = (const_xkmper*c + alt)*math.cos(lat)
  5985.     sgp_location.x = achcp*math.cos(theta) -- kilometros
  5986.     sgp_location.y = achcp*math.sin(theta)
  5987.     sgp_location.z = (const_xkmper*s + alt)*math.sin(lat)
  5988.     sgp_location.vx = -const_mfactor*sgp_location.y --kilometers/second
  5989.     sgp_location.vy =  const_mfactor*sgp_location.x
  5990.     sgp_location.vz =  0
  5991.    
  5992.     sgp_location.m = SGP_magnitud(sgp_location.x,sgp_location.y,sgp_location.z)
  5993.     sgp_location.vm = SGP_magnitud(sgp_location.vx,sgp_location.vy,sgp_location.vz)
  5994. end
  5995.  
  5996. function SGP_calculateSatellite (s , jtime)
  5997.         SGP_calculateSatPosVel (sgp_sats[s], jtime)
  5998.         SGP_calculateSatIllum(sgp_sats[s])
  5999.         SGP_calculateSatLatLonAlt (sgp_sats[s], jtime)
  6000.         SGP_calculateSatAzElev (sgp_sats[s], jtime)
  6001.         SGP_calculateSatVisibility(sgp_sats[s])
  6002. end
  6003.  
  6004. function SGP_fixangle(a)
  6005.     return ((a) - 360.0 * (math.floor((a) / 360.0)))
  6006. end
  6007.  
  6008. --  KEPLER  --  Solve the equation of Kepler.
  6009.  
  6010. function SGP_kepler( m, ecc)
  6011.  
  6012.     local e, delta
  6013.     local EPSILON = 0.000001
  6014.    
  6015.     m = SGP_dtr(m)
  6016.     e = m
  6017.     repeat
  6018.         delta = e - ecc * math.sin(e) - m
  6019.         e = e - delta / (1 - ecc * math.cos(e))
  6020.     until not (math.abs(delta) > EPSILON)
  6021.     return e
  6022. end
  6023.  
  6024. function SGP_fixangr(a)
  6025.     return ((a) - (math.pi*2) * (math.floor((a) / (math.pi*2))))
  6026. end
  6027.  
  6028. function SGP_dtr(x)
  6029.     return ((x) * (math.pi / 180.0))
  6030. end
  6031.  
  6032. function SGP_rtd(x)
  6033.     return ((x) / (math.pi / 180.0))
  6034. end
  6035.  
  6036. function SGP_highmoon(jd)
  6037.     local t, t2, t3, t4, lprime, d, m, mprime, f, a1, a2, a3
  6038.     local e = {}
  6039.     local sigmaL, sigmaB, sigmaR, ang
  6040.     local i
  6041.     local l, b, r
  6042.  
  6043.     t = (jd - const_J2000) / const_JulianCentury
  6044.  
  6045.     t2 = t * t
  6046.     t3 = t * t2
  6047.     t4 = t * t3
  6048.  
  6049.     lprime = 218.3164591 + 481267.88134236 * t - 0.0013268 * t2 +
  6050.                 t3 / 538841.0 - t4 / 65194000.0
  6051.  
  6052.     d = 297.8502042 + 445267.1115168 * t - 0.00163 * t2 +
  6053.                 t3 / 545868.0 - t4 / 113065000.0
  6054.  
  6055.     m = 357.5291092 + 35999.0502909 * t - 0.0001536 * t2 +
  6056.                 t3 / 24490000.0
  6057.  
  6058.     mprime = 134.9634114 + 477198.8676313 * t + 0.008997 * t2 +
  6059.                 t3 / 69699.0 - t4 / 14712000.0
  6060.  
  6061.     f = 93.2720993 + 483202.0175273 * t - 0.0034029 * t2 -
  6062.                 t3 / 3526000.0 + t4 / 863310000.0
  6063.  
  6064.     a1 = 119.75 + 131.849 * t
  6065.  
  6066.     a2 = 53.09 + 479264.29 * t
  6067.  
  6068.     a3 = 313.45 + 481266.484 * t
  6069.  
  6070.     e[1] = 1
  6071.     e[2] = 1 - (0.002516 * t) - (0.0000074 * t2)
  6072.     e[3] = e[2] * e[2]
  6073.  
  6074.     lprime = SGP_fixangr(SGP_dtr(lprime))
  6075.     d = SGP_fixangr(SGP_dtr(d))
  6076.     m = SGP_fixangr(SGP_dtr(m))
  6077.     mprime = SGP_fixangr(SGP_dtr(mprime))
  6078.     f = SGP_fixangr(SGP_dtr(f))
  6079.     a1 = SGP_fixangr(SGP_dtr(a1))
  6080.     a2 = SGP_fixangr(SGP_dtr(a2))
  6081.     a3 = SGP_fixangr(SGP_dtr(a3))
  6082.  
  6083.     sigmaR = 0
  6084.     sigmaB = 0
  6085.     sigmaL = 0
  6086.  
  6087.     for i = 1,const_NTERMS do
  6088.         ang = sgp_lrCoeff[i][1] * d + sgp_lrCoeff[i][2] * m +
  6089.               sgp_lrCoeff[i][3] * mprime + sgp_lrCoeff[i][4] * f
  6090.         sigmaL = sigmaL + sgp_lTerms[i] * math.sin(ang) * e[math.abs(sgp_lrCoeff[i][2])+1]
  6091.         if (sgp_rTerms[i] ~= 0) then
  6092.             sigmaR = sigmaR + sgp_rTerms[i] * math.cos(ang) * e[math.abs(sgp_lrCoeff[i][2])+1]
  6093.         end
  6094.  
  6095.         ang = sgp_bCoeff[i][1] * d + sgp_bCoeff[i][2] * m +
  6096.               sgp_bCoeff[i][3] * mprime + sgp_bCoeff[i][4] * f
  6097.         sigmaB = sigmaB + sgp_bTerms[i] * math.sin(ang) * e[math.abs(sgp_bCoeff[i][2])+1]
  6098.     end
  6099.    
  6100.     sigmaL = sigmaL + 3958.0 * math.sin(a1) + 1962.0 * math.sin(lprime - f) + 318.0 * math.sin(a2)
  6101.  
  6102.     sigmaB = sigmaB - 2235.0 * math.sin(lprime) + 382.0 * math.sin(a3) + 175.0 * math.sin(a1 - f) +
  6103.               175.0 * math.sin(a1 + f) + 127.0 * math.sin(lprime - mprime) - 115.0 * math.sin(lprime + mprime)
  6104.  
  6105.     l = SGP_rtd(lprime) + sigmaL / 1000000.0
  6106.     b = sigmaB / 1000000.0
  6107.     r = 385000.56 + sigmaR / 1000.0
  6108.    
  6109.     return l, b, r
  6110. end
  6111.  
  6112.  
  6113. function SGP_Asec(x)
  6114.     return ((x) / 3600.0)
  6115. end
  6116.  
  6117.  
  6118. --  OBLIQEQ  --  Calculate the obliquity of the ecliptic for a given Julian
  6119.          --date.  This uses Laskar's tenth-degree polynomial fit
  6120.          --(J. Laskar, Astronomy and Astrophysics, Vol. 157, page 68 [1986])
  6121.          --which is accurate to within 0.01 arc second between AD 1000
  6122.          --and AD 3000, and within a few seconds of arc for +/-10000
  6123.          --years around AD 2000.  If we're outside the range in which
  6124.          --this fit is valid (deep time) we simply return the J2000 value
  6125.         -- the obliquity, which happens to be almost precisely the mean.  */
  6126.  
  6127. function SGP_obliqeq( jd )
  6128.  
  6129.     local oterms = {SGP_Asec(-4680.93),
  6130.         SGP_Asec(   -1.55),
  6131.         SGP_Asec( 1999.25),
  6132.         SGP_Asec(  -51.38),
  6133.         SGP_Asec( -249.67),
  6134.         SGP_Asec(  -39.05),
  6135.         SGP_Asec(    7.12),
  6136.         SGP_Asec(   27.87),
  6137.         SGP_Asec(    5.79),
  6138.         SGP_Asec(    2.45)}
  6139.  
  6140.  
  6141.     local eps, i, u, v
  6142.     eps = 23 + (26 / 60.0) + (21.448 / 3600.0)
  6143.     u = (jd - const_J2000) / (const_JulianCentury * 100)
  6144.     v = u
  6145.  
  6146.     if (math.abs(u) < 1.0) then
  6147.         for i = 1, 10 do
  6148.             eps = eps + oterms[i] * v
  6149.             v = v * u
  6150.         end
  6151.     end
  6152.     return eps
  6153. end
  6154.  
  6155.  
  6156. --  ECLIPTOEQ  --  Convert celestial (ecliptical) longitude and latitude into
  6157.         --right ascension (in degrees) and declination.  We must supply
  6158.         --the time of the conversion in order to compensate correctly
  6159.         --for the varying obliquity of the ecliptic over time.  */
  6160.  
  6161. function SGP_ecliptoeq(jd,Lambda, Beta)
  6162.     local eps
  6163.     local Ra, Dec
  6164.     -- Obliquity of the ecliptic.
  6165.  
  6166.     eps = SGP_dtr(SGP_obliqeq(jd))
  6167.  
  6168.     Ra = SGP_fixangle(SGP_rtd(math.atan2((math.cos(eps) * math.sin(SGP_dtr(Lambda)) -
  6169.                          (math.tan(SGP_dtr(Beta)) * math.sin(eps))), math.cos(SGP_dtr(Lambda)))))
  6170.     Dec = SGP_rtd(math.asin((math.sin(eps) * math.sin(SGP_dtr(Lambda)) * math.cos(SGP_dtr(Beta))) +
  6171.                  (math.sin(SGP_dtr(Beta)) * math.cos(eps))))
  6172.                  
  6173.     return Ra, Dec               
  6174. end
  6175.  
  6176. function SGP_RADecToAzElev (jd, ra, dec, lat, lon)
  6177.     local r = math.rad(ra)
  6178.     local d = math.rad(dec)
  6179.     local n = math.rad(lat)    -- North latitude of tracking station
  6180.     local e = -math.rad(lon)   -- East longitude of tracking station
  6181.  
  6182.     -- Find siderial time in radians
  6183.  
  6184.     local t=(jd-2451545.0)/36525.0
  6185.     local teg=280.46061837+360.98564736629*(jd-2451545.0)+(0.000387933*t-t*t/38710000.0)*t
  6186.  
  6187.     while (teg>360.0) do
  6188.         teg = teg - 360.0
  6189.     end
  6190.    
  6191.     th = math.rad(teg - lon)
  6192.     while (th > 2*math.pi) do
  6193.         th = th - 2*math.pi
  6194.     end
  6195.    
  6196.     local h=th-r
  6197.  
  6198.     az=math.atan2(math.sin(h),math.cos(h)*math.sin(n)-math.tan(d)*math.cos(n))+math.pi
  6199.     el=math.asin(math.sin(n)*math.sin(d)+math.cos(n)*math.cos(d)*math.cos(h))
  6200.  
  6201.     local el_with_atmosphere_refraction = el + math.rad((1.02/math.tan(math.rad(math.deg(el)+10.3/(math.deg(el)+5.11))))/60.0)
  6202.     if (el_with_atmosphere_refraction < 0) then
  6203.         el_with_atmosphere_refraction = el  --reset to true elevation
  6204.     end
  6205.    
  6206.     return math.deg(az), math.deg(el_with_atmosphere_refraction)
  6207. end    
  6208.  
  6209.  
  6210.  
  6211.  
  6212. --  GMST  --  Calculate Greenwich Mean Siderial Time for a given instant expressed as a Julian date and fraction.
  6213.  
  6214. function SGP_gmst(jd)
  6215.     local t, theta0
  6216.    
  6217.     -- Time, in Julian centuries of 36525 ephemeris days, measured from the epoch 1900 January 0.5 ET.
  6218.     t = ((math.floor(jd + 0.5) - 0.5) - 2415020.0) / const_JulianCentury
  6219.     theta0 = 6.6460656 + 2400.051262 * t + 0.00002581 * t * t
  6220.     t = (jd + 0.5) - (math.floor(jd + 0.5))
  6221.     theta0 = theta0 + (t * 24.0) * 1.002737908
  6222.     theta0 = (theta0 - 24.0 * (math.floor(theta0 / 24.0)))
  6223.     return theta0
  6224. end
  6225.  
  6226.  
  6227. function SGP_calculateMoon(jt)           
  6228.     local Day, N, M, Ec, Lambdasun, ml, MM, MN, Ev, Ae, A3, MmP
  6229.     local mEc, A4, lP, Varia, lPP, NP, magnitud, z, y, x, Lambdamoon
  6230.     local MoonAge, MoonPhase, MoonDist, MoonDFrac, MoonAng
  6231.     local F, SunDist, SunAng
  6232.  
  6233.     -- Calculation of the Sun's position
  6234.  
  6235.     Day = jt - const_epoch                  --Date within epoch
  6236.     N = SGP_fixangle((360 / 365.2422) * Day)   -- Mean anomaly of the Sun
  6237.     M = SGP_fixangle(N + const_elonge - const_elongp)  --Convert from perigee co-ordinates to epoch 1980.0
  6238.     Ec = SGP_kepler(M, const_eccent)                    -- Solve equation of Kepler
  6239.     Ec = math.sqrt((1 + const_eccent) / (1 - const_eccent)) * math.tan(Ec / 2)
  6240.     Ec = 2 * SGP_rtd(math.atan(Ec))                 -- True anomaly
  6241.     Lambdasun = SGP_fixangle(Ec + const_elongp)     -- Sun's geocentric ecliptic longitude
  6242.     -- Orbital distance factor
  6243.     F = ((1 + const_eccent * math.cos(SGP_dtr(Ec))) / (1 - const_eccent * const_eccent))
  6244.     SunDist = const_SunSMAX / F                 -- Distance to Sun in km
  6245.     SunAng = F * const_sunangsiz                --Sun's angular size in degrees */
  6246.  
  6247.     -- Calculation of the Moon's position
  6248.  
  6249.     -- Moon's mean longitude
  6250.     ml = SGP_fixangle(13.1763966 * Day + const_mmlong)
  6251.  
  6252.     -- Moon's mean anomaly
  6253.     MM = SGP_fixangle(ml - 0.1114041 * Day - const_mmlongp)
  6254.  
  6255.     -- Moon's ascending node mean longitude
  6256.     MN = SGP_fixangle(const_mlnode - 0.0529539 * Day)
  6257.  
  6258.     -- Evection
  6259.     Ev = 1.2739 * math.sin(SGP_dtr(2 * (ml - Lambdasun) - MM))
  6260.  
  6261.     -- Annual equation
  6262.     Ae = 0.1858 * math.sin(SGP_dtr(M))
  6263.  
  6264.     -- Correction term
  6265.     A3 = 0.37 * math.sin(SGP_dtr(M))
  6266.  
  6267.     -- Corrected anomaly
  6268.     MmP = MM + Ev - Ae - A3
  6269.  
  6270.     -- Correction for the equation of the centre
  6271.     mEc = 6.2886 * math.sin(SGP_dtr(MmP))
  6272.  
  6273.     -- Another correction term
  6274.     A4 = 0.214 * math.sin(SGP_dtr(2 * MmP))
  6275.  
  6276.     -- Corrected longitude
  6277.     lP = ml + Ev + mEc - Ae + A4
  6278.  
  6279.     -- Variation
  6280.     Varia = 0.6583 * math.sin(SGP_dtr(2 * (lP - Lambdasun)))
  6281.  
  6282.     -- True longitude
  6283.     lPP = lP + Varia
  6284.  
  6285.     -- Corrected longitude of the node
  6286.     NP = MN - 0.16 * math.sin(SGP_dtr(M))
  6287.    
  6288.     -- Y inclination coordinate
  6289.     y = math.sin(SGP_dtr(lPP - NP)) * math.cos(SGP_dtr(const_minc))
  6290.  
  6291.     -- X inclination coordinate
  6292.     x = math.cos(SGP_dtr(lPP - NP))
  6293.  
  6294.  
  6295.    
  6296.     -- Ecliptic longitude
  6297.     Lambdamoon = SGP_rtd(math.atan2(y, x))
  6298.     Lambdamoon = Lambdamoon + NP
  6299.  
  6300.     -- Ecliptic latitude
  6301.     -- BetaM = rtd(asin(sin(dtr(lPP - NP)) * sin(dtr(minc))));
  6302.  
  6303.     -- Calculation of the phase of the Moon
  6304.  
  6305.     -- Age of the Moon in degrees
  6306.     MoonAge = lPP - Lambdasun
  6307.  
  6308.     -- Phase of the Moon
  6309.     MoonPhase = (1 - math.cos(SGP_dtr(MoonAge))) / 2
  6310.  
  6311.     -- Calculate distance of moon from the centre of the Earth
  6312.  
  6313.     MoonDist = (const_msmax * (1 - const_mecc * const_mecc)) / (1 + const_mecc * math.cos(SGP_dtr(MmP + mEc)))
  6314.  
  6315.     -- Calculate Moon's angular diameter
  6316.  
  6317.     MoonDFrac = MoonDist / const_msmax
  6318.     MoonAng = const_mangsiz / MoonDFrac
  6319.  
  6320.     -- Calculate Moon's parallax
  6321.  
  6322.     --    MoonPar = mparallax / MoonDFrac;
  6323.  
  6324.     SGP_calculateLocation(jt)  
  6325.    
  6326.     local igmst = SGP_gmst(jt)
  6327.    
  6328.     local moonlong, moonlat, mooncrv = SGP_highmoon( jt )      
  6329.     local moonra, moondec = SGP_ecliptoeq(jt, moonlong, moonlat)
  6330.     moonlong = (igmst * 15) - moonra;
  6331.    
  6332.     moonlong = -moonlong
  6333.     if (moonlong > 180) then
  6334.         moonlong = moonlong - 360
  6335.     elseif (moonlong < -180) then
  6336.         moonlong = moonlong + 360
  6337.     end
  6338.    
  6339.     local moonaz, moonele = SGP_RADecToAzElev(jt, moonra, moondec,sgp_location.lat, sgp_location.lon)
  6340.  
  6341.     sgp_moon.ra = moonra
  6342.     sgp_moon.dec = moondec 
  6343.     sgp_moon.lon = moonlong
  6344.     sgp_moon.lat = moonlat
  6345.     sgp_moon.azi = moonaz
  6346.     sgp_moon.ele = moonele + (1.02/math.tan(math.rad(moonele+10.3/(moonele+5.11))))/60.0
  6347.    
  6348.  
  6349.     sgp_moon.phase = MoonPhase
  6350.  
  6351.     local aom = const_SynMonth * (SGP_fixangle(MoonAge) / 360.0)
  6352.     sgp_moon.age.cicle = aom / const_SynMonth
  6353.     sgp_moon.age.d = math.floor(aom)
  6354.     sgp_moon.age.h = math.floor(24 * (aom - math.floor(aom)))
  6355.     sgp_moon.age.m = math.floor((1440 * (aom - math.floor(aom))) % 60)
  6356.     sgp_moon.dist = MoonDist
  6357.     sgp_moon.ang = MoonAng
  6358.     sgp_moon.sun_dist = SunDist
  6359.     sgp_moon.sun_ang = SunAng
  6360.  
  6361. end
  6362.  
  6363.  
  6364.  
  6365.  
  6366. function SGP_calculateSunRaiseTime()
  6367.     SGP_getCurrentTime()
  6368.     local jtime = sgp_time.ujtime
  6369.     SGP_calculateSun (jtime)
  6370.  
  6371.     if sgp_sun.ele > 0 then
  6372.         repeat
  6373.             jtime = jtime - 0.000694446 * 10
  6374.             SGP_calculateSun (jtime)
  6375.         until (sgp_sun.ele < 0)
  6376.         repeat
  6377.             jtime = jtime + 0.000694446
  6378.             SGP_calculateSun (jtime)   
  6379.         until (sgp_sun.ele > 0)
  6380.         repeat
  6381.             jtime = jtime - 0.000694446 * 0.16
  6382.             SGP_calculateSun (jtime)   
  6383.         until (sgp_sun.ele <= 0)
  6384.     else
  6385.         repeat
  6386.             jtime = jtime + 0.000694446 * 10
  6387.             SGP_calculateSun (jtime)
  6388.         until (sgp_sun.ele > 0)
  6389.         repeat
  6390.             jtime = jtime - 0.000694446
  6391.             SGP_calculateSun (jtime)   
  6392.         until (sgp_sun.ele < 0)
  6393.         repeat
  6394.             jtime = jtime + 0.000694446 * 0.16
  6395.             SGP_calculateSun (jtime)   
  6396.         until (sgp_sun.ele >= 0)   
  6397.     end
  6398.    
  6399.     SGP_calculateSun (sgp_time.ujtime)
  6400.    
  6401.     sgp_sun.raise.jt = SGP_UTC_to_Local(jtime) 
  6402.     sgp_sun.raise.year , sgp_sun.raise.month, sgp_sun.raise.day, sgp_sun.raise.hour, sgp_sun.raise.min, sgp_sun.raise.sec = SGP_Julian_To_Date(sgp_sun.raise.jt)
  6403. end
  6404.  
  6405.  
  6406. function SGP_calculateMoonRaiseTime()
  6407.     SGP_getCurrentTime()
  6408.     local jtime = sgp_time.ujtime
  6409.     SGP_calculateMoon (jtime)
  6410.  
  6411.     if sgp_moon.ele > 0 then
  6412.         repeat
  6413.             jtime = jtime - 0.000694446 * 10
  6414.             SGP_calculateMoon (jtime)
  6415.         until (sgp_moon.ele < 0)
  6416.         repeat
  6417.             jtime = jtime + 0.000694446
  6418.             SGP_calculateMoon (jtime)  
  6419.         until (sgp_moon.ele > 0)
  6420.         repeat
  6421.             jtime = jtime - 0.000694446 * 0.16
  6422.             SGP_calculateMoon (jtime)  
  6423.         until (sgp_moon.ele <= 0)
  6424.     else
  6425.         repeat
  6426.             jtime = jtime + 0.000694446 * 10
  6427.             SGP_calculateMoon (jtime)
  6428.         until (sgp_moon.ele > 0)
  6429.         repeat
  6430.             jtime = jtime - 0.000694446
  6431.             SGP_calculateMoon (jtime)  
  6432.         until (sgp_moon.ele < 0)
  6433.         repeat
  6434.             jtime = jtime + 0.000694446 * 0.16
  6435.             SGP_calculateMoon (jtime)  
  6436.         until (sgp_moon.ele >= 0)  
  6437.     end
  6438.    
  6439.     SGP_calculateMoon (sgp_time.ujtime)
  6440.    
  6441.     sgp_moon.raise.jt = SGP_UTC_to_Local(jtime)
  6442.     sgp_moon.raise.year , sgp_moon.raise.month, sgp_moon.raise.day, sgp_moon.raise.hour, sgp_moon.raise.min, sgp_moon.raise.sec = SGP_Julian_To_Date(sgp_moon.raise.jt)
  6443. end
  6444.  
  6445.  
  6446. function SGP_calculateSunSetTime()
  6447.     SGP_getCurrentTime()
  6448.     local jtime = sgp_time.ujtime
  6449.     SGP_calculateSun (jtime)
  6450.  
  6451.     if sgp_sun.ele < 0 then
  6452.         repeat
  6453.             jtime = jtime - 0.000694446 * 10
  6454.             SGP_calculateSun (jtime)
  6455.         until (sgp_sun.ele > 0)
  6456.         repeat
  6457.             jtime = jtime + 0.000694446
  6458.             SGP_calculateSun (jtime)   
  6459.         until (sgp_sun.ele < 0)
  6460.         repeat
  6461.             jtime = jtime - 0.000694446 * 0.16
  6462.             SGP_calculateSun (jtime)   
  6463.         until (sgp_sun.ele >= 0)
  6464.     else
  6465.         repeat
  6466.             jtime = jtime + 0.000694446 * 10
  6467.             SGP_calculateSun (jtime)
  6468.         until (sgp_sun.ele < 0)
  6469.         repeat
  6470.             jtime = jtime - 0.000694446
  6471.             SGP_calculateSun (jtime)   
  6472.         until (sgp_sun.ele > 0)
  6473.         repeat
  6474.             jtime = jtime + 0.000694446 * 0.16
  6475.             SGP_calculateSun (jtime)   
  6476.         until (sgp_sun.ele <= 0)   
  6477.     end
  6478.    
  6479.     SGP_calculateSun (sgp_time.ujtime)
  6480.    
  6481.     sgp_sun.set.jt = SGP_UTC_to_Local(jtime)   
  6482.     sgp_sun.set.year , sgp_sun.set.month, sgp_sun.set.day, sgp_sun.set.hour, sgp_sun.set.min, sgp_sun.set.sec = SGP_Julian_To_Date(sgp_sun.set.jt)
  6483. end
  6484.  
  6485. function SGP_calculateMoonSetTime()
  6486.     SGP_getCurrentTime()
  6487.     local jtime = sgp_time.ujtime
  6488.     SGP_calculateMoon (jtime)
  6489.  
  6490.     if sgp_moon.ele < 0 then
  6491.         repeat
  6492.             jtime = jtime - 0.000694446 * 10
  6493.             SGP_calculateMoon (jtime)
  6494.         until (sgp_moon.ele > 0)
  6495.         repeat
  6496.             jtime = jtime + 0.000694446
  6497.             SGP_calculateMoon (jtime)  
  6498.         until (sgp_moon.ele < 0)
  6499.         repeat
  6500.             jtime = jtime - 0.000694446 * 0.16
  6501.             SGP_calculateMoon (jtime)  
  6502.         until (sgp_moon.ele >= 0)
  6503.     else
  6504.         repeat
  6505.             jtime = jtime + 0.000694446 * 10
  6506.             SGP_calculateMoon (jtime)
  6507.         until (sgp_moon.ele < 0)
  6508.         repeat
  6509.             jtime = jtime - 0.000694446
  6510.             SGP_calculateMoon (jtime)  
  6511.         until (sgp_moon.ele > 0)
  6512.         repeat
  6513.             jtime = jtime + 0.000694446 * 0.16
  6514.             SGP_calculateMoon (jtime)  
  6515.         until (sgp_moon.ele <= 0)  
  6516.     end
  6517.    
  6518.     SGP_calculateMoon (sgp_time.ujtime)
  6519.    
  6520.     sgp_moon.set.jt = SGP_UTC_to_Local(jtime)  
  6521.     sgp_moon.set.year , sgp_moon.set.month, sgp_moon.set.day, sgp_moon.set.hour, sgp_moon.set.min, sgp_moon.set.sec = SGP_Julian_To_Date(sgp_moon.set.jt)
  6522. end
  6523.  
  6524.  
  6525. function SGP_calculateMoonPhases ()
  6526.  
  6527.     SGP_getCurrentTime();
  6528.    
  6529.     local day = sgp_time.day
  6530.     local mon = sgp_time.month
  6531.     local year = sgp_time.year
  6532.    
  6533.     local s
  6534.     local k, T, T2, T3, RAD, M, M1, F, AJ, BJ, CJ
  6535.     local JD
  6536.    
  6537.  
  6538.    
  6539.     for s=0,3 do
  6540.         k=math.floor((year+((mon+(day/30.0)-1.0)/12.0)-1900.0)*12.3685+0.5)
  6541.         k=k+0.25*s
  6542.         T=(k/1236.85)
  6543.         T2=T*T
  6544.         T3=T2*T
  6545.         RAD=57.29577951
  6546.         -- Tiempo para las fases medias
  6547.         JD=2415020.75933+29.53058868*k+0.0001178*T2-0.000000155*T3+0.00033*math.sin((166.56+132.87*T-0.009173*T2)/RAD)
  6548.         -- Anomalias mediasde Sol y Luna y arg. latitud
  6549.         M=359.2242+29.10535608*k-0.0000333*T2-0.00000347*T3
  6550.         M1=306.0253+385.81691806*k+0.0107306*T2+0.00001236*T3
  6551.         F=21.2964+390.67050646*k-0.0016528*T2-0.00000239*T3
  6552.         M=M/RAD
  6553.         M1=M1/RAD
  6554.         F=F/RAD
  6555.         -- Correción para Luna Nueva y Llena
  6556.         AJ=(0.1734-0.000393*T)*math.sin(M)+0.0021*math.sin(2*M)-0.4068*math.sin(M1)+0.0161*math.sin(2*M1)
  6557.         AJ=AJ-0.0004*math.sin(3*M1)+0.0104*math.sin(2*F)-0.0051*math.sin(M+M1)-0.0074*math.sin(M-M1)
  6558.         AJ=AJ+0.0004*math.sin(2*F+M)-0.0004*math.sin(2*F-M)-0.0006*math.sin(2*F+M1)+0.0010*math.sin(2*F-M1)
  6559.         AJ=AJ+0.0005*math.sin(M+2*M1)
  6560.         -- Correción para Cuarto creciente y menguante
  6561.         BJ=(0.1721-0.0004*T)*math.sin(M)+0.0021*math.sin(2*M)-0.6280*math.sin(M1)+0.0089*math.sin(2*M1)
  6562.         BJ=BJ-0.0004*math.sin(3*M1)+0.0079*math.sin(2*F)-0.0119*math.sin(M+M1)-0.0047*math.sin(M-M1)
  6563.         BJ=BJ+0.0003*math.sin(2*F+M)-0.0004*math.sin(2*F-M)-0.0006*math.sin(2*F+M1)+0.0021*math.sin(2*F-M1)
  6564.         BJ=BJ+0.0003*math.sin(M+2*M1)+0.0004*math.sin(M-2*M1)-0.0003*math.sin(2*M+M1)
  6565.         CJ=0.0028-0.0004*math.cos(M)+0.0003*math.cos(M1)
  6566.         if s==0 then
  6567.             sgp_moon.new_moon=JD+AJ
  6568.         end
  6569.         if s==1 then
  6570.             sgp_moon.first_quarter_moon=JD+BJ+CJ
  6571.         end
  6572.         if s==2 then
  6573.             sgp_moon.full_moon=JD+AJ
  6574.         end
  6575.         if s==3 then
  6576.             sgp_moon.third_quarter_moon=JD+BJ-CJ
  6577.         end
  6578.  
  6579.     end
  6580. end
  6581.  
  6582.  
  6583. function SGP_calculateNightEnding()
  6584.     SGP_getCurrentTime()
  6585.     local jtime = sgp_time.ujtime
  6586.     SGP_calculateSun (jtime)
  6587.  
  6588.     if sgp_sun.ele > const_twilightCivil then
  6589.         repeat
  6590.             jtime = jtime - 0.000694446 * 10
  6591.             SGP_calculateSun (jtime)
  6592.         until (sgp_sun.ele < const_twilightCivil)
  6593.         repeat
  6594.             jtime = jtime + 0.000694446
  6595.             SGP_calculateSun (jtime)   
  6596.         until (sgp_sun.ele > const_twilightCivil)
  6597.         repeat
  6598.             jtime = jtime - 0.000694446 * 0.16
  6599.             SGP_calculateSun (jtime)   
  6600.         until (sgp_sun.ele <= const_twilightCivil)
  6601.     else
  6602.         repeat
  6603.             jtime = jtime + 0.000694446 * 10
  6604.             SGP_calculateSun (jtime)
  6605.         until (sgp_sun.ele > const_twilightCivil)
  6606.         repeat
  6607.             jtime = jtime - 0.000694446
  6608.             SGP_calculateSun (jtime)   
  6609.         until (sgp_sun.ele < const_twilightCivil)
  6610.         repeat
  6611.             jtime = jtime + 0.000694446 * 0.16
  6612.             SGP_calculateSun (jtime)   
  6613.         until (sgp_sun.ele >= const_twilightCivil) 
  6614.     end
  6615.    
  6616.     SGP_calculateSun (sgp_time.ujtime)
  6617.    
  6618.     sgp_location.day.jt = SGP_UTC_to_Local(jtime)  
  6619.     sgp_location.day.year , sgp_location.day.month, sgp_location.day.day, sgp_location.day.hour, sgp_location.day.min, sgp_location.day.sec = SGP_Julian_To_Date(sgp_location.day.jt)
  6620. end
  6621.  
  6622.  
  6623. function SGP_calculateNightBegining()
  6624.     SGP_getCurrentTime()
  6625.     local jtime = sgp_time.ujtime
  6626.     SGP_calculateSun (jtime)
  6627.  
  6628.     if sgp_sun.ele < const_twilightCivil then
  6629.         repeat
  6630.             jtime = jtime - 0.000694446 * 10
  6631.             SGP_calculateSun (jtime)
  6632.         until (sgp_sun.ele > const_twilightCivil)
  6633.         repeat
  6634.             jtime = jtime + 0.000694446
  6635.             SGP_calculateSun (jtime)   
  6636.         until (sgp_sun.ele < const_twilightCivil)
  6637.         repeat
  6638.             jtime = jtime - 0.000694446 * 0.16
  6639.             SGP_calculateSun (jtime)   
  6640.         until (sgp_sun.ele >= const_twilightCivil)
  6641.     else
  6642.         repeat
  6643.             jtime = jtime + 0.000694446 * 10
  6644.             SGP_calculateSun (jtime)
  6645.         until (sgp_sun.ele < const_twilightCivil)
  6646.         repeat
  6647.             jtime = jtime - 0.000694446
  6648.             SGP_calculateSun (jtime)   
  6649.         until (sgp_sun.ele > const_twilightCivil)
  6650.         repeat
  6651.             jtime = jtime + 0.000694446 * 0.16
  6652.             SGP_calculateSun (jtime)   
  6653.         until (sgp_sun.ele <= const_twilightCivil) 
  6654.     end
  6655.    
  6656.     SGP_calculateSun (sgp_time.ujtime)
  6657.    
  6658.     sgp_location.night.jt = SGP_UTC_to_Local(jtime)
  6659.     sgp_location.night.year , sgp_location.night.month, sgp_location.night.day, sgp_location.night.hour, sgp_location.night.min, sgp_location.night.sec = SGP_Julian_To_Date(sgp_location.night.jt)
  6660. end
  6661.  
  6662.  
  6663.  
  6664.  
  6665. -- COMIENZO DEL PROGRAMA - MAIN ()
  6666.  
  6667. SF_initialize()
  6668.  
  6669. -- Calculamos otras informaciones (fases lunares, amaneceres y ocasos...)
  6670. SF_clacularOtrosDatos()
  6671.  
  6672. sf_timer = timer.new()
  6673.  
  6674. while true do
  6675.     -- Obtenemos el tiempo actual
  6676.     SGP_getCurrentTime()   
  6677.  
  6678.     -- Guardamos el valor anterior del tiempo
  6679.     if sf_primer_calculo then
  6680.         sf_current_jtime_ant = sgp_time.jtime
  6681.         sf_current_jtime = sgp_time.jtime
  6682.     else
  6683.         sf_current_jtime_ant = sf_current_jtime
  6684.         sf_current_jtime = sgp_time.jtime
  6685.     end
  6686.    
  6687.     -- Iniciamos el timer que mide el tiempo empleado en calculos
  6688.     sf_timer:start()
  6689.    
  6690.     --COMIENZA CALCULO
  6691.  
  6692.     -- Borramos el lienzo que se sacará a pantalla
  6693.     screen.clear(sf_sky.color)
  6694.    
  6695.     -- Dibujamos las lineas y texto de referencia
  6696.     SF_dibuarLineasReferencia()
  6697.    
  6698.     -- Dibujamos las constelacioens
  6699.     SF_procesarConstelaciones(sgp_time.ujtime)
  6700.    
  6701.     -- Dibujamos estrellas
  6702.     SF_procesarEstrellas(sgp_time.ujtime)
  6703.  
  6704.     -- Dibujamos el Sol
  6705.     SF_procesarSol(sgp_time.ujtime)
  6706.    
  6707.     -- Dibujamos la Luna
  6708.     SF_procesarLuna(sgp_time.ujtime)
  6709.  
  6710.     -- Dibujamos los satélites artificiales
  6711.     SF_procesarSatelites(sgp_time.ujtime)
  6712.    
  6713.     if sf_r_pressed and not sf_zoom_mode then
  6714.         SF_drawLocationInfo ()
  6715.         SF_drawTimeInfo ()
  6716.         SF_drawPSPInfo ()
  6717.     end
  6718.     -- Cogemos el tiempo invertido
  6719.     sf_milis_calculos = sf_timer:stop()
  6720.     sf_timer:reset()   
  6721.    
  6722.     -- Guardamos fotogramas por segundo
  6723.     sf_fps = 1000.0 / sf_milis_calculos
  6724.     --screen.print(0, 0, string.format ("%.2f",sf_fps) .. " fps - "..string.format ("%.1f",sgp_mag_limit).." magnitud - sat ".. cursor_satelite.."  star ".. cursor_stars , sf_color_white)
  6725.    
  6726.     if not sf_r_pressed then
  6727.         SF_drawInfoBar(true)
  6728.     end
  6729.    
  6730.     -- Esperamos al refresco vertical
  6731.     screen.waitvblankstart()
  6732.     -- Pintar lienzo en pantalla
  6733.     screen.flip()
  6734.  
  6735.     -- Reproducir voces de aviso si hay que avisar de algo
  6736.     SND_playSoundsIfNeeded()   
  6737.    
  6738.     -- Vemos si hay alguna tecla pulsada y actuamos en consecuencia
  6739.     SF_readInput()
  6740.    
  6741.  
  6742.    
  6743.     -- Si terminamos el primer calculo, establecer que ya no estamos en el primer calculo
  6744.     if sf_primer_calculo then          
  6745.         -- Cambiamos a la paleta adecuada a las condiciones del cielo.
  6746.         if sgp_location.noche then
  6747.             sf_vision_nocturna = true
  6748.             SF_changePalette (sf_color_night.r,sf_color_night.g,sf_color_night.b)
  6749.         else
  6750.             sf_vision_nocturna = false
  6751.             SF_changePalette (sf_color_day.r,sf_color_day.g,sf_color_day.b)
  6752.         end
  6753.         sf_primer_calculo = false
  6754.     end    
  6755.    
  6756. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement