Skip to content

Instantly share code, notes, and snippets.

#!/usr/bin/env python
#coding=utf-8
# Nathive (and this file) is free software: you can redistribute it and/or
# modify it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or newer.
#
# You should have received a copy of the GNU General Public License along with
# this file. If not, see <http://www.gnu.org/licenses/>.
@mathsam
mathsam / b2r.m
Created November 14, 2013 21:08
Red to blue color map, with while for zero.
function newmap = b2r(cmin_input,cmax_input)
%BLUEWHITERED Blue, white, and red color map.
% this matlab file is designed to draw anomaly figures, the color of
% the colorbar is from blue to white and then to red, corresponding to
% the anomaly values from negative to zero to positive, respectively.
% The color white always correspondes to value zero.
%
% You should input two values like caxis in matlab, that is the min and
% the max value of color values designed. e.g. colormap(b2r(-3,5))
%
@mathsam
mathsam / PBS_sample.sh
Created August 10, 2013 19:02
Sample PBS script
# Sample PBS job script
#
# Copy this script, customize it and then submit it with the ``qsub''
# command. For example:
#
# cp pbs-template.sh myjob-pbs.sh
# {emacs|vi} myjob-pbs.sh
# qsub myjob-pbs.sh
#
# PBS directives are fully documented in the ``qsub'' man page. Directives
@mathsam
mathsam / fl.c
Created July 10, 2013 00:33
list files in directory very fast for Linux OS.
#define _GNU_SOURCE
#include <dirent.h> /* Defines DT_* constants */
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#define handle_error(msg) \
function flux2dIsen = calIsenFlux(vcomp3d,PT3d,pfull,isenCoord,deltaP,minP,maxP,ps2d)
% calculate mass flux in isentropic coordinate
%----INPUT----
% vcomp3d: meridonal velocity at one time, 3d matrix (lon,lat,press)
% PT3d: potential temperature, 3d matrix
% pfull: temperature coordinate, unit mb, 1d array, for PT3d and
% vcomp3d
% isenCoord:isentropic coordinate that converts to, increases with index, 1d array
% usually should have regular spacing, or unexpect problem can happen
% deltaP: usually 10 mb
@mathsam
mathsam / nakeinterp3.c
Created July 8, 2013 03:40
It is actually doing a specific batch linear interpolation job for a 3D array.
/* nakeinterp3.c, use specifically for calIsenFlux */
/* interpolate a 3d array A(i,j,k) only in k direction */
/* interface as PT3dInterp = nakeinterp3(numx,numy,z,PT3d,zi) */
/* numx, numy are the first two dimensions */
/* z is the coordinate, zi is the interoplated coordinate */
/* PT3d is the function to be interoplated, only done so in z */
/* z and zi should be column vector */
#include "mex.h"
#include "matrix.h"
@mathsam
mathsam / nakeinterp1.c
Created July 8, 2013 00:00
linear interpolation with extrapolation; matlab mex file
#include "mex.h"
#include "matrix.h"
// Gateway routine
void mexFunction(int nlhs, mxArray *plhs[],int nrhs, const mxArray *prhs[])
{
const mxArray *xi, *xgrid;
mxArray *idx;
size_t nx, m, k, i1, i9, imid;
function flux1d = calIsenFlux1d(vcomp1d,PT1d,pfull,isenCoordEdge,pInterp)
% unit is not converted into standard using G
vcomp1dInterp = interp1(pfull,vcomp1d, pInterp,'spline','extrap');
PT1dInterp = interp1(pfull,PT1d, pInterp,'spline','extrap');
[~, bin] = histc(PT1dInterp,isenCoordEdge);
flux1d = zeros(length(isenCoordEdge)-1,1);
@mathsam
mathsam / p2isen.c
Created July 7, 2013 21:43
a mex file for converting p coordinate into isentropic coordinate. improve the performance by about 500 times
#include "mex.h"
#include "matrix.h"
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
double *vcomp3dInterp, *bin3d; /* pointers to the input matrixces */
double *flux2dIsen; /* pointer to output matrix */
double *numIndex; /* num of index array is third input */
mwSignedIndex lon,lat,p,numIsen; /* matrix dimensions */
@mathsam
mathsam / driverIsenFlux.m
Created July 7, 2013 15:04
driver script for isentropic flux analysis
% driver script for callsenFlux
temp = ncread('Symmetric_HS=60K.nc','temp');
vcomp = ncread('Symmetric_HS=60K.nc','vcomp');
pfull = ncread('Symmetric_HS=60K.nc','pfull');
tStart = 1000;
tEnd = 1100;
isenCoord = 240:5:360;
deltaP = 10;