Changeset 14689 for trunk/FACT++/src


Ignore:
Timestamp:
11/25/12 12:01:03 (12 years ago)
Author:
tbretz
Message:
Added functions to calculate Sun and Moon rise and set times.
Location:
trunk/FACT++/src
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/FACT++/src/InterpreterV8.cc

    r14688 r14689  
    1010
    1111#ifdef HAVE_NOVA
     12#include <libnova/solar.h>
    1213#include <libnova/lunar.h>
    1314#include <libnova/transform.h>
     
    15171518    return handle_scope.Close(self);
    15181519}
     1520
     1521Handle<Object> InterpreterV8::ConstructRiseSet(const Handle<Value> time, const ln_rst_time &rst)
     1522{
     1523    Handle<Object> obj = Object::New();
     1524    obj->Set(String::New("time"), time, ReadOnly);
     1525    if (rst.transit<0)
     1526        return obj;
     1527
     1528    Handle<Value> rise  = Date::New(Time(rst.rise).JavaDate());
     1529    Handle<Value> set   = Date::New(Time(rst.set).JavaDate());
     1530    Handle<Value> trans = Date::New(Time(rst.transit).JavaDate());
     1531    if (rise.IsEmpty() || set.IsEmpty() || trans.IsEmpty())
     1532        return Handle<Object>();
     1533
     1534    obj->Set(String::New("rise"), rise, ReadOnly);
     1535    obj->Set(String::New("set"),  set, ReadOnly);
     1536    obj->Set(String::New("transit"), trans, ReadOnly);
     1537    return obj;
     1538}
     1539
     1540Handle<Value> InterpreterV8::SunHorizon(const Arguments &args)
     1541{
     1542    if (args.Length()>2)
     1543        return ThrowException(String::New("Sun.horizon must not be called with one or two arguments."));
     1544
     1545    HandleScope handle_scope;
     1546
     1547    double hrz = NAN;
     1548    if (args.Length()<2)
     1549        hrz = LN_SOLAR_STANDART_HORIZON;
     1550    if (args.Length()==2 && args[1]->IsNumber())
     1551        hrz = args[1]->NumberValue();
     1552    if (args.Length()==2 && args[1]->IsString())
     1553    {
     1554        char *arg = *String::AsciiValue(args[1]);
     1555        transform(arg, arg+strlen(arg), arg, ::tolower);
     1556
     1557        const string str = Tools::Trim(arg);
     1558        if (str==string("civil").substr(0, str.length()))
     1559            hrz = LN_SOLAR_CIVIL_HORIZON;
     1560        if (str==string("nautic").substr(0, str.length()))
     1561            hrz = LN_SOLAR_NAUTIC_HORIZON;
     1562        if (str==string("astronomical").substr(0, str.length()))
     1563            hrz = LN_SOLAR_ASTRONOMICAL_HORIZON;
     1564    }
     1565
     1566    if (!finite(hrz))
     1567        return ThrowException(String::New("Second argument did not yield a valid number."));
     1568
     1569    const Local<Value> date =
     1570        args.Length()==0 ? Date::New(Time().JavaDate()) : args[0];
     1571    if (date.IsEmpty())
     1572        return Undefined();
     1573
     1574    const uint64_t v = uint64_t(date->NumberValue());
     1575    const Time utc(v/1000, v%1000);
     1576
     1577    ln_lnlat_posn obs;
     1578    obs.lng = -(17.+53./60+26.525/3600);
     1579    obs.lat =   28.+45./60+42.462/3600;
     1580
     1581    // get Julian day from local time
     1582    const double JD = utc.JD();
     1583
     1584    ln_rst_time sun;
     1585    const bool rc = ln_get_solar_rst_horizon(JD-0.5, &obs, hrz, &sun);
     1586    if (rc)
     1587        sun.transit = -1;
     1588
     1589    Handle<Object> rst = ConstructRiseSet(date, sun);
     1590    rst->Set(String::New("horizon"), Number::New(hrz));
     1591    return handle_scope.Close(rst);
     1592};
     1593
     1594Handle<Value> InterpreterV8::MoonHorizon(const Arguments &args)
     1595{
     1596    if (args.Length()>1)
     1597        return ThrowException(String::New("Moon.horizon must not be called with one argument."));
     1598
     1599    HandleScope handle_scope;
     1600
     1601    const Local<Value> date =
     1602        args.Length()==0 ? Date::New(Time().JavaDate()) : args[0];
     1603    if (date.IsEmpty())
     1604        return Undefined();
     1605
     1606    const uint64_t v = uint64_t(date->NumberValue());
     1607    const Time utc(v/1000, v%1000);
     1608
     1609    ln_lnlat_posn obs;
     1610    obs.lng = -(17.+53./60+26.525/3600);
     1611    obs.lat =   28.+45./60+42.462/3600;
     1612
     1613    // get Julian day from local time
     1614    const double JD = utc.JD();
     1615
     1616    ln_rst_time moon;
     1617    const bool rc = ln_get_lunar_rst(JD-0.5, &obs, &moon);
     1618
     1619    if (rc)
     1620        moon.transit = -1;
     1621
     1622    Handle<Object> rst = ConstructRiseSet(date, moon);
     1623    return handle_scope.Close(rst);
     1624};
    15191625#endif
    15201626
     
    17921898
    17931899    Handle<ObjectTemplate> dimctrl = ObjectTemplate::New();
    1794     dimctrl->Set(String::New("newState"), FunctionTemplate::New(WrapNewState),  ReadOnly);
    1795     dimctrl->Set(String::New("setState"),  FunctionTemplate::New(WrapSetState),  ReadOnly);
    1796     dimctrl->Set(String::New("getState"),  FunctionTemplate::New(WrapGetState),  ReadOnly);
     1900    dimctrl->Set(String::New("defineState"), FunctionTemplate::New(WrapNewState),  ReadOnly);
     1901    dimctrl->Set(String::New("setState"),    FunctionTemplate::New(WrapSetState),  ReadOnly);
     1902    dimctrl->Set(String::New("getState"),    FunctionTemplate::New(WrapGetState),  ReadOnly);
    17971903
    17981904    Handle<ObjectTemplate> v8 = ObjectTemplate::New();
     
    18521958    moon->SetClassName(String::New("Moon"));
    18531959    moon->Set(String::New("disk"), FunctionTemplate::New(MoonDisk), ReadOnly);
     1960    moon->Set(String::New("horizon"), FunctionTemplate::New(MoonHorizon), ReadOnly);
    18541961    global->Set(String::New("Moon"), moon, ReadOnly);
     1962
     1963    Handle<FunctionTemplate> sun = FunctionTemplate::New();
     1964    sun->SetClassName(String::New("Sun"));
     1965    sun->Set(String::New("horizon"), FunctionTemplate::New(SunHorizon), ReadOnly);
     1966    global->Set(String::New("Sun"), sun, ReadOnly);
    18551967
    18561968    fTemplateLocal = loc;
  • trunk/FACT++/src/InterpreterV8.h

    r14688 r14689  
    1717
    1818class Database;
     19
     20#ifdef HAVE_NOVA
     21struct ln_rst_time;
     22#endif
    1923
    2024class InterpreterV8
     
    8387    v8::Handle<v8::Value> OnChangeSet(v8::Local<v8::String>, v8::Local<v8::Value>, const v8::AccessorInfo &);
    8488
     89    static v8::Handle<v8::Value> Constructor(const v8::Arguments &args);
     90
     91#ifdef HAVE_NOVA
    8592    static double GetDataMember(const v8::Arguments &args, const char *name);
    86     static v8::Handle<v8::Value> Constructor(const v8::Arguments &args);
    8793
    8894    static v8::Handle<v8::Value> LocalToString(const v8::Arguments &args);
     
    97103    static v8::Handle<v8::Value> ConstructorSky(const v8::Arguments &args);
    98104    static v8::Handle<v8::Value> ConstructorLocal(const v8::Arguments &args);
     105    static v8::Handle<v8::Value> MoonHorizon(const v8::Arguments &args);
     106    static v8::Handle<v8::Value> SunHorizon(const v8::Arguments &args);
     107    static v8::Handle<v8::Object> ConstructRiseSet(const v8::Handle<v8::Value>, const ln_rst_time &);
     108#endif
    99109
    100110    static v8::Handle<v8::Value> WrapInclude(const v8::Arguments &args)  { if (This) return This->FuncInclude(args);  else return v8::Undefined(); }
Note: See TracChangeset for help on using the changeset viewer.