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

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

added new subset PP test proc

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