Skip to content

Instantly share code, notes, and snippets.

@trondkr
Last active April 6, 2017 18:54
Show Gist options
  • Save trondkr/56c049acb5a30eab4bf2cb3ae2dac618 to your computer and use it in GitHub Desktop.
Save trondkr/56c049acb5a30eab4bf2cb3ae2dac618 to your computer and use it in GitHub Desktop.
Remove duplicates of timesteps in netcdf files
```python
from netCDF4 import Dataset, datetime, date2num,num2date
import numpy as np
import os
from subprocess import call
__author__ = 'Trond Kristiansen'
__email__ = 'trond.kristiansen@niva.no'
__created__ = datetime(2017, 2, 1)
__modified__ = datetime(2017, 2, 1)
__version__ = "1.0"
__status__ = "Development, modified on 01.02.2015, 06.04.2017"
# NOTE!: To make this wortk you have to type into command window prior to execution:
# export SKIP_SAME_TIME=1
# Then you can run
# python mergetime.py
myprefix='A20'
outprefix='A20'
myvars=['Pair','Uwind','Vwind','cloud','Tair','Qair','rain']
myvars=['swrad','lwrad']
years=[year for year in xrange(1980,2015,1)]
command = "export SKIP_SAME_TIME=1"
call(command,shell=True)
for myvar in myvars:
newfilename="%s_%s_%s_%s.nc"%(outprefix,myvar,years[0],years[-1])
mylistoffiles=[]
if os.path.exists(newfilename): os.remove(newfilename)
for year in years:
filename="%s_%s_%s.nc"%(myprefix,myvar,year)
mylistoffiles.append(filename)
if len(mylistoffiles)>0:
options=""
for f in mylistoffiles:
options=options+"%s "%(f)
options=options+newfilename
command = "cdo mergetime %s"%(options)
# print "%s"%(command)
call(command,shell=True)
print "Result written to file: %s"%(newfilename)
print "\nFinished with %s -----"%(myvar)
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment