Wednesday, December 8, 2021

Creating and using a dynamic library from JPL toolkit

Generating a dynamic library

In a previous post I showed how to generate a dynamic library from jpl toolkit, for linux systems (libcspice.so) and for Windows (libcspice.dll).

Before we proceed to the compilation of the library in the Termux environment, let's talk of the programs that we have just compiled from sources launching the script importCSpice.csh.

Open Termux in your phone and activate ssh:

sshd (and press Enter, from now on I take it for granted)

In your PC open your terminal emulator (cmd on Windows, gnome-terminal or whatever in Linux) and type:

ssh u0_a119@192.168.1.120 -p 8022 (use your own coordinates, this is an example, see previous post)

when you are in, type:

~ $ ls -l
total 107956
drwx------ 9 u0_a119 u0_a119      4096 Apr  8  2017 cspice
-rw------- 1 u0_a119 u0_a119 110530560 Apr 10  2017 cspice.tar
-rw------- 1 u0_a119 u0_a119        84 Apr 10  2017 importCSpice.csh
drwx------ 2 u0_a119 u0_a119      4096 Dec  6 19:26 storage
~ $

the directory cspice has been created during the compilation process

it contains several subdirs:

~ $ ls cspice
data  doc  etc  exe  include  lib  makeall.csh  src
~ $

let's look inside cspice/exe

 $ ls cspice/exe
brief        ckbrief.exe   dskexp       inspekt.exe  msopck      spacit.exe    states      tictoc.exe  version
brief.exe    commnt        dskexp.exe   mkdsk        msopck.exe  spkdiff       states.exe  tobin       version.exe
chronos      commnt.exe    frmdiff      mkdsk.exe    simple      spkdiff.exe   subpt       tobin.exe
chronos.exe  dskbrief      frmdiff.exe  mkspk        simple.exe  spkmerge      subpt.exe   toxfr
ckbrief      dskbrief.exe  inspekt      mkspk.exe    spacit      spkmerge.exe  tictoc      toxfr.exe
~ $

all of these files are executables, but built for different systems. The ones without extension are used in aarch64 architecture, those ending with .exe are for Windows.

You can personally check the nature of these executables, but first you must install the package file:

pkg install file

now change to subdirectory cspice/exe:

cd cspice/exe

and type file brief

~/cspice/exe $ file brief
brief: ELF 64-bit LSB pie executable, ARM aarch64, version 1 (SYSV), dynamically linked, interpreter /system/bin/linker64, not stripped
~/cspice/exe $

this executable is one of our interest. We'll make a good use of it later. If you type file brief.exe instead:

~/cspice/exe $ file brief.exe
brief.exe: PE32+ executable (console) x86-64, for MS Windows
~/cspice/exe $

this exe file won't run in your phone, it's generated for cgywin, which is a posix compatible programming environment for Windows systems.

Just for curiosity, launch brief:

~/cspice/exe $ ./brief
WARNING: linker: /data/data/com.termux/files/home/cspice/exe/brief: unsupported flags DT_FLAGS_1=0x8000001

BRIEF -- Version 4.0.0, September 8, 2010 -- Toolkit Version N0066

   BRIEF is a command-line utility program that displays a summary for
   one or more binary SPK or binary PCK files. The program usage is:

      % brief [-options] file [file ...]

   The most useful options are shown below. For the complete set of
   options, run BRIEF with the -h option. The order of options is not
   significant. The case of option keys is significant: they must be
   lowercase as shown below.

      -c           display centers of motion/relative-to frames
      -t           display summary in a tabular format
      -a           treat all files as a single file
      -utc         display times in UTC calendar date format (needs LSK)
      -utcdoy      display times in UTC day-of-year format (needs LSK)
      -etsec       display times as ET seconds past J2000

   An LSK file must be provided on the command line to display times in
   UTC formats. FK file(s) must be provided on the command line to
   display names of any frames that are not built into the Toolkit.

~/cspice/exe $

Let's proceed to the creation of a dynamic library.

Change directory to cspice/src/cspice

~/cspice/src $ cd
~ $ cd cspice/src/cspice
~/.../src/cspice $ ls
F77_aloc.c     daftb.c     et2lst_c.c         lnknxt.c      rav2xf_c.c  spkr15.c       vnorm_c.c     zzeknres.c
SpiceCK.h      dafus_c.c   et2utc.c           lnkprv.c      rawio.h     spkr17.c       vnormg.c      zzeknrml.c
SpiceCel.h     dafwcr.c    et2utc_c.c         lnksiz.c      raxisa.c    spkr18.c       vnormg_c.c    zzekordc.c
... a lot of files ...
dafrfr_c.c     errint_c.c  lnkfsl.c           r_sqrt.c      spkr08.c    vlcomg_c.c     zzeklled.c
dafrrr.c       errprt.c    lnkhl.c            r_tan.c       spkr09.c    vminug.c       zzekllei.c
dafrs_c.c      errprt_c.c  lnkila.c           r_tanh.c      spkr10.c    vminug_c.c     zzeklltc.c
dafrwa.c       esrchc.c    lnkilb.c           radrec.c      spkr12.c    vminus.c       zzeklltd.c
dafrwd.c       esrchc_c.c  lnkini.c           radrec_c.c    spkr13.c    vminus_c.c     zzekllti.c
daft2b.c       et2lst.c    lnknfn.c           rav2xf.c      spkr14.c    vnorm.c        zzekmloc.c
~/.../src/cspice $

It's a huge amount of C sources, we need to compile them in a standard .so dynamic library.

within the same directory type:

gcc -Wall -O2 -fPIC -shared -o libcspice.so *.c -lm -ldl

You deserve a brief explanation of the options I have given to the compiler:

gcc is the compiler, here used as assembler, compiler and linker altogether, any further consideration is beyond our scope.

-Wall stands for the maximum warning alert level, during compilation you'll see a ton of warnings.

-O2 is the optimization level, set to a good level compatible with robustness.

-fPIC -shared is the combination of two instructions aimed at generating a shared library with Position Independent Code (PIC)

-o libcspice.so is the name I have choosen for the compiled library, "lib" and ".so" are the structural parts.

*.c are all the C sources that are present in this directory

-lm -ldl are linked libraries, math and dynamic loader

After some time and a big amount of warnings, hopefully no errors found, you'll see a new file in your directory, libcspice.so. Move it to your home directory, to have it handy

mv libcspice.so ~/

Ok, it's enough for now, see you in the next post

Tuesday, December 7, 2021

JPL Toolkit on Termux in a cheap chinese smartphone

My laziness is unforgivable. Even if I rarely have some interesting news to offer to readers (as you can see from the following lines, I'm still deeply interested in exploring basic planetary position calculus from an astrological point of view, and I have already talked a lot about JPL toolkit), once in a while I feel the urge to communicate what I have learnt to anyone who can be find some interest in it. Something has changed, however. First of all, I'm going to write next posts in plain English because most of my readers are from abroad, google translator does a good job, but anyway ...

Another important novelty is the fact that I have totally abandoned Python 2, because it is deprecated and Python ver 3 is much richer and interesting. Most of my post in the past utilized the versions 2, I'll try to upgrade them every time it's necessary. But let's talk again about the JPL toolkit, which is the de facto standard for astronomical calculations. Many astrological softwares are based upon the Swiss Ephemeris, which are C libraries that make use of the jpl ephemeris, so I think it's better go with the original, as the development of software with the Swisseph is free only for non commercial products. As you could appreciate from my past posts, building a shared library from the C NASA routines is relatively straightforward, so why not use it? Let's recapitulate the fundamentals, and then let's implement the necessary routines on a specific debian-like platform, named TERMUX, which is usable on most cheap smartphones (mine is Meizu M5 Note, 64bit Arm Android 7 based, rather old phone, but it's fine for my purposes). No need to root your device, you can use it as it is, so you won't have any problem with your bank application or other institutional ones.

Main installation

To install Termux, forget about Google store, the app is unmantained. Install F-Droid app on your Android phone, than download the Termux apk from the F-Droid repository, it's easily recognizable from its icon:

Now launch the application from your screen. You will be welcomed to Termux and you'll see something like this photo:

The keyboard you see in the photo is the Hacker's Keyboard, you can install it from Play Store, but feel free to use the one you feel most comfortable with

Now let's do a few maintaining tasks. At prompt type:

pkg install root-repo
apt update
apt upgrade -y
apt install build-essential binutils wget openssh

N.B.: all four arguments in one line

build-essential and binutils are the minimum tools required to perform compilation and linking, wget is aimed at downloading files from the web, openssh is needed to install a ssh server on our phone.

If you see an error message like "The following signatures couldn't be verified because the public key is not available", don't worry. Write the command termux-change-repo, press enter and select the main repository, than the default repository or any one in the list, than repeat the commands.

To have access to the phone memory, use the following command:

termux-setup-storage and press enter

in your home directory, type ls and you'll see a new storage subdirectory, which contains the access to the main directories used by your phone during normal usage, the most important is storage/downloads. We'll use it later

Create a ssh connection from PC

As you may have ascertained, it's rather difficult to use a phone keyboard. To make this stuff easy, let's give access to Termux to your computer.

If you have installed, as in my previous instructions, the package openssh, it's a matter of seconds activating a ssh access to your phone. Type sshd and press enter. Remember: by default the ssh port in Termus is 8022, not 22.

Now you must find your phone coordinates: type whoami and press enter, this give you your identity, mine, in this exact moment, is u0_a119.

Create a password for ssh: type passwd and press enter. You will be prompted to type a password, then to retype it. Don't create a read only password, if you are prompted to do so.

Connect your phone to your home LAN, and get its address by typing ip addr and pressing enter. Under a long list of devices, you'll find and address like 192.168.1.120 or similar, the most important part are the first two numbers 192.168. Write the full address down, it's your ip coordinate in the LAN.

Now in your PC type in a terminal emulator (cmd in Windows, any terminal available in Linux or Mac): ssh u0_a119@192.168.1.120 -p 8022. Of course, change the argument to represent your real coordinates. If all is fine, you'll be within your phone home directory. You'll receive a warm welcome message, identical to the one you have already seen opening Termux. Hooray! Every new command you'll be typing in your PC terminal, will be executed in your phone memory.

Get the right JPL source

From your browser go to the following web page:

NAIF toolkit C sources.

Browse the following link:

PC, CYGWIN, gCC, 64bit

A new page will open, copy the link address of cspice.tar.gz (if you don't know how to do right click the link and copy the address, not the string).

Then in the Termux terminal you just opened type wget <paste here your link address>

After the download has finished (it will take a while), you will find a cspice.tar.gz file in your home directory

Same procedure for the file importCspice.csh, until you see it in your directory next to the .gz file

Now install tcsh, it's the shell you need to perform some fundamental operations. Type in your terminal apt install tcsh and press enter

Almost done. Unzip your cspice.tar.gz with gunzip cspice.tar.gz, your file is now named cspice.tar

Type tcsh importCSpice.csh and let the script do its job, it will take some time, ignore warnings, and wait until you see the message Toolkit Build Complete.

Where do we go from here?

As I told you a few years ago, the just finished compilation produces a lot of useful function to work with ephemeris files and a static library, not exactly the one we need.

In the upcoming posts we will talk about the creation of a dynamic C library and the linking of it in a basic C program, and how to bind it from a python3 script. In the following posts we will find a way to use these new libraries in a graphical setting, something similar to a true Android application. Let me know if the procedures described in this page are good for you or if you need some help. See you soon ...

How to create a virtual linux machine with qemu under Debian or Ubuntu with near native graphics performance

It's been a long time since my latest post. I know, I'm lazy. But every now and then I like to publish something that other people c...