#!/usr/bin/python #======================================================================================== # name: clmann.py # # category: Python script # # description # This script constructs annual mean climatology files for specified variables of MIROC output # # input: individual year output = yYYYY (e.g., y0300, y0301,...) OR # multi-year output = yYYYY-yYYYY (e.g., y0300-y0309, y0310-y0319,...) # # call: ngt commands (ngtavr, ngtcf) and CDO commands (settaxis, invertlat) # # usage # ./clmann.py: to create an executable file, "clmann.csh" # ./clmann.csh: to execute # # note: works on clima@AORI # # history # 2010.06.03: originally written # 2010.08.08: class structure introduced # 2012.02.05: code cleaned up # 2012.08.14: code cleaned up # 2012.08.15: fixed time axis problem with a CDO command and reversed # latitude from the South to the North for netCDF # 2014.08.05: last updated # # author # m.yoshimori (myoshimo AT ees.hokudai.ac.jp) #======================================================================================== import os import mydigit # my module to change digit (e.g., 100 to 0100) #------------------- # user defined block #------------------- # C-shell script to be created by this Python script fcsh = 'clmann.csh' #--------- # settings #--------- # input path datapathi = '/mnt/clm12/masakazu/test' # output path datapatho = '/mnt/clm12/masakazu/test2' # netCDF output (1: Yes, 0: No) netcdf = 1 # gtool output (1: Yes, 0: No) gtool = 1 # sub-directories subdir = '' #subdir = 'L20' # variables variable_list = ['T2','prcp'] #variable_list = ['T'] #--------------------------------------- # specify parameters for each experiment #--------------------------------------- class Experiment: pass #-------------------- # list of experiments #-------------------- # exp. 0 EXP0 = Experiment() EXP0.inp = datapathi + '/ContASGCMo' EXP0.year_start = 431 EXP0.year_end = 460 EXP0.out = datapatho + '/ContASGCMo' # exp. 1 EXP1 = Experiment() EXP1.inp = datapathi + '/ASGCM2xCO2o' EXP1.year_start = 411 EXP1.year_end = 440 EXP1.out = datapatho + '/ASGCM2xCO2o' exp_list = [EXP0,EXP1] #-------------------------- # end of user defined block #-------------------------- #----------------------- # do not edit below here #----------------------- # number of years in one file year_step = 1 f = open(fcsh,'w') f.write('#!/bin/csh \n') for exp in exp_list: inp = exp.inp year_start = exp.year_start year_end = exp.year_end out = exp.out + '/' + subdir nstep = (year_end - year_start + 1)/year_step # number of iterations yyyys = mydigit.year2yyyy(year_start) yyyye = mydigit.year2yyyy(year_end) f.write('\n') f.write('# Create output directory if not exist\n') f.write('mkdir -p '+out+'\n') f.write('\n') for var in variable_list: fout = out+'/av_'+var+'_y'+str(yyyys)+'-y'+str(yyyye)+'_ann' f.write('# Average multi-year files\n') f.write('ngtavr -o '+fout+' ') i=0 # initialize counter while i < nstep: tmp = year_start + year_step*(i) yyyy1 = mydigit.year2yyyy(tmp) tmp = year_start + year_step*(i+1) - 1 yyyy2 = mydigit.year2yyyy(tmp) if year_step == 1: dir = inp+'/y'+str(yyyy1)+'/'+subdir else: dir = inp+'/y'+str(yyyy1)+'-y'+str(yyyy2)+'/'+subdir f.write(dir+'/'+var+' ') i += 1 f.write('\n') f.write('\n') if netcdf == 1: # netCDF output f.write('# Convert the created file from Gtool to netCDF\n') f.write('ngtcf '+fout+' tmp1.nc\n') f.write('cdo settaxis,1850-07-01,12:00,1year tmp1.nc tmp2.nc\n') #f.write('cdo invertlat tmp2.nc '+fout+'.nc\n') f.write('/bin/mv tmp2.nc '+fout+'.nc\n') #f.write('/bin/mv tmp1.nc '+fout+'.nc\n') f.write('/bin/rm tmp?.nc\n') f.write('\n') if gtool == 0: # no gtool output f.write('# Delete the intermediate Gtool file\n') f.write('/bin/rm '+fout+'\n') f.write('\n') f.write('exit\n') os.chmod(fcsh, 0755)