%matplotlib inline
from __future__ import print_function, division
def inverse(input):
print("starting")
x = 2*input
y = 1/x
print("done")
return y
#%pdb
Convenient means of defining lists
inverse(1)
starting done
0.5
[item**2 for item in range(10)]
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
[item**2 for item in range(10) if item % 2 == 0]
[0, 4, 16, 36, 64]
names = ['Bill', 'Sally', 'Bozo', 'Sue', 'Kathryn']
[name + ' the great' for name in names if len(name) == 4]
['Bill the great', 'Bozo the great']
# printing with no new line
print("hello there",end='') # in the python 2 style print it would be: print "hello there",
print(" hello again")
hello there hello again
##Getting interactive input
value = raw_input('enter a positive integer')
print(value*2)
enter a positive integer22 2222
# but what if I really want to multiply the value above as an integer
2 * int(value)
44
",".join(names) # If consistent and logical, very unintuitive to newcomers!
'Bill,Sally,Bozo,Sue,Kathryn'
# str() vs repr()
# First is how something usually would be printed, second is often how it would be created in
# python source code. (sort of)
import numpy as np
a = np.arange(5)
b = 2
c = "2"
print(str(a), str(b), str(c))
print(a, b, c)
print(repr(a), repr(b), repr(str(b)), repr(c)) # what you see if you just type the value at the command line
[0 1 2 3 4] 2 2 [0 1 2 3 4] 2 2 array([0, 1, 2, 3, 4]) 2 '2' '2'
s = 'abcd Efgh'
print(s.upper(),'\n', s.lower(),'\n', s.capitalize())
ABCD EFGH abcd efgh Abcd efgh
print(s.find('cd'), s.find('zz'))
print(s.startswith('ab'))
2 -1 True
f = open('data/ascii/sextractor.dat') # get a file handle
for line in f: print(line)
f.close()
# 1 NUMBER Galaxy ID number # 2 FLUX_ISO # 3 FLUXERR_ISO # 4 VALUES Note column 5 is missing # 6 FLAG 1 0.02580616000000000 0.03974229000000000 1.6770000000000000 0.2710000000000000 0 2 5.72769100000000009 0.20643300000000001 2.6250000000000000 2.5219999999999998 0 3 88.31933999999999685 0.59369850000000002 5.9249999999999998 4.7140000000000004 0
f = open('data/ascii/sextractor.dat') # get a file handle
lines = f.readlines()
lines
['# 1 NUMBER Galaxy ID number\n', '# 2 FLUX_ISO \n', '# 3 FLUXERR_ISO \n', '# 4 VALUES Note column 5 is missing\n', '# 6 FLAG\n', '1 0.02580616000000000 0.03974229000000000 1.6770000000000000 0.2710000000000000 0\n', '2 5.72769100000000009 0.20643300000000001 2.6250000000000000 2.5219999999999998 0\n', '3 88.31933999999999685 0.59369850000000002 5.9249999999999998 4.7140000000000004 0\n']
# writing a file
outtext = ''.join(lines)
outtext
'# 1 NUMBER Galaxy ID number\n# 2 FLUX_ISO \n# 3 FLUXERR_ISO \n# 4 VALUES Note column 5 is missing\n# 6 FLAG\n1 0.02580616000000000 0.03974229000000000 1.6770000000000000 0.2710000000000000 0\n2 5.72769100000000009 0.20643300000000001 2.6250000000000000 2.5219999999999998 0\n3 88.31933999999999685 0.59369850000000002 5.9249999999999998 4.7140000000000004 0\n'
outfile = open('test.dat','w')
outfile.write(outtext)
outfile.close()
ls
Astropy_Basics.ipynb
More_Python.ipynb
Python_Basics.ipynb
README.md
Untitled0.ipynb
data/
joinnb.py
lat_background_model_slice.fits
lat_background_model_slice_allhdus.fits
random_array.fits
random_array2.fits
random_array3.fits
test.dat
test.fits
test.vot
test2.dat
re
: regular expressions [not covered here]sys
: system level informationos
: access to OS-level servicesos.path
: platform independent path and filename manipulationshutil
: higher level file/directory operationsglob
: file wildcard utilitiestime
: time functionsdatetime
: date/time functionsargparse
: tools for parsing command line optionsStringIO
: reading and writing from strings as though they were filespdb
: debugger [not covered hereimport sys
print(sys.path)
print(sys.version)
# also contains sysout sysin pointers
['', '/usr/stsci/ssbx/python/lib/python2.7/site-packages/six-1.8.0-py2.7.egg', '/usr/stsci/ssbx/python/lib/python2.7/site-packages/matplotlib-1.4.2-py2.7-macosx-10.6-x86_64.egg', '/usr/stsci/ssbx/python/lib/python2.7/site-packages/mock-1.0.1-py2.7.egg', '/usr/stsci/ssbx/python/lib/python2.7/site-packages/pytz-2014.10-py2.7.egg', '/usr/stsci/ssbx/python/lib/python2.7/site-packages/pyobjc_core-3.0.3-py2.7-macosx-10.6-x86_64.egg', '/usr/stsci/ssbx/python/lib/python2.7/site-packages/pyobjc_framework_Cocoa-3.0.3-py2.7-macosx-10.6-x86_64.egg', '/usr/stsci/ssbx/python/lib/python2.7/site-packages/pyobjc_framework_Quartz-3.0.3-py2.7-macosx-10.6-x86_64.egg', '/usr/stsci/ssbx/python/lib/python2.7/site-packages/Cython-0.21.1-py2.7-macosx-10.6-x86_64.egg', '/usr/stsci/ssbx/python/lib/python2.7/site-packages/webbpsf-0.3.2-py2.7.egg', '/usr/stsci/ssbx/python/lib/python2.7/site-packages/jsonschema-2.4.0-py2.7.egg', '/usr/stsci/ssbx/python/lib/python2.7/site-packages/d2to1-0.2.13.dev-py2.7.egg', '/usr/stsci/ssbx/python/lib/python2.7/site-packages/stsci.distutils-0.3.8.dev38362-py2.7.egg', '/usr/stsci/ssbx/python/lib/python2.7/site-packages/stsci.tools-3.3.0.dev38362-py2.7.egg', '/usr/stsci/ssbx/python/lib/python2.7/site-packages/stsci.ndimage-0.10.1.dev38362-py2.7-macosx-10.6-x86_64.egg', '/usr/stsci/ssbx/python/lib/python2.7/site-packages/stsci.convolve-2.1.3.dev38362-py2.7-macosx-10.6-x86_64.egg', '/usr/stsci/ssbx/python/lib/python2.7/site-packages/pyfits-3.4.dev-py2.7-macosx-10.6-x86_64.egg', '/usr/stsci/ssbx/python/lib/python2.7/site-packages/stwcs-1.2.1.dev38362-py2.7.egg', '/usr/stsci/ssbx/python/lib/python2.7/site-packages/stsci.stimage-0.2.1.dev38362-py2.7-macosx-10.6-x86_64.egg', '/usr/stsci/ssbx/python/lib/python2.7/site-packages/stsci.sphere-0.2-py2.7-macosx-10.6-x86_64.egg', '/usr/stsci/ssbx/python/lib/python2.7/site-packages/stsci.imagestats-1.4.1.dev38362-py2.7-macosx-10.6-x86_64.egg', '/usr/stsci/ssbx/python/lib/python2.7/site-packages/stsci.imagemanip-1.1.1.dev38362-py2.7-macosx-10.6-x86_64.egg', '/usr/stsci/ssbx/python/lib/python2.7/site-packages/stsci.image-2.1.1.dev38362-py2.7-macosx-10.6-x86_64.egg', '/usr/stsci/ssbx/python/lib/python2.7/site-packages/pywcs-1.12.1.dev4596-py2.7-macosx-10.6-x86_64.egg', '/usr/stsci/ssbx/python/lib/python2.7/site-packages/pysynphot-0.9.7.dev-py2.7-macosx-10.6-x86_64.egg', '/usr/stsci/ssbx/python/lib/python2.7/site-packages/pydrizzle-6.4.4.dev38362-py2.7-macosx-10.6-x86_64.egg', '/usr/stsci/ssbx/python/lib/python2.7/site-packages/nictools-1.1.3.dev38362-py2.7.egg', '/usr/stsci/ssbx/python/lib/python2.7/site-packages/fitsblender-0.2.5.dev38362-py2.7.egg', '/usr/stsci/ssbx/python/lib/python2.7/site-packages/calcos-3.0-py2.7-macosx-10.6-x86_64.egg', '/usr/stsci/ssbx/python/lib/python2.7/site-packages/astrolib.coords-0.39.6.dev4596-py2.7-macosx-10.6-x86_64.egg', '/usr/stsci/ssbx/python/lib/python2.7/site-packages/wfpc2tools-1.0.3.dev38362-py2.7.egg', '/usr/stsci/ssbx/python/lib/python2.7/site-packages/wfc3tools-1.3.1.dev38362-py2.7.egg', '/usr/stsci/ssbx/python/lib/python2.7/site-packages/stsci.sphinxext-1.2.2.dev-py2.7.egg', '/usr/stsci/ssbx/python/lib/python2.7/site-packages/stsci.skypac-0.9.dev38362-py2.7.egg', '/usr/stsci/ssbx/python/lib/python2.7/site-packages/stsci.samplepackage-1.1.dev38362-py2.7-macosx-10.6-x86_64.egg', '/usr/stsci/ssbx/python/lib/python2.7/site-packages/stsci.numdisplay-1.6.1.dev38362-py2.7.egg', '/usr/stsci/ssbx/python/lib/python2.7/site-packages/stistools-1.0.3.dev38362-py2.7.egg', '/usr/stsci/ssbx/python/lib/python2.7/site-packages/reftools-1.6.7.dev38362-py2.7-macosx-10.6-x86_64.egg', '/usr/stsci/ssbx/python/lib/python2.7/site-packages/pyregion-1.1.2-py2.7-macosx-10.6-x86_64.egg', '/usr/stsci/ssbx/python/lib/python2.7/site-packages/pyraf-2.2.dev-py2.7-macosx-10.6-x86_64.egg', '/usr/stsci/ssbx/python/lib/python2.7/site-packages/opuscoords-1.0.2.dev38362-py2.7-macosx-10.6-x86_64.egg', '/usr/stsci/ssbx/python/lib/python2.7/site-packages/multidrizzle-3.4.3.dev38362-py2.7.egg', '/usr/stsci/ssbx/python/lib/python2.7/site-packages/drizzlepac-2.0.0.dev38362-py2.7-macosx-10.6-x86_64.egg', '/usr/stsci/ssbx/python/lib/python2.7/site-packages/costools-1.2.1.dev38362-py2.7.egg', '/usr/stsci/ssbx/python/lib/python2.7/site-packages/acstools-1.8.0.dev38362-py2.7-macosx-10.6-x86_64.egg', '/usr/stsci/ssbx/python/lib/python2.7/site-packages/jwst_lib.stpipe-0.0-py2.7.egg', '/usr/stsci/ssbx/python/lib/python2.7/site-packages/jwst_lib.modeling-0.2.4-py2.7.egg', '/usr/stsci/ssbx/python/lib/python2.7/site-packages/jwst_lib.models-0.0-py2.7.egg', '/usr/stsci/ssbx/python/lib/python2.7/site-packages/jwst_lib.astdata-0.0-py2.7.egg', '/usr/stsci/ssbx/python/lib/python2.7/site-packages/jwst_lib.wcs-0.0-py2.7.egg', '/usr/stsci/ssbx/python/lib/python2.7/site-packages/jwst_pipeline.ami-0.1-py2.7.egg', '/usr/stsci/ssbx/python/lib/python2.7/site-packages/jwst_pipeline.assign_wcs-0.1-py2.7.egg', '/usr/stsci/ssbx/python/lib/python2.7/site-packages/jwst_pipeline.bias_drift-0.1-py2.7.egg', '/usr/stsci/ssbx/python/lib/python2.7/site-packages/jwst_pipeline.dark_current-0.2-py2.7.egg', '/usr/stsci/ssbx/python/lib/python2.7/site-packages/jwst_pipeline.dq_init-0.1-py2.7.egg', '/usr/stsci/ssbx/python/lib/python2.7/site-packages/jwst_pipeline.emission-0.1-py2.7.egg', '/usr/stsci/ssbx/python/lib/python2.7/site-packages/jwst_pipeline.extract_1d-0.1-py2.7.egg', '/usr/stsci/ssbx/python/lib/python2.7/site-packages/jwst_pipeline.extract_2d-0.1-py2.7.egg', '/usr/stsci/ssbx/python/lib/python2.7/site-packages/jwst_pipeline.flatfield-0.1-py2.7.egg', '/usr/stsci/ssbx/python/lib/python2.7/site-packages/jwst_pipeline.fringe-0.1-py2.7.egg', '/usr/stsci/ssbx/python/lib/python2.7/site-packages/jwst_pipeline.ipc-0.1-py2.7.egg', '/usr/stsci/ssbx/python/lib/python2.7/site-packages/jwst_pipeline.jump-0.2-py2.7.egg', '/usr/stsci/ssbx/python/lib/python2.7/site-packages/jwst_pipeline.lastframe-0.1-py2.7.egg', '/usr/stsci/ssbx/python/lib/python2.7/site-packages/jwst_pipeline.linearity-0.3-py2.7.egg', '/usr/stsci/ssbx/python/lib/python2.7/site-packages/jwst_pipeline.persistence-0.1-py2.7.egg', '/usr/stsci/ssbx/python/lib/python2.7/site-packages/jwst_pipeline.photom-0.1-py2.7.egg', '/usr/stsci/ssbx/python/lib/python2.7/site-packages/jwst_pipeline.pipeline-0.1-py2.7.egg', '/usr/stsci/ssbx/python/lib/python2.7/site-packages/jwst_pipeline.ramp_fitting-0.1-py2.7.egg', '/usr/stsci/ssbx/python/lib/python2.7/site-packages/jwst_pipeline.resample-0.1a0-py2.7.egg', '/usr/stsci/ssbx/python/lib/python2.7/site-packages/jwst_pipeline.reset-0.1-py2.7.egg', '/usr/stsci/ssbx/python/lib/python2.7/site-packages/jwst_pipeline.saturation-0.1-py2.7.egg', '/usr/stsci/ssbx/python/lib/python2.7/site-packages/jwst_pipeline.straylight-0.2-py2.7.egg', '/usr/stsci/ssbx/python/lib/python2.7/site-packages/jwst_pipeline.wfs_combine-0.1-py2.7.egg', '/usr/stsci/ssbx/python/lib/python2.7/site-packages/jwst_tools.csv_tools-0.6-py2.7.egg', '/usr/stsci/ssbx/python/lib/python2.7/site-packages/jwst_tools.fits_generator-0.1-py2.7.egg', '/usr/stsci/ssbx/python/lib/python2.7/site-packages/jwst_tools.jwpsf-3.1-py2.7.egg', '/usr/stsci/ssbx/python/lib/python2.7/site-packages/jwst_tools.spectools-0.1-py2.7.egg', '/usr/stsci/ssbx/python/lib/python2.7/site-packages/jwst_tools.associations-0.6-py2.7.egg', '/Users/perry/ssbvirt/ssbx-osx/lib/python27.zip', '/Users/perry/ssbvirt/ssbx-osx/lib/python2.7', '/Users/perry/ssbvirt/ssbx-osx/lib/python2.7/plat-darwin', '/Users/perry/ssbvirt/ssbx-osx/lib/python2.7/plat-mac', '/Users/perry/ssbvirt/ssbx-osx/lib/python2.7/plat-mac/lib-scriptpackages', '/Users/perry/ssbvirt/ssbx-osx/lib/python2.7/lib-tk', '/Users/perry/ssbvirt/ssbx-osx/lib/python2.7/lib-old', '/Users/perry/ssbvirt/ssbx-osx/lib/python2.7/lib-dynload', '/usr/stsci/ssbx/python/lib/python2.7', '/usr/stsci/ssbx/python/lib/python2.7/plat-darwin', '/usr/stsci/ssbx/python/lib/python2.7/lib-tk', '/usr/stsci/ssbx/python/lib/python2.7/plat-mac', '/usr/stsci/ssbx/python/lib/python2.7/plat-mac/lib-scriptpackages', '/Users/perry/ssbvirt/ssbx-osx/lib/python2.7/site-packages', '/usr/stsci/ssbx/python/lib/python2.7/site-packages', '/usr/stsci/ssbx/python/lib/python2.7/site-packages/IPython/extensions'] 2.7.5 (default, Mar 13 2015, 01:50:03) [GCC 4.2.1 (Apple Inc. build 5664)]
import os
print(os.environ)
{'IDL_DIR': '/Applications/exelis/idl84', 'UR_TMP': '/Users/perry/.ureka_tmp/ssbx', 'hbin': '/usr/stsci/ssbx/iraf//unix/bin.macosx/', 'INFOPATH': '/sw/share/info:/sw/info:/usr/share/info', 'VIRTUAL_ENV': '/Users/perry/ssbvirt/ssbx-osx', 'SHELL': '/bin/tcsh', 'XDG_DATA_DIRS': '/sw/share', 'GDFONTPATH': '/Library/Fonts', 'HSI_LFLAGS': '', 'GNOME_DISABLE_CRASH_DIALOG': '1', 'MANPATH': '/usr/stsci/ssbx/share/man:/usr/stsci/ssbx/variants/common/man:/usr/stsci/ssbx/variants/common/man:/usr/stsci/ssbx/variants/common/share/man:/usr/stsci/ssbx/python/man:/usr/stsci/ssbx/python/share/man::/sw/share/man:/usr/share/man:/usr/local/share/man:/opt/X11/share/man:/usr/texbin/man:/Applications/Xcode.app/Contents/Developer/usr/share/man:/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/share/man:/sw/lib/perl5/5.16.2/man', 'XDG_CONFIG_HOME': '/Users/perry/.config', 'VENDOR': 'apple', 'XDG_RUNTIME_DIR': '/var/folders/m1/8vfhq2315vlcqkbn6bhh4wgh0003y2/C/', 'RANLIB': 'ranlib', 'HSI_FF': '-I/usr/stsci/ssbx/iraf//include/ -O -arch i386 -m32 -DBLD_KERNEL -mmacosx-version-min=10.4', 'DBUS_SESSION_BUS_ADDRESS': 'launchd:env=DBUS_FINK_SESSION_BUS_SOCKET', 'F2C': '/usr/stsci/ssbx/iraf//unix/bin.macosx//f2c.e', 'IMTOOLRC': '/usr/stsci/ssbx/iraf/dev/imtoolrc', 'hostid': 'unix', 'HSI_OSLIBS': '', 'HSI_LIBS': '/usr/stsci/ssbx/iraf//unix/hlib/libboot.a /usr/stsci/ssbx/iraf//lib/libsys.a /usr/stsci/ssbx/iraf//lib/libvops.a /usr/stsci/ssbx/iraf//unix/hlib/libos.a ', 'MACHTYPE': 'x86_64', 'GROUP': 'STSCI\\science', 'XDG_DATA_HOME': '/Users/perry/.local/share', 'USER': 'perry', 'QTDIR': '/sw/lib/qt3', 'hlib': '/usr/stsci/ssbx/iraf//unix/hlib/', 'tmp': '/tmp/', 'HSI_CF': '-I/usr/stsci/ssbx/iraf//include/ -O -DMACOSX -w -Wunused -arch i386 -m32 -mmacosx-version-min=10.4', 'SHLVL': '1', 'GNUPLOT_DEFAULT_GDFONT': 'Arial', 'FERMI_DIR': '/Applications/ST_sci/heasoft-6.15.1/i386-apple-darwin13.0.2', 'TERM_SESSION_ID': 'D09F90D8-889F-4B0D-9557-A9DFEB9CDE4B', 'HSI_XF': '-I/usr/stsci/ssbx/iraf//include/ -Inolibc -/DMACOSX -w -/Wunused -/m32 -/arch -//i386 -/mmacosx-version-min=10.4', 'XAPPLRESDIR': '/sw/etc/app-defaults/', 'iraf': '/usr/stsci/ssbx/iraf/', 'host': '/usr/stsci/ssbx/iraf//unix/', 'PKG_CONFIG_PATH': '/usr/stsci/ssbx/python/lib/pkgconfig', 'TMPDIR': '/var/folders/m1/8vfhq2315vlcqkbn6bhh4wgh0003y2/T/', 'PERL5LIB': '/sw/lib/perl5:/sw/lib/perl5/darwin', 'XDG_CONFIG_DIRS': '/sw/etc/xdg', 'HSI_F77LIBS': '', 'GDK_USE_XFT': '1', 'IDL_PATH': '.:+/Applications/exelis/local/astron:+/Applications/exelis/idl84/lib', 'EXELIS_DIR': '/Applications/exelis', 'GDL_PATH': '+/sw/share/gnudatalanguage/lib', 'UR_VARIANT': 'common', 'PAGER': 'cat', 'HEADAS': '/Applications/ST_sci/heasoft-6.15.1/i386-apple-darwin13.0.2', 'REMOTEHOST': '', 'TERM_PROGRAM_VERSION': '326', 'UR_DIR': '/usr/stsci/ssbx', 'HOME': '/Users/perry', 'UR_DIR_PKG': '/usr/stsci/ssbx/variants/common/', 'DISPLAY': '/tmp/launch-sGTuMd/org.macosforge.xquartz:0', 'TERM_PROGRAM': 'Apple_Terminal', 'LANG': 'en_US.UTF-8', 'Apple_PubSub_Socket_Render': '/tmp/launch-OITUtg/Render', 'GVBINDIR': '/usr/stsci/ssbx/python/lib/graphviz', 'ac_cv_path_INTLTOOL_PERL': '/usr/bin/perl', 'OSTYPE': 'darwin', 'UR_CPU': 'x86', 'CC': 'cc', 'HOST': 'beeblebrox.home', 'GIT_PAGER': 'cat', 'IRAFARCH': 'macosx', 'F77': '/usr/stsci/ssbx/iraf//unix/hlib//f77.sh', 'UR_BITS': '64', '__CF_USER_TEXT_ENCODING': '0xFC2:0:0', '__CHECKFIX1436934': '1', 'HOSTTYPE': 'intel-mac', 'LOGNAME': 'perry', 'PATH': '/Users/perry/ssbvirt/ssbx-osx/bin:/usr/stsci/ssbx/variants/common/bin:/usr/stsci/ssbx/bin:/usr/stsci/ssbx/python/bin:/Users/perry/bin:/Applications/ST_sci/SM/bin:/sw/bin:/sw/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin:/usr/texbin:/usr/X11R6/bin', 'TERM': 'xterm-color', 'UR_OS': 'osx-9', 'SSH_AUTH_SOCK': '/tmp/launch-sSf0Et/Listeners', 'XDG_CACHE_HOME': '/Users/perry/.cache', 'XML_CATALOG_FILES': '/sw/etc/xml/catalog', 'MACH': 'macosx', 'PGPLOT_DIR': '/sw/lib/pgplot', 'HSI_LF': '-I/usr/stsci/ssbx/iraf//include/ -arch i386 -m32 -mmacosx-version-min=10.4', 'CLICOLOR': '1', 'PWD': '/Users/perry/dropbox/gitfork/UserTraining2015', 'SGML_CATALOG_FILES': '/sw/etc/sgml/catalog'}
# Contains functions to run commands, change directories, get environment, get uid, etc
print(os.system('ls -l')) #works in terminal window, but not in notebook
import subprocess
print(subprocess.check_output(['ls','-l']))
0 total 6256 -rw-r--r--@ 1 perry STSCI\science 311605 May 6 08:24 Astropy_Basics.ipynb -rw-r--r--@ 1 perry STSCI\science 16354 May 6 08:32 More_Python.ipynb -rw-r--r--@ 1 perry STSCI\science 56393 May 6 08:09 Python_Basics.ipynb -rw-r--r--@ 1 perry STSCI\science 334 May 1 13:32 README.md -rw-r--r--@ 1 perry STSCI\science 2630 May 4 13:30 Untitled0.ipynb drwxr-xr-x@ 12 perry STSCI\science 408 Apr 28 11:17 data -rw-r--r--@ 1 perry STSCI\science 446 May 1 08:19 joinnb.py -rw-r--r--@ 1 perry STSCI\science 1039680 May 6 08:11 lat_background_model_slice.fits -rw-r--r--@ 1 perry STSCI\science 1045440 May 6 08:11 lat_background_model_slice_allhdus.fits -rw-r--r--@ 1 perry STSCI\science 135360 May 6 08:11 random_array.fits -rw-r--r--@ 1 perry STSCI\science 138240 May 6 08:12 random_array2.fits -rw-r--r--@ 1 perry STSCI\science 406080 May 6 08:12 random_array3.fits -rw-r--r--@ 1 perry STSCI\science 369 May 6 08:33 test.dat -rw-r--r--@ 1 perry STSCI\science 8640 May 6 08:11 test.fits -rw-r--r--@ 1 perry STSCI\science 1403 May 6 08:11 test.vot -rw-r--r--@ 1 perry STSCI\science 369 May 5 20:54 test2.dat
# or just this
!ls -l
total 6256
-rw-r--r--@ 1 perry STSCI\science 311605 May 6 08:24 Astropy_Basics.ipynb
-rw-r--r--@ 1 perry STSCI\science 16354 May 6 08:32 More_Python.ipynb
-rw-r--r--@ 1 perry STSCI\science 56393 May 6 08:09 Python_Basics.ipynb
-rw-r--r--@ 1 perry STSCI\science 334 May 1 13:32 README.md
-rw-r--r--@ 1 perry STSCI\science 2630 May 4 13:30 Untitled0.ipynb
drwxr-xr-x@ 12 perry STSCI\science 408 Apr 28 11:17 data
-rw-r--r--@ 1 perry STSCI\science 446 May 1 08:19 joinnb.py
-rw-r--r--@ 1 perry STSCI\science 1039680 May 6 08:11 lat_background_model_slice.fits
-rw-r--r--@ 1 perry STSCI\science 1045440 May 6 08:11 lat_background_model_slice_allhdus.fits
-rw-r--r--@ 1 perry STSCI\science 135360 May 6 08:11 random_array.fits
-rw-r--r--@ 1 perry STSCI\science 138240 May 6 08:12 random_array2.fits
-rw-r--r--@ 1 perry STSCI\science 406080 May 6 08:12 random_array3.fits
-rw-r--r--@ 1 perry STSCI\science 369 May 6 08:33 test.dat
-rw-r--r--@ 1 perry STSCI\science 8640 May 6 08:11 test.fits
-rw-r--r--@ 1 perry STSCI\science 1403 May 6 08:11 test.vot
-rw-r--r--@ 1 perry STSCI\science 369 May 5 20:54 test2.dat
import os.path
os.path.join('mydir','data.dat') # handles platform variances like windows '\'
'mydir/data.dat'
os.path.splitext(os.path.split('/a/b/c/file.txt')[1])
('file', '.txt')
import shutil
shutil.copyfile('test.dat','test2.dat')
# has copytree(), rmtree(), move(), make_archive()...
import glob
fl = glob.glob('*.fits')
print(fl)
['lat_background_model_slice.fits', 'lat_background_model_slice_allhdus.fits', 'random_array.fits', 'random_array2.fits', 'random_array3.fits', 'test.fits']
import time
t = time.time()
print(t)
tt = time.localtime(t)
print(tt)
print(time.strftime('%y, %m, %d', tt))
time.sleep(5)
1430915614.24 time.struct_time(tm_year=2015, tm_mon=5, tm_mday=6, tm_hour=8, tm_min=33, tm_sec=34, tm_wday=2, tm_yday=126, tm_isdst=1) 15, 05, 06
a = np.zeros((10,10))
a = np.ones((10,10))
a = np.arange(10)
a = np.linspace(1,10,100)
a = np.array([1,2,6,9.2])
a = np.arange(10)
a.shape=(2,5)
print(a)
b = np.arange(5)
a*b
c = np.arange(2)
a*c
[[0 1 2 3 4] [5 6 7 8 9]]
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-34-3d63b1e84bf7> in <module>() 5 a*b 6 c = np.arange(2) ----> 7 a*c ValueError: operands could not be broadcast together with shapes (2,5) (2,)
a*c[:, np.newaxis]
x = np.arange(10.)* 2
y = x**2
import numpy.random as ran
xx = ran.uniform(0., 18., size=(100,)) # 100 random numbers between 0 and 18
xind = np.searchsorted(x, xx)-1 # indices where x is next value below that in xx
xfract = (xx - x[xind])/(x[xind+1]-x[xind]) # fractional distance between x points
yy = y[xind] + xfract*(y[xind+1]-y[xind]) # the interpolated values for xx
import astropy.io.fits as fits
from matplotlib import pyplot as plt
im = fits.getdata('data/pix.fits')
plt.imshow(im,vmax=500)
<matplotlib.image.AxesImage at 0x108b2b850>
y, x = np.indices((512,512)) # first determine radii of all pixels
plt.imshow(y)
<matplotlib.image.AxesImage at 0x108becc10>
r = np.sqrt((x-257.)**2+(y-258)**2)
plt.imshow(r)
<matplotlib.image.AxesImage at 0x108d7db10>
ind = np.argsort(r.flat) # get sorted indices (could use sort
# if not needed to arange image values too
sr = r.flat[ind] # sorted radii
sim = im.flat[ind] # image values sorted by radii
ri = sr.astype(np.int16) # integer part of radii (bin size = 1)
# Things get trickier here; nd numbers in same radius bin
# by looking for where radius change value and determining distance between changes
deltar = ri[1:] - ri[:-1] # assume all radii represented # (more work if not)
rind = np.where(deltar)[0] # location of changed radius
nr = rind[1:] - rind[:-1] # number in radius bin
csim = np.cumsum(sim, dtype=np.float64) # cumulative sum to figure out sums for each radii bin
tbin = csim[rind[1:]] - csim[rind[:-1]] # sum for image values in radius bins
radialprofile = tbin/nr # the answer
plt.semilogy(radialprofile)
[<matplotlib.lines.Line2D at 0x108d19f50>]
p = .4
n = 1000000 # initial ensemble size
n2heads = 0; n2tails = 0 # totals for each case
import numpy.random as ran
prev = ran.rand(n) < p # true if heads, false if not
while n>0:
next = ran.rand(n) < p
notdone = (next != prev)
n2heads += np.sum(prev & next,dtype=np.int64) # careful about sums!
n2tails += np.sum(np.logical_not(prev | next),dtype=np.int64)
prev = next[notdone]
n = len(prev)
print(n)
480867 240103 115234 57285 27242 13680 6644 3324 1592 792 362 175 87 34 15 10 3 2 2 0
grid = np.zeros((500,500),np.float32)
prevgrid = grid.copy()
grid[200:300,200:300] = 100.
done = False
i=0
while not done:
i +=1
prevgrid[:,:] = grid # just reuse arrays
# new value is average of 4 neighboring values
grid[1:-1,1:-1] = 0.25*(
grid[:-2,1:-1]
+ grid[2:, 1:-1]
+ grid[1:-1,:-2]
+ grid[1:-1,2:])
grid[200:300,200:300] = 100.
diffmax = np.abs(grid - prevgrid).max()
print(i, diffmax)
if diffmax < 0.1: done = True
1 25.0 2 12.5 3 7.8125 4 6.25 5 4.6875 6 4.02832 7 3.49121 8 3.05481 9 2.69775 10 2.40269 11 2.15626 12 1.94833 13 1.82656 14 1.71423 15 1.61106 16 1.51649 17 1.42983 18 1.3504 19 1.2775 20 1.21053 21 1.14888 22 1.09204 23 1.03954 24 0.990955 25 0.956783 26 0.924164 27 0.893057 28 0.863405 29 0.835148 30 0.808222 31 0.782564 32 0.75811 33 0.734795 34 0.712559 35 0.691338 36 0.671085 37 0.651741 38 0.633259 39 0.615585 40 0.598682 41 0.585667 42 0.573036 43 0.560783 44 0.548899 45 0.537373 46 0.526194 47 0.515352 48 0.504837 49 0.494638 50 0.484747 51 0.475151 52 0.465839 53 0.456802 54 0.448036 55 0.439522 56 0.431255 57 0.423229 58 0.415436 59 0.40786 60 0.400505 61 0.394524 62 0.38867 63 0.382938 64 0.377327 65 0.371834 66 0.366455 67 0.361187 68 0.35603 69 0.350981 70 0.346041 71 0.3412 72 0.336458 73 0.33182 74 0.327278 75 0.322826 76 0.318466 77 0.314201 78 0.310017 79 0.30592 80 0.301907 81 0.297974 82 0.294121 83 0.29034 84 0.28664 85 0.283524 86 0.280457 87 0.277435 88 0.27446 89 0.271534 90 0.268648 91 0.265808 92 0.263014 93 0.260262 94 0.257553 95 0.254883 96 0.252256 97 0.249668 98 0.247122 99 0.244614 100 0.242146 101 0.239708 102 0.237312 103 0.234955 104 0.232628 105 0.230343 106 0.228088 107 0.225868 108 0.223682 109 0.221527 110 0.219398 111 0.217308 112 0.215248 113 0.213469 114 0.211712 115 0.209976 116 0.208258 117 0.20656 118 0.204884 119 0.203224 120 0.20159 121 0.199972 122 0.198374 123 0.196795 124 0.195232 125 0.193689 126 0.192163 127 0.190657 128 0.189167 129 0.187696 130 0.186237 131 0.184803 132 0.18338 133 0.181976 134 0.180588 135 0.179218 136 0.17786 137 0.176517 138 0.175194 139 0.173885 140 0.172588 141 0.17131 142 0.17004 143 0.168793 144 0.167553 145 0.166466 146 0.165388 147 0.164318 148 0.163261 149 0.162212 150 0.161175 151 0.160147 152 0.159128 153 0.158117 154 0.157118 155 0.156126 156 0.155146 157 0.154171 158 0.15321 159 0.152254 160 0.15131 161 0.150372 162 0.149445 163 0.148525 164 0.147614 165 0.146713 166 0.145817 167 0.144932 168 0.144054 169 0.143185 170 0.142323 171 0.141468 172 0.140621 173 0.139782 174 0.13895 175 0.13813 176 0.137314 177 0.136505 178 0.135704 179 0.134911 180 0.134121 181 0.133417 182 0.132721 183 0.13203 184 0.131344 185 0.130663 186 0.129988 187 0.129318 188 0.128651 189 0.127989 190 0.127333 191 0.126682 192 0.126036 193 0.125395 194 0.12476 195 0.124126 196 0.123501 197 0.122879 198 0.122259 199 0.121647 200 0.12104 201 0.120436 202 0.119839 203 0.11924 204 0.118652 205 0.118065 206 0.117485 207 0.116905 208 0.116333 209 0.115768 210 0.115204 211 0.114643 212 0.11409 213 0.113533 214 0.112988 215 0.112442 216 0.1119 217 0.111366 218 0.110832 219 0.110306 220 0.109783 221 0.10931 222 0.108837 223 0.108372 224 0.107906 225 0.107447 226 0.106987 227 0.106531 228 0.106079 229 0.105629 230 0.105179 231 0.104736 232 0.104294 233 0.103855 234 0.10342 235 0.102987 236 0.102556 237 0.102127 238 0.1017 239 0.101278 240 0.100859 241 0.100441 242 0.100027 243 0.0996132
plt.imshow(grid)
<matplotlib.image.AxesImage at 0x10b868090>