Re: Problem using conditional statements (if then else) in NCL

From: Mary Haley <haley_at_nyahnyahspammersnyahnyah>
Date: Fri, 24 Jul 2009 10:20:02 -0600 (MDT)

On Fri, 24 Jul 2009, Rosa Maria Nascimento dos Santos wrote:

> Hi,
>
> Thanks for the help!
>
> I've tryed to do as you both told me but it still didn't work. the "any"
> function caused NCL to make all values equal (=100) and spliting the “if “
> statement in two cause an "end if error"!

Rosa,

Is "ur" supposed to be an array? Are you trying to set ur equal
to umrl where umrl <= 100, and to 100 otherwise?

If so, the following code:

   if (.not.all(ismissing(umrl)) .and. umrl .gt. 100) then
     ur = 100
   else
     ur = umrl
   end if

can be rewritten as

   ur = where(umrl.gt.100,100,umrl)

"where" is a very useful function:

    http://www.ncl.ucar.edu/Document/Functions/Built-in/where.shtml

You can also do something like this:

   ur = umrl < 100

but this isn't quite right, because you want it to be "<=" and
not just "<", right?

--Mary

> Maybe it can be a problem in my NCL version (5.1.1)?
>
> Rosa
>
> 2009/7/24 Dennis Shea <shea_at_ucar.edu>
>
>> Maybe, the "any" function
>> http://www.ncl.ucar.edu/Document/Functions/Built-in/any.shtml
>>
>> if (.not.all(ismissing(umrl)) .and. any(umrl .gt. 100)) then
>>
>> Also, rather than
>>
>>> ;******************* criar as novas variaveis TS e ur ****************
>>> TS = new(dimsizes(temp),float)
>>> TS!0 = "time"
>>> TS!1 = "lev"
>>> TS!2 = "lat"
>>> TS!3 = "lon"
>>> TS&time = time
>>> TS&lev = lev
>>> TS&lat = lat
>>> TS&lon = lon
>>> TS_at_long_name = "temperatura do ar a superficie"
>>> TS_at_units = "oC"
>>> TS_at_grid_type = "gaussian"
>>> TS = temp - 273.15
>>
>> I would suggest, either of the following for 'cleaner' code
>>
>> TS = temp ; create a copy including all meta data
>> TS = temp - 273.15
>> TS_at_long_name = "temperatura do ar a superficie"
>> TS_at_units = "oC"
>> TS_at_grid_type = "gaussian"
>>
>> or
>> TS = temp - 273.15
>> copy_VarCoords(temp, TS) ; copy coordinate information
>> TS_at_long_name = "temperatura do ar a superficie"
>> TS_at_units = "oC"
>> TS_at_grid_type = "gaussian"
>>
>> The same for 'ur'
>>
>> Rosa Maria Nascimento dos Santos wrote:
>>
>>> Hi,
>>>
>>> I have a problem when trying to use "if then else" statments i my script
>>> it gets the following error mensage: "fatal:Conditional statements (if and
>>> do while) require SCALAR logical values, see all and any functions". I've
>>> tryed to use the functions "all" and "ismissing" to solve it but it didn't
>>> work. What can i do to solve this?! Could anyone help me?!
>>>
>>> Thanks!
>>>
>>> Rosa
>>> P.S.: I'm sending a part of the script bellow!
>>> ====================================================
>>> begin
>>>
>>> file_in = addfile("/home/rosa/CCSM3/dados_paulo/
>>> BRAMS.1997_mes06_dia18_hora18.nc <http://BRAMS.1997_mes06_dia18_hora18.nc
>>>> ","r")
>>>
>>>
>>> att_names = getvaratts(file_in) ; get cdf_file file's global
>>> attributes
>>> psnm = file_in->psnm
>>> uvel = file_in->uvel
>>> vvel = file_in->vvel
>>> zgeo = file_in->zgeo
>>> temp = file_in->temp
>>> umrl = file_in->umrl
>>> time = file_in->time
>>> lev = file_in->lev
>>> lat = file_in->lat
>>> lon = file_in->lon
>>>
>>> ;******************* criar as novas variaveis TS e ur ****************
>>> TS = new(dimsizes(temp),float)
>>> TS!0 = "time"
>>> TS!1 = "lev"
>>> TS!2 = "lat"
>>> TS!3 = "lon"
>>> TS&time = time
>>> TS&lev = lev
>>> TS&lat = lat
>>> TS&lon = lon
>>> TS_at_long_name = "temperatura do ar a superficie"
>>> TS_at_units = "oC"
>>> TS_at_grid_type = "gaussian"
>>> TS = temp - 273.15
>>> ;============================================================
>>> ur = new(dimsizes(umrl),float)
>>> ur!0 = "time"
>>> ur!1 = "lev"
>>> ur!2 = "lat"
>>> ur!3 = "lon"
>>> ur&time = time
>>> ur&lev = lev
>>> ur&lat = lat
>>> ur&lon = lon
>>> ur_at_long_name = "temperatura do ar a superficie"
>>> ur_at_units = "oC"
>>> ur_at_grid_type = "gaussian"
>>>
>>> if (.not.all(ismissing(umrl)) .and. umrl .gt. 100) then
>>> ur = 100
>>> else
>>> ur = umrl
>>> end if
>>>
>>> print(ur)
>>> ==================================================
>>>
>>> --
>>> Rosa Maria Nascimento Santos
>>> Profra. Dra. do Curso de Meteorologia
>>> EST / UEA
>>> Av. Darcy Vargas, 1200 - Parque 10
>>> CEP 690050-020, Manaus – AM – BR
>>> Fone: +55 92 32365573 Ramal 22
>>>
>>>
>>> ------------------------------------------------------------------------
>>>
>>> _______________________________________________
>>> ncl-talk mailing list
>>> List instructions, subscriber options, unsubscribe:
>>> http://mailman.ucar.edu/mailman/listinfo/ncl-talk
>>>
>>
>
>
> --
> Rosa Maria Nascimento Santos
> Profra. Dra. do Curso de Meteorologia
> EST / UEA
> Av. Darcy Vargas, 1200 - Parque 10
> CEP 690050-020, Manaus – AM – BR
> Fone: +55 92 32365573 Ramal 22
>

_______________________________________________
ncl-talk mailing list
List instructions, subscriber options, unsubscribe:
http://mailman.ucar.edu/mailman/listinfo/ncl-talk
Received on Fri Jul 24 2009 - 10:20:02 MDT

This archive was generated by hypermail 2.2.0 : Wed Jul 29 2009 - 09:07:41 MDT