Move everything to my own blog

Too many ads on this blog so I decide to build one.

Powered by Github page, and Jekyll.


Git work flow E3SM

1) Fork a branch to my own repo on Github

2) Clone a branch from my own repository with submodules

$ git --recursive -b branch/name/here

3) Make changes to the code, add them,and commit

$ git add --all
$ git commit –a -m "comments to this commit"

4) Push changes to my repo

$git push origin name/of/the/branch:name/of/the/branch

5) Send pull request on Github

Additional commands

check submodule list

$ git submodule

if there are submodules, clone the submodules by

$ git submodule init
$ git submodule update


check remote and branch

$ git remote 
$ git branch -v

Add aliases

This following command create a “git hist” aliases based on “git log” to visually check the commit history

git config --global alias.hist "log --pretty=format:'%h %ad | %s%d [%an]' --graph --date=short"

Go back to old commits

find the hash of the target commit by checking the commit history, then

git checkout    # something like "git checkout 911e8c9"

Add ssh key to Github to avoid input password

  • Type cd ~/.ssh.
  • Within the .ssh folder, there should be these two files: id_rsa and If not, create them by typing
 ssh-keygen -t rsa -C ""
  • Open, copy everything and paste it into GitHub and/or BitBucket under the Account Settings, SSH Keys

Compile FORTRAN in Eclipse with netcdf

I like interpreted languages such as Matlab or Python, mostly because I’m more like a software user, not developer. These languages usually come with an Integrated Development Environment (IDE) for easy code editing and debugging. However, for compiled languages such as Fortran, there isn’t many choices for IDEs but Eclipse has some potential to be a good alternative.

Steps to compile a Fortran program in Eclipse, in Windows system, using NetCDF library:

  1. Eclipse is a programming platform. To compile Fortran code in Eclipse, a compiler is still needed. I chose cygwin gfortran as it’s easy to configure by the installation wizard.
  2. After installing gfortran in cygwin, install netcdf in cygwin too.
  3. Create a new Fortran project in Eclipse and import the code files.
  4. In properties for the project, select “internal builder” under “Fortran Build”, then in “Tool Chain Editor” choose “cygwin GCC” and “CDT internal builder”. In “Select Tools”, add “GNU Fortran Compiler” and “GNU Fortran Linker”
  5. In “Settings”, define Include locations. Under “GNU Fortran Compiler” -> “Directorories”, add Include paths (-I) as “usr/include” (assuming the NetCDF is installed in this directory”
  6. Same place, define the NetCDF library locations. Under “GNU Fortran Linker” -> “Libraries”, add “netcdff” for “-l” and “usr/lib” for “-L”
  7. Try compile the code. Basically the command is something like
gfortran XX.f90 -L/usr/lib -lnetcdff -I/usr/include

Important Notes:

  • 1) may need to change file suffix from “F90” to “f90” .
  • 2) put the file name “XX.f90” after “gfortran”, followed by “-L, -l, and -I”. I tried to put -L first, won’t work.
  • 3) the paths “/usr/lib” and “/usr/include” are “sudo” Linux paths used in cygwin system. Do not use the absolute windows path (e.g. C:\cygwin64\usr\include) to compile the code. Won’t work.
  • 4) installing NetCDF library yourself (instead of configuring in cygwin) might be a pain. But this video might help.


Reading numbers from a string in MATLAB

I always have needs to find numbers from a formatted string using Matlab. For example, find the lats and longs for all the grid cells of a river basin based on a bunch of input files, something like (forcings_35.25_-100.75). Then in Matlab you have two ways to read these numbers out.

  • using ‘sscanf’. It’s kinda stupid that you can’t find the two numbers by
  • sscanf('forcings_35.25_-100.75','%s_%f_%f')

    instead, you will get an 1d array that contains 22 numbers, with each number representing a single character in that string. What you need to do is using

  • sscanf('forcings_35.25_-100.75','forcings_%f_%f')

    I guess the reason behind this is that sscanf can only output one numerical matrix that based on the first character it processes. So if you do

  • sscanf('35.25_-100.75_forcings','%f_%f_%s')

    you will get the first two number you want, followed by 8 numbers representing the string “forcings”.

  • Another way is to use “textscan”. I feel safer using this method.
  • A = textscan('forcings_35.25_-100.75','%s %f %f', 'Delimiter', '_')

    This way, you will end up getting a three cell array representing the three parts separated by “_” in the string. Then simply using cell2mat() to convert the numbers to regular array. To convert “forcings” to string, using

  • char(A{1})

Interpolate values along a line from a surface in ArcGIS

This is kinda stupid and I’m sure there are much more elegant ways to do this. But for a quick and dirty job it’s good enough to me.

Anyways, what I want to do is to draw a line on a surface (could be anything, DEM, velocity field from CFD, or precipitation map) and extract the values along this line then output as a table. It can be easily done using ArcGIS 3D Analyst toolbar then you get something like this:


By clicking right mouse this data can be saved in a file and job done.

However, I just found that if you need to output data along the same line for different surfaces, this toolbar thing could become very annoying and you may need to draw multiple lines manually, which make them not exactly the same.

So here is another way to do this. First create a polyline, then in “Editor” tool, split it to equal length segments. Second, in 3D Analyst Tools -> Functional Surface -> Add Surface Information, select the surface and polyline just created, and choose Z_MEAN as output property. Run it, then you get the mean value of each segment in the attribute table.

Final trick, in attribute table, select some features, then right click the very left column and hold “shift” at the same time, there’s a copy option to allow you paste the table in Excel.

ArcGIS shape file plot in Matlab

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.


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 is a 3-column matrix of RGB triplets with length of LenS

Next is to create a map frame to plot on:

ax = usamap(latlim,lonlim); %plot frame using usamap as template 
axis off; framem on; gridm on; mlabel on; plabel on;
setm(gca,'MLabelLocation',10) %interval for meridians labeling
setm(gca,'PLabelLocation',5)  %interval for parallels labeling

Then plot the polygons

geoshow(ax, huc, 'SymbolSpec', faceColors);

When plotting points on the map, use scatterm

scatterm(lat,lon,s,c,'filled'); % s is vector defines the size of the dots
                                % c is RGB defines the color of the dots