Skip to content

Instantly share code, notes, and snippets.

View tenomoto's full-sized avatar

Takeshi Enomoto tenomoto

View GitHub Profile
@tenomoto
tenomoto / read_binary.h
Last active April 27, 2016 07:28
Read m elements from a binary file with offset of n elements and return C++ std::vector
#include <iostream>
#include <fstream>
#include <vector>
template <typename T>
std::vector<T> read_binary(std::string fname, size_t n, size_t m, bool reverse = false, bool verbose = false)
{
union u
{
char c[sizeof(T)];
@tenomoto
tenomoto / function.php
Created April 27, 2016 05:50
Multilingual title and description in WordPress with bogo
add_filter( 'option_blogname', 'tr_option_blogname' );
function tr_option_blogname( $blogname ) {
return __($blogname, 'my-theme-child');
}
add_filter( 'option_blogdescription', 'tr_option_blogdescription' );
function tr_option_blogdescription( $blogdescription ) {
return __($blogdescription, 'my-theme-child');
@tenomoto
tenomoto / find_index.cc
Created May 27, 2016 05:43
Find index of the first element larger or equal to a given value in C++
#include <iterator>
#include <algorithm>
template <typename Iter, typename T>
size_t find_index(Iter begin, Iter end, T x0)
{
return std::distance(begin, std::find_if(begin, end, [x0](double x){return x >= x0;}));
}
@tenomoto
tenomoto / cyclic.cc
Created May 27, 2016 07:07
Get value for a negative index or for an index larger than the size of container in C++
template <typename Iter>
Iter cyclic(Iter begin, Iter end, int i)
{
auto n = std::distance(begin, end);
while (i < 0) i += n;
return begin + (i % n);
}
@tenomoto
tenomoto / polyconic.sh
Last active June 7, 2016 04:48
Draw a blank map of the world in polyconic projection with GMT
#!/bin/sh
ps=polyconic_coast.ps
w=2.0944c
proj=Poly/${w}
lon0=-180
lon1=`expr ${lon0} + 30`
reg=${lon0}/${lon1}/-90/90
gmt psbasemap -R${reg} -J${proj} -K -Bg10/g10 > ${ps}
gmt pscoast -R${reg} -J${proj} -O -K -W -B >> ${ps}
while [ ${lon0} -lt 120 ]; do
@tenomoto
tenomoto / muc.m
Created July 12, 2016 08:22
Calculate the critical value for the non dimensional wave number in the Eady problem
#!/usr/bin/env octave -qf
function y = f(x)
y = coth(0.5*x) - 0.5*x;
endfunction
format long
fsolve("f", 1.0)
@tenomoto
tenomoto / mum.m
Created July 12, 2016 08:29
Calculate the most developing wave number in the Eady problem
#!/usr/bin/env octave -qf
function y = f(x)
# the maximum of kci = sqrt((mu/2 - tanh mu/2)(mu/2 - cot mu/2)) is found by
# finding the zero of the derivative of the inside of sqrt.
y = x - (tanh(0.5*x) + coth(0.5*x)) - 0.5 * x * (1/(cosh(0.5*x))^2 - 1/(sinh(0.5*x))^2);
endfunction
format long
fsolve("f", 3.2)
@tenomoto
tenomoto / incrh
Created September 15, 2016 04:16
increment hour using date command
#!/bin/sh
incrh () {
# Arguments
# 1: yyyymmddhh
# 2: dh
yyyymmddhh=$1
dh=$2
hour2sec=3600
t0=`date -jf "%Y%m%d%H%M%S" ${yyyymmddhh}0000 "+%s"`
dh=`expr $dh \* $hour2sec`
@tenomoto
tenomoto / cleancsv.awk
Created November 16, 2016 05:13
Remove paired double quotes and commas in between
#!/usr/bin/awk -f
# http://unix.stackexchange.com/questions/48672/remove-comma-between-the-quotes-only-in-a-comma-delimited-file
BEGIN {
FS = "\""
OFS = ""
}
{
for (i = 2; i <= NF; i += 2) {
gsub(",", "", $i)
}
@tenomoto
tenomoto / natural_scrolling
Last active May 5, 2017 10:06
Natural (reverse) scrolling of mouse wheel using xinput
# xinput set-prop device property value [...]
# device: 10 indentified with `xinput list`
# property: 272 or Evdev Scrolling Distance indentified with `xinput list-props device`
# value: -1 1 1 changed from 1 1 1
$ xinput set-prop 10 272 -1 1 1