source: nappy/trunk/nappy/bin/scanFAAM.py @ 3314

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/nappy/trunk/nappy/bin/scanFAAM.py@3314
Revision 3314, 2.7 KB checked in by astephen, 12 years ago (diff)

Re-factored under a nappy directory for a start.

  • Property svn:executable set to *
Line 
1#!/usr/bin/env python
2
3"""
4scanFAAM.py
5===========
6
7Holds the scanFAAM function that is used to work out the first and last
8index of real data (i.e. non-missing data in a load of FAAM flight data).
9It is used in conjunction with nappy to reduce the size of output files
10by missing out beginning and end periods that hold only misssing values.
11
12Usage
13=====
14
15    scanFAAM.py -f <filename> [-m <missing_value>]
16
17Where:
18------
19
20    filename   - path to a FAAM NetCDF file
21    missing_value - missing value to use for variables.
22
23"""
24
25import os, sys, cdms, getopt
26
27def scanFAAM(fileName=None, vars=None, nth=4, missingValuesToUse=(-9999., -32767.)):
28    """
29    Scans every 'nth' variable in the list of variables (or found in the
30    file and gets the first and last index of the first (time) dimension
31    that holds real (non-missing) values.
32    """
33    if type(missingValuesToUse)!=type((1,2)):
34        missingValuesToUse=(missingValuesToUse,)
35    startList=[]
36    endList=[]
37    start=None
38    end=None
39
40    if not fileName and not vars:
41        raise "You must provide either a file name or a list of cdms variables."
42   
43    if fileName:
44        f=cdms.open(fileName)
45        vars=f.listvariables()
46
47    for var in vars:
48        if type(var)!=type(""):
49            id=var.id
50        else:
51            id=var
52
53        if id[-4:]=="FLAG" or id=="Time":
54            continue
55
56        if type(var)==type(""):
57            var=f(var)
58
59        step=1000
60        while (start, end)==(None, None):
61            (start, end)=findMissing(var, step, missingValuesToUse)
62            step=step/2
63
64        startList.append(start)
65        endList.append(end)
66        print "Start/End index: %s %s:%s" % (id, start, end)
67 
68    startMin=min(startList)
69    endMax=max(endList)
70    return (startMin, endMax)
71
72           
73def findMissing(var, step, missingValuesToUse):
74    """
75    Returns the (start, end) tuple for a given variable where
76    they are indices of an array where missing values end and begin.
77    """
78    start=None
79    end=None
80    i0=0   
81    sh=var.shape
82    iend=sh[0]-1
83
84    print var.id, step
85    for miss in missingValuesToUse:
86        for i in range(i0, iend, step):
87            if var[i][0]==miss:
88                start=i
89                break
90           
91        for e in range(iend, i0, -step):
92            if var[e][0]==miss:
93                end=e
94                break
95           
96    return (start, end)
97
98if __name__=="__main__":
99
100    argList=sys.argv[1:]
101    args=getopt.getopt(argList, "f:m:")
102    fileName=None
103    missingValue=None
104
105    for arg,value in args[0]:
106        if arg=="-f":
107            fileName=value
108        elif arg=="-m":
109            missingValue=float(value)
110
111    scanFAAM(fileName=fileName, missingValuesToUse=(missingValue,))
Note: See TracBrowser for help on using the repository browser.