Last active
January 20, 2016 15:54
-
-
Save bitsgalore/bfb3201aae2586689148 to your computer and use it in GitHub Desktop.
Rename PCRaster map stack example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env python | |
# Rename PCRaster map stack with names following prefix.yyymmmdd to stack with valid | |
# PCRaster time step numbers | |
# Johan van der Knijff | |
# | |
# Example input stack: | |
# | |
# precip.19810101 | |
# precip.19810102 | |
# precip.19810103 | |
# precip.19810104 | |
# precip.19810105 | |
# | |
# Then run script with following arguments: | |
# | |
# python renpcrstack.py precip 1 | |
# | |
# Result: | |
# | |
# precip00.001 | |
# precip00.002 | |
# precip00.003 | |
# precip00.004 | |
# precip00.005 | |
# | |
import sys | |
import os | |
import argparse | |
import math | |
import datetime | |
import glob | |
# Create argument parser | |
parser = argparse.ArgumentParser( | |
description="Rename map stack") | |
def parseCommandLine(): | |
# Add arguments | |
parser.add_argument('prefix', | |
action="store", | |
type=str, | |
help="prefix of input map stack (also used as output prefix)") | |
parser.add_argument('stepStartOut', | |
action="store", | |
type=int, | |
help="time step number that is assigned to first map in output stack") | |
# Parse arguments | |
args = parser.parse_args() | |
return(args) | |
def dateToJulianDay(date): | |
# Calculate Julian Day from date | |
# Source: https://en.wikipedia.org/wiki/Julian_day#Converting_Julian_or_Gregorian_calendar_date_to_Julian_day_number | |
a = (14 - date.month)/12 | |
y = date.year + 4800 - a | |
m = date.month +12*a - 3 | |
JulianDay = date.day + math.floor((153*m + 2)/5) + 365*y + math.floor(y/4) \ | |
- math.floor(y/100) + math.floor(y/400) - 32045 | |
return(JulianDay) | |
def genStackNames(prefix,start,end, stepSize): | |
# Generate list with names of all maps | |
# map name is made up of 11 characters, and chars 8 and 9 are | |
# separated by a dot. Name starts with prefix, ends with time step | |
# number and all character positions in between are filled with zeroes | |
# define list that will contain map names | |
listMaps = [] | |
# Count no chars prefix | |
charsPrefix = len(prefix) | |
# Maximum no chars needed for suffix (end step) | |
maxCharsSuffix = len(str(end)) | |
# No of free positions between pre- and suffix | |
noFreePositions = 11 - charsPrefix - maxCharsSuffix | |
# Trim prefix if not enough character positions are available | |
if noFreePositions < 0: | |
# No of chars to cut from prefix if 11-char limit is exceeded | |
charsToCut = charsPrefix + maxCharsSuffix - 11 | |
charsToKeep = charsPrefix - charsToCut | |
# Updated prefix | |
prefix = prefix[0:charsToKeep] | |
# Updated prefix length | |
charsPrefix = len(prefix) | |
# Generate name for each step | |
for i in range(start,end + 1,stepSize): | |
# No of chars in suffix for this step | |
charsSuffix = len(str(i)) | |
# No of zeroes to fill | |
noZeroes = 11 - charsPrefix - charsSuffix | |
# Total no of chars right of prefix | |
charsAfterPrefix = noZeroes + charsSuffix | |
# Name of map | |
thisName = prefix + (str(i)).zfill(charsAfterPrefix) | |
thisFile = thisName[0:8]+"." + thisName[8:11] | |
listMaps.append(thisFile) | |
return listMaps | |
def main(): | |
# Parse command line arguments | |
args = parseCommandLine() | |
prefix = args.prefix | |
stepStartOut = args.stepStartOut | |
# Glob pattern for input maps: prefix + dot + 8 char extension | |
pattern = prefix + ".????????" | |
# Get list of all input maps based on glob pattern | |
mapsIn = glob.glob(pattern) | |
# Set time format | |
tfmt = "%Y%m%d" | |
# Set up dictionary that will act as lookup table between Julian Days (key) | |
# and Date string | |
jDayDate = {} | |
for map in mapsIn: | |
baseNameIn = os.path.splitext(map)[0] | |
dateIn = os.path.splitext(map)[1].strip(".") | |
# Convert to date / time format | |
dt = datetime.datetime.strptime(dateIn, tfmt) | |
# Convert date to Julian day number | |
jDay = int(dateToJulianDay(dt)) | |
# Store as key-value pair in dictionary | |
jDayDate[jDay] = dateIn | |
# Number of input maps (equals number of key-value pairs) | |
noMaps = len(jDayDate) | |
# Create list of names for output files | |
mapNamesOut = genStackNames(prefix, stepStartOut, noMaps + stepStartOut -1, 1) | |
# Iterate over Julian Days (ascending order) | |
i = 0 | |
for key in sorted(jDayDate): | |
# Name of input file | |
fileIn = prefix + "."+ jDayDate[key] | |
# Name of output file | |
fileOut = mapNamesOut[i] | |
# Rename file | |
os.rename(fileIn, fileOut) | |
print("Renamed " + fileIn + " ---> " + fileOut) | |
i += 1 | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment