source: ddcv/scan_ar4.py @ 1286

Subversion URL: http://proj.badc.rl.ac.uk/svn/exarch/ddcv/scan_ar4.py
Revision 1286, 1.5 KB checked in by mjuckes, 6 months ago (diff)

adding ddcvis

  • Property svn:executable set to *
Line 
1#!/usr/bin/env python
2"""
3Process AR4 v2 data directories with cdscan to create necessary cdml files.
4
5Each Model/Scenario combination has it's own cdml file.  Climatologies and
6anomalies have separate cdml files.
7
8"""
9
10import re, os, sys
11
12from cdat_lite.scripts import cdscan
13
14ar4Pattern = re.compile(r'(?P<model>.*?)_(?P<scenario>.*?)_1_(?P<field>.*?)-?(?P<change>change)?_o?(?P<period1>.*?)-(?P<period2>.*?)\.nc')
15
16def processDirectory(dir, cdmlDir):
17    files = os.listdir(dir)
18
19    cdmlFileMap = {}
20    # Gather the files together into each cdscan run
21    for f in files:
22        categoriseFile(f, cdmlFileMap)
23       
24    # Run cdscan on each key in cdmlFileMap
25    for cdmlFile, ncFiles in cdmlFileMap.items():
26        cdscan.main(['cdscan', '-x', os.path.join(cdmlDir, cdmlFile)] +
27                    [os.path.join(dir, f) for f in ncFiles])
28
29def categoriseFile(filename, cdmlFileMap):
30    mo = ar4Pattern.match(filename)
31    if not mo:
32        print "Filename %s not recognised" % filename
33        return
34       
35    params = mo.groupdict()
36    if params['change']:
37        change = '-change'
38    else:
39        change = '-clim'
40
41    p1 = int(params['period1'])
42    p2 = int(params['period2'])
43    period = p2 - p1 + 1
44
45
46    cdmlFile = '%s_%s_1_%s%s_%s.cdml' % (params['model'], params['scenario'],
47                                    params['field'], change, period)
48    cdmlFileMap.setdefault(cdmlFile, []).append(filename)
49
50
51if __name__ == '__main__':
52    cdmlDir = sys.argv[1]
53    for dir in sys.argv[2:]:
54        processDirectory(dir, cdmlDir)
55       
Note: See TracBrowser for help on using the repository browser.