HWRF  trunk@4391
check_lcfiles.py
1 #!/usr/bin/env python
2 
3 ##@namespace pom.check_lcfiles
4 #Checks loop current file timestamps.
5 #
6 #This module checks the datestamp in the Loop Current files and compare
7 #with the forecast start date. If the datestmp is 90 days older than
8 #forecast start date, it tells the ocean init scripts to not use
9 #them. Also note that if the datestamp in the Loop Current files later
10 #than forecast date, it tells the ocean init scripts to not not use
11 #them.
12 #
13 #@note Please report bugs/questions/comments to bijuthomas(at)mail(dot)uri(dot)edu.
14 #@author Biju Thomas, GSO, University of Rhode Island.
15 #@date August 5, 2014
16 
17 from os.path import exists, getsize
18 from datetime import datetime
19 import logging
20 
21 def Isthis_LCuseful(startdate, lfile, rfile, logger=None):
22  """!Determines if loop current files are still usable for a cycle of interest.
23 
24  @param startdate the cycle of interest as a string, format YYYYMMDDHH
25  @param lfile,rfile the loop current l and r files
26  @param logger a logging.Logger for log messages."""
27  if logger is None: logger=logging.getLogger('pom')
28  if exists(lfile) and getsize(lfile) > 0 and \
29  exists(rfile) and getsize(rfile) > 0:
30  logger.info('LC: %s and %s exists' %(lfile,rfile))
31  try:
32  ldate = int(get_endline(lfile))
33  except ValueError as e:
34  logger.error("datestamp can not be accessed from %s" %(lfile))
35  logger.error("Error: %s" %(e))
36  return False
37  try:
38  rdate = int(get_endline(rfile))
39  except ValueError as e:
40  logger.error("datestamp can not be accessed from %s" %(rfile))
41  logger.error("Error: %s" %(e))
42  return False
43  if ldate == rdate:
44  if isinstance(startdate, str):
45  ymdh = startdate
46  try:
47  t1 = datetime(int(ymdh[0:4]),int(ymdh[4:6]),int(ymdh[6:8]),int(ymdh[8:10]))
48  except ValueError as e:
49  logger.error("Invalid startdate format %s" %(startdate))
50  logger.error("Error: %s" %(e))
51  return False
52  ymdh = str(ldate)
53  try:
54  t2 = datetime(int(ymdh[0:4]),int(ymdh[4:6]),int(ymdh[6:8]),int(ymdh[8:10]))
55  except ValueError as e:
56  logger.error("Invalid LC datestamp format in %s" %(str(ldate)))
57  logger.error("Error: %s " %(e))
58  return False
59  days = (t1 - t2).days
60  if days >= 0 and days <= 90:
61  logger.info("LC files are created by %i days ago from %s" %(days, startdate))
62  logger.info("%s and %s files can be used for sharpening" %(lfile,rfile))
63  return True
64  else:
65  logger.warn("LC files are created by %i days ago from %s" %(days,startdate))
66  logger.warn("%s and %s are OLD and Not using for sharpening" %(lfile,rfile))
67  return False
68  else:
69  logger.error("Invalid startdate(in Isthis_LCuseful) %s", startdate)
70  return False
71  else:
72  logger.error("Datestamps in %s and %s do not match" %(lfile,rfile))
73  return False
74  else:
75  logger.error("%s %s files do not exists" %(lfile,rfile))
76  return False
77 
78 def get_endline(file):
79  """!Read the last line of the given file and return it.
80  @param file the file to read"""
81  with open(file) as fid:
82  data = fid.read()
83  return data.split()[len(data.split())-1]
84 
85 ##@var LCdir
86 # Loop current directory for running the test program
87 
88 ##@var lfile
89 # rmy5 loop current file to test
90 
91 ##@var rfile
92 # wc_ring_rmy5 loop current file to test
93 
94 ##@var startdate
95 # Cycle to check
96 
97 if __name__ == '__main__':
98  LCdir = '/mnt/lfs2/projects/hwrf-data/fix-files/hwrf-20140617-fix/fix/loop_curr'
99  lfile = LCdir+'/hwrf_gfdl_loop_current_rmy5.dat.20131001'
100  rfile = LCdir+'/hwrf_gfdl_loop_current_wc_ring_rmy5.dat.20131001'
101  startdate = '2013101123'
102  print(Isthis_LCuseful(startdate, lfile, rfile))
def Isthis_LCuseful
Determines if loop current files are still usable for a cycle of interest.
def get_endline(file)
Read the last line of the given file and return it.