This page demonstrates Python tips and tricks that I use in my everyday programming as an atmospheric science graduate student.
-Brian Blaylock

Friday, January 9, 2015

Loop through dates

I often want to loop through set of dates.

#Loop through a set of dates

#Import datetime and time functions
from datetime import datetime, timedelta

#Start Date
current_time = datetime(2015,01,01)

#Loop until a specified date is reached
while current_time < datetime(2015,01,30):
     #Do whatever you want to after getting the date
     string_time = current_time.strftime("%Y%m%d_%H%M")
     print string_time
     #add one minute and do the next date

     current_time = current_time+timedelta(minutes=1)

Tuesday, January 6, 2015

Adding additional packages through Anaconda Command Prompt

If you downloaded Python from the Anaconda (I recommend this distribution service) then you can use the Anaconda Command Prompt to download additional packages. The ones I need are netcdf4, hdf5, basemap, and maybe some others will be useful later.

The available packages are found here: http://docs.continuum.io/anaconda/pkg-docs.html

Simply open the Anaconda Command Prompt:
Type conda install <name of package>
Below is an example of downloading the hdf5 packagae

When the prompt comes up, type conda install hdf5



 You will then be prompted to answer whether or not you really want to install it. Type y
 It will then fetch the package and install it.







Pretty easy. The hardest part is remembering what the names of what packages are available. Again, here is the link and a list of packages from Continuum's website:

Python version: 2.7
Number of supported packages: 224
NameVersionLicenseIn Installer
abstract-rendering Linux Mac0.5.13rd-clause BSDTrue
apptools4.2.1BSDFalse
argcomplete0.8.1Apache Software LicenseTrue
astroid1.2.1LGPLFalse
astropy0.4.2BSDTrue
atom0.3.9BSDTrue
basemap1.0.7PSFFalse
bcolz0.7.1BSDFalse
beautiful-soup4.3.2PSF/MITTrue
binstar0.7.1BSDTrue
biopython1.64BSD-likeFalse
bitarray0.8.1PSFTrue
blaze0.6.3BSDTrue
blist1.3.6BSDFalse
blz0.6.2BSDTrue
bokeh0.6.1New BSDTrue
boto2.32.1MITTrue
bsddb Linux Mac1.0PSFFalse
bsdiff41.1.4BSDFalse
cairo Linux1.12.2LGPL 2.1 and MPL 1.1True
casuarius1.1LGPLTrue
cdecimal2.3BSDTrue
cffi0.8.6MITTrue
chaco4.4.1BSDTrue
chameleon2.16BSD-likeFalse
cheetah2.4.4MITFalse
chrpath Linux0.13GPLFalse
colorama0.3.1BSDTrue
conda3.7.0BSDTrue
conda-api1.1.0BSDFalse
conda-build1.8.2BSDTrue
configobj5.0.6BSDTrue
coverage3.7.1BSDFalse
cryptography0.5.4Apache 2.0True
cssselect0.9.1BSDFalse
cubes0.10.2MITFalse
curl Linux Mac7.38.0MIT/X derivateTrue
cython0.21Apache 2.0True
cytoolz0.7.0BSDTrue
datashape0.3.0BSDTrue
dateutil2.1BSDTrue
db Linux Mac5.3.28AGPLv3False
decorator3.4.0BSDTrue
django1.7BSDFalse
dnspython1.10.0as-isFalse
docutils0.12Public-Domain, PSF, 2-clause BSD, GPL3True
dynd-python0.6.5BSDTrue
ecdsa0.11MITFalse
enable4.3.0BSDTrue
enaml0.9.8BSDTrue
envisage4.4.0BSDFalse
ephem3.7.5.3LGPLFalse
faulthandler2.3BSDFalse
feedparser5.1.3MITFalse
fiona1.1.6BSDFalse
flake82.2.3MITFalse
flask0.10.1BSDTrue
freetype Linux Mac2.4.10FreeType LicenseTrue
future0.13.1MITTrue
futures2.1.6BSDTrue
gdal1.11.0MITFalse
gdata2.0.18Apache 2.0False
gensim0.10.2LGPLFalse
geos Linux Mac3.3.3LGPLFalse
gevent1.0.1MITTrue
gevent-websocket0.9.3ApacheTrue
googlecl0.9.12Apache 2.0False
greenlet0.4.4MITTrue
grin1.2.1BSDTrue
gunicorn Linux Mac19.1.0MITFalse
h5py2.3.1New BSDTrue
hdf5 Linux Mac1.8.13BSD-styleTrue
html5lib0.999MITFalse
hyde Linux Mac0.8.5MITFalse
iopro1.6.7proprietary - Continuum Analytics, Inc.False
ipython2.2.0BSDTrue
itsdangerous0.24BSD LicenseTrue
jdcal1.0BSDTrue
jinja22.7.3BSDTrue
jpeg Linux Mac8dCustom free software licenseTrue
kiwisolver0.1.3BSDTrue
launcher Mac Windows1.0.0proprietary - Continuum Analytics, Inc.True
lcms Linux Mac1.19MITTrue
libdynd Linux Mac0.6.5BSDTrue
libffi Linux3.0.13MITTrue
libnetcdf Linux Mac4.3.2MITFalse
libpng Linux Mac1.5.13Open SourceTrue
libsodium Linux Mac0.4.5MITTrue
libtiff Linux Mac4.0.2as-isTrue
libxml2 Linux Mac2.9.0MITTrue
libxslt Linux Mac1.1.28MITTrue
llvm Linux Mac3.3Open SourceTrue
llvmpy0.12.7New BSD LicenseTrue
logilab-common0.62.1LGPLFalse
lxml3.4.0BSDTrue
markdown Linux Mac2.5BSDFalse
markupsafe0.23BSDTrue
mathjax2.2ApacheFalse
matplotlib1.4.0PSF-basedTrue
mayavi4.3.1BSDFalse
mccabe0.2.1ExpatFalse
mdp3.3BSDFalse
menuinst Windows1.0.4BDFTrue
mercurial3.1GPLv2False
mingw Windows4.7GPLTrue
mock1.0.1BSDTrue
mpi4py Linux1.3BSDTrue
mpich2 Linux1.4.1p1mpich licenseTrue
multimethods1.0.0MITFalse
multipledispatch0.4.7BSDTrue
natsort3.5.0MITFalse
netcdf41.1.1MITFalse
networkx1.9.1BSDTrue
nltk3.0.0Apache 2.0True
node-webkit Mac Windows0.10.1MITTrue
nose1.3.4LGPLTrue
numba0.14.0numba licenseTrue
numexpr2.3.1MITTrue
numpy1.9.0BSDTrue
numpydoc0.4BSDFalse
openpyxl1.8.5MIT/ExpatTrue
openssl Linux Mac1.0.1hApache-styleTrue
pandas0.14.1BSDTrue
pandasql0.6.1BSDFalse
paramiko1.14.1LGPLFalse
passlib1.6.2BSDFalse
pastedeploy Linux Mac1.5.2MITFalse
patchelf Linux0.6GPL3False
patsy0.3.0BSD LicenseTrue
pep381client1.5Academic Free License, version 3False
pep81.5.7MIT LicenseTrue
pil1.1.7PIL licenseTrue
pillow Linux Mac2.5.1Standard PIL licenseFalse
pip1.5.6MITTrue
pixman Linux0.26.2MITTrue
ply3.4BSDTrue
psutil2.1.1BSDTrue
py1.4.25MITTrue
py2cairo Linux1.10.0LGPL 2.1 and MPL 1.1True
pyasn1 Linux0.1.6BSDFalse
pyaudio Mac0.2.7MITTrue
pycosat0.6.1MITTrue
pycparser2.10BSDTrue
pycrypto2.6.1Public DomainTrue
pycurl Linux Mac7.19.5LGPL and MIT/XTrue
pyface4.4.0BSDTrue
pyflakes0.8.1MITTrue
pygments1.6BSDTrue
pylint1.3.1GPLFalse
pymc Linux2.3.3Academic Free LicenseFalse
pymongo2.7.2Apache 2.0False
pyodbc3.0.7MITFalse
pyopenssl0.14APL2True
pyparsing2.0.1MITTrue
pyqt4.10.4GPLTrue
pyreadline Windows2.0BSDTrue
pysal1.6.0New BSD LicenseFalse
pysam Linux Mac0.6MITFalse
pyserial2.7PSFFalse
pytables3.1.1BSDTrue
pytest2.6.3MITTrue
python2.7.8PSFTrue
pytz2014.7MITTrue
pywin32 Windows219PSFTrue
pyyaml3.11MITTrue
pyzmq14.3.1LGPL and BSDTrue
qt Linux Mac4.8.5LGPLTrue
queuelib1.2.2BSDFalse
readline Linux Mac6.2GPL 3True
redis Linux Mac2.6.93-clause BSDTrue
redis-py Linux Mac2.9.1MITTrue
reportlab3.1.8BSDFalse
repoze.lru0.6BSDFalse
requests2.4.1ISCTrue
rope0.9.4GPLTrue
runipy0.1.1BSDTrue
scikit-bio Linux Mac0.2.0BSDFalse
scikit-image0.10.1Modified BSDTrue
scikit-learn0.15.23-clause BSDTrue
scipy0.14.0BSDTrue
scrapy0.24.4BSDFalse
setuptools5.8PSF or ZPLTrue
shapely Linux Mac1.4.1BSDFalse
sip Linux Mac4.15.5GPLTrue
six1.8.0MITTrue
sockjs-tornado1.0.1MITTrue
sphinx1.2.3BSDTrue
spyder2.3.1MITTrue
sqlalchemy0.9.7MITTrue
sqlite Linux Mac3.8.4.1Public DomainTrue
sqlparse0.1.12BSDFalse
ssh1.8.0LGPLFalse
ssl_match_hostname3.4.0.2PSFTrue
starcluster Linux0.93.3LGPLFalse
statsmodels0.5.03-clause Modified BSDTrue
sympy0.7.5New BSDTrue
theano Linux0.6.0BSDTrue
tk Linux Mac8.5.15BSD-styleTrue
toolz0.7.0BSDTrue
tornado4.0.2ApacheTrue
traits4.4.0BSDTrue
traitsui4.4.0BSDTrue
twisted14.0.2MITFalse
ujson1.33BSDTrue
unicodecsv0.9.4BSDTrue
unixodbc Linux2.3.1???False
util-linux Linux2.21GPLTrue
uuid1.30???False
venusian1.0BSDFalse
vtk5.10.1BSDFalse
w3lib1.8.1BSDFalse
werkzeug0.9.6BSDTrue
whoosh2.5.7BSDFalse
workerpool Linux0.9.2MITFalse
xlrd0.9.3BSDTrue
xlsxwriter0.5.7BSDTrue
xlutils1.7.1MITFalse
xlwings Windows0.2.2BSD 3-clauseTrue
xlwt0.7.5BSDTrue
yaml Linux Mac0.1.4MITTrue
yt Linux Mac3.0.1BSDFalse
zeromq Linux Mac4.0.4LGPLTrue
zlib Linux Mac1.2.7zlibTrue
zope.interface4.1.1Zope Public LicenseFalse