#!/usr/bin/python #======================================================================================== # name: clmmonall.py # # category: Python script # # description # This script constructs monthly mean climatology files from all variables of MIROC output # # input: individual year output = yYYYY (e.g., y0300, y0301,...) # # call: ngt commands (ngtavr, ngtcf) and CDO commands (settaxis, invertlat) # # usage # ./clmmonall.py: to create an executable file, "clmmonall.csh" # ./clmmonall.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 # 2013.06.24: code cleaned up # 2014.08.05: code cleaned up # 2015.06.16: ignore ERROUT, SYSOUT, Restart, RSTA, RSTO, ftrace files # 2015.06.25: ignore SYSIN file # # 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 = 'clmmonall.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_list = ['L20'] #--------------------------------------- # 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 nmonth = year_step*12 + 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 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') # list files and dircotries from the first-year directory of the analysis din = inp+'/y'+str(yyyys) file_list = os.listdir(din) # search files in specified directory for var in file_list: # ignore files that contain the following words if 'ERROUT' in var: continue if 'SYSIN' in var: continue if 'SYSOUT' in var: continue if 'Restart' in var: continue if 'RSTA' in var: continue if 'RSTO' in var: continue if 'ftrace' in var: continue # process other files x = din + '/' + var if os.path.isfile(x): # check if x is a file or not; use only files fout = out +'/av_'+var+'_y'+str(yyyys)+'-y'+str(yyyye)+'_mon' imonth = 1 f.write('# Average multi-year files for month = '+str(imonth)+'\n') f.write('ngtavr -t '+str(imonth)+' -o '+fout+' ') i=0 while i < nstep: tmp = year_start + year_step*(i) yyyy = mydigit.year2yyyy(tmp) dir = inp+'/y'+str(yyyy) f.write(dir+'/'+var+' ') i += 1 f.write('\n') f.write('\n') for imonth in range(2,nmonth): f.write('# Average multi-year files for month = '+str(imonth)+', and append\n') f.write('ngtavr -t '+str(imonth)+' -a -o '+fout+' ') i=0 while i < nstep: tmp = year_start + year_step*(i) yyyy = mydigit.year2yyyy(tmp) dir = inp+'/y'+str(yyyy) 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-01-16,12:00,1mon 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') # search files in specified sub-directories for subdir in subdir_list: out = exp.out + '/' + subdir f.write('\n') f.write('# Create output directory if not exist\n') f.write('mkdir -p '+out+'\n') f.write('\n') # list files and dircotries from the first-year directory of the analysis din = inp+'/y'+str(yyyys)+'/'+subdir file_list = os.listdir(din) for var in file_list: x = din + '/' + var if os.path.isfile(x): # check if x is a file or not; use only files fout = out +'/av_'+var+'_y'+str(yyyys)+'-y'+str(yyyye)+'_mon' imonth = 1 f.write('# Average multi-year files for month = '+str(imonth)+'\n') f.write('ngtavr -t '+str(imonth)+' -o '+fout+' ') i=0 while i < nstep: tmp = year_start + year_step*(i) yyyy = mydigit.year2yyyy(tmp) dir = inp+'/y'+str(yyyy)+'/'+subdir f.write(dir+'/'+var+' ') i += 1 f.write('\n') f.write('\n') for imonth in range(2,nmonth): f.write('# Average multi-year files for month = '+str(imonth)+', and append\n') f.write('ngtavr -t '+str(imonth)+' -a -o '+fout+' ') i=0 while i < nstep: tmp = year_start + year_step*(i) yyyy = mydigit.year2yyyy(tmp) dir = inp+'/y'+str(yyyy)+'/'+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-01-16,12:00,1mon 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)