Tuesday, June 21, 2016

Installing IRAM GILDAS on Ubuntu (2016/06)

What is IRAM GILDAS? See here : http://www.iram.fr/IRAMFR/GILDAS/

NOTE: Updated for Ubuntu 16.04 Xenial Xerus.
Nowadays the readme file on the IRAM webpage gives pretty good instructions for Linux as well: http://www.iram.fr/~gildas/dist/gildas.README

First download the source code from

named "gildas-src-mmmyya.tar.gz" where "mmm" is month e.g. Oct, and "yy" is year e.g. 16 and "a" is some letter which signifies the release number, starting with a.

Unpack and enter the directory (it will be installed in the same directory as the unpacked directory, but with "exe" instead of "src" in the name):
tar -xvzf gildas-src-mmmyya.tar
cd gildas-src-mmmyya
For the new GNOME3 framework, we still need to download libgtk2.0. Hence the dependencies are (including optional dependencies):
sudo apt install gfortran g++ libgtk2.0-dev python-dev python-numpy libblas-dev liblapack-dev libfftw3-dev libcfitsio3-dev

export GAG_SEARCH_PATH="/usr/lib:/usr/lib/x86_64-linux-gnu"
export gagexedir=/home/magnusp/applications/gildas-current

Then configure environment variables and similar with the bash script:

source admin/gildas-env.sh -s /usr/lib:/usr/lib/x86_64-linux-gnu

Note: the -s flag with "/usr/lib:/usr/lib/x86_64-linux-gnu" is to add more search paths so that it find CFITSIO and PNG libraries

Here you probably get one warning, something related to some ALMA holography thing, no worries you wont need it. If you do get some other warnings, resolve those dependencies first. After this just run 

this takes a couple of minutes. Then run
make install

to finally install GILDAS. This will install it in another directory, with "exe" instead. You can move this directory some place else if you want.

Now, to set the environment variables you get a message when the "make install" script finishes, you get a message of what to add, e.g.,:
export GAG_ROOT_DIR=/path-to-gildas/gildas-exe-MMMYYa
export GAG_EXEC_SYSTEM=x86_64-ubuntu13.10-gfortran
source $GAG_ROOT_DIR/etc/bash_profile
NB: The part that says "x86_64-ubuntuXX.YY-gfortran" will be specific for your system, e.g. for the latest Ubuntu this should be "x86_64-ubuntu16.04-gfortran".
Just add the statement printed out last in in make install to the end of your .bashrc or .bashrc_profile file (e.g., "nano ~/.bashrc" or "nano ~/.bash_profile").

To remove the start-up message i.e. the one that says something like this,
Selecting GILDAS version: mmYYa (21jun16 09:46 cest), executable tree, x86_64-ubuntu16.04-gfortran

every time you start a terminal you have to comment out the following lines in the file /path-to-gildas/gildas-exe-MMMYYa/etc/bash_profile :
if [ -n "$PS1" ]; then
    echo "Selecting GILDAS version: mmYYa (21jun16 09:46 cest), executable tree, ${GAG_EXEC_SYSTEM}"

that is, put hash-tags (#) in front of all the lines. 
Done! Now to run a GILDAS program just fire up the terminal and run whatever GILDAS program you want: mapping, class, astro, clic.

Wednesday, June 15, 2016

Screen brightness using Fn keys in i3wm on Lenovo Thinkpad X260

Note: this is a first draft, I just want to jot down what  I currently have.

I got a new laptop for work, Lenovo Thinkpad X260. Installed Ubuntu 16.04 Xenial Xerus without much problem.
There are several Fn-hotkeys for the F1-F12 keys, most of them do not work out of the box in Ubuntu, at least not when running i3-window manager.
I managed to solve it in the end. This is how. Part of this solution is from
Thanks for that!

Changing the brightness

I figured out that the brightness can be changed with

$ sudo echo VALUE > /sys/class/backlight/intel_backlight/brightness

where VALUE is between 0 and what is given by the output

$ cat /sys/class/backlight/intel_backlight/max_brightness

These paths might be different for you, so you have to figure it out, and edit the paths in the script below to fit your needs.

The script

So I created a script that changes the brightness, as mentioned it borrows heavily from the above mentioned page, but there are some changes.

I save it as brightness_control in some place, perhaps in your bin folder or something.
The script is as follows:


# script to control the brightness of a Lenovo Thinkpad X260
# in i3wm (Ubuntu Linux)
# Magnus Persson, with help from 
# http://ttrmw.co.uk/linux/hardware_button_brightness.html

# how much we change the brightness is the input parameter
echo $change

# get the maximum brightness value
max_brightness=$(cat /sys/class/backlight/intel_backlight/max_brightness)

echo $max_brightness
# get the current brightness
brightness=$(cat /sys/class/backlight/intel_backlight/brightness)
echo $brightness

# calculate the new value that is requested.
# if input is negative it will subtract (?)
new_value=$(($brightness + $change))
echo $new_value

# if the new value is less than 1, we just set it to 1
# 0 is completely pitch black, just put pc to sleep
# if you want that.
if (( $new_value < 10 )); then  
        let brightness=10
# if the new value is greater than max brightness, set it to max brightness
elif  (($new_value > $max_brightness)); then
        let brightness=$max_brightness
# if none of the above if-statements are true, just set it to the 
# new brightness value
        let brightness=$new_value

# now we can just echo the value into the brightness acpi(?) file
echo $brightness > /sys/class/backlight/intel_backlight/brightness #| bash #or zsh, csh or whateva

As you might see the main difference from the linked source, it takes an input argument, how much to lower or increase the brightness, no need for several scripts for up/down in brightness. If you do not have so many levels, i.e. if the output of the max brightness is very low, you have to change the value "10" that I have in the script, it is just to set the lowest possible level, so the screen wont go completely black because the brightness goes down to 0. Be careful.

To run the program it needs super user (sudo) privileges. So to run it without it we need to add it to the visudo file. To do this, run

$ sudo visudo 

and add, to the end of the file

your_user your_machine = NOPASSWD: /path/to/brightness_control

Now this makes it possible to run the script as before, but it wont ask for a password
(i.e. $ sudo ./brightness_control -400, to lower the brightness 400 units )

HOWEVER, this is a bit un-secure because if anyone edits your file, they can run whatever they want!

Give your file to root

Soooo we just give it to the root user

$ sudo chown root:root brightness_control


$ sudo chmod 0711 brightness_control

now you can run the script without filling in the password, but to edit it you need to put the sudo password in.

i3 window manager

Now you can start xev and klick your brightness up/down keys (with Fn), or you run

$ xmodmap -pk | grep Brightness

For me it shows

    232     0x1008ff03 (XF86MonBrightnessDown) 0x0000 (NoSymbol) 0x1008ff03 (XF86MonBrightnessDown)
    233     0x1008ff02 (XF86MonBrightnessUp) 0x0000 (NoSymbol) 0x1008ff02 (XF86MonBrightnessUp)
    237     0x1008ff06 (XF86KbdBrightnessDown) 0x0000 (NoSymbol) 0x1008ff06 (XF86KbdBrightnessDown)
    238     0x1008ff05 (XF86KbdBrightnessUp) 0x0000 (NoSymbol) 0x1008ff05 (XF86KbdBrightnessUp)

Here Mon is for monitor, and Kbd for keyboard (the backlit keyboard).

Now I have in my $HOME/.config/i3/config file

# Brightness control (without any fuzz)
bindsym XF86MonBrightnessUp exec --no-startup-id sudo /home/magnusp/scripts/brightness_control +100
bindsym XF86MonBrightnessDown exec --no-startup-id sudo /home/magnusp/scripts/brightness_control -100

Here you can change the number, "100" to whatever fits your hardware.

Done, Shift+Super+r, to reload i3 and then just hit brightness up/down to control brightness on your X260 in the awesome i3 tiling window manager.

Friday, October 17, 2014

Installing SMA MIRIAD on Ubuntu (2014/05)

What is MIRIAD? See http://bima.astro.umd.edu/miriad/ for information.

Edit : Tested on the Ubuntu 13.04 Raring Ringtail
Edit2 : Tested on the Ubuntu 13.10 Saucy Salamander
Edit3 : Tested on the Ubuntu 14.04 Trusty Tahr

I am not 100% sure about the prerequisites, it should work with the ones below, however (especially after installing IRAM GILDAS).
sudo apt-get install build-essential pgplot5 gfortran libreadline-dev libforms-bin csh cvs
Then go and grab the latest binary release from :
NOTE: Got into catalog with highest number. At time of writing this is "2014.7". Choose miriad_linux64_u14_gfortran.tar.gz for Ubuntu.
Unpack it where you want it to install:
tar zxf miriad_linux64_BINARY_gfortran.tar.gz
where "BINARY" is your Ubuntu version i.e.  miriad_linux64_u14_gfortran.tar.gz for Ubuntu 14.04.

Enter the catalog and run the "install script":
cd miriad
then to source the miriad binaries you have to run
source miriad_start.sh
from the miriad catalog, or add these lines to your hidden .bashrc/.bash_aliases file in your home-directory
(nano ~/.bashrc):
#source miriad environment variables
alias mirenv="source /your_path/to_the_installation/directory/miriad_start.sh"
and to launch miriad type mirenv, followed by miriad (or your choice of miriad task) or just add (to the same file)

#source miriad environment variables
source /your_path/to_installation/miriad_start.sh
To have it accessible directly when you start a terminal.

EDIT: You can now update the installation at any time by running
(after sourcing "miriad_start.sh") it will get all the updated files via CVS and recompile all binaries.

Thursday, May 29, 2014

Off-topic : TP-Link WN725N v2 WIFI on the Raspberry PI

TP-Link WN725N v2 on Raspberry Pi at N speeds (150 Mbps).

So I managed to get the wireless nano-dongle to work on one of my Raspberry Pi's. It's a TP-Link WN725N version 2. It has N capabilities, and it wasn't easy making sure I got N speeds either, more on that later.


To get the drivers into the kernel, I used the script downloadable in this forum post:

You might have to edit the script, basically change the part .../phpBB3/... to .../forums/...
Just run the script and reboot.

N-speeds and connecting it to your network 

Then we want to make sure we get the N-protocol up and running. The key to this lies in the security settings of the wireless, not the Raspberry Pi.
If you run

iwconfig wlan0

the output should look something like:

wlan0     IEEE 802.11bgn  ESSID:"SSID"  Nickname:"<WIFI@REALTEK>"
          Mode:Managed  Frequency:2.437 GHz  
          Bit Rate:72.2 Mb/s   Sensitivity:0/0  
          Retry:off   RTS thr:off   Fragment thr:off
          Encryption key:****-****-
          Security mode:open
          Power Management:off
          Link Quality=100/100  Signal level=100/100  

and some other things.
Here you can see I get all three, b+g+n. If it says "bg" you're doing it wrong, given that you have a N-capable router...

To get it working, you have to only have the AES (CCMP) security ONLY, i.e. NOT TKIP, TKIP is the older security protocol, and gives you only G-speeds, i.e. 54 Mbps. So to fix this, make sure the wireless is setup with WPA(2) AES security. NOTE: this dongle only has one (1) antenna, that's why the N-speed is 150 Mbps, you need an adapter with 2 antennae to get 300 Mbps, but then you probably need a powered USB-hub too.

Then in /etc/network/interfaces you can have (given that you have DHCP and not static IPs):

auto lo

# ethernet
iface lo inet loopback
iface eth0 inet dhcp

# The WLAN network interface
allow-hotplug wlan0
iface wlan0 inet manual
wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
iface wlan0 inet dhcp

and then for the important part, in /etc/wpa_supplicant/wpa_supplicant.conf

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev


and then it should all be dandy. Some guides tell you to go with something else for "proto" and "pairwise" and don't include "group", these are the TKIP guides, giving G-speeds.

Note: If only get, ~72 Mbps, you should try to activate channel bonding, i.e. get the channel bandwidth of the wireless network (router, ap, etc) of 40 MHz, as opposed to 20 MHz (so it clumps two 20 MHz channels together). However, this can degrade the signal in crowded areas, that's why mine says 72 Mbps.

Reboot and enjoy!

Usefull links:
TKIP vs. AES and setup : http://www.raspberrypi.org/forums/viewtopic.php?t=36081&p=517178
WPA2-PSK setup: https://coderwall.com/p/v290ta

Monday, June 3, 2013

RATRAN : get RESTFREQ written to FITS file

Current versions of RATRAN (http://www.sron.rug.nl/~vdtak/ratran/) does not put the FITS keyword RESTFREQ and the corresponding value in the output FITS-files by default. Below is a quick fix that I forgot to note down when I figured it out and thus had to rediscover twice...

Note about getting FITS output working:
make sure your shell variable CFITSIO is set to the path where it is installed, on my Ubuntu 13.04 this is CFITSIO=/usr/include:/usr/lib/x86_64-linux-gnu/:/usr/lib (several paths to make sure its correct) and also the LD_LIBRARY_PATH path (e.g. LD_LIBRARY_PATH=$LD_LIBRARY_PATH/usr/include)

So for the RESTFREQ to be printed we need to add it to the routine 'openfits', so edit 'Ratran_install_dir/sky/openfits.f'

first add restfreq to the definition of the routine
SUBROUTINE openfits(lout,outname,naxis,naxes,status)
SUBROUTINE openfits(lout,outname,naxis,naxes,status,restfreq)
 then define the type
this should be added after the declaration of the other variables, i.e. after these lines

      INTEGER lout,blocksize,bitpix,status,naxis,naxes(3)
    LOGICAL simple,extend
    CHARACTER outname*60

then we need to tell it when and how to write it to the fits, so just after all the other header keywords are written, i.e.
call ftpkyd(lout,'CDELT3',dble(velres),-8,'',status)
call ftpkyd(lout,'CRPIX3',dble(nchan/2+1),-8,'',status)
call ftpkyd(lout,'CRVAL3',0.d0,-8,'',status)
we add
call ftpkyd(lout,'RESTFREQ',dble(restfreq),-8,'',status)

So now the function openfits can add the restfeq keyword, but now we also need to add this input argument to the call of that routine.
Save and close openfits.f, and open 'Ratran_install_dir/sky/fmain.f' and in this file, find the line that call the openfits rouitine, i.e. use the find function of your editor and search for 'openfits'
Change the line
call openfits(lout(iline),outname,naxis,naxes,status)
call openfits(lout(iline),outname,naxis,naxes,status,nu(iline))

and done!

Friday, March 22, 2013

Atmospheric transmission curve for Plateau de Bure Interferometer

I tried to find the atmospheric transmission curve for Plateau de Bure Interferometer (PdBI) site the other day. There is no plot/data file showing the atmospheric transmission for the Plateaude Bure Interferometer site! Then I realized that I might be able to plot it in the GILDAS software ASTRO - the observation planning tasks of the bundle. So in this post I will explain how to plot the atmospheric transmission curve at the Plateau de Bure Interferometer (PdBI) using the atmospheric modeling function included in the ASTRO package included in the IRAM GILDAS software bundle, and Python. I guess you can do the same plot in ASTRO using GREG, but I like Python/matplotlib more.

After checking and sending mails back and forth with Jan Martin Winters at IRAM, I realized that there is not direct command to plot the whole transmission curve. What does exist is the command ATMOSPHERE, that calculates τ (the opacity) and a whole bunch of other stuff for a given frequency (running "HELP ATM" gives you the run through of the input/output). This means that I can write a loop that calculates τ for a given frequency. Then I just have to calculate the transmission from I/I0=e-τ and plot it against the frequency.

After each calculation I want to print the frequency and the τ to a file. Thus, the following script produces a file where the first column is frequency and the second the total opacity (τ).

! use the new atm, the 2009 version 
! test that it works with "exam atm%version" 
! after the command below
set atm new
! set observatory, the available names are
! if not satisfied, use coordinates as
! OBS [longitude latitude altitude [radius]]
! radius is the sun avoidance zone in degrees, (def 30) 
obs pdbi                      !<---- change site here!
! which airmass to calculate for 1 = zenith
let airmass 1
! ground temperature in Kelvin
let temperature 273
! create variable freq 
! and the incremental change in frequency
define real freq
let freq 0.1
define real incr
let incr 0.1
! the amount of water vapor in mm 
let water 0.3                ! <---- change water vapor here!
! the name of the output file
sic out pdbi_atm_0_3.dat
for n 0 to 16000
    let freq_sig = freq
    let freq_ima = freq
    ! write to file
    say 'freq' 'tau_tot'
    ! add incr to the frequency and 
    ! restart the loop
    let freq = freq+incr
sic out

Just run this with

astro -nw @script_name.astro

After this you are left with the file pdbi_atm_0_3.dat in this case. Run this script for different amount of water vapor, say 0.3, 0.8 and 1.5. Then you jump into Python and do the following in the same directory as your files

# general imports
from scipy import exp, loadtxt
import matplotlib.pyplot as pl

# various settings
pl.rcParams['font.size'] = 15

# calculate the transmission in percent
transm_percent = lambda tau : exp(-1*tau)*100

# load the data columns
data0_3 = loadtxt('pdbi_atm_0_3.dat').transpose()
data0_8 = loadtxt('pdbi_atm_0_8.dat').transpose()
data1_5 = loadtxt('pdbi_atm_1_5.dat').transpose()

# initialize the figure

# plot the graphs
pl.plot(data0_3[0], transm_percent(data0_3[1]))
pl.plot(data0_8[0], transm_percent(data0_8[1]))
pl.plot(data1_5[0], transm_percent(data1_5[1]))

# get some label action going
pl.legend(['0.3mm', '0.8mm', '1.5mm'])
pl.xlabel('Frequency [GHz]')
pl.ylabel('Transmission (%)')
pl.title('Atmospheric Transmission at PdBI')

# and adjust the margins

What you should end up with is the following

Notable is that it "only" goes up to 1 THz, after this the model does not work, or I could not get it to work at least. The values are not correct. If you figure it out, please let me know. Next I will add the receiver bands and some nice water lines, or something. I have to thank Susanne Wampfler for helping me out with the ASTRO script, programming in SIC is not straight forward...

Friday, October 28, 2011

Querying Splatalogue.net with a Python script

I have written a Python script/module to query the Splatalogue.net spectral line catalog compilation. It does not rely on any SPLAT-VO protocol at all. It queries the database through the normal web interface, which makes it possible to query with the same parameters as through the web interface. Yay! The instructions below is for Ubuntu Linux, but is probably easily adapted to other platforms.

EDIT2: I have rewritten the module and the updated version is now part of astropy.astroquery (http://github.com/astropy). However, the CLI interface is stripped out of this version. Furthermore, according to Splatalogue.net/NRAO, the php interface will be replace by a java onlye sometime this year (https://safe.nrao.edu/wiki/bin/view/Splat/WebHome, see Milestone no 4.) which would mean that I have to update it.  
The code is hosted on my Launchpad.net trunk (bazaar versioning):


EDIT: I have moved my code to github (!) https://github.com/vilhelmp/adapy

the file splatsearch (.py, in the "bin" folder on github) is all that is needed from Launchpad. The prerequisite modules are UrlLib2 and ClientForm, for the form submission/HTML parsing.

In Ubuntu the ClientForm (http://wwwsearch.sourceforge.net/old/ClientForm/) module is installed by running

sudo apt-get install python-clientform

in a terminal. UrlLib should come packed with Python. There is a plan to move the splatsearch script over to use some newer module, like the Mechanize module (http://wwwsearch.sourceforge.net/mechanize/).

EDIT: Mechanize now actually takes care the clientform and urllib (linked) part. I haven't had time to move the script over to it yet. (Branch and fix perhaps?)

It is runnable from the command line (set it to be runnable "chmod +x splatsearch.py"). After this, running

splatsearch.py --help

without any input, from the terminal should give you the usage rundown. Although, first you have to add the path to splatsearch.py to your environment variables. Alternatively run it from the same catalog as the file with:

./splatsearch.py [parameters]

It can also be used inside your favorite Python script/interactive session with:

Python : from splatsearch import splatsearch

Yes, I guess it is bad programming to name them the same (module and function). After this initial import it is easy to get the idea of how to search and get it to return what/how you want (print on screen, list or dictionary) with:

Python : help splatsearch

My Launchpad.net/github page also hosts other astronomical data analysis and visualization scripts to do different things on interferometric spectral cubes (small FOV, <60 arcsec). The ADAVIS module has e.g. a function for plotting spectrum and at the same time fit Gaussians, take the fitted frequencies and query Splatalogue.net for line identification. It is far from done though, and I have a huge to-do list of things to fix, but most things work well enough.

 Happy line identification!