1#!/usr/bin/env python3
  2
  3"""Functions to extract a tuple of SID, sensing_start from various types of filename.
  4All schedule triggers that monitor directories must specify one of these functions.
  5Projects can supply their own there is no need to call a function from the core.
  6"""
  7
  8import logging
  9from datetime import datetime
 10
 11from chart.products.eps import dds
 12from chart.products.sf00 import sf
 13from chart.products.eps.gpfs import EPSFilename
 14
 15
 16def fileattr_eps_native(path):
 17    """Given a file `path` and an `activity` object, return a tuple of (sid, sensing_start).
 18    Maybe, change to give each Activity its own fileattr function attribute.
 19    The implementation should avoid opening the file since 1. that will slow it down and
 20    2. it might be on an FTP site and not exist as a local file.
 21
 22    Args:
 23        `path` (Path): Filename to examine
 24
 25    Raises:
 26        ValueError
 27    """
 28    try:
 29        eps_file = EPSFilename(path)
 30
 31    except EPSFilename.NotEPSFilename:
 32        logging.error('Non EPS Filename format for {path}. Skip file and continue'.format(
 33                path=path))
 34        return
 35
 36    # if activity.name == 'ADA_INGESTER':
 37        # return SID('M02'), eps_file.sensing_start
 38
 39    # else:
 40    return eps_file.sid, eps_file.sensing_start
 41
 42
 43def fileattr_sf00(path):
 44    """Pull a SID and sensing start from an SF00 file name."""
 45    sf00_file = sf.SFFilename(path)
 46    # except SFReader.BadSFFile
 47    return sf00_file.sid, sf00_file.sensing_start
 48
 49
 50def fileattr_dds(path):
 51    """Pull a SID and sensing start from an DDS file name."""
 52    try:
 53        dds_file = dds.DDSFilename(path)
 54    except dds.DDSFilename.NotDDSFilename:
 55        logging.error('Non DDS Filename format for {path}. Skip file and continue'.format(
 56                                path=path))
 57        return
 58    return dds_file.sid, dds_file.sensing_start
 59
 60
 61def fileattr_saphire(path):
 62    """See PAROT comment. This function needs splitting and all SAPHIRE scheduler XML files
 63    with DirectoryMonitor triggers need to point to one or the other."""
 64    # elif activity.name in ('DMEA_INGESTER', 'PMEA_INGESTER', 'SMETB_INGESTER'):
 65    # Currently the DMEA PMEA SMETB files retreived from EUMETSAT ftpserver and file names
 66    # do not conform to EPS standards, so handle individually
 67    try:
 68        return 'M02', datetime.strptime(path.name[5:16], '%y%j%H%M%S')
 69    except ValueError:
 70        logging.error('Invalid filename {path} so skip, and continue'.format(path=path))
 71        return
 72    # Handle SAPHIRE FTP wotis files
 73    # elif (filename.startswith('dmea') or filename.startswith('pmea') or
 74          # filename.startswith('smet')):
 75
 76        # return gentime_to_datetime(filename[5:16])
 77
 78
 79# def fileattr_parot(path):
 80    # """This is wrong.
 81    # The function no longer accepts an Activity parameter so the PAROT project needs to specify
 82    # different filename interpreters in different scheduler XML files.
 83    # This function should be split into 2.
 84    # """
 85    # elif activity.name in ('DUMPSYNC', 'FDF_GEOEVENT', 'WIMPY', 'WOTIS', 'ADAREPORT'):   # mja
 86    # try:
 87        # eps_file = EPSFilename(path)
 88    # except ValueError:
 89        # logging.error('Non EPS Filename format for {path}. Skip file and continue'.format(
 90                # path=path))
 91        # return
 92
 93    # return eps_file.sid.scid, eps_file.sensing_start
 94
 95    # PAROT
 96    # elif activity.name in ('STEPMSG', 'NOAARECORDER'):   # mja
 97        # File is for all NOAA spacecraft
 98        # return 'IJP', gentime_to_datetime(path.name.split('_')[4])
 99
100    # else:
101        # raise ValueError('Unknown activity {a}'.format(a=activity))
102    # pass
103
104# def fileattr_sensing_start(filename):
105    # """Return the sensing start time of `filename`.
106    # No directory component is supplied.
107    # No reading of the actual file, just use name.
108
109    # This function is used by the `scheduler` with DirectoryMonitor triggers.
110
111    # Projects can plug in their own versions of this module by editing the
112    # settings.py file.
113    # """
114
115    # for .sf00 files only discard everything on the first pass.
116    # This will only affect N18 SF00 files from the DDS.
117    # if filename.endswith('.SF00'):
118        # return gentime_to_datetime(filename[0:15])
119
120    # ?
121    # elif filename.endswith('.gz'):
122        # return None
123
124    # Handle all other files
125    # else:
126        # try:
127            # Handle both EPS format filenames:
128            # HKTM_xxx_00_M02_20130305230600Z_20130305230900Z_N_O_20130305235757Z
129            # and stepmsg PAROT files:
130            # xxxx_BOC_xx_IJPS_20130430000000_20130507000000_20130415064133_NOAA_\
131            # step02_msg.txt
132            # return gentime_to_datetime(filename.split('_')[4])
133        # except ValueError:
134            # logging.error('Invalid filename {path} so skip, and continue'.format(
135                    # path=filename))
136            # return None