Kevin Anchukaitis
Assistant Research Professor

Lamont-Doherty Earth Observatory
Columbia University

Ph.D. Arizona, 2007

Tree-Ring Laboratory
61 Route 9W
Lamont-Doherty Earth Observatory
Palisades, NY 10964 USA
Phone: +(845) 365-8783
http://www.ldeo.columbia.edu/~kja

MATLAB Code for Earth and Environmental Science | Last updated 20 May 2011

These are functions or blocks of scripts that I've written or hacked over the course of my research. I'm making these available on the off-chance others might find them useful. If you do, please drop me a line and let me know. As is standard, all code and functions are provided 'as-is' with no expressed or implied warranty, and you use it at your own peril

MEBOOT - Implements the Maximum Entropy Bootstrap


MEBOOT - Maximum Entropy Bootstrapping

[ensemble] = meboot(x,reps,lb,ub,scale,clt)

Implements the Maximum Entropy Bootstrap (MEBOOT) for time series from Vinod and Lopez-de-Lacalle (2009).

FIXPERMIL - Correct MATLAB's infuriating inability to make a proper 'permil' sign in figures.


FIXPERMIL - Fix notation for permil in postscript Matlab figures

fixpermil(filename)

Matlab improperly codes the symbol for \permil in exported postscript. This wrapper function edits the raw postscript automatically so that the symbol will display correctly.

This function should be called after a PRINT command which has also used the -adobecset switch

Example:
figure(1); clf;
plot(randn(100,1),randn(100,1));
ylabel(['5',char(8240)]);
print -depsc2 -adobecset figure.eps
fixpermil('figure.eps')

PIN - Preindustrial correction for carbon isotope tree-ring series (d13C) for pC02


PIN - PreINdustrial correction for carbon isotope tree-ring series

[corrected, [details]] = pin(year,c,span,varargin)

A user-written Matlab function that performs a correction on carbon isotope series from tree-rings to account for changes in atmospheric pCO2 during the last ~150 years. The function is based on the physiological mechanisms and logical constraints discussed in detail in McCarroll et al. [2009].

RWL2MAT - a function to read Tucson decadal format files into Matlab


RWL2MAT a function to read Tucson decadal format files into Matlab

[timeseries,years,seriesnames] = rwl2mat(filename)

This function reads in the selected file from Tucson decadal format
and makes it available in the Matlab workspace. It is simpler, but
less powerful, than David Meko's RWL2TSM.

MAT2RWL - a function to write Tucson decadal format files from Matlab


MAT2RWL a function to write Tucson decadal format files from Matlab

mat2rwl(data,years,seriesnames,filename)

This function writes a matrix into a Tucson decadal format ASCII file.
Missing values should be 'NaN'.

EBISUZAKI - a method for estimating significant correlation values for autocorrelated time series


EBISUZAKI a method for estimating significant correlation values for autocorrelated time series

[F] = ebisuzaki(x,y,sig,nsim)

This function creates 'nsim' random time series that have the same power spectrum as the original time series but with random phases.

These are then used to estimate the significance of a correlation between real, autocorrelated data.

References
----------
Ebisuzaki, W, 1997: A method to estimate the statistical significance of a correlation when the data are serially correlated. J. of Climate, 10, 2147-2153.

GSBTEST - a method for estimating the significance of moving correlation analyses


GSBTEST - Monte Carlo Test of Moving Correlation Significance Levels following Gershunov et al. 2001

[sd99,sd95,sd90,sd5]=gsbTest(sL,sN,window,R,method)

Implementation of a Monte Carlo type simulation of moving correlation
functions between two weakly correlated white noise series for the purposes of constructing significance test for moving correlation functions based on their standard deviation (Gershunov et al. 2001).

Tips and Tricks for Scientific Computing with OSX

This is mostly a collection of LaTeX, Matlab, FORTRAN, or NCL code, assorted computing tricks, and other random bits of information that I'm trying to remember and keep track of, but hopefully you'll find them useful too. If you do, drop me a line and let me know.

Andy Jacobson's excellent Mac OS X for Oceanographers and Atmospheric Scientists is an great resource for earth and environmental scientists using OSX as a scientific computing platform. John Chiang also has some good notes, as does Eric Salathe.

[11] Mismatch between MATLAB and Macports libtiff


Description of the Problem: calls to ghostscript from MATLAB use the wrong libtiff as part of the Macports installation.

setenv('DYLD_LIBRARY_PATH',['/opt/local/lib/:' getenv('DYLD_LIBRARY_PATH')])

Tags: MATLAB, ghostscript, libtiff, system, eps, print, printeps

[10] Create single PDF from lots of EPS files


Description of the Problem: Create a single PDF from a large number of individual EPS files (say, those you've output from MATLAB). There are lots of ways of doing this, including using Preview.app, but from the command line this gives you a nice, single PDF

gs -sDEVICE=pdfwrite -sOutputFile=myfile.pdf -dBATCH -NOPAUSE -f *.eps

Tags: PDF, EPS, combine, ghostscript

[9] OPENDaP-enabled NCL for OSX


Description of the Problem: Despite having the correct libraries installed, running NCL version 5.1, OPENDaP-enabled for Intel Mac OSX, results in the following error:

dyld: Library not loaded: /Users/haley/tmp/5.1.0/lib/libnc-dap.3.dylib

The solution is eventually described by piecing together clues from this thread at the NCAR NCL discussion forum. If you have installed the correct libraries (libdap, libnc-dap, and ssl) via Macports, for instance, you should include the following line in one of your .bashrc or .profile files:

export DYLD_LIBRARY_PATH=/opt/local/lib

You'll also need to install gfortran, but if you get it from HPC, NCL will find it.

UPDATE: However, setting DYLD_LIBRARY_PATH seems to mess up Apple's native X11 (which Matlab uses. Doh.)

UPDATE 2: The trick is to set DYLD_FALLBACK_LIBRARY_PATH, which will allow X11 to see the default Apple path, but also let NCL find the libraries it wants in /op/local/lib. So:

export DYLD_FALLBACK_LIBRARY_PATH=/opt/local/lib


Tags: NCAR, NCL, libdap, libnc-dap, netcdf, OSX, gfortran, DYLD_LIBRARY_PATH

[8] For my eventual migration to Python ...


Description of the Problem: MATLAB ain't free

mohinish.blogspot.com/2007/01/python-for-matlab-users-on-mac.html
vnoel.wordpress.com/2008/05/03/bye-matlab-hello-python-thanks-sage
www.scipy.org/NumPy_for_Matlab_Users
www.ii.uib.no/~avle/python.html
matplotlib.sourceforge.net
www.pyngl.ucar.edu
www.geos.ed.ac.uk/homes/ceford/glggmt
code.google.com/p/pymc
macinscience.org
xkcd

On the otherhand, zero indexing and indentation as a language requirement for loops? Really? Ugh. Maybe not.

Tags: python, matlab, OSX

[7] Cleaning out the Macports distributions


Description of the Problem: Macports can leave a lot of old junk hanging around in /opt

Step by step suggestions here (be cautious and be wary of following these steps if you don't know exactly how it will affect your system.)

Tags: clean, macports, darwinports, disk space

[6] Make OSX Terminal Behavior More Like Linux


Description of the Problem: Annoyed at the differences between a Linux terminal and the OSX version? Yeah.

Try these modifications from the Macromates blog.

Tags: OSX, Terminal, Terminal.app, ssh, bash, .bashrc, dotfiles, history, completion, .inputrc

[5] Installing mexnc and OPeNDAP for Matlab on Mac OSX [OUTDATED]


Description of the Problem: This is mostly just for me, since I've now done this half a dozen times, each time just long enough after the previous time to have forgotten the little details. But maybe you'll also find this helpful. [UPDATE: This is becoming increasingly outdated.]

[1] Download the precompiled binaries for mexnc here. Make sure the correct files can be found on your matlabpath.

[2] I still use Chuck Dedham's object-oriented netcdf toolbox, which can be found here.

[3] You can setup OPeNDAP for Matlab using the new Matlab OPeNDAP Ocean Toolbox. Previously, you had to install libdap and libxml2, in addition to the Matlab OPeNDAP structure tools. Now it appears the Ocean Toolbox takes care of everything (although you might need to install libxml2 or libdap still?). The Ocean Toolbox appears to also put the proper files in the proper directory.

[4] There are some bugs in the loaddap and whodap functions used in Matlab to interface with OPeNDAP. First, you'll have to edit the function whodap.m, and remove the './' from in front of the system call to writedap. Second, the URL for the testing files is wrong in the documentation. The proper testing URL command is:

whodap http://test.opendap.org/opendap/data/nc/fnoc1.nc

Some further details and hints here:

http://mailman.opendap.org/pipermail/opendap/2006-November/000584.html

[5] Rob Hetland at Texas A+M University has notes on building your own netCDF for OSX here.

Tags: netcdf, Matlab, mexnc, OPeNDAP

[4] Using netCDF operators (NCO) to concatenate files without a record dimension


Description of the Problem: Imagine you have a set of netcdf files, each with a single month of a multiyear dataset. In some cases, the time dimension will not be the record dimension. This means you can't use the netcdf (NCO) operator ncrcat to concatenate the individual months along the time dimension. But, there is a work around:

Solution: run the following commands in a bash shell script [you can get mine here: makeSingleNC.sh]

list=`ls *.nc`
for i in $list;
do
ncecat -O $i $i
ncpdq -O -a time,record $i $i
ncwa -O -a record $i $i
done
ncrcat -h *.nc out.nc


Tags: netcdf, nco, .nc, no record dimension, ncrcat, ncecat

[3] Matlab m_map contours not filled


Description of the Problem: Filled contour maps using m_contourf from the m_map mapping package are not filled

Solution: Insert the following command after the call to m_grid

set(findobj('tag','m_grid_color'),'facecolor','none')

Tags: matlab, m_map, m_contourf, white, unfilled, patch, disappear

[2] OSX (Tiger) Updates and Security Updates Break Kerberos, SSH, and (sometimes) Keychain


Description of the Problem: Following Software Update installation of OSX Security and Software Updates, Kerberos and SSH are broken. One symptom is that ssh attempts result in a 'segmentation fault'.

Solution: The current (best? only?) description of the solution for this problem is from Eric Salathe, in this thread from the Apple Discussion Forums.

Other links to pages discussing this problem:

Mac Singularity (April 20, 2007)
Mac Singularity (March 27 2007)
Mac Singularity (March 4, 2007)
macosxhints (February 2007)
Mactalk (March 2007)

An alternative fix has been suggested by free variable.

Tags: osx, kerberos, ssh, segmentation fault, update, Tiger, 10.4

[1] Stable isotope notation in Matlab


UPDATE: I've written a MATLAB function to deal with this here: FIXPERMIL.

There is a discussion and work-around of the issue of Matlab mishandling permil notation here.

However, I found that this doesn't specifically address the problem as it is manifesting itself on my Mac Intel, running R2007b. Here is a description of my solution:

Here is what I entered:
=======================
ylabel(['\delta^{18}O (',char(8240),' VSMOW)'])


Here is how matlab interprets the output figure with the permil:
================================================================
%%IncludeResource: font Helvetica
/Helvetica /ISOLatin1Encoding 120 FMSR

516 1779 mt -90 rotate
(O \(\32 VSMOW\)) s
90 rotate


Here is how matlab should have interpreted it in the output:
============================================================
%%IncludeResource: font Helvetica
/Helvetica /StandardEncoding 120 FMSR

516 1779 mt -90 rotate
(O \(\275 VSMOW\)) s
90 rotate


The key here is to change the encoding to '/StandardEncoding' and the character code itself from '\32' to '\275'.

Tags: stable, isotope, notation, delta, oxygen, carbon, Matlab, latex, axis, labels

myspace counter