SHARE
TWEET

Untitled

a guest Jul 19th, 2019 67 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import os, shutil
  2. import datetime as datetime
  3. from netCDF4 import Dataset
  4. import numpy as np
  5. import glob
  6. import string
  7.  
  8. __author__   = 'Trond Kristiansen'
  9. __email__    = 'me (at) trondkristiansen.com'
  10. __created__  = datetime.datetime(2015, 10, 5)
  11. __modified__ = datetime.datetime(2015, 10, 5)
  12. __version__  = "1.0"
  13. __status__   = "Production"
  14.  
  15. """This script takes the output of running ROMS where the name of result files are an increasing integer ROMS.1, ROMS.2,
  16. ROMS.3 ... ROMS.N. The new filenames uses the startdate (ocean_time ) of the ROMS file as the integer instead.
  17. This means that all files have names indicating start date as days since 1948/1/1. """
  18.  
  19. def extractTimeFromNetCDF(infile):
  20.  
  21.     cdf = Dataset(infile)
  22.     times = cdf.variables["ocean_time"][:]
  23.     refDate=datetime.datetime(1948, 1, 1, 0, 0, 0)
  24.     currentDate=refDate + datetime.timedelta(seconds=times[0])
  25.     print "time-step: %s - %s" % (times[0], currentDate)
  26.     difference = currentDate - refDate
  27.     print "Days since refdate %s - %s"%(refDate,difference.days)
  28.     return difference.days
  29.  
  30. def renameROMSFiles(datapath,mypattern,outputDirectory,newFilename):
  31.  
  32.     counter=0
  33.     argument="%s%s"%(datapath,mypattern)
  34.     allFiles = glob.glob(argument)
  35.     allFiles.sort()
  36.        
  37.     print "argument %s"%(argument)
  38.     print "Sorting %s files found in NS8KM datadirectory"%(len(allFiles))
  39.  
  40.     for resultfile in allFiles:
  41.  
  42.         days = extractTimeFromNetCDF(resultfile)
  43.  
  44.         newName = "%s%s_%s.nc"%(outputDirectory,newFilename,days)
  45.         shutil.move(resultfile,newName)
  46.  
  47.         print "File %s renamed to %s"%(resultfile,newName)
  48.  
  49. def main():
  50.  
  51.     # Where the ROMS results files are stored
  52.     datapath ="/work/users/trondk/KINO/FORWARD/Run/SAVE/"
  53.     # The pattern of the ROMS files
  54.     mypattern="ocean_avg_0*[01234]*.nc"
  55.     # The directory to store the renamed files
  56.     outputDirectory="/work/users/trondk/KINO/FORWARD/Run/RESULTS/"
  57.     # The new filename pattern where days since 1948/1/1 will be added at the end: kino_1600m_29765.nc
  58.     newFilename="kino_1600m"
  59.  
  60.     # Create results folder  
  61.     if not os.path.exists(outputDirectory):
  62.         os.makedirs(outputDirectory)
  63.  
  64.     # Rename all of the files in the datatpath
  65.     renameROMSFiles(datapath,mypattern,outputDirectory,newFilename)
  66.  
  67. if __name__ == "__main__":
  68.     main()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top