Re: Converting Time in NetCDF Reanalysis

From: Paul Nutter (pnutter@XXXXXX)
Date: Wed Feb 26 2003 - 11:42:49 MST

  • Next message: pan1: "Are there default functions to calculate Ta, Qa, Qsat etc?"

    Mary,

    Thanks for your help on this. I have it up and running with just one
    trick... The pre-compiled ncl binary you sent me requires "udunits.dat"
    in what appears to be a local NCAR directory tree. I "hardwired" a fix
    by recreating the requested directory tree on my system and linking
    "udunits.dat" from my GrADS directory.

    Output from "print(date_str)" is exactly what I'd hoped for:

    Variable: date_str
    Type: string
    Total Size: 20 bytes
                5 values
    Number of Dimensions: 1
    Dimensions and sizes: [5]
    Coordinates:
    (0) 00Z 01 Jul 2002
    (1) 06Z 01 Jul 2002
    (2) 12Z 01 Jul 2002
    (3) 18Z 01 Jul 2002
    (4) 00Z 02 Jul 2002

    Components of my animation script follow (loop from example 9):

    ;......................................................................
    load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.ncl"
    load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl"

    ; Convert a numerical month to a 3-character abbreviation (Mary Haley,
    NCAR)
    function month_conv(month)
    begin
       month_abbr =
    (/"","Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep", \
                         "Oct","Nov","Dec"/)
    ;
    ; Be sure to append a space at the end.
    ;
       return(month_abbr(floattointeger(month))+" ")
    end

    ;......................................................................
    ;......................................................................

    begin

      cdf_file = addfile("$HOME/NCL_test/hgt.2002.nc","r")

      Zt = cdf_file->hgt(724:728,5,6:30:-1,72:143) ; geopotential height
      lat = cdf_file->lat ; latitude
      lon = cdf_file->lon ; longitude
      time = cdf_file->time ; time

      printVarSummary(Zt) ; original array

    ; Correct for scale factor and offset of NetCDF data
      Z=(Zt*Zt@scale_factor)+Zt@add_offset
      copy_VarAtts(Zt,Z)
      copy_VarCoords(Zt,Z)
      Z@add_offset = 0
      Z@scale_factor = 1
      printVarSummary(Z)

      ntime = dimsizes(Z&time)

      delete(Zt)
      delete(lat)
      delete(lon)
      delete(time)

      print(Z@long_name) ; Print long name of Z
      print(Z@units) ; Print units of Z.
      print(Z&lat) ; Print latitudes
      print(Z&lon) ; print longitudes
      print(Z&time) ; print time

    ; Convert the "hours since" values to year, month, day, etc. values.
    ; Function returns a 6xN array date_info, where
    ; date_info(0,:) --> year
    ; date_info(1,:) --> month
    ; date_info(2,:) --> day
    ; date_info(3,:) --> hour
    ; date_info(4,:) --> minute
    ; date_info(5,:) --> second
      date_info = ut_calendar(Z&time, Z&time@units, 0) ; Mary Haley, NCAR

      date_str = sprinti("%0.2iZ ",floattointeger(date_info(3,:))) + \
                 sprinti("%0.2i ", floattointeger(date_info(2,:))) + \
                 month_conv(date_info(1,:)) + \
                 sprinti("%0.4i", floattointeger(date_info(0,:)))

      print(date_str)

    ; Create a new two-dimensional array containing field to contour.
      Z2D = new( (/dimsizes(Z&lat),dimsizes(Z&lon)/), typeof(Z) )
      printVarSummary(Z2D)
    ;......................................................................

      xwks = gsn_open_wks("x11","NCL_test") ; Open an X11 workstation.

      resources = True ; Indicate you want to set some
      resources@cnInfoLabelOn = True ; Turn on informational label.

    ; --- set other resources here ---

      Z2D(:,:) = Z(0,:,:)
      resources@cnInfoLabelString = date_str(0)
      plot = gsn_contour_map(xwks,Z2D,resources) ; Draw first contour
    plot.

      do t = 1,4

        setvalues plot@contour
          "cnInfoLabelString" : date_str(t)
        end setvalues

        Z2D = Z(t,:,:)
        setvalues plot@data ; Change the data for the contour plot.
          "sfDataArray" : Z2D(:,:)
        end setvalues
     
        draw(plot) ; Draw the contour plot.
        frame(xwks) ; Advance the frame.
      end do

      delete(plot) ; Clean up.
      delete(Z)
      delete(resources)

    end

    Mary Haley wrote:
    >
    > >
    > > Hi,
    > >
    > > I'm struggling to convert time obtained from NetCDF Reanalysis data.
    > > Output from the time variable looks like this:
    > >
    > > Variable: time (coordinate)
    > > Type: double
    > > Total Size: 40 bytes
    > > 5 values
    > > Number of Dimensions: 1
    > > Dimensions and sizes: [time | 5]
    > > Coordinates:
    > > Number Of Attributes: 4
    > > units : hours since 1-1-1 00:00:0.0
    > > long_name : Time
    > > actual_range : <ARRAY>
    > > delta_t : 0000-00-00 06:00:00
    > > (0) 17544792
    > > (1) 17544798
    > > (2) 17544804
    > > (3) 17544810
    > > (4) 17544816
    > >
    > > How should I make this conversion so that I can plot correct time stamps
    > > in an animation of images?
    > >
    > > Thanks,
    > > Paul Nutter
    >
    > Hi Paul,
    >
    > We've added a "udunits" type function to NCL that takes as input a
    > unit of time of the form like you mentioned above: "hours since 1-1-1
    > 00:00:0.0", along with the actual double precision values that
    > represent the "hours since", and it returns a 6 x n array (6 x 5 in
    > your case) that contains the years, months, days, hours, minutes, and
    > seconds.
    >
    > We haven't customized this function much beyond returning this 6 x n
    > array, although eventually we might add the capability to have this
    > info returned in a nice string format, like:
    >
    > "2001080100"
    > "2001080106"
    > "2001080112"
    >
    > where the last two digits represent the hour. This type of string
    > could then be used as a time stamp, like you mentioned.
    >
    > If you are interested in testing this function as it is, let me know
    > what kind of system you have, and I'll provide an NCL binary for you
    > to try. Also, let me know what kind of time stamp you are interested
    > in, and I'll write an NCL script that shows how to do this.
    >
    > --Mary



    This archive was generated by hypermail 2b29 : Wed Feb 26 2003 - 11:45:53 MST