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

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

Added sorting to code

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            stashCodesFound = []
72
73            for i in range(ppf.nfields):
74
75                h = ppf.headers[i]
76                l = "Field %s: Stash: %s, Level: %s, ValidTime: %s, DataTime: %s, LBYR: %s, LBYRD: %s\n" % \
77                       (i, h.stashcode, h.lblev, h.getValidityTime(), h.getDataTime(), h.lbyr, h.lbyrd)
78                resp += l
79
80                if h.stashcode not in stashCodesFound:
81                    stashCodesFound.append(h.stashcode)
82
83            # Write to file
84            fout = open(outputFilePath, "w")
85            fout.write(resp)
86            fout.close()
87
88            stashCodesFound.sort()
89
90            size = os.path.getsize(outputFilePath)
91       
92            # Add the output list to the XML output section: ProcessSpecificContent
93            context.outputs['ProcessSpecificContent'] = {
94                                  "NumberOfPPRecords": ppf.nfields,
95                                  "StashCodes": " ".join([str(i) for i in stashCodesFound])} 
96
97        else:
98            # Make it up for dry run
99            size = 2345 
100
101        if not dry_run:
102            # We can log information at any time to the main log file
103            context.log.info('Written output file: %s' % outputFilePath)
104        else:
105            context.log.debug("Running dry run.")
106
107        # Add the stations file to the outputs
108        self._addFileToOutputs(outputFilePath, 'Output File', size = size)
109
110        if not dry_run:
111            # Finish up by calling function to set status to complete and zip up files etc
112            # In this case we set keep = True so that output file is accessible to downstream process
113            # outside of the main output zip file (so downstream process does not need to unzip it).
114            # This is fine as files are small.
115            process_support.finishProcess(context, self.fileSet, self.startTime, keep = True)
116        else:
117            estimated_duration = 10 # seconds
118            process_support.finishDryRun(context, [], self.fileSet,
119                            estimated_duration, acceptedMessage = 'Dry run complete')           
120
121
122    def _validateInputs(self):
123        """
124        Runs specific checking of arguments and their compatibility.
125        """
126        if not os.path.isfile(self.args["FilePath"]):
127            raise Exception("Invalid arguments provided. 'FilePath' must point to a real file.")
Note: See TracBrowser for help on using the repository browser.