ArcGIS shape file plot in Matlab

Tian's Hydro Life

ArcGIS shapes such as points and polygon, or even raster files can be plotted in Matlab. Here is an example of reading and plotting HUC4 polygons in Matlab.

First if read in the shape file that’s generated in ArcGIS

%read in shapefile
huc = shaperead('C:HUC4.shp','UseGeoCoords', true, 'BoundingBox', [lonlim', latlim']);

then we get a struct like this, in which most of the columns are attribute table variables, with the first few columns showing the geometrical information about each polygon.

Capture

Sometimes the vertices are too many for each polygon, which makes the plotting process extremely slow. Here I am using a tool called DecimatePoly to reduce the total number of vertices 10 times less without making noticeable changes on the shape.

Next we will define the color of the polygon by one of the variables in the attribute table using makesymbolspec:

faceColors = makesymbolspec('Polygon',{'INDEX',[1 lenS],'FaceColor',color_att}); %lenS is the number of polygon %color_att…

View original post 87 more words

Change Jupyter Python notebook default start-up folder

  • Open cmd (or Anaconda Prompt) and run jupyter notebook --generate-config.

This writes a file to C:\Users\username\.jupyter\jupyter_notebook_config.

  • Search for the following line: #c.NotebookApp.notebook_dir = ''

Replace by c.NotebookApp.notebook_dir = '/the/path/to/home/folder/'

** Make sure you uncommented this line,

** Make sure no indentation at the beginning of the line.

** Make sure you use forward slashes ‘/’ in your path and use the full path /home/user/ instead of ~/ for your home directory,

UNIX SORT

 

MAY052015,FA,KA32,2015-06-11 11:00:00,P1,NA,NA,NA
APR032014,FA,KA32,2014-04-07 14:00:00,P2,NA,NA,0.04
APR032014,FA,KA32,2014-04-28 10:44:00,P2,NA,NA,0.13
APR032014,FA,KA32,2014-05-05 11:15:00,P2,NA,NA,0.1
APR032014,FA,KA32,2014-05-12 11:29:00,P2,NA,NA,0.14
APR032014,FA,KA32,2014-05-15 15:59:00,P2,NA,NA,NA
APR032014,FA,KA32,2014-05-16 07:44:00,P2,NA,NA,0.5
APR032014,FA,KA32,2014-05-19 09:14:00,P2,NA,NA,0.12
DEC162014,FA,KA32,2014-12-31 12:00:00,P2,NA,NA,0.12

How do I sort first by the 5th field then by date ? First, it will sort 5th column (p%d) and then 4th column (time).

$ sort -t',' -gk5.2n -gk4n data.file.txt

Where,

  • -t defines delimiter, in this case, comma
  • -gk5.2n : Sort the 5th field numerically. The option -k is -key, -n is  numerically, 5.2n tells the program to sort the 2nd character of the 5th column.

Run Python in Unix

The environment variable PYTHONPATH is actually only added to the list of locations Python searches for modules. You can print out the full list in the terminal like this:

python -c "import sys; print sys.path"

Or if want the output in the UNIX directory list style (separated by :) you can do this:

python -c "import sys; print ':'.join(x for x in sys.path if x)"

Which will output something like this:

/usr/local/lib/python2.7/dist-packages/feedparser-5.1.3-py2.7.egg:/usr/local/lib/
python2.7/dist-packages/stripogram-1.5-py2.7.egg:/home/qiime/lib:/home/debian:/us
r/lib/python2.7:/usr/lib/python2.7/plat-linux2:/usr/lib/python2.7/lib-tk:/usr/lib
/python2.7/lib-old:/usr/lib/python2.7/lib- dynload:/usr/local/lib/python2.7/dist-
packages:/usr/lib/python2.7/dist-packages:/usr/lib/python2.7/dist-packages/PIL:/u
sr/lib/python2.7/dist-packages/gst-0.10:/usr/lib/python2.7/dist-packages/gtk-2.0:
/usr/lib/pymodules/python2.7

Python Plotting: Broken Axis

ax = plt.subplot(1, 1, 1) 

# Divide the plot into 1 * 9 grids
gs = GridSpec(1, 9)

# Adjust margins of subplots
gs.update(hspace=0.4,wspace=0.1, right=0.8, left=0.1, bottom=0.1, top=0.1)

# Four subplots share the same frame 
ax1 = plt.subplot(gs[0, 0],sharey=ax)
plt.ylabel('Temp. ($^{o}C$)', fontsize=14., labelpad=5)
ax1.plot_date(DailyObs['2003'].index.to_pydatetime(), DailyObs['2003'], 'b-', label='Obs', linewidth=3, alpha=0.8)
ax1.plot_date(DailySim['2003'].index.to_pydatetime(), DailySim['2003'], 'r-', label='Sim', linewidth=3, alpha=0.8) 

ax2 = plt.subplot(gs[0, 1],sharey=ax)
ax2.plot_date(DailyObs['2003'].index.to_pydatetime(), DailyObs['2003'], 'b-', label='Obs', linewidth=3, alpha=0.8)
ax2.plot_date(DailySim['2003'].index.to_pydatetime(), DailySim['2003'], 'r-', label='Sim', linewidth=3, alpha=0.8) 

# remove the axis spine
ax1.spines['right'].set_visible(False)
ax2.spines['left'].set_visible(False)

ax1.yaxis.tick_left() #hide the right tick
ax1.tick_params(labeltop='off') 
ax2.tick_params(axis='y', which='both',left='off',right='off',labelright='off',labelleft='off')
 
# arguments to pass plot, just so we don't keep repeating them
d = 0.02
kwargs = dict(transform=ax1.transAxes, color='k', clip_on=False)
ax1.plot((1-d,1+d),(-d,+d), **kwargs) # top-left diagonal
ax1.plot((1-d,1+d),(1-d,1+d), **kwargs) # bottom-left diagonal

kwargs.update(transform=ax2.transAxes) # switch to the bottom axes
ax2.plot((-d,d),(-d,+d), **kwargs) # top-right diagonal
ax2.plot((-d,d),(1-d,1+d), **kwargs) # bottom-right diagonal

Python Pandas Time Series Plotting Tips

Pandas time stamp object is different from python standard datetime objectes. As a result, when formatting x-axis ticks for a time series graph plotted from a Pandas time series object, the standard commands used to format major and minor ticks and their labels do not work properly (often displaying wrong/strange year values) .

In order to alter the axis without converting the pandas times, the scripts need to slightly altered.

Instead of standard Pandas plotting command,

DailyObs.plot(label='Obs', color='b', linewidth=3, style='-', alpha=0.8)

use the function to_pydatetime() to convert Pandas time index to python standard index.

ax.plot_date(DailyObs.index.to_pydatetime(), DailyObs, 'b-', label='Obs', linewidth=3, alpha=0.8)

This way, we can modify the axis tick locating and formatting with:

years = dates.YearLocator(2, month=7, day=4)       # ticking every 2 years on July 4th
 months = dates.MonthLocator()                                  # ticking every month by default
 monthsFmt = dates.DateFormatter('%b')                # make format
 yearsFmt = dates.DateFormatter('\n%Y')
 ax.xaxis.set_major_locator(years)
 ax.xaxis.set_major_formatter(yearsFmt)
 ax.xaxis.set_minor_locator(months)
 ax.xaxis.set_minor_formatter(monthsFmt)

Capture

See C:\Users\sunn067\Dropbox\IPython Notebooks\COCA\TwDaily.ipynb for complete plotting script.

 

 

Remove new line character in a text file

Say I want to remove the new line character (\n) every 3 lines in the example file junk.txt.

AL001 19 68 13 19 68 13 19 67 14 19 65 16
19 64 18 20 60 20 20 55 24 24 52 24
25 40 35 0 0 0 0 0 0
AL002 16 69 15 16 69 15 16 68 16 12 60 28
12 59 29 14 52 34 18 39 42 21 30 48
21 27 52 21 22 57 0 0 0

Running the command in UNIX:

cat junk.txt | paste -d " " - - -

The results look like:

AL001 19 68 13 19 68 13 19 67 14 19 65 16 19 64 18 20 60 20 20 55 24 24 52 24 25 40 35 0 0 0 0 0 0
AL002 16 69 15 16 69 15 16 68 16 12 60 28 12 59 29 14 52 34 18 39 42 21 30 48 21 27 52 21 22 57 0 0 0

Likewise, the command line below remove the new line character at every 2th line.

cat junk.txt | paste -d " " - -