[ncl-talk] Using a loop to find values greater than 0 (calculate PDD)
Kyle Griffin
ksgriffin2 at wisc.edu
Mon Aug 4 10:36:03 MDT 2014
This also sounds like an ideal spot for implementing the 'where' function.
https://www.ncl.ucar.edu/Document/Functions/Built-in/where.shtml
pos_dd = where(tasC.ge.0,tasC,tasC at _FillValue)
total_pos_dd = dim_sum_n_Wrap(pos_dd,0)
This will assign the missing value of tasC to any location in the array
that is greater than or equal to zero and have each point less than zero
assigned to the missing value (usually in the _FillValue attribute. If
tasC at _FillValue
is not set, you can set it to anything). You can then add up the values
over the 0th dimension (time) to get a total at each lat/lon point.
Hope that helps,
Kyle
----------------------------------------
Kyle S. Griffin
Department of Atmospheric and Oceanic Sciences
University of Wisconsin - Madison
Room 1421
1225 W Dayton St, Madison, WI 53706
Email: ksgriffin2 at wisc.edu
On Mon, Aug 4, 2014 at 11:11 AM, Dennis Shea <shea at ucar.edu> wrote:
> The variable, tasC, is an *array *
>
> tasC = tasL - 273.15 ; Convert K to C
> printVarSummary(tasC)
>
> You can not use an array as a loop variable ... in any language.
>
> do tasC = 0,364
>
> could be, say,
>
> do day = 0,364
>
> See:
>
> https://www.ncl.ucar.edu/Document/Manuals/Ref_Manual/NclStatements.shtml#Loops
>
> Specifically:
>
> do *loop_identifier* = *scalar_start_expr* ,
>
> *end_expr*
>
> *T*he "scalar_start_expr , end_expr" are scalar.
>
>
>
> ===
>
> a = addfile("tas_day_CCSM4_lgm_
> r2i1p1_18700101-19001231.nc","r")
> tasL = a->tas(0:364,:,:)
>
> TIME = cd_calendar(tasL&time,0) ; TIME(ntim,6)
> print(TIME)
>
> tasC = tasL - 273.15 ; Convert K to C
> tasC = where(tasC.gt.0.0, tasc, 0.0)
> pdd = dim_sum_n_Wrap(tasC, 0) ; (lat,lon)
> pdd at long_name = "degree days for year="+toint(TIME(0,0))
> pdd at units = "degC"
>
>
>
>
> On Mon, Aug 4, 2014 at 9:16 AM, Lauren Jean Vargo <lvargo at unm.edu> wrote:
>
>> Hello,
>>
>> I’m having trouble doing a calculation with NCL, ultimately I am trying
>> to calculate the Positive degree-day (PDD) sum. I’ve read in daily
>> temperature data (which is 3D [time,lat,lon]). What I am trying to do is
>> calculate the sum of the temperatures that are greater than 0C for 1 year.
>>
>> The way I was trying to do this was with a loop, and to first set any
>> temperature less than or equal to zero, just to zero. Next I want to sum
>> all the temperatures over 365 days at each lat & lon point. If there is an
>> easier way to do this using NCL functions, that would be great.
>>
>> The error message that I am getting is that the “loop must be scalar".
>> However, I’m not sure how to specify that I want the temperature value to
>> be analyzed when tasC is (time,lat,lon).
>>
>> I’ve uploaded the file "tas_day_CCSM4_lgm_r2i1p1_18700101-19001231.nc” to
>> the ftp account.
>>
>> I’m running ncl version 6.1.2, and the system is Darwin Kernel Version
>> 13.3.0
>>
>>
>> Here is the script:
>>
>> load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl"
>> load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_csm.ncl"
>> load “$NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.ncl"
>> load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/shea_util.ncl"
>>
>> begin
>>
>> ; Read in near surface air temperature (Ta)
>>
>> a =
>> addfile("tas_day_CCSM4_lgm_r2i1p1_18700101-19001231.nc","r")
>> tasL = a->tas(0:364,:,:)
>> tasC = tasL - 273.15 ; Convert K to C
>> print (tasC)
>> printVarSummary (tasC)
>>
>> do tasC = 0,364
>> if (tasC .le. 0)
>> H = 0
>> end if
>> end do
>>
>> PDD_sum = dim_cumsum_n_Wrap(tasC)
>>
>> end
>>
>>
>> Any help would be greatly appreciated.
>>
>> Thanks,
>>
>> Lauren Vargo
>> M.S. Candidate
>> The University of New Mexico
>> _______________________________________________
>> ncl-talk mailing list
>> List instructions, subscriber options, unsubscribe:
>> http://mailman.ucar.edu/mailman/listinfo/ncl-talk
>>
>
>
> _______________________________________________
> ncl-talk mailing list
> List instructions, subscriber options, unsubscribe:
> http://mailman.ucar.edu/mailman/listinfo/ncl-talk
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mailman.ucar.edu/pipermail/ncl-talk/attachments/20140804/17872935/attachment.html
More information about the ncl-talk
mailing list