Friday, January 30, 2015

Test sharing code with Gist



I am using gist for sharing code here. Credits to the original blogger's post.





How Do I Enable remote access to PostgreSQL database server?


This is a much discussed topic.
I will just quote links here  and here.

Note that if allowing the whole world to connect is desired. Append the following line

 host  all       all       0.0.0.0/0        md5  

Wednesday, March 5, 2014

Site matching query does not exist -- Django

Background: 

When migrating from sqlite3 to postgresql, after running

python manage.py syncdb

got error message “Site matching query does not exist.

The cause: 

Table django_site does not have any entry.

The fix: 

python manage.py shell
from django.contrib.sites.models import Site
Site.objects.all()
Site.objects.create(pk=1, domain='127.0.0.1:8000', name='localhost'

Credit : 
http://stackoverflow.com/questions/6957360/admin-login-stopped-functioning-django

Sunday, November 11, 2012

Gnuplot: how to draw histograms, the easy way

Suppose the test data is like


Num_clients S1 S2 S3 S4
1 6.853 6.812 6.811 6.813
2 10.726 10.701 10.699 10.7
3 15.122 15.101 15.099 15.1
4 21.761 21.699 21.695 21.696
5 28.254 28.231 28.233 28.232


We can plot a clustered histogram graph with the gnuplot script:

set terminal postscript eps enhanced 
set output 'test.eps'
set size 0.6,0.6
set yrange [1:50]
set xlabel "Num Mobile Clients (x10^5)"
set ylabel 'Server Workload (s)'

set xtics border nomirror
set ytics border nomirror

set style fill pattern
set style data histogram
set style histogram clustered gap 1

plot 'test.dat' u 2:xtic(1) title columnheader, \
'' u 3:xtic(1) title columnheader, \
'' u 4:xtic(1) title columnheader, \
'' u 5:xtic(1) title columnheader fs pattern 6



Wednesday, June 20, 2012

Latex: have two tables side by side?


\begin{table}
\parbox{.45\linewidth}{
\centering
\begin{tabular}{ccc}
\hline
a&b&c\\
\hline
\end{tabular}
\caption{Foo}
}
\hfill
\parbox{.45\linewidth}{
\centering
\begin{tabular}{ccc}
\hline
d&e&f\\
\hline
\end{tabular}
\caption{Bar}
}
\end{table}

Tuesday, April 10, 2012

Installing PostGIS 1.4.0 on Postgresql 8.4 on Ubuntu 10.04

http://www.paolocorti.net/2008/01/30/installing-postgis-on-ubuntu/


Install postgres 8.4
sudo apt-get install postgresql postgresql-client postgresql-contrib pgadmin3
Install postgis
sudo apt-get install libpostgis-java osm2pgsql postgresql-8.4-postgis
Create template
sudo su postgres
createdb postgistemplate
createlang plpgsql postgistemplate
psql -d postgistemplate -f /usr/share/postgresql/8.4/contrib/postgis.sql
psql -d postgistemplate -f /usr/share/postgresql/8.4/contrib/spatial_ref_sys.sql
Testing successful installation
psql -d postgistemplate -c "SELECT postgis_full_version();"

Monday, April 9, 2012

Fixing "font not embedded" issue to pass the IEEE PDF eXpress check

I spent two hours making my pdf file to pass the IEEE PDF eXpress check.


After I latex and submit the pdf file, I got the following errors.

Errors & Warnings
Severity Description
? 8 Error Font TimesNewRomanPSMT is not embedded (817x)
? 8 Error Font TimesNewRomanPS-ItalicMT is not embedded (204x)
? 8 Error Font TimesNewRomanPS-BoldItalicMT is not embedded (6x)
? 8 Error Font TimesNewRomanPS-BoldMT is not embedded (13x)
? 8 Error Font Arial-ItalicMT is not embedded
? 8 Error Font ArialMT is not embedded (12x)


You may also get other fonts embedding problem.


In Windows, the solution can be very simple.


1. open your pdf file, 
2. select file -> print
3. set your printer to be Adobe printer (assuming you already installed it)
4. click on properties
5. click the tab "Adobe PDF Settings"
6. uncheck "Rely on system fonts only; do not use document fonts"
7. click on the Edit... after Default Settings
8. click on Fonts, add those missing fonts to "Always Embed" (It is recommended to save the properties as a new setting.)
9. print the pdf file with the new settings, and your new pdf file should be good to go.



Thursday, March 15, 2012

Installing ruby on rails (ruby version 1.9.3, rails version 3.2.2) on Ubuntu Lucid LTS 10.04

The post shows a step-by-step installation of the Ruby on Rails on Ubuntu 10.04 LTS.

1. install build essential, github and 

sudo apt-get install build-essential

2. install rvm (which includes ruby 1.9.3 and gem 1.8.19)

bash -s stable < <(curl -s https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer)
source ~/.bashrc
rvm requirement -- install output required libraries by copy and paste the output text
rvm install 1.9.3  -- it takes a while


3. install rails


copy and paste the text below at the end of ~/.bashrc: 
[[-s "$HOME/.rvm/scripts/rvm"]] && . "$HOME/.rvm/scripts/rvm" 

source $HOME/.bashrc

check the successful installation of rvm:
type rvm | head -1
output should be 'rvm is a function'

gem install rails --include-dependencies


Congratulations! You have successfully installed Ruby on Rails!

Sunday, February 26, 2012

UNIX / Linux: 10 Netstat Command Examples

sourcehttp://www.thegeekstuff.com/2010/03/netstat-command-examples/



Netstat command displays various network related information such as network connections, routing tables, interface statistics, masquerade connections, multicast memberships etc.,
In this article, let us review 10 practical unix netstat command examples.
 sudo netstat -pnutl

1. List All Ports (both listening and non listening ports)

List all ports using netstat -a

# netstat -a | more
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 localhost:30037         *:*                     LISTEN
udp        0      0 *:bootpc                *:*                                

Active UNIX domain sockets (servers and established)
Proto RefCnt Flags       Type       State         I-Node   Path
unix  2      [ ACC ]     STREAM     LISTENING     6135     /tmp/.X11-unix/X0
unix  2      [ ACC ]     STREAM     LISTENING     5140     /var/run/acpid.socket

List all tcp ports using netstat -at

# netstat -at
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 localhost:30037         *:*                     LISTEN
tcp        0      0 localhost:ipp           *:*                     LISTEN
tcp        0      0 *:smtp                  *:*                     LISTEN
tcp6       0      0 localhost:ipp           [::]:*                  LISTEN

List all udp ports using netstat -au

# netstat -au
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
udp        0      0 *:bootpc                *:*
udp        0      0 *:49119                 *:*
udp        0      0 *:mdns                  *:*

2. List Sockets which are in Listening State

List only listening ports using netstat -l

# netstat -l
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 localhost:ipp           *:*                     LISTEN
tcp6       0      0 localhost:ipp           [::]:*                  LISTEN
udp        0      0 *:49119                 *:*

List only listening TCP Ports using netstat -lt

# netstat -lt
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 localhost:30037         *:*                     LISTEN
tcp        0      0 *:smtp                  *:*                     LISTEN
tcp6       0      0 localhost:ipp           [::]:*                  LISTEN

List only listening UDP Ports using netstat -lu

# netstat -lu
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
udp        0      0 *:49119                 *:*
udp        0      0 *:mdns                  *:*

List only the listening UNIX Ports using netstat -lx

# netstat -lx
Active UNIX domain sockets (only servers)
Proto RefCnt Flags       Type       State         I-Node   Path
unix  2      [ ACC ]     STREAM     LISTENING     6294     private/maildrop
unix  2      [ ACC ]     STREAM     LISTENING     6203     public/cleanup
unix  2      [ ACC ]     STREAM     LISTENING     6302     private/ifmail
unix  2      [ ACC ]     STREAM     LISTENING     6306     private/bsmtp

3. Show the statistics for each protocol

Show statistics for all ports using netstat -s

# netstat -s
Ip:
    11150 total packets received
    1 with invalid addresses
    0 forwarded
    0 incoming packets discarded
    11149 incoming packets delivered
    11635 requests sent out
Icmp:
    0 ICMP messages received
    0 input ICMP message failed.
Tcp:
    582 active connections openings
    2 failed connection attempts
    25 connection resets received
Udp:
    1183 packets received
    4 packets to unknown port received.
.....

Show statistics for TCP (or) UDP ports using netstat -st (or) -su

# netstat -st

# netstat -su

4. Display PID and program names in netstat output using netstat -p

netstat -p option can be combined with any other netstat option. This will add the “PID/Program Name” to the netstat output. This is very useful while debugging to identify which program is running on a particular port.
# netstat -pt
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        1      0 ramesh-laptop.loc:47212 192.168.185.75:www        CLOSE_WAIT  2109/firefox
tcp        0      0 ramesh-laptop.loc:52750 lax:www ESTABLISHED 2109/firefox

5. Don’t resolve host, port and user name in netstat output

When you don’t want the name of the host, port or user to be displayed, use netstat -n option. This will display in numbers, instead of resolving the host name, port name, user name.
This also speeds up the output, as netstat is not performing any look-up.
# netstat -an
If you don’t want only any one of those three items ( ports, or hosts, or users ) to be resolved, use following commands.
# netsat -a --numeric-ports

# netsat -a --numeric-hosts

# netsat -a --numeric-users

6. Print netstat information continuously

netstat will print information continuously every few seconds.
# netstat -c
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 ramesh-laptop.loc:36130 101-101-181-225.ama:www ESTABLISHED
tcp        1      1 ramesh-laptop.loc:52564 101.11.169.230:www      CLOSING
tcp        0      0 ramesh-laptop.loc:43758 server-101-101-43-2:www ESTABLISHED
tcp        1      1 ramesh-laptop.loc:42367 101.101.34.101:www      CLOSING
^C

7. Find the non supportive Address families in your system

netstat --verbose
At the end, you will have something like this.
 netstat: no support for `AF IPX' on this system.
 netstat: no support for `AF AX25' on this system.
 netstat: no support for `AF X25' on this system.
 netstat: no support for `AF NETROM' on this system.

8. Display the kernel routing information using netstat -r

# netstat -r
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
192.168.1.0     *               255.255.255.0   U         0 0          0 eth2
link-local      *               255.255.0.0     U         0 0          0 eth2
default         192.168.1.1     0.0.0.0         UG        0 0          0 eth2
Note: Use netstat -rn to display routes in numeric format without resolving for host-names.

9. Find out on which port a program is running

# netstat -ap | grep ssh
(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
tcp        1      0 dev-db:ssh           101.174.100.22:39213        CLOSE_WAIT  -
tcp        1      0 dev-db:ssh           101.174.100.22:57643        CLOSE_WAIT  -
Find out which process is using a particular port:
# netstat -an | grep ':80'

10. Show the list of network interfaces

# netstat -i
Kernel Interface table
Iface   MTU Met   RX-OK RX-ERR RX-DRP RX-OVR    TX-OK TX-ERR TX-DRP TX-OVR Flg
eth0       1500 0         0      0      0 0             0      0      0      0 BMU
eth2       1500 0     26196      0      0 0         26883      6      0      0 BMRU
lo        16436 0         4      0      0 0             4      0      0      0 LRU
Display extended information on the interfaces (similar to ifconfig) using netstat -ie:
# netstat -ie
Kernel Interface table
eth0      Link encap:Ethernet  HWaddr 00:10:40:11:11:11
          UP BROADCAST MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)
          Memory:f6ae0000-f6b00000

Tuesday, February 14, 2012

PostGRES performance issues

Installing Postgres9.0 and PostGIS on CentOS

Original Link: http://anujjaiswal.wordpress.com/2011/06/14/installing-postgres9-0-and-postgis-on-centos/


So, I work with spatial (GIS) data quite often. Since quite often I have to rebuild databases I am going to write down the steps I take to install PostGIS + Postgresql. The first step on CentOS is to configure the repositories:
  1. Update /etc/yum.repos.d/CentOS-Base.repo, add the line exclude=postgresql* so that it will look like
  2. [base]name=CentOS-$releasever – Base
    mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os #baseurl=http://mirror.centos.org/centos/$releasever/os/$basearch/
    gpgcheck=1
    exclude=postgresql*
    gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-5
  1. Use http://www.pgrpms.org/ for postgresql. As root, go to /etc/yum.repo.d/ and create a file called pgrpms9.repo and add the following lines:
  2. [pgdg90]
    name=PostgreSQL 9.0 $releasever - $basearch
    baseurl=http://yum.pgrpms.org/9.0/redhat/rhel-$releasever-$basearch
    enabled=1
    gpgcheck=0
    gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-PGDG
    [pgdg90-source]
    name=PostgreSQL 9.0 $releasever – $basearch – Source
    failovermethod=priority
    enabled=0
    baseurl=http://yum.pgrpms.org/srpms/9.0/redhat/rhel-$releasever-$basearch
    gpgcheck=0
    gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-PGDG
  3. Add EPEL repository by doing the following from shell:
  4. su -c 'rpm -Uvh http://download.fedora.redhat.com/pub/epel/5/i386/epel-release-5-4.noarch.rpm'
  5. Update CentOS base repository to remove postgresql being downloaded by adding “exclude=postgresql*” to the end of the repo Centos-Base.repo
  6. Install PostGRES server, PostGIS, GEOS and PROJ on x86_64 using yum from shell prompt as given the command below:
  7. $ yum install postgresql90-contrib.x86_64 postgresql90-docs.x86_64 postgresql90-devel.x86_64 postgis90 postgis90-utils postgis90-docs postgresql90-server.x86_64 proj-devel.x86_64 geos-devel.x86_64
  8. Install PostGRES server, PostGIS, GEOS and PROJ on i386 linux using yum from shell prompt as given the command below:
  9. $ yum install postgresql90-contrib postgresql90-docs postgresql90-devel postgis90 postgis90-utils postgis90-docs postgresql90-server proj-devel geos-devel
  10. Initialize and Start Postgres Database server
  11. $ /sbin/service postgresql-9.0 initdb
    $ /sbin/service postgresql-9.0 start
  12. Create PostGIS template database
  13. $ su - postgres
    $ createdb -E UTF8 -T template0 postgis_template
    $ createlang -d postgis_template plpgsql
    $ psql -d postgis_template -f /usr/pgsql-9.0/share/contrib/postgis-1.5/postgis.sql
    $ psql -d postgis_template -f /usr/pgsql-9.0/share/contrib/postgis-1.5/spatial_ref_sys.sql
    $ psql -d postgis_template -c "GRANT ALL ON geometry_columns TO PUBLIC;"
    $ psql -d postgis_template -c "GRANT ALL ON geography_columns TO PUBLIC;"
    $ psql -d postgis_template -c "GRANT ALL ON spatial_ref_sys TO PUBLIC;"
    $ psql -d postgis_template -c "VACUUM FULL;"
    $ psql -d postgis_template -c "VACUUM FREEZE;"
    $ psql -d postgres -c "UPDATE pg_database SET datistemplate='true' WHERE datname='postgis_template';"
    $ psql -d postgres -c "UPDATE pg_database SET datallowconn='false' WHERE datname='postgis_template';"
  14. Create PostGRES database using postgis_template template database
  15. $ su - postgres
    $ psql -Upostgres
    $ create role owner with createdb createrole login;
    $ create database db with owner=owner template=postgis_template;

Wednesday, January 4, 2012

Mac updatedb to enable locate command

The first time to call locate command, one will get the message:

WARNING: The locate database (/var/db/locate.database) does not exist.
To create the database, run the following command:

  sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.locate.plist

Please be aware that the database can take some time to generate; once
the database has been created, this message will no longer appear.


In order to use locate command, the database should be created with the command:

sudo /usr/libexec/locate.updatedb

Thursday, December 1, 2011

Linux and Java: Disk usage, Memory check, JVM virtual memory, Processor Info, Kernel Info

Check disk usage:
df -h

Check file size in a directory
du -sh *


Check memory usage, display total memory in MB:
free -t -m


Set JVM virtual memory:
java -jar -Xms128m -Xmx512m bigapp.jar

Processor Info:
cat /proc/cpuinfo

Kernel version:
uname -r

Sunday, October 23, 2011

Acrobat: The crop page pop-up menu in Acrobat X tools

In the latest version of Acrobat professional readers,
Clicking on Tools > Pages > Crop only shows manual rectangle crop selection. 
There is no pop-up menu showing me options such as "removed white margins" etc as in older versions.


The way to crop pages is to  make a rectangle with the crop tool, 
then double-click there is an check-box to remove white margins, similar to older versions.


ScreenSnapz.png





Wednesday, October 19, 2011

Latex: how to number your equation at any line


1. Number each line


\begin{align}
\label{equ:example}
D_{o_1o_2}&(t) =  ((v_{x_1}-v_{x_2})^2+(v_{y_1}-v_{y_2})^2) \cdot t^2
\\  & + \left( (x_1-x_2)(v_{x_1}-v_{x_2})+(y_1-y_2)(v_{y_1}-v_{y_2}) \right )\cdot 2t
\\  & +(x_1-x_2)^2+(y_1-y_2)^2
\end{align}


2. Number one line

\begin{align}
\label{equ:example}
\notag D_{o_1o_2}&(t) =  ((v_{x_1}-v_{x_2})^2+(v_{y_1}-v_{y_2})^2) \cdot t^2
\notag \\  & + \left( (x_1-x_2)(v_{x_1}-v_{x_2})+(y_1-y_2)(v_{y_1}-v_{y_2}) \right )\cdot 2t
\notag \\  & +(x_1-x_2)^2+(y_1-y_2)^2
\end{align}

3. No numbering

\begin{align*}
\label{equ:example}
 D_{o_1o_2}&(t) =  ((v_{x_1}-v_{x_2})^2+(v_{y_1}-v_{y_2})^2) \cdot t^2 
\\  & + \left( (x_1-x_2)(v_{x_1}-v_{x_2})+(y_1-y_2)(v_{y_1}-v_{y_2}) \right )\cdot 2t 
\\  & +(x_1-x_2)^2+(y_1-y_2)^2
\end{align*}

Wednesday, October 12, 2011

In-memory index structures

Points
K-d tree (Point K-d tree)


Recursive subdivision of point-set into two halves using
vertical/horizontal line.
Horizontal line on even levels, vertical on odd levels
One point in each leaf
    Alternating splits on dimensions.
    Cost in two dimensions: O(√N + T) query time, O(N) storage
    Cost in higher dimensions: O(N^(1-1/d) + T) time complexity for d > 2
      Range tree
        Nested binary search trees
        Cost in two dimensions: O((logN)^2 + T) query time, O(NlogN) storage, Fractional cascading reduces query complexity to O(log N + T).
          Quadtree
            Division into 4 subspaces
            Point and region versions
            Range query complexity similar to K-d tree

            Can be used for compressing/clustering
            information (Point versus region quadtree)



            Intervals

            Used for stabbing queries on 1-d intervals
            – Root defined by median end-point
            – Left child stores all intervals that end before the median
            – Right child stores all intervals that begin after the median
            – The root stores all intervals that intersect the median; order by left end-points and by right end-points in separate lists


            Used together with Range Tree for window query:
            O(N log N) storage, O(N log N) construction time, O((logN)^2 + T) query time



            Voronoi Diagram for NN Search

            O(N) space, O(N log N) construction time, O(logN) query time


            kth order Voronoi diagram defines the partitioning based on k-closest sites. For 2 dimensions, O(k(N-k)) space, O(k2N log k + kN log N) construction time, O(k + logN) query time.

            A number of approximate algorithms.

            References:

            Computational geometry, algorithms and applications, de Berg et al, Springer.


            Wednesday, September 14, 2011

            Josm editor User Manual PDF

            I want to use Josm editor for a project. But it seems there is no pdf version of the entire user manual. So I create this pdf file and share online. It can be downloaded here

            Wednesday, September 7, 2011

            How To Install Python + Django + Aptana Studio on Windows 7

            In this article, I will explain how to get Python, Django, and Aptana Studio running smoothly on windows.

            1. Install Python


            •  Go to http://www.python.org/download/ and download the Python. I am currently using the 2.7 version. Note that the latest version may not work with Windows yet.
            •  Execute and install what you’ve just downloaded with the default options
            • open command window and type 'python' to test if it is successfully installed. If command not recognized, follow these steps to add the path in Windows 7. Start -> Control Panel -> System and Security->Systems-> Advanced system settings->Environment Variables-> System Variables -> PATH->modify PATH by appending ;C:\Python27;C:\Python27\Scripts”. Also make sure you have a semicolon (;) before what you write.
            2. Installing Django
            • Go to http://www.djangoproject.com/download/ and download the version that best suits you. Version 1.3 works fine for me.
            • Extract the files to a folder. WinZip will do the job. Please REMEMBER THE FOLDER WHERE YOU EXTRACTED DJANGO.
            • Open a command prompt (Start > Run > Type in “cmd” and press OK). In the black window that is going to show up, type in cd “[directory_of_django]“. For example: cd "C:\www\python\Django-1.3″. 
            • setup.py install
            3. Installing aptana studio
            Aptana studio is a version of eclipse that already comes with HTML and CSS editors, and installing PyDev in it is really easy.
            • Go to http://www.aptana.org/studio/download and download the standalone full installer version.
            • go to Window > Preferences > PyDev > Interpreter – Python and click “Auto-Config”
            • Click OK on everything else
            4. Installing MySQL for Python
            If you are going to use django with MySQL, you will probably need to perform those steps:
            • Go to http://www.codegood.com/downloads and download
            • MySQL-python-1.2.2.win32-py2.6.exe, if you have a 32 bit OS or MySQL-python-1.2.2.win-amd64-py2.6.exe, if you have a 64 bit OS. In doubt, download the first one.
            • Install it.
            Have fun!