Skip to content

Instantly share code, notes, and snippets.

@ScottWales
Last active May 17, 2017 04:00
Show Gist options
  • Save ScottWales/d4cf756c1599f7cbc3b5657de790cf07 to your computer and use it in GitHub Desktop.
Save ScottWales/d4cf756c1599f7cbc3b5657de790cf07 to your computer and use it in GitHub Desktop.
set -eu
# Get input files from the command line
INPUTS=$*
add_time() {
INPUT=$1
OUTPUT=${INPUT}.nc
# Get the time from the filename
TIME=$(python modis_time.py $INPUT)
echo "${INPUT} has time ${TIME}"
# Create a new UNLIMITED dimension 'time', with the value from the file's timestamp
ncap2 -s 'defdim("time",-1);time[time]='$TIME';time@units="days since 1980-01-01 00:00"' -O $INPUT $OUTPUT
}
export -f add_time
# Add the time record to each file in parallel
parallel add_time ::: $INPUTS
# Join all the files along the UNLIMITED dimension
ncrcat $(sed 's/\(\S*\)/\1.nc/g' <<< $INPUTS) MOD06_L2.nc
from datetime import datetime
import re
import sys
epoch = datetime(1980,1,1,0,0,0,0,None)
# MOD06_L2.A2002059.0525.006.2014311051339.hdf
date_re = re.compile(r'MOD06_L2.A(?P<datestamp>\d{7}\.\d{4})\.006.*\.hdf')
def calculate_date(filename):
"""
Calculate the days since the epoch from a MODIS file's name
"""
datestamp = date_re.match(filename).group('datestamp')
date = datetime.strptime(datestamp,'%Y%j.%H%M')
days_since_epoch = (date - epoch).total_seconds() / 60.0 / 60.0 / 24.0
return days_since_epoch
print(calculate_date(sys.argv[1]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment