source: DataExtractor/trunk/dx-0.1.0/subset.py @ 70

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/DataExtractor/trunk/dx-0.1.0/subset.py@70
Revision 70, 2.4 KB checked in by astephen, 16 years ago (diff)

Beta-release of dx: version 0.1.0

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1#!/usr/local/cdat/bin/python
2
3helpMessage="""
4subset.py
5=========
6
7A python wrapper around the fortran subsetter.exe application.
8
9Usage:
10======
11
12    subset.py -i <infile(s)> -o <outfile> [-domain <N>,<W>,<S>,<E>] [-lonres <lon_res>]
13                 [-latres <lat_res>]
14
15Where:
16------
17
18    infile(s)   - is one or more input files
19    outfile     - is the output file
20    N,W,S,E     - are the North, West, South, East boundaries of the subset
21    lon_res     - is the longitudinal resolution
22    lat_res     - is the latitudinal resolution
23
24"""
25
26import os, sys
27
28# Set ENVIRONMENT VARIABLES for EMOS tables
29os.environ["MARS_LSM_PATH"]="/usr/local/lib/emos_tables/land_sea_mask"
30os.environ["ECMWF_LOCAL_TABLE_PATH"]="/usr/local/lib/emos_tables/gribtables"
31
32subsetter="/home/tornado/internal/badc/software/datasets/ecmwf/badc_grib_utilities/subsetter.exe"
33
34
35def exitNicely(error):
36
37    print helpMessage
38    print "\nERROR: ", error
39    sys.exit()
40
41
42def subset(infiles, outfile, domain=None, lon_res=None, lat_res=None):
43
44    (north, west, south, east)=domain
45    if len(infiles)>1:
46        # Group them all together
47        pid=os.getpid()
48        tempfile="/tmp/%s_temp.grb" % pid
49        filesstring=""
50        for file in infiles:  filesstring=filesstring+" "+file
51        os.system("cat %s > %s" % (filesstring, tempfile)) 
52    else:
53        tempfile=infiles[0]
54
55    command="%s %s %s %s %s %s %s %s %s" % (subsetter, tempfile, outfile, north, west, south, east, lon_res, lat_res)
56    print command
57    os.system(command)
58    return 1
59
60if __name__=="__main__":
61
62    args=sys.argv[1:]
63    if len(args)!=10:
64        exitNicely("Wrong number of arguments.")
65
66    domain=None
67    lon_res=None
68    lat_res=None
69    iflag=None
70
71    for arg in args:
72   
73        if arg in ["-i", "-infile", "-infiles"]:
74            infiles=[args[args.index(arg)+1]]
75            del args[args.index(arg)+1]
76            iflag=1
77        elif arg in ["-o", "-outfile"]:
78            iflag=None
79            outfile=args[args.index(arg)+1]   
80        elif arg in ["-dom", "-domain"]:
81            domain=(args[args.index(arg)+1]).split(",")
82        elif arg in ["-lon", "-lonres"]:
83            lon_res=args[args.index(arg)+1]
84        elif arg in ["-lat", "-latres"]:
85            lat_res=args[args.index(arg)+1]
86        elif iflag==1 and arg[0]!="-":
87            infiles.append(arg)
88    print infiles
89
90    subset(infiles, outfile, domain, lon_res, lat_res) 
Note: See TracBrowser for help on using the repository browser.