[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[IDV #PHU-486914]: question



Ah yes, my approach was a little bit too simple. I've attached a .py file that 
uses my code, but has been appropriately modified by Jeff McWhirter to handle 
indexing into your variable (Thanks Jeff!). Use the contents of the python file 
in your Jython Library and you should be good to go. extractBitField(data, N) 
is the function you should use when you want to find the bits for your data.

Let me know if this works :-)

Sean


> Dear Sean,
> I created your function in the local Jython library and then created another 
> function
> 
> KNMIQC(QC):
> ff=getBit(QC,17)
> return ff
> 
> and then I made a formula for this last one (by rightclicking the function)
> Then I tried to display this formula with input the quality flag field in a 
> netCDF file.
> But then I get the error:
> TypeError: unsupported operand type(s) for &: 'int' and 
> 'ucar.visad.data.GeoGridFlatField'
> 
> Probably I have to do something more to do this mask comparison for all 
> gridpoints in the field?
> 
> Kind regards,
> Tilly
> 
> -----Oorspronkelijk bericht-----
> Van: Unidata IDV Support [mailto:address@hidden]
> Verzonden: woensdag 5 oktober 2011 23:18
> Aan: Driesenaar, Tilly (KNMI)
> CC: address@hidden; Driesenaar, Tilly (KNMI)
> Onderwerp: [IDV #PHU-486914]: question
> 
> Sorry, replace "bitNumber" with "n" in the line before the if statement.
> 
> Sean
> 
> > Greetings Tilly,
> >
> > Add this function to your local Jython library (click "Edit" -> "Formulas" 
> > -> "Jython Library", and select "Local Jython" -> "User's Library"), and 
> > give it a try:
> >
> > def getBit(flag, n):
> > """
> > return the value of the nth bit of the given flag
> > """
> > bit_mask = 1 << (bitNumber -1)
> > if ((bit_mask & flag) == 0):
> > return 0
> > else:
> > return 1
> >
> > (note: if the formatting of this function does not come out right in my 
> > message, be sure to add four spaces to everything after the first line [def 
> > getBit(flag, n):], and an additional four spaces to the "return" lines)
> >
> > Cheers!
> >
> > Sean
> >
> >
> > > Dear Sean
> > > Thank you for your help!
> > > I'm dealing with a 32 bit integer.
> > > Cheers,
> > > Tilly
> > >
> > > -----Oorspronkelijk bericht-----
> > > Van: Unidata IDV Support [mailto:address@hidden]
> > > Verzonden: vrijdag 23 september 2011 18:59
> > > Aan: Driesenaar, Tilly (KNMI)
> > > Onderwerp: [IDV #PHU-486914]: question
> > >
> > > Greetings Tilly!
> > >
> > > What kind of interger is QCF (16 bit, 32 bit?)? In Jython 2.6, the bin 
> > > operator will be supported, so things will be much easier. However, you 
> > > will need to create a bitmask and use the bit-wise operator & to extract 
> > > the information you need. If I knew what kind of int you were dealing 
> > > with, I could give you an example to extract out the 17th bit.
> > >
> > > Cheers!
> > >
> > > Sean
> > >
> > > > Dear IDV support desk
> > > >
> > > >
> > > >
> > > > I would like to plot the information in an integer quality flag in IDV.
> > > >
> > > > The flag consists of several masks, e.g. the first, or second or 17th
> > > > bit are set .
> > > >
> > > > In python I would use any of the following commands to extract
> > > > information from this integer quality flag (say QCF).
> > > >
> > > > (As you can see I'm especially interested in the 17th bit (or mask value
> > > > 2**17) )
> > > >
> > > >
> > > >
> > > > bin(int(QCF))[-18]  (look at the value of the 17th bit )
> > > >
> > > > or
> > > >
> > > > (int(QCF)/2**17)%2  (so divide by mask value and do modulo 2)
> > > >
> > > >
> > > >
> > > > How do I get this into an IDV formula?
> > > >
> > > > I tried (QCF/2**17)%2 , but that doesn't seem to evaluate in the right
> > > > way.
> > > >
> > > >
> > > >
> > > > Kind regards,
> > > >
> > > > Tilly Driesenaar
> > > >
> > > > KNMI
> > > >
> > > > The Netherlands
> > > >
> > > >
> > > >
> > > >
> > > >
> > >
> > >
> > > Ticket Details
> > > ===================
> > > Ticket ID: PHU-486914
> > > Department: Support IDV
> > > Priority: Normal
> > > Status: Closed
> > >
> > >
> >
> 
> 
> Ticket Details
> ===================
> Ticket ID: PHU-486914
> Department: Support IDV
> Priority: Normal
> Status: Open
> 
> 


Ticket Details
===================
Ticket ID: PHU-486914
Department: Support IDV
Priority: Normal
Status: Open
from visad.python.JPythonMethods import *
def getBit(flag, bitNumber):
   """return the value of the nth bit of the given flag"""
   bitMask = 1 << (bitNumber - 1)
   if ((bitMask & int(flag)) == 0):
      return 0
   else:
      return 1


def extractBitField(data, N):
    """this assumes the give data field is a bit mask. This procedure returns a 
new grid with 0/1 from the Nth bit in the mask """
    newData = data.clone();
    if (GridUtil.isTimeSequence(newData)):
        for t in range(newData.getDomainSet().getLength()):
            rangeObject = newData.getSample(t)
            values = rangeObject.getFloats(0);
            for i in range(len(values)):
               for j in range(len(values[0])):
                 values[i][j]=getBit(values[i][j],N);
            rangeObject.setSamples(values,1);
    else:
        rangeObject = newData;
        values = rangeObject.getFloats(0);
        for i in range(len(values)):
          for j in range(len(values[0])):
             values[i][j]=getBit(values[i][j],N);
        rangeObject.setSamples(values,1);
    return newData;