[ncl-talk] Time dimension in output to NetCDF

Dave Allured - NOAA Affiliate dave.allured at noaa.gov
Thu Nov 30 12:19:21 MST 2017


1.  Grads wants CF time coordinates, like "days since ..." as Dennis said.

2.  Also I agree that NCO operators would be the quickest solution to make
this subset file, if you do not need some extra calculation within NCL.
However there is a typo in Dennis's command, an extra dash near the
beginning.  I think this is correct:

ncks -d lat,12.0,33.0 -d lon,30.0,60.0 -O precip.mon.total.v7.nc prc_3mon.nc

3.  The source file name tells me that this is probably GPCC precip,
downloaded from NOAA/ESRL/PSD.  If so, then you already have Grads
compatible time coordinates in the source file.  Therefore the easiest path
is to copy the existing time coordinates, not try to generate new ones.

This is easy if you skip the type conversion from float to double with
"flt2dble".  Here is a trimmed example.  If you read prc in directly like
this, it will include an attached time coordinate that is already Grads and
CF compatible:

    fili   = "precip.mon.total.v7.nc"
    f      = addfile(fili, "r")
    prc    = f->precip(:,{12:33},{30:60})
    printVarSummary (prc)

    ncdf = addfile("prc_3mon.nc", "c")

; Make time an UNLIMITED dimension; recommended for most applications.
; This step is OPTIONAL if you do not care about unlimited.

    filedimdef(ncdf,"time",-1,True)

; Output variable directly.  Coordinates and attributes will be included.

    ncdf->prc = prc                          ; 3D

--Dave


On Thu, Nov 30, 2017 at 7:38 AM, Dennis Shea <shea at ucar.edu> wrote:

> I don't know GrADS and how it handles 'time'
>
> ===
> My speculation is that
>       prc&time = (/ yyyymm /)
> is the source of the problem.
>  Likely, GrADS expects 'time' to have units like
> 'seconds/minutes/hours/days since ...."
>
> I would eliminate the above line .....ALso, not sure why you are
> 'promoting' the float variable to a double.
>
> GrADS questions should be sent to the GrADS support group.
> =======================
>
>   diri   = "./"
>   fili   = "precip.mon.total.v7.nc"
>   f      = addfile(diri+fili, "r")
>
>   prc    = flt2dble(f->precip(:,{12:33},{30:60}))
>   pmsg   = prc at _FillValue      ; convenience
>
> ;===============================================================
> ;write to nc file
> ;================================================================
>
> prc    = flt2dble(f->precip(:,{12:33},{30:60}))   ; <<< ??why are you
> promoting to double?
>
> ====
>
> Actually, the netCDF Operators could be used
>
> %> ncks --d lat,12.0,33.0 -d lon,30.0,60.0 -O precip.mon.total.v7.nc
> <http://precip.mon.total.v7.nc>prc_3mon.nc
>
> On Thu, Nov 30, 2017 at 2:17 AM, Dr. Muhammad Afzaal <
> afzaalkarori at gmail.com> wrote:
>
>> in continuation to my previous email, following is the defined variable
>> summary
>>
>> Variable: prcp
>> Type: double
>> Total Size: 27336960 bytes
>>             3417120 values
>> Number of Dimensions: 3
>> Dimensions and sizes:   [time | 1356] x [lat | 42] x [lon | 60]
>> Coordinates:
>>             time: [190101..201312]
>>             lat: [12.25..32.75]
>>             lon: [30.25..59.75]
>> Number Of Attributes: 3
>>   units :       (mm)
>>   long_name :
>>   _FillValue :  -9.969209968386869e+36
>>
>> Please read the second last line of my code as ncdf->prcp = prcp
>>
>> Thanks
>>
>> *Dr. MUHAMMAD AFZAAL KARORI*
>> Pakistan Meteorological Department
>> Sector H-8/2, Pitras Bukhari Road,
>> Islamabad - 44000
>> Pakistan
>>
>>
>> On Thu, Nov 30, 2017 at 12:06 PM, Dr. Muhammad Afzaal <
>> afzaalkarori at gmail.com> wrote:
>>
>>> Dear NCL users
>>>
>>> I am using NCL 6.4.0 on ubuntu. I need to write a variable to netcdf
>>> file. I did it successfully using Method 1 given on ncl homepage. I have
>>> little problem that when I open nc file in grads, the time is not
>>> realistic. I mean its like;
>>>
>>> Time values set: 2320:6:25:0 2320:6:25:0
>>>
>>> while the variable has the time from 190101 to 201312
>>>
>>> Can someone please help me how can add time dim as that attached with
>>> the define variable? My code is given below
>>>
>>> ;====================================================
>>> begin
>>>
>>>   diri   = "./"
>>>   fili   = "precip.mon.total.v7.nc"
>>>   f      = addfile(diri+fili, "r")
>>>
>>>   prc    = flt2dble(f->precip(:,{12:33},{30:60}))
>>>   pmsg   = prc at _FillValue      ; convenience
>>>
>>> ;*********************************
>>> ; plot parameters
>>> ;*********************************
>>>   dimprc = dimsizes(prc)
>>>   ntim   = dimprc(0)
>>>   nlat   = dimprc(1)
>>>   mlon   = dimprc(2)
>>>
>>>   tim  = f->time
>>>   utc_date = cd_calendar(tim, 0)
>>>   year    = tointeger(utc_date(:,0))
>>>   yrStrt  = year(0)
>>>   yrLast  = year(ntim-1)
>>>   nyear   = yrLast-yrStrt+1
>>> ;print(year + " " + yrStrt + " "+ yrLast +" "+ nyear)
>>>   yyyymm  = yyyymm_time(yrStrt, yrLast, "integer")
>>>
>>>   yrfrac  = (/ yyyymm_to_yyyyfrac(yyyymm, 0.0) /)
>>>   prc&time = (/ yyyymm /)
>>>
>>> ;===============================================================
>>> ;write to nc file
>>> ;================================================================
>>>
>>>     system("/bin/rm -f prc_3mon.nc") ;remove any pre-existing file
>>>     ncdf = addfile("prc_3mon.nc" ,"c") ; open output netCDF file
>>>
>>> ;===============================================================
>>> ; create global attributes of the file (optional)
>>> ;================================================================
>>>        fAtt               = True        ; assign file attributes
>>>        fAtt at title         = "GPCC Rainfall"
>>>        fAtt at source_file   = "precip.mon.total.v7.nc"
>>>        fAtt at Conventions   = "None"
>>>        fAtt at creation_date = systemfunc ("date")
>>>        fileattdef( ncdf, fAtt )           ; copy file attributes
>>>
>>> ;===============================================================
>>> ; make time an UNLIMITED dimension;recommended for most applications
>>> ;===============================================================
>>>
>>>        filedimdef(ncdf,"time",-1,True)
>>>
>>> ;===============================================================
>>> ; output variables directly; NCL will call appropriate functions
>>> ; to write the meta data associated with each variable
>>> ;===============================================================
>>>        ncdf->spi  = spi                          ; 3D
>>> end
>>>
>>>
>>> Regards
>>> *Dr. MUHAMMAD AFZAAL KARORI*
>>> Pakistan Meteorological Department
>>> Sector H-8/2, Pitras Bukhari Road,
>>> Islamabad - 44000
>>> Pakistan
>>>
>>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.ucar.edu/pipermail/ncl-talk/attachments/20171130/7adce4e5/attachment.html>


More information about the ncl-talk mailing list