[Thread Prev][Thread Next][Index]

Re: [ferret_users] string comparison



Hello,
Yes, you can do all of this.

Use the STRCMP function to compare the strings. (try "SHOW FUNCTION *STR*  to see a list of most of the functions that operate on strings).  There are a couple of things you can do to take care of the case where there are no units. One is to just add a known string when defining the symbol unt, and take that into account when making the comparison. So your example could go something like this:
use monthly_navy_winds

! This will create a symbol with value UWIND_UNITS if there are no units.

def sym unt=`uwnd,r=unit`UWIND_UNITS

if `STRCMP("($unt)", "cmUWIND_UNITS")) EQ 0` then
  let nwnd=uwnd/100
else
  let nwnd=uwnd
end if
Another option would be to use the attribute handling syntax in Ferret to first check whether the variable has a units attribute.  Here I also show how to convert the units to uppercase before making the comparison.  If the input file has units listed as "CM" or "cm" you would still want to treat them the same way"
use monthly_navy_winds
let uatts = uwnd.attnames   ! a list of all the attribute names
list uatts

! if one of the attributes is units, then use its upper-cased value

if `IS_ELEMENT_OF_STR(uatts, "units")` then

  define symbol uin = `uwnd,return=units`
  define symbol unt = `UPCASE("($uin)")`
else
  define symbol unt = none
endif

if `STRCMP("($unt)", "CM") EQ 0` then
   let nwnd=uwnd/100
else
   let nwnd=uwnd
end if


Sudheer Joseph wrote:
Dear Users,
                             Is there any way in ferret to do string comparison,

ie, I want to use the unit information in netcdf variable and do
action based on that.
Also if there is no unit for the variable the code should not break
instead continue echoing that there was no unit.
it could  be some thing like below

use monthly_navy_winds

def sym unt=`uwnd,r=ubnit`
if (($unt) eq "cm") then
nwnd=uwnd/100
else
nwnd=uwnd
end if

though the above code is written it will break if a data file do not
have unit information
  

[Thread Prev][Thread Next][Index]

Contact Us
Dept of Commerce / NOAA / OAR / PMEL / TMAP

Privacy Policy | Disclaimer | Accessibility Statement