#!/usr/bin/env python3 # -*- coding: utf-8 -*- #======================================================================================== # name: split.py # # category: Python module script # # description: # This module script writes tc-shell scripts which splits bundled multi-year output # files of MIROC model to single-year files. # # input: multi-year output = yYYYY (e.g., y0300-y0350) # # call: ngt commands (ngtredist) # # use: import split; see the sample main script, SplitMain.py # # note: works on rocky@AORI # # history # 2013.06.24: originally written # 2014.08.05: last updated # 2019.12.22: modularized with class # 2022.07.01: python2 -> python3 # # author # m.yoshimori (masakazu AT aori.u-tokyo.ac.jp) #======================================================================================== import os class SPLIT(): def __init__(self, exp): self.inp = exp.inp self.year_start = exp.year_start self.year_end = exp.year_end self.year_step = exp.year_step self.out = exp.out def redist(self, f, dig): nstep, rem = divmod((self.year_end - self.year_start + 1),self.year_step) # number of iterations if rem != 0: print('ERROR: year_step in split.py') else: f.write('\n') f.write('# Create output directory if not exist\n') f.write('mkdir -p '+self.out+'\n') f.write('\n') for i in range(nstep): year = self.year_start + self.year_step*i yyyy1 = str(year).zfill(dig) year = self.year_start + self.year_step*(i+1) - 1 yyyy2 = str(year).zfill(dig) if self.year_step != 1: dir = self.inp+'/y'+str(yyyy1)+'-y'+str(yyyy2) f.write('cd '+dir+'\n') f.write('ngtredist "'+self.out+'/y%04y/%f" `find . -type f` \n') f.write('\n')