source: MILK/trunk/milk_server/milk_server/lib/csml_cache.py @ 4469

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/MILK/trunk/milk_server/milk_server/lib/csml_cache.py@4992
Revision 4469, 2.5 KB checked in by cbyrom, 11 years ago (diff)

Strip out code not relevant to MILK - mainly WCS and WMS stuff - also including the CSML server code + trackback code
Also tidy up structure of 'public' dir - setting up new 'style' dir and
centralising icons in icons dir + remove all unused icons, javascript and stylesheets.
Also strip out testcase code and populate new test directory structure.

Line 
1# Copyright (C) 2007 STFC & NERC (Science and Technology Facilities Council).
2# This software may be distributed under the terms of the
3# Q Public License, version 1.0 or later.
4# http://ndg.nerc.ac.uk/public_docs/QPublic_license.txt
5"""
6Manages a Cache of a fixed number of GridSeries extracts.
7
8Extracts are cached to disk as NetCDF and opened on demand.
9@todo: More info on how this works
10
11"""
12
13from cache import Cache
14import os, tempfile, logging
15try:
16    import cdms2 as cdms
17except:
18    import cdms
19
20class CDMSEntry(object):
21    """
22    A reference to a CDMS file that will delete the file when it is
23    garbage collected.
24
25    Instances of this class are used as Cache entry values.  When entries are
26    automatically removed from the cache the CDMSEntry object will be
27    garbage collected unless it is being accessed by another thread.
28
29    """
30
31    __slots__ = ['_f', 'var']
32   
33    def __init__(self, filename, varname):
34        logging.info('Caching file variable %s in %s' % (varname, filename))
35        self._f = cdms.open(filename)
36        self.var = self._f[varname]
37    def __del__(self):
38        filename = self._f.id
39        logging.info('Removing file %s' % filename)
40        self._f.close()
41        os.remove(filename)
42 
43
44class CSMLExtractCache(Cache):
45   
46    def __init__(self, cache_dir, max_size=0):
47        super(CSMLExtractCache, self).__init__(max_size)
48        self._cache_dir = cache_dir
49
50    def _extract(self, feature, sel):
51        (fd, filename) = tempfile.mkstemp('.nc', 'csml_wxs_', self._cache_dir)
52        os.close(fd)
53        (outputdir, ncname) = os.path.split(filename)
54
55        # Workarround until ticket:778 (TestExtractAll) is fixed
56        sel.update(longitude=(-180, 180), latitude=(-90, 90))
57
58        feature.subsetToGridSeries(ncname=ncname, outputdir=outputdir, **sel)
59
60        return filename
61   
62    def key(self, name):
63        """
64        Cache provides this method to map externally visible entry names
65        to internal keys.  We use it here to turn selectors into hashable
66        values.
67
68        """
69        (feature, sel) = name
70       
71        return (feature.id, tuple(sorted(sel.items())))
72
73    def build(self, key, name, opened, entry):
74        (feature, sel) = name
75        filename = self._extract(feature, sel)
76
77        return CDMSEntry(filename, feature.name.CONTENT)
78
79    def __getitem__(self, name):
80        """
81        Returns the opened CDMS object.
82
83        """
84        return super(CSMLExtractCache, self).__getitem__(name).var
Note: See TracBrowser for help on using the repository browser.