<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Dec  9 17:09:17 2020

@author: mvojta
"""

#!/usr/bin/env python3
# Generate AVAILABLE file for Data named XXJJMMDDHH e.g. EI17010100

import glob
import os
import sys

#Gets the pathnames of the subdirectories
#-------------------------------------------------------------------------
def fast_scandir(dirname):
    subfolders= [f.path for f in os.scandir(dirname) if f.is_dir()]
    for dirname in list(subfolders):
        subfolders.extend(fast_scandir(dirname))
    return subfolders
#------------------------------------------------------------------------



# Read in dir of Data, dir of Available and Begin &amp; End of Data to be used
#--------------------------------------------------------------------------
DIR = "/jetfs/shared-data/ECMWF/ERA5_glob_0.5deg_1h"
AVAILABLE_DIR = "/jetfs/scratch/ievangelou/MP_measur_study/flexpart"
begin=2012
end=2024
#            JJMMDDHH    e.g. 17010100
Data_Begin = 12100100                   
Data_End   = 24010100
#Prefex not really used yet - maybe in the future
#-------------------------------------------------------------------------
PREFIX = "*"

#-----------------------------------------------------------------------
years=range(begin, (end+1))

# Test if dir of Data exists
#-------------------------------------------------------------------------
if os.path.isdir(DIR):
  os.chdir(DIR)
else:
  print("\nDirectory does not exist.")
  sys.exit()






# Load all files and sort as in AVAILABLE (if subdirectories -&gt; load files from them)
#-------------------------------------------------------------------------
files=[]
for year in years:
    directory=DIR+"/"+str(year)
    
    subdir=fast_scandir(directory)
  
    
    if len(subdir)!=0:
        subdir.sort()
        
        for i in range(len(subdir)):
            os.chdir(subdir[i])
            files.extend(glob.glob(PREFIX))
            files.sort()

    else:
        files = glob.glob(PREFIX)
        files.sort()



# Check if any file exists
#-------------------------------------------------------------------------
if not files:
  print("\nDirectory does not contain files.")
  sys.exit()
  

# Write AVAILABLE by using strings from the files 
#-------------------------------------------------------------------------

AVAILABLE = open(AVAILABLE_DIR +'/AVAILABLE', 'w')
AVAILABLE.write("-\n-\n-\n")
for file in files:  
    if Data_Begin &lt;= int(file[4:12]) and Data_End&gt;= int(file[4:12]):
      file_with_path=str(file[2:6])+"/"+str(file[6:8]) + "/" + str(file)
      AVAILABLE.write("%s %s0000      %s      %s\n" % (file[2:10], str((file[10:12])), file_with_path, "ON DISK")) 
AVAILABLE.close()

# If everything works out: "YEAH" :)
#-------------------------------------------------------------------------
print("YEAH! AVAILABLE file created in %s." % AVAILABLE_DIR)
</pre></body></html>