Python directly read in USGS flow by given site number

Python packages are very powerful. Here is an example for hydrological data analysis. This small piece of code reads USGS flow by a given site number and store it as pandas DataFrame. You need to install “ulmo” first.

Original post is from here

import pandas as pd
import numpy as np
import ulmo

def importusgssite(siteno):
    sitename = {}
    sitename = ulmo.usgs.nwis.get_site_data(siteno, service="daily", period="all")
    sitename = pd.DataFrame(sitename['00060:00003']['values'])
    sitename['dates'] = pd.to_datetime(pd.Series(sitename['datetime']))
    sitename.set_index(['dates'],inplace=True)
    sitename[siteno] = sitename['value'].astype(float)
    sitename[str(siteno)+'qual'] = sitename['qualifiers']
    sitename = sitename.drop(['datetime','qualifiers','value'],axis=1)
    sitename = sitename.replace('-999999',np.NAN)
    sitename = sitename.dropna()
    #sitename['mon']=sitename.index.month
    return sitename

d = importusgssite('12472800');
d.plot(style='r', linewidth=1.0)

flowseries.png

Advertisements

Plotting in Python

Finally start to use python for plotting.

  • Plotting in notebook

ipython notebook is good and plots can be lined up with scripts. But if I need to run the same script on cluster without Xming, it will crash. Simple way is to add

import matplotlib as mpl
mpl.use('Agg')

when importing libraries.

  • Seaborn

Seaborn is a great package for color scheme picking. Color schemes are called “palettes” in seaborn. There are many pre-set palettes for you to choose. If you want to use “colorblind”, just simply add

import seaborn as sns
with sns.color_palette("colorblind"):

before the plotting script.

Customizing the palette is easy too:

colors = ["windows blue", "amber", "greyish", "faded green", "dusty purple"]
with sns.xkcd_palette(colors):

or

flatui = ["#9b59b6", "#3498db", "#95a5a6", "#e74c3c", "#34495e", "#2ecc71"]
sns.set_palette(flatui)
with sns.color_palette():

 

Tecplot in cmd

It’s kinda weird to use command lines in Windows (maybe a lot of people do it, who knows) but the reason I’m using it is that in Windows based Matlab, the “system” function will just call cmd. So if I need to run something inside a Matlab loop, I should learn how to use cmd.

Here is the command line I used to open a data file and run a python script in Tecplot:

C:\ /WAIT tec360 -b -datafile C:\something.plt -p C:\some\python.py

“/WAIT” is to hold the program until it’s done
“-b” is batch mode, include this otherwise you will see the GUI
“-datafile” is reading file
“-p” is executing python script
Note that the python script must be put in the python path otherwise Tecplot won’t find it.

Log in to a password protected FTP

Some password protected FTP sites can be visited by anonymous users  but if you don’t input user name and password the protected contents won’t show up.

In web browser, use

ftp://(user name):(password)@(ftp_site_address)

In shell,

wget --user=XXX --password='XXX' ftp://ftp.XXX.edu/*

Types of meshing elements

Mesh elements can be 1D (lines), or 2D (triangles, or quadrilaterals.), but most of the time, when starting with 3D CAD data, the elements will be tetrahedra (tets; 3-sided pyramids), hexahedra (hexes or bricks), polyhedra (with any number of sides), prisms, or pyramids (with 5 sides.)

Untitled