source: cows_wps/trunk/process_modules/list_pp_file_header.py @ 7159

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/cows_wps/trunk/process_modules/list_pp_file_header.py@7159
Revision 7159, 4.2 KB checked in by astephen, 10 years ago (diff)

Added pp header listing process.

Line 
1"""
2list_pp_file_header.py
3===================
4
5Process list_pp_file_header that holds the ListPPFileHeader class.
6
7"""
8
9# Standard library imports
10import os, stat, time, sys, logging
11
12# WPS imports
13from cows_wps.process_handler.fileset import FileSet, FLAG
14import cows_wps.process_handler.process_support as process_support
15from cows_wps.process_handler.context.process_status import STATUS
16import process_modules.process_base
17
18# Import process-specific modules
19sys.path.append("/usr/local/cwps/swarv/lib/python")
20import pypp.pp_file
21
22# NOTE ABOUT LOGGING:
23# You can log with the context.log object
24
25log = logging.getLogger(__name__)
26log.setLevel(logging.DEBUG)
27
28
29class ListPPFileHeader(process_modules.process_base.ProcessBase):
30
31    # Define arguments that we need to set from inputs
32    # Based on args listed in process config file
33    args_to_set = ["PPFilePath"]
34
35    # Define defaults for arguments that might not be set
36    # A dictionary of arguments that we can over-write default values for
37    # Some args might be mutually-exclusive or inclusive so useful to set
38    # here as well as in the config file.
39    input_arg_defaults = {}
40
41    # Define a dictionary for arguments that need to be processed
42    # before they are set (with values as the function doing the processing).
43    arg_processers = {}
44
45   
46    def _executeProc(self, context, dry_run):
47        """
48        This is called to step through the various parts of the process
49        executing the actual process if ``dry_run`` is False and just
50        returning information on the volume and duration of the outputs
51        if ``dry_run`` is True.
52        """
53        # Call standard _setup
54        self._setup(context)
55        a = self.args
56
57        if not dry_run:
58            # Now set status to started
59            context.setStatus(STATUS.STARTED, 'Job is now running', 0)
60
61        # Add output file
62        outputFile = 'output.txt'
63        outputFilePath = os.path.join(context.processDir, "outputs", outputFile)
64
65        if not dry_run:
66            # Really generate output
67            ppf = pypp.pp_file.PP_File(a["PPFilePath"]) 
68   
69            resp = "Number of fields: %s\n" % ppf.nfields
70
71            for i in range(ppf.nfields):
72
73                h = ppf.headers[i]
74                l = "Field %s: Stash: %s, Level: %s, ValidTime: %s, DataTime: %s, LBYR: %s, LBYRD: %s\n" % \
75                       (i, h.stashcode, h.lblev, h.getValidityTime(), h.getDataTime(), h.lbyr, h.lbyrd)
76                resp += l
77
78            # Write to file
79            fout = open(outputFilePath, "w")
80            fout.write(resp)
81            fout.close()
82
83            size = os.path.getsize(outputFilePath)
84       
85            # Add the output list to the XML output section: ProcessSpecificContent
86            context.outputs['ProcessSpecificContent'] = {"NumberOfPPRecords": ppf.nfields} 
87
88        else:
89            # Make it up for dry run
90            size = 2345 
91
92        if not dry_run:
93            # We can log information at any time to the main log file
94            context.log.info('Written output file: %s' % outputFilePath)
95        else:
96            context.log.debug("Running dry run.")
97
98        # Add the stations file to the outputs
99        self._addFileToOutputs(outputFilePath, 'Output File', size = size)
100
101        if not dry_run:
102            # Finish up by calling function to set status to complete and zip up files etc
103            # In this case we set keep = True so that output file is accessible to downstream process
104            # outside of the main output zip file (so downstream process does not need to unzip it).
105            # This is fine as files are small.
106            process_support.finishProcess(context, self.fileSet, self.startTime, keep = True)
107        else:
108            estimated_duration = 10 # seconds
109            process_support.finishDryRun(context, [], self.fileSet,
110                            estimated_duration, acceptedMessage = 'Dry run complete')           
111
112
113    def _validateInputs(self):
114        """
115        Runs specific checking of arguments and their compatibility.
116        """
117        if not os.path.isfile(self.args["PPFilePath"]):
118            raise Exception("Invalid arguments provided. 'PPFilePath' must point to a real file.")
Note: See TracBrowser for help on using the repository browser.