[ncl-talk] total number of times in files opened with addfiles

Zilore Mumba zmumba at gmail.com
Sat Apr 2 14:24:00 MDT 2022


Thanks Dave for the assistance. I have made some progress. While I know
that I need time to learn ncl subscripting, perhaps I could be helped to
make the script work. This script is an adaptation of one of the provided
wrf-ncl scripts. The script plots several variables at different levels. I
have reproduced below, parts of the script which plot temperature. I have
marked in
1. yellow, some guidance I got from Dave
2. blue some changes I made to getting the times
3. purple, getting the variables, with files_all (I assume it is equivalent
to files_all[:] and gets variables from all files)
4. red I have to specify from which file to plot. This is where I have a
problem, how to index files_all.
I hope I can be assisted.

  files_all = addfiles(FILES+".nc","r")

; get time information
  times_in_file = files_all[:]->Times

* dims = dimsizes (times)  ntimes = dimsizes (times(:,0))*

; strip out the day and hour
  times_to_use = new(dims(0),string)

  do i=0,dims(0)-1
    times_to_use(i) = chartostring(times_in_file(i,8:12))
  end do
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  do it = 0,ntimes(0)-1,2             ; TIME LOOP

    print("Working on time: " + times(it,0) )
    res at TimeLabel = times(it,0)   ; Set Valid time to use on plots
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; First get the variables we will need
    tc = wrf_user_getvar(*files_all**,*"tc",it)        ; T in C
    u  = wrf_user_getvar(files_all,"ua",it)        ; u averaged to mass
points
    v  = wrf_user_getvar(files_all,"va",it)        ; v averaged to mass
points
    p  = wrf_user_getvar(files_all, "pressure",it) ; pressure is our
vertical coordinate
    z  = wrf_user_getvar(files_all, "z",it)        ; grid point height
    rh = wrf_user_getvar(files_all,"rh",it)        ; relative humidity
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    do level = 0,nlevels-1                 ; LOOP OVER LEVELS

      pressure = pressure_levels(level)
      tc_plane = wrf_user_intrp3d(tc,p,"h",pressure,0.,False)
      z_plane  = wrf_user_intrp3d( z,p,"h",pressure,0.,False)
      rh_plane = wrf_user_intrp3d(rh,p,"h",pressure,0.,False)
      u_plane  = wrf_user_intrp3d( u,p,"h",pressure,0.,False)
      v_plane  = wrf_user_intrp3d( v,p,"h",pressure,0.,False)

      spd     = (u_plane*u_plane + v_plane*v_plane)^(0.5) ; m/sec
      spd at description = "Wind Speed"
      spd at units = "m/s"
      u_plane = u_plane*1.94386     ; kts
      v_plane = v_plane*1.94386     ; kts
      u_plane at units = "kts"
      v_plane at units = "kts"

      wks = gsn_open_wks(type,"Plots00-" + "P" + pressure_levels(level) +
"_" + times_to_use(it) + "Z")

      ; Plotting options for T
        opts = res
        opts at cnLineColor = "Red"
        opts at ContourParameters = (/ 5.0 /)
        opts at cnInfoLabelOrthogonalPosF = 0.07  ; offset second label
information
        opts at gsnContourLineThicknessesScale = 2.0
        contour_tc = wrf_contour(*files_all*,wks,tc_plane,opts)
        delete(opts)

    end do      ; END OF LEVEL LOOP
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  end do        ; END OF TIME LOOP

<https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail>
Virus-free.
www.avast.com
<https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail>
<#DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2>

On Sat, Apr 2, 2022 at 5:06 PM Dave Allured - NOAA Affiliate <
dave.allured at noaa.gov> wrote:

> Here are two different ways to get the size of the time dimension.  The
> second method uses dimension reduction to find the size of only one slice
> of the 2-D character array.
>
> dims = dimsizes (times)
> ntimes = dims(0)
>
> ntimes = dimsizes (times(:,0))
>
>
> On Fri, Apr 1, 2022 at 11:04 PM Zilore Mumba <zmumba at gmail.com> wrote:
>
>> I have 13 files, i.e. analysis plus 12 hourly files of forecasts.  print
>> (files_all[0]->Times) and printVarSummary(times) give the following output:
>> Variable: Times (file variable)
>> Type: character
>> Total Size: 19 bytes
>>             19 values
>> Number of Dimensions: 2
>> Dimensions and sizes: [Time | 1] x [DateStrLen | 19]
>> Coordinates:
>> (0,0) 2
>> (0,1) 0
>> (0,2) 2
>> (0,3) 2
>> (0,4) -
>> (0,5) 0
>> (0,6) 3
>> (0,7) -
>> (0,8) 2
>> (0,9) 8
>> (0,10) _
>> (0,11) 0
>> (0,12) 0
>> (0,13) :
>> (0,14) 0
>> (0,15) 0
>> (0,16) :
>> (0,17) 0
>> (0,18) 0
>>
>> Variable: times
>> Type: character
>> Total Size: 247 bytes
>>             247 values
>> Number of Dimensions: 2
>> Dimensions and sizes: [Time | 13] x [DateStrLen | 19]
>> Coordinates:
>>
>>
>> On Fri, Apr 1, 2022 at 11:53 PM Dave Allured - NOAA Affiliate <
>> dave.allured at noaa.gov> wrote:
>>
>>> Please show print (files_all[0]->Times) and printVarSummary(times).
>>>
>>>
>>> On Fri, Apr 1, 2022 at 3:42 PM Zilore Mumba via ncl-talk <
>>> ncl-talk at mailman.ucar.edu> wrote:
>>>
>>>> I have a number of WRF output files, one file per hour, which I open
>>>> with
>>>> files_all = addfiles(FILES+".nc","r")
>>>>
>>>> I then get the times and the total number with.
>>>>   times = files_all[:]->Times      ; get all times in the file
>>>>   ntimes = dimsizes(times)         ; number of times in the file
>>>>
>>>> I think I am missing something here because  with print(ntimes) I see
>>>> two numbers.
>>>> I want to use ntimes in a loop: do it = 0,ntimes-1,2
>>>>
>>>> This gives me the error: fatal:Loop end must be scalar, can't execute
>>>> loop.
>>>> How do I get the times from files opened with addfiles. I will
>>>> appreciate any assistance.
>>>>
>>>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mailman.ucar.edu/pipermail/ncl-talk/attachments/20220402/ddb68672/attachment.html>


More information about the ncl-talk mailing list