Esempio n. 1
0
    def rgrd(self, dataIn, missingValueIn, missingMatch, logYes = 'yes', positionIn = None, maskIn = None, missingValueOut = None):

        """        #---------------------------------------------------------------------------------
        #
        #    PURPOSE: To perform all the tasks required to regrid the input data, dataIn, into the ouput data, dataout in 
        #             the latitude-level plane.
        #
        #    DEFINITION:
        #
        #             def rgrd(self, dataIn, missingValueIn, missingMatch, positionIn = None, maskIn = None,
        #                                                                               missingValueOut = None):
        # 
        # 
        #    PASSED :  dataIn -- data to regrid
        #
        #             missingValueIn -- the missing data value to use in setting missing in the mask. It is required
        #                               and there are two choices:
        #                                     None -- there is no missing data
        #                                     A number -- the value to use in the search for possible missing data.
        #                               The presence of missing data at a grid point leads to recording 0.0 in the mask.
        #
        #             missingMatch -- the comparison scheme used in searching for missing data in dataIn using the value passed
        #                             in as missingValueIn. The choices are:
        #                                  None -- used if None is the entry for missingValueIn
        #                                  exact -- used if missingValue is the exact value from the file
        #                                  greater -- the missing data value is equal to or greater than missingValueIn
        #                                  less -- the missing data value is equal to or less than missingValueIn
        #
        #             logYes -- choose the level regrid as linear in log of level or linear in level. Set to 
        #                       'yes' for log. Anything else is linear in level.
        #         
        #
        #              positionIn -- a tuple with the numerical position of the dimensions
        #                            in C or Python order specified in the sequence latitude, 
        #                            level and time. Latitude and level are required. If time is missing submit None in its
        #                            slot in the tuple. Notice that the length of the tuple is
        #                            always three.
        #
        #                            Explicitly, in terms of the shape of dataIn as returned by python's shape function
        #
        #                                 positionIn[0] contains the position of latitude in dataIn      
        #                                 positionIn[1] contains the position of level in dataIn or None      
        #                                 positionIn[2] contains the position of time in dataIn or None      
        #
        #                            As  examples:
        #                                 If the c order shape of 3D data is
        #                                     (number of times, number of levels, number of latitudes)
        #                                 submit
        #                                      (2, 1, 0). 
        #
        #                                 If the c order shape of 2D data is 
        #                                     (number of times, number of latitudes)
        #                                 submit
        #                                     (1, None, 0). 
        #
        #                            Send in None if the shape is a subset of (time, level, latitude) which is evaluated
        #                            as follows:
        #                               2D -- code assumes (1,0,None)
        #                               3D -- code assumes (2,1,0)
        #
        #              maskIn -- an array of 1.0 and 0.0 values where the 0.0 value is used to mask the input data. This
        #                        mask only works on the latitude grid. It is not possible to mask out a region in the level
        #                        plane. The 0.0 value removes the data from correponding grid point. The user can supply the
        #                        following choices:
        #
        #                        None -- an array of 1.0s is created followed by substituting 0.0s for grid points with missing
        #                                data in the input data array, dataIn
        #
        #                        array -- an array of 1.0s or 0.0s which must be either 2D or the actual size of the input data,
        #                                 dataIn. This user supplied mask might be used to mask a latitude region. It is not
        #                                 required to account for missing data in the input data. The code uses missingValueIn
        #                                 and missingMatch to supply the 0.0s for grid points with missing data in the input
        #                                 data array, dataIn.
        #
        #
        #              missingValueOut -- the value for the missing data used in writing the output data. If left at the
        #                                 default entry, None, the code uses missingValueIn if present or as a last resort
        #                                 1.0e20
        #
        # 
        #    RETURNED : dataOut -- the regridded data
        #
        #                
        #    USAGE: 
        #                
        #          Example 1.  To regrid dataIn into dataOut using all the defaults where None, None signifies no
        #                      missing data.                   
        #              dataOut = x.rgrd(dataIn, None, None)    
        #
        #          Example 2.  To regrid dataIn into dataOut using 1.0e20 and greater as the missing data
        #                
        #                      dataOut = x.rgrd(dataIn, 1.e20, 'greater')    
        #                
        #    WARNING: This code does not regrid cross sections which have a single dummy longitude value!
        #                
        #
        #---------------------------------------------------------------------------------------------------------------------"""

        # check the required input -- dataIn, missingValueIn and  missingMatch

        # make sure that dataIn is an array

        try:
            z = len(dataIn)
        except TypeError:
            sendmsg('Error in calling the rgrd method -- dataIn must be an array')
            raise TypeError

        # try to identify a single dummy longitude

        dataShape = dataIn.shape

        if len(dataShape) > 3: 
            msg = 'Error in call to rgrd -- cross section data can not have more than 3 dimensions'
            sendmsg(msg)
            raise TypeError

        if positionIn != None:
            if self.nlati != (dataShape[ positionIn[0] ]): 
                msg = 'Latitude vector is inconsistent with input data'
                sendmsg(msg)
                raise ValueError
       
            if self.nlevi != (dataShape[ positionIn[1] ]): 
                msg = 'Level vector is inconsistent with input data'
                sendmsg(msg)
                raise ValueError

        # check the missingValueIn pass

        if missingValueIn != None:
            try:
                z = abs(missingValueIn)
            except TypeError:
                sendmsg('Error in calling the rgrd method -- missingvalueIn must be None or a number. Now it is  ', missingValueIn)
                raise TypeError

        # check the missingMatch pass

        missingPossibilities = ['greater', 'equal', 'less', None]
        if missingMatch not in missingPossibilities:
            msg = 'Error in missingMatch -- it must be None or the string greater, equal, or less. Now it is '
            sendmsg(msg, missingMatch)
            raise ValueError

        # set missing value to be used in dataOut

        if missingValueOut == None:
            if missingValueIn != None:
                omit =  missingValueIn                                                                                # default
            else: 
                omit = 1.0e20                                                                                 # default
        else:
            omit = missingValueOut                                                                        # user choice

        # --- Check data type and change to float if necessary ----

        if dataIn.dtype.char != 'f':
            dataIn = dataIn.astype(numpy.float32)

        # produce the input for rgdlength not generated by maplength

        dataShape = dataIn.shape
        numberDim = len(dataShape)

        if numberDim < 2: 
            msg = 'Error in call to rgrd -- data must have at least 2 dimensions'
            sendmsg(msg)
            raise TypeError
        
        if positionIn == None:               # construct the default positionIn tuple
            positionList =[]
            for n in range(numberDim):       # insert a sequence of numbers
                positionList.append(n)
            positionList.reverse()

            if numberDim == 2:               # fill end of list with a None
                positionList.append(None)

            positionIn = tuple(positionList)

        if len(positionIn) != 3: 
            msg = 'Error in call to rgrd -- positionIn must be a tuple of length 3'
            sendmsg(msg)
            raise TypeError


        # set ilon, ilat in Fortran order except that the first index is 0 - not 1
        ilat = numberDim - 1 - positionIn[0]

        itim1 = itim2 = -1
        ntim1 = ntim2 = 0

        if numberDim == 2:                         # lat and level field 
            itim1 = numberDim - 1 - positionIn[1]  
            ntim1 = dataShape[ positionIn[1] ]

        if numberDim == 3:                         # lon_lat field + level + time
                itim1 = numberDim  -1 - positionIn[1] 
                ntim1 = dataShape[ positionIn[1] ]
                itim2 = numberDim  -1 - positionIn[2]  
                ntim2 = dataShape[ positionIn[2] ]

        # check for consistency between the grid axiss and the dataIn shape
       
        if self.nlati != (dataShape[ positionIn[0] ]): 
            msg = 'Latitude vector is inconsistent with input data'
            sendmsg(msg)
            raise ValueError
       
        if self.nlevi != (dataShape[ positionIn[1] ]): 
            msg = 'Level vector is inconsistent with input data'
            sendmsg(msg)
            raise ValueError

        # allocate memory for aout -- the array with original number of levels but the new number of latitudes

        aoutList = list(dataIn.shape)
        aoutList[ positionIn[0] ] = self.nlato
        aout = numpy.zeros(tuple(aoutList), numpy.float32)                      # memory for aout

        # generate the mask

        amskin = sectionmask(dataIn, positionIn, maskIn, missingValueIn, missingMatch)

        #      ------------- call rgdlength to regrid latitude  ----------------------- 

        amskout = _regrid.rgdlength(ilat, itim1, itim2, ntim1, ntim2,  self.nlati, self.nlato, omit, self.latdx, self.latpt, self.wtlat, amskin, dataIn, aout)  

        #      ------------- call rgdpressure to regrid pressure  -----------------------

        # allocate memory for ap -- the array with new number of levels and the new number of latitudes

        apList = list(dataIn.shape)
        apList[ positionIn[0] ] = self.nlato
        apList[ positionIn[1] ] = self.nlevo
        ap = numpy.zeros(tuple(apList), numpy.float32)                      # memory for ap

        nlon = 0
        if missingMatch == None:                                                # if no missing do not pass None
            missingMatch = 'none'

        if missingValueIn == None:                                                # if no missing do not pass None
            missingValueIn = 1.333e33

        if logYes != 'yes':
            logYes = 'no'

        levIn = self.levIn[:].astype(numpy.float64)
        levOut = self.levOut[:].astype(numpy.float64)
        _regrid.rgdpressure(self.nlevi, self.nlevo, self.nlato, nlon, ntim2, missingValueIn, missingMatch, logYes, levIn, levOut, aout, ap)  

        if missingMatch == 'none':                    # if no missing do not pass None
            missingMatch = None
        if missingValueIn == 1.333e33:              
            missingValueIn = None

        return ap 
Esempio n. 2
0
    def rgrd(self,
             dataIn,
             missingValueIn,
             missingMatch,
             logYes='yes',
             positionIn=None,
             maskIn=None,
             missingValueOut=None):
        """        #---------------------------------------------------------------------------------
        #
        #    PURPOSE: To perform all the tasks required to regrid the input data, dataIn, into the ouput data, dataout in 
        #             the latitude-level plane.
        #
        #    DEFINITION:
        #
        #             def rgrd(self, dataIn, missingValueIn, missingMatch, positionIn = None, maskIn = None,
        #                                                                               missingValueOut = None):
        # 
        # 
        #    PASSED :  dataIn -- data to regrid
        #
        #             missingValueIn -- the missing data value to use in setting missing in the mask. It is required
        #                               and there are two choices:
        #                                     None -- there is no missing data
        #                                     A number -- the value to use in the search for possible missing data.
        #                               The presence of missing data at a grid point leads to recording 0.0 in the mask.
        #
        #             missingMatch -- the comparison scheme used in searching for missing data in dataIn using the value passed
        #                             in as missingValueIn. The choices are:
        #                                  None -- used if None is the entry for missingValueIn
        #                                  exact -- used if missingValue is the exact value from the file
        #                                  greater -- the missing data value is equal to or greater than missingValueIn
        #                                  less -- the missing data value is equal to or less than missingValueIn
        #
        #             logYes -- choose the level regrid as linear in log of level or linear in level. Set to 
        #                       'yes' for log. Anything else is linear in level.
        #         
        #
        #              positionIn -- a tuple with the numerical position of the dimensions
        #                            in C or Python order specified in the sequence latitude, 
        #                            level and time. Latitude and level are required. If time is missing submit None in its
        #                            slot in the tuple. Notice that the length of the tuple is
        #                            always three.
        #
        #                            Explicitly, in terms of the shape of dataIn as returned by python's shape function
        #
        #                                 positionIn[0] contains the position of latitude in dataIn      
        #                                 positionIn[1] contains the position of level in dataIn or None      
        #                                 positionIn[2] contains the position of time in dataIn or None      
        #
        #                            As  examples:
        #                                 If the c order shape of 3D data is
        #                                     (number of times, number of levels, number of latitudes)
        #                                 submit
        #                                      (2, 1, 0). 
        #
        #                                 If the c order shape of 2D data is 
        #                                     (number of times, number of latitudes)
        #                                 submit
        #                                     (1, None, 0). 
        #
        #                            Send in None if the shape is a subset of (time, level, latitude) which is evaluated
        #                            as follows:
        #                               2D -- code assumes (1,0,None)
        #                               3D -- code assumes (2,1,0)
        #
        #              maskIn -- an array of 1.0 and 0.0 values where the 0.0 value is used to mask the input data. This
        #                        mask only works on the latitude grid. It is not possible to mask out a region in the level
        #                        plane. The 0.0 value removes the data from correponding grid point. The user can supply the
        #                        following choices:
        #
        #                        None -- an array of 1.0s is created followed by substituting 0.0s for grid points with missing
        #                                data in the input data array, dataIn
        #
        #                        array -- an array of 1.0s or 0.0s which must be either 2D or the actual size of the input data,
        #                                 dataIn. This user supplied mask might be used to mask a latitude region. It is not
        #                                 required to account for missing data in the input data. The code uses missingValueIn
        #                                 and missingMatch to supply the 0.0s for grid points with missing data in the input
        #                                 data array, dataIn.
        #
        #
        #              missingValueOut -- the value for the missing data used in writing the output data. If left at the
        #                                 default entry, None, the code uses missingValueIn if present or as a last resort
        #                                 1.0e20
        #
        # 
        #    RETURNED : dataOut -- the regridded data
        #
        #                
        #    USAGE: 
        #                
        #          Example 1.  To regrid dataIn into dataOut using all the defaults where None, None signifies no
        #                      missing data.                   
        #              dataOut = x.rgrd(dataIn, None, None)    
        #
        #          Example 2.  To regrid dataIn into dataOut using 1.0e20 and greater as the missing data
        #                
        #                      dataOut = x.rgrd(dataIn, 1.e20, 'greater')    
        #                
        #    WARNING: This code does not regrid cross sections which have a single dummy longitude value!
        #                
        #
        #---------------------------------------------------------------------------------------------------------------------"""

        # check the required input -- dataIn, missingValueIn and  missingMatch

        # make sure that dataIn is an array

        try:
            z = len(dataIn)
        except TypeError:
            sendmsg(
                'Error in calling the rgrd method -- dataIn must be an array')
            raise TypeError

        # try to identify a single dummy longitude

        dataShape = dataIn.shape

        if len(dataShape) > 3:
            msg = 'Error in call to rgrd -- cross section data can not have more than 3 dimensions'
            sendmsg(msg)
            raise TypeError

        if positionIn != None:
            if self.nlati != (dataShape[positionIn[0]]):
                msg = 'Latitude vector is inconsistent with input data'
                sendmsg(msg)
                raise ValueError

            if self.nlevi != (dataShape[positionIn[1]]):
                msg = 'Level vector is inconsistent with input data'
                sendmsg(msg)
                raise ValueError

        # check the missingValueIn pass

        if missingValueIn != None:
            try:
                z = abs(missingValueIn)
            except TypeError:
                sendmsg(
                    'Error in calling the rgrd method -- missingvalueIn must be None or a number. Now it is  ',
                    missingValueIn)
                raise TypeError

        # check the missingMatch pass

        missingPossibilities = ['greater', 'equal', 'less', None]
        if missingMatch not in missingPossibilities:
            msg = 'Error in missingMatch -- it must be None or the string greater, equal, or less. Now it is '
            sendmsg(msg, missingMatch)
            raise ValueError

        # set missing value to be used in dataOut

        if missingValueOut == None:
            if missingValueIn != None:
                omit = missingValueIn  # default
            else:
                omit = 1.0e20  # default
        else:
            omit = missingValueOut  # user choice

        # --- Check data type and change to float if necessary ----

        if dataIn.dtype.char != 'f':
            dataIn = dataIn.astype(numpy.float32)

        # produce the input for rgdlength not generated by maplength

        dataShape = dataIn.shape
        numberDim = len(dataShape)

        if numberDim < 2:
            msg = 'Error in call to rgrd -- data must have at least 2 dimensions'
            sendmsg(msg)
            raise TypeError

        if positionIn == None:  # construct the default positionIn tuple
            positionList = []
            for n in range(numberDim):  # insert a sequence of numbers
                positionList.append(n)
            positionList.reverse()

            if numberDim == 2:  # fill end of list with a None
                positionList.append(None)

            positionIn = tuple(positionList)

        if len(positionIn) != 3:
            msg = 'Error in call to rgrd -- positionIn must be a tuple of length 3'
            sendmsg(msg)
            raise TypeError

        # set ilon, ilat in Fortran order except that the first index is 0 - not 1
        ilat = numberDim - 1 - positionIn[0]

        itim1 = itim2 = -1
        ntim1 = ntim2 = 0

        if numberDim == 2:  # lat and level field
            itim1 = numberDim - 1 - positionIn[1]
            ntim1 = dataShape[positionIn[1]]

        if numberDim == 3:  # lon_lat field + level + time
            itim1 = numberDim - 1 - positionIn[1]
            ntim1 = dataShape[positionIn[1]]
            itim2 = numberDim - 1 - positionIn[2]
            ntim2 = dataShape[positionIn[2]]

        # check for consistency between the grid axiss and the dataIn shape

        if self.nlati != (dataShape[positionIn[0]]):
            msg = 'Latitude vector is inconsistent with input data'
            sendmsg(msg)
            raise ValueError

        if self.nlevi != (dataShape[positionIn[1]]):
            msg = 'Level vector is inconsistent with input data'
            sendmsg(msg)
            raise ValueError

        # allocate memory for aout -- the array with original number of levels but the new number of latitudes

        aoutList = list(dataIn.shape)
        aoutList[positionIn[0]] = self.nlato
        aout = numpy.zeros(tuple(aoutList), numpy.float32)  # memory for aout

        # generate the mask

        amskin = sectionmask(dataIn, positionIn, maskIn, missingValueIn,
                             missingMatch)

        #      ------------- call rgdlength to regrid latitude  -----------------------

        amskout = _regrid.rgdlength(ilat, itim1, itim2, ntim1, ntim2,
                                    self.nlati, self.nlato, omit, self.latdx,
                                    self.latpt, self.wtlat, amskin, dataIn,
                                    aout)

        #      ------------- call rgdpressure to regrid pressure  -----------------------

        # allocate memory for ap -- the array with new number of levels and the new number of latitudes

        apList = list(dataIn.shape)
        apList[positionIn[0]] = self.nlato
        apList[positionIn[1]] = self.nlevo
        ap = numpy.zeros(tuple(apList), numpy.float32)  # memory for ap

        nlon = 0
        if missingMatch == None:  # if no missing do not pass None
            missingMatch = 'none'

        if missingValueIn == None:  # if no missing do not pass None
            missingValueIn = 1.333e33

        if logYes != 'yes':
            logYes = 'no'

        levIn = self.levIn[:].astype(numpy.float64)
        levOut = self.levOut[:].astype(numpy.float64)
        _regrid.rgdpressure(self.nlevi, self.nlevo, self.nlato, nlon, ntim2,
                            missingValueIn, missingMatch, logYes, levIn,
                            levOut, aout, ap)

        if missingMatch == 'none':  # if no missing do not pass None
            missingMatch = None
        if missingValueIn == 1.333e33:
            missingValueIn = None

        return ap
Esempio n. 3
0
    def rgrd(self, dataIn, missingValueIn, missingMatch, logYes = 'yes', positionIn = None, missingValueOut = None):

        """        #---------------------------------------------------------------------------------
        #
        #    PURPOSE: To perform all the tasks required to regrid the input data, dataIn, into the ouput data,
        #             dataout along the level dimension only.
        #
        #    DEFINITION:
        #
        #             def rgrd(self, dataIn, missingValueIn, missingMatch, positionIn = None, missingValueOut = None):
        # 
        # 
        #    PASSED :  dataIn -- data to regrid
        #
        #             missingValueIn -- the missing data value to use in setting missing in the mask. It is required
        #                               and there are two choices:
        #                                     None -- there is no missing data
        #                                     A number -- the value to use in the search for possible missing data.
        #                               The presence of missing data at a grid point leads to recording 0.0 in the mask.
        #
        #             missingMatch -- the comparison scheme used in searching for missing data in dataIn using the value passed
        #                             in as missingValueIn. The choices are:
        #                                  None -- used if None is the entry for missingValueIn
        #                                  exact -- used if missingValue is the exact value from the file
        #                                  greater -- the missing data value is equal to or greater than missingValueIn
        #                                  less -- the missing data value is equal to or less than missingValueIn
        #
        #             logYes -- choose the level regrid as linear in log of level or linear in level. Set to 
        #                       'yes' for log. Anything else is linear in level.
        #         
        #
        #
        #             positionIn -- a tuple with the numerical position of the dimensions
        #                           in C or Python order specified in the sequence longitude,
        #                           latitude, level and time. Longitude, latitude and level are
        #                           required. If time is missing submit None in its slot in the 
        #                           tuple. Notice that the length of the tuple is always four.
        #
        #                           Explicitly, in terms of the shape of dataIn as returned by Python's shape function
        #
        #                                positionIn[0] contains the position of longitude in dataIn      
        #                                positionIn[1] contains the position of latitude in dataIn      
        #                                positionIn[2] contains the position of level in dataIn or None      
        #                                positionIn[3] contains the position of time in dataIn or None      
        #
        #                           As  examples:
        #                                If the C order shape of 4D data is
        #                                    (number of longitudes, number of times, number of levels, number of latitudes)
        #                                submit
        #                                     (0, 3, 2, 1) 
        #
        #                                If the C order shape of 3D data is 
        #                                    (number of longitudes, number of times, number oflatitudes)
        #                                submit
        #                                    (0, 2, 1, None) 
        #
        #                           Send in None if the shape is a subset of (time, level,
        #                           latitude, longitude) which is evaluated as follows:
        #                              3D -- code assumes (2,1,0,None)
        #                              4D -- code assumes (3,2,1,0)
        #
        #              missingValueOut -- the value for the missing data used in writing the output data. If left at the
        #                                 default entry, None, the code uses missingValueIn if present or as a last resort
        #                                 1.0e20
        #
        # 
        #    RETURNED : dataOut -- the regridded data
        #
        #                
        #    USAGE: 
        #                
        #          Example 1.  To regrid dataIn into dataOut using all the defaults where None, None signifies no
        #                      missing data.                   
        #              dataOut = x.rgrd(dataIn, None, None)    
        #
        #          Example 2.  To regrid dataIn into dataOut using 1.0e20 and greater as the missing data
        #                
        #                      dataOut = x.rgrd(dataIn, 1.e20, 'greater')    
        #
        #---------------------------------------------------------------------------------------------------------------------"""

        # check the required input -- dataIn, missingValueIn and  missingMatch

        # make sure that dataIn is an array

        try:
            z = len(dataIn)
        except TypeError:
            sendmsg('Error in calling the rgrd method -- dataIn must be an array')
            raise TypeError

        # check the missingValueIn pass

        if missingValueIn != None:
            try:
                z = abs(missingValueIn)
            except TypeError:
                sendmsg('Error in calling the rgrd method -- missingvalueIn must be None or a number. Now it is  ', missingValueIn)
                raise TypeError

        # check the missingMatch pass

        missingPossibilities = ['greater', 'equal', 'less', None]
        if missingMatch not in missingPossibilities:
            msg = 'Error in missingMatch -- it must be None or the string greater, equal, or less. Now it is '
            sendmsg(msg, missingMatch)
            raise ValueError

        # --- Check data type and change to float if necessary ----

        if dataIn.dtype.char != 'f':
            dataIn = dataIn.astype(numpy.float32)

        dataShape = dataIn.shape
        numberDim = len(dataShape)

        if numberDim < 2: 
            msg = 'Error in call to rgrd -- data must have at least 2 dimensions'
            sendmsg(msg)
            raise TypeError

        # --- evaluate positionIn ----
        
        # --- make standard positionIn as a check----
        positionList =[]
        for n in range(numberDim):              # insert a sequence of numbers
            positionList.append(n)
        positionList.reverse()

        for n in range(numberDim, 4):            # fill end of list with Nones
            positionList.append(None)

        positionCheck = tuple(positionList)      


        standardPosition = 0                            # transpose required

        if positionIn == None:                          # construct the default positionIn tuple
            positionIn = positionCheck
            standardPosition = 1                        # no need for a transpose with this data
        else:
            if positionIn == positionCheck:             # compare to the standard
                standardPosition = 1                    # no need for a transpose with this data

        if len(positionIn) != 4: 
            msg = 'Error in call to rgrd -- positionIn must be a tuple of length 4'
            sendmsg(msg)
            raise TypeError

        if standardPosition == 0:                        # transpose data to the standard order (t,z,y,x)

            newOrder, inverseOrder = checkorder(positionIn)

            dataIn = numpy.transpose(dataIn, newOrder)                    # transpose data to standard order (t,z,y,x)
            dataIn = numpy.array(dataIn.astype(numpy.float32), numpy.float32)       # make contiguous 


        # set dimension sizes and check for consistency 

        if positionIn[0] != None: 
            self.nlon = (dataShape[ positionIn[0] ]) 
        else:
            self.nlon = 0 
        if positionIn[1] != None: 
            self.nlat = (dataShape[ positionIn[1] ]) 
        else:
            self.nlat = 0 
        if positionIn[2] != None: 
            if self.nlevi != (dataShape[ positionIn[2] ]): 
                msg = 'Level size is inconsistent with input data'
                sendmsg(msg)
                raise ValueError
        if positionIn[3] != None: 
            self.ntime = (dataShape[ positionIn[3] ]) 
        else:
            self.ntime = 0 

        # allocate memory for dataOut -- the array with new number of levels

        outList = list(dataIn.shape)

        for i in range(len(outList)):
            if outList[i] == self.nlevi:
                outList[i] = self.nlevo
                break

        dataOut = numpy.zeros(tuple(outList), numpy.float32)                      # memory for aout


        if missingMatch == None:                                                # if no missing do not pass None
            missingMatch = 'none'

        if missingValueIn == None:                                                # if no missing do not pass None
            missingValueIn = 1.333e33

        if logYes != 'yes':
            logYes = 'no'

        levIn = self.axisIn[:].astype(numpy.float64)
        levOut = self.axisOut[:].astype(numpy.float64)
        _regrid.rgdpressure(self.nlevi, self.nlevo, self.nlat, self.nlon, self.ntime, missingValueIn, missingMatch, logYes, levIn, levOut, dataIn, dataOut)  

        if missingMatch == 'none':                                                # if no missing do not pass None
            missingMatch = None
        if missingValueIn == 1.333e33:              
            missingValueIn = None

        if standardPosition == 0:
            dataOut = numpy.transpose(dataOut, inverseOrder)                                   # transpose data to original order
            dataOut = numpy.array(dataOut.astype(numpy.float32), numpy.float32)            # make contiguous 

        if missingValueOut != None:                # set the missing value in data to missingValueOut

            if missingMatch == 'greater': 
                if missingValueIn > 0.0: 
                    missing = 0.99*missingValueIn
                else: 
                    missing = 1.01*missingValueIn

                dataOut = numpy.where(numpy.greater(dataOut,missing), missingValueOut, dataOut)

            elif missingMatch == 'equal': 
                missing = missingValueIn
                dataOut = numpy.where(numpy.equal(dataOut,missing), missingValueOut, dataOut)

            elif missingMatch == 'less': 
                if missingValueIn < 0.0: 
                    missing = 0.99*missingValueIn
                else: 
                    missing = 1.01*missingValueIn

                dataOut = numpy.where(numpy.less(dataOut,missing), missingValueOut, dataOut)

        return dataOut 
Esempio n. 4
0
    def rgrd(self,
             dataIn,
             missingValueIn,
             missingMatch,
             logYes='yes',
             positionIn=None,
             missingValueOut=None):
        """        #---------------------------------------------------------------------------------
        #
        #    PURPOSE: To perform all the tasks required to regrid the input data, dataIn, into the ouput data,
        #             dataout along the level dimension only.
        #
        #    DEFINITION:
        #
        #             def rgrd(self, dataIn, missingValueIn, missingMatch, positionIn = None, missingValueOut = None):
        # 
        # 
        #    PASSED :  dataIn -- data to regrid
        #
        #             missingValueIn -- the missing data value to use in setting missing in the mask. It is required
        #                               and there are two choices:
        #                                     None -- there is no missing data
        #                                     A number -- the value to use in the search for possible missing data.
        #                               The presence of missing data at a grid point leads to recording 0.0 in the mask.
        #
        #             missingMatch -- the comparison scheme used in searching for missing data in dataIn using the value passed
        #                             in as missingValueIn. The choices are:
        #                                  None -- used if None is the entry for missingValueIn
        #                                  exact -- used if missingValue is the exact value from the file
        #                                  greater -- the missing data value is equal to or greater than missingValueIn
        #                                  less -- the missing data value is equal to or less than missingValueIn
        #
        #             logYes -- choose the level regrid as linear in log of level or linear in level. Set to 
        #                       'yes' for log. Anything else is linear in level.
        #         
        #
        #
        #             positionIn -- a tuple with the numerical position of the dimensions
        #                           in C or Python order specified in the sequence longitude,
        #                           latitude, level and time. Longitude, latitude and level are
        #                           required. If time is missing submit None in its slot in the 
        #                           tuple. Notice that the length of the tuple is always four.
        #
        #                           Explicitly, in terms of the shape of dataIn as returned by Python's shape function
        #
        #                                positionIn[0] contains the position of longitude in dataIn      
        #                                positionIn[1] contains the position of latitude in dataIn      
        #                                positionIn[2] contains the position of level in dataIn or None      
        #                                positionIn[3] contains the position of time in dataIn or None      
        #
        #                           As  examples:
        #                                If the C order shape of 4D data is
        #                                    (number of longitudes, number of times, number of levels, number of latitudes)
        #                                submit
        #                                     (0, 3, 2, 1) 
        #
        #                                If the C order shape of 3D data is 
        #                                    (number of longitudes, number of times, number oflatitudes)
        #                                submit
        #                                    (0, 2, 1, None) 
        #
        #                           Send in None if the shape is a subset of (time, level,
        #                           latitude, longitude) which is evaluated as follows:
        #                              3D -- code assumes (2,1,0,None)
        #                              4D -- code assumes (3,2,1,0)
        #
        #              missingValueOut -- the value for the missing data used in writing the output data. If left at the
        #                                 default entry, None, the code uses missingValueIn if present or as a last resort
        #                                 1.0e20
        #
        # 
        #    RETURNED : dataOut -- the regridded data
        #
        #                
        #    USAGE: 
        #                
        #          Example 1.  To regrid dataIn into dataOut using all the defaults where None, None signifies no
        #                      missing data.                   
        #              dataOut = x.rgrd(dataIn, None, None)    
        #
        #          Example 2.  To regrid dataIn into dataOut using 1.0e20 and greater as the missing data
        #                
        #                      dataOut = x.rgrd(dataIn, 1.e20, 'greater')    
        #
        #---------------------------------------------------------------------------------------------------------------------"""

        # check the required input -- dataIn, missingValueIn and  missingMatch

        # make sure that dataIn is an array

        try:
            z = len(dataIn)
        except TypeError:
            sendmsg(
                'Error in calling the rgrd method -- dataIn must be an array')
            raise TypeError

        # check the missingValueIn pass

        if missingValueIn != None:
            try:
                z = abs(missingValueIn)
            except TypeError:
                sendmsg(
                    'Error in calling the rgrd method -- missingvalueIn must be None or a number. Now it is  ',
                    missingValueIn)
                raise TypeError

        # check the missingMatch pass

        missingPossibilities = ['greater', 'equal', 'less', None]
        if missingMatch not in missingPossibilities:
            msg = 'Error in missingMatch -- it must be None or the string greater, equal, or less. Now it is '
            sendmsg(msg, missingMatch)
            raise ValueError

        # --- Check data type and change to float if necessary ----

        if dataIn.dtype.char != 'f':
            dataIn = dataIn.astype(numpy.float32)

        dataShape = dataIn.shape
        numberDim = len(dataShape)

        if numberDim < 2:
            msg = 'Error in call to rgrd -- data must have at least 2 dimensions'
            sendmsg(msg)
            raise TypeError

        # --- evaluate positionIn ----

        # --- make standard positionIn as a check----
        positionList = []
        for n in range(numberDim):  # insert a sequence of numbers
            positionList.append(n)
        positionList.reverse()

        for n in range(numberDim, 4):  # fill end of list with Nones
            positionList.append(None)

        positionCheck = tuple(positionList)

        standardPosition = 0  # transpose required

        if positionIn == None:  # construct the default positionIn tuple
            positionIn = positionCheck
            standardPosition = 1  # no need for a transpose with this data
        else:
            if positionIn == positionCheck:  # compare to the standard
                standardPosition = 1  # no need for a transpose with this data

        if len(positionIn) != 4:
            msg = 'Error in call to rgrd -- positionIn must be a tuple of length 4'
            sendmsg(msg)
            raise TypeError

        if standardPosition == 0:  # transpose data to the standard order (t,z,y,x)

            newOrder, inverseOrder = checkorder(positionIn)

            dataIn = numpy.transpose(
                dataIn, newOrder)  # transpose data to standard order (t,z,y,x)
            dataIn = numpy.array(dataIn.astype(numpy.float32),
                                 numpy.float32)  # make contiguous

        # set dimension sizes and check for consistency

        if positionIn[0] != None:
            self.nlon = (dataShape[positionIn[0]])
        else:
            self.nlon = 0
        if positionIn[1] != None:
            self.nlat = (dataShape[positionIn[1]])
        else:
            self.nlat = 0
        if positionIn[2] != None:
            if self.nlevi != (dataShape[positionIn[2]]):
                msg = 'Level size is inconsistent with input data'
                sendmsg(msg)
                raise ValueError
        if positionIn[3] != None:
            self.ntime = (dataShape[positionIn[3]])
        else:
            self.ntime = 0

        # allocate memory for dataOut -- the array with new number of levels

        outList = list(dataIn.shape)

        for i in range(len(outList)):
            if outList[i] == self.nlevi:
                outList[i] = self.nlevo
                break

        dataOut = numpy.zeros(tuple(outList), numpy.float32)  # memory for aout

        if missingMatch == None:  # if no missing do not pass None
            missingMatch = 'none'

        if missingValueIn == None:  # if no missing do not pass None
            missingValueIn = 1.333e33

        if logYes != 'yes':
            logYes = 'no'

        levIn = self.axisIn[:].astype(numpy.float64)
        levOut = self.axisOut[:].astype(numpy.float64)
        _regrid.rgdpressure(self.nlevi, self.nlevo, self.nlat, self.nlon,
                            self.ntime, missingValueIn, missingMatch, logYes,
                            levIn, levOut, dataIn, dataOut)

        if missingMatch == 'none':  # if no missing do not pass None
            missingMatch = None
        if missingValueIn == 1.333e33:
            missingValueIn = None

        if standardPosition == 0:
            dataOut = numpy.transpose(
                dataOut, inverseOrder)  # transpose data to original order
            dataOut = numpy.array(dataOut.astype(numpy.float32),
                                  numpy.float32)  # make contiguous

        if missingValueOut != None:  # set the missing value in data to missingValueOut

            if missingMatch == 'greater':
                if missingValueIn > 0.0:
                    missing = 0.99 * missingValueIn
                else:
                    missing = 1.01 * missingValueIn

                dataOut = numpy.where(numpy.greater(dataOut, missing),
                                      missingValueOut, dataOut)

            elif missingMatch == 'equal':
                missing = missingValueIn
                dataOut = numpy.where(numpy.equal(dataOut, missing),
                                      missingValueOut, dataOut)

            elif missingMatch == 'less':
                if missingValueIn < 0.0:
                    missing = 0.99 * missingValueIn
                else:
                    missing = 1.01 * missingValueIn

                dataOut = numpy.where(numpy.less(dataOut, missing),
                                      missingValueOut, dataOut)

        return dataOut