Changeset 3442 for nappy


Ignore:
Timestamp:
15/02/08 18:50:16 (12 years ago)
Author:
astephen
Message:

Started looking at allowing various FFIs to be selected when converting from NetCDF.

Location:
nappy/trunk/nappy
Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • nappy/trunk/nappy/cdms_utils/axis_utils.py

    r3351 r3442  
    2020import cdms 
    2121 
    22 def areAxesIdentical(ax1, ax2): 
     22def areAxesIdentical(ax1, ax2, is_subset=False): 
    2323    """ 
    2424    Takes 2 CDMS axis objects returning True if they are essentially 
    2525    the same and False if not. 
     26    If is_subset == True then return True if ax1 is same as ax2 except that it is 
     27    only defined on a subset of regularly spaced values within ax2. 
    2628    """ 
    2729    for axtype in ("time", "level", "latitude", "longitude"): 
     
    3133    # Check ids 
    3234    if ax1.id != ax2.id: return False 
    33     # Check lengths 
    34     if len(ax1) != len(ax2): return False 
    35     # Check values 
    36     if ax1._data_ != ax2._data_: return False 
    3735    # Check units 
    3836    if ax1.units != ax2.units: return False 
     37     
     38    # Do different comparisons depending on 'is_subset' argument 
     39    if is_subset == False: 
     40        # Check lengths and values 
     41        if len(ax1) != len(ax2) or ax1._data_ != ax2._data_: return False 
     42    elif is_subset == True: 
     43        # Check whether values are a subset 
     44        len1 = len(ax1) 
     45        len2 = len(ax2) 
     46        # Check length of 1 divides into length of 2 
     47        if len2 % len1 != 0: 
     48            return False 
     49        # Now test if it is subset 
     50        n = len2 / len1 
     51 
     52        for i in range(len(ax1)): 
     53            ax2_value = ax2[n * i] 
     54            test_value = ax1[i]  
     55            if ax2_value != test_value: 
     56                return False 
     57 
    3958    # OK, I think they are the same axis! 
    4059    return True 
    4160 
     61def isAxisRegularlySpacedSubsetOf(ax1, ax2): 
     62    """ 
     63    Returns True if ax1 is same as ax2 except that it is only defined on a 
     64    subset of regularly spaced values within ax2. Otherwise returns False. 
     65    """ 
     66    return areAxesIdentical(ax1, ax2, is_subset=True) 
  • nappy/trunk/nappy/nc_interface/na_content_collector.py

    r3441 r3442  
    4646    """ 
    4747     
    48     def __init__(self, variables, global_attributes={}): 
     48    def __init__(self, variables, global_attributes={}, requested_ffi=None): 
    4949        """ 
    5050        Sets up instance variables and calls appropriate methods to 
     
    6969        self.var_ids = None 
    7070        self.globals = global_attributes         
     71        self.requested_ffi = ffi 
    7172        self.rank_zero_vars = [] 
    7273        self.rank_zero_var_ids = [] 
     
    114115        """ 
    115116        self.unused_vars = [] 
     117        ffis_limited = False 
    116118 
    117119        highest_rank = -1 
     
    169171 
    170172                first_axis = var.getAxis(0) 
    171  
     173                # Check if axis is identical to first axis of main best variable, if so, can be auxiliary var 
    172174                if nappy.cdms_utils.axis_utils.areAxesIdentical(best_var_axes[0], first_axis) == False:  
    173                     self.unused_vars.append(var) 
    174                     continue 
     175                    # If not identical, then it might still qualify as an auxiliary every n time points - valid for 1020 
     176                    if len(var.shape) == 1 and nappy.cdms_utils.axis_utils.isAxisRegularlySpacedSubsetOf(first_axis, best_var_axes[0]) == True: 
     177                        ffis_limited = [1020] 
     178                    else: 
     179                        self.unused_vars.append(var) 
     180                        continue 
    175181 
    176182                # I think it is an auxiliary variable 
     
    179185                print "NOTE: Auxiliary variables are recorded in file but also placed in unused category just in case they should be re-used in other files." 
    180186                self.unused_vars.append(var) 
     187 
    181188            else: 
    182189                this_var_axes = var.getAxisList() 
     
    196203 
    197204        # Get the FFI 
    198         self.na_dict["FFI"] = self._decideFileFormatIndex(number_of_dims, aux_vars_for_na) 
     205        self.na_dict["FFI"] = self._decideFileFormatIndex(number_of_dims, aux_vars_for_na, ffis_limited) 
    199206        return (vars_for_na, aux_vars_for_na) 
    200207 
     
    245252 
    246253 
    247     def _decideFileFormatIndex(self, number_of_dims, aux_vars_for_na): 
     254    def _decideFileFormatIndex(self, number_of_dims, aux_vars_for_na, ffis_limited=False): 
    248255        """ 
    249256        Based on the number of dimensions and the NASA Ames dictionary return 
    250         the File Format Index. 
    251         """ 
    252         if number_of_dims in (2,3,4): 
     257        the File Format Index.  
     258        If there is a choice then make the most sensible selection. 
     259        If the user has specified a 'requested_ffi' then try and deliver 
     260        that. Raise an error if not possible. 
     261        """ 
     262        # If ffis_limited is set then must use one of those 
     263        if self.requested_ffi and ffis_limited: 
     264            if self.requested_ffi not in ffis_limited: 
     265                raise Exception("Cannot write this data to FFI '" + str(self.requested_ffi) + "', can only write to: " + str(ffis_limited) + ".") 
     266            else: 
     267                return self.requested_ffi 
     268 
     269        # Base the sub-selection on number of dimensions 
     270        if number_of_dims > 1: 
    253271            ffi = 10 + (number_of_dims * 1000) 
    254272        elif number_of_dims > 4: 
     
    259277            else: 
    260278                ffi = 1001 
     279 
     280        if self.requestd_ffi and ffi != self.requested_ffi: 
     281            raise Exception("Cannot write this data to FFI '" + str(self.requested_ffi) + "', can only write to: " + str(ffi) + ".") 
    261282        return ffi 
    262283 
Note: See TracChangeset for help on using the changeset viewer.