[ncl-talk] write variable attribute

Dennis Shea shea at ucar.edu
Wed Sep 2 09:19:39 MDT 2015


if 'temp2' has lat, lon coordinate variables

    printVarSummary(temp2)
    delete( [/ temp2&lat, temp2&lon /] )    ; 6.3.0

    delete(temp2&lat)    ; previous versions of NCL
    delete(temp2&lon)

    printVarSummary(temp2)

    ncdf->temp2 = temp2       ;***** NOT (/temp2/)

If you are writing TFLAG, you must do the same for that variable


On Fri, Aug 28, 2015 at 4:51 PM, Luo, Chao <chao.luo at eas.gatech.edu> wrote:
> Sorry, I made the mistake. It works. This write out metadata, including lat/lon. Is it possible to just write out variable value and attribute, not lat/lon data?
>
> Thanks much,
>
> Chao
>
> ----- Original Message -----
> From: "Dennis Shea" <shea at ucar.edu>
> To: "Chao Luo" <chao.luo at eas.gatech.edu>
> Cc: "Alan Brammer" <abrammer at albany.edu>, "ncl-talk Talk" <ncl-talk at ucar.edu>
> Sent: Friday, August 28, 2015 1:41:32 PM
> Subject: Re: [ncl-talk] write variable attribute
>
> Alan's script should work.
>
> system("/bin/rm -f test.nc")
> ncout = addfile("test.nc","c")
> x = 0
> x!0 = "test"
> x&test = 23
> x at Some_att = "First Att"
> x at Some_other_att = "Second Att"
> x at date = systemfunc("date")
> ncout->x = x
> quit
>
> If the above does not work ... something is wrong
>
>     diro    = "/data11/cluo/cmaq4.6/data/post/smk31_post/"
>     filo    = "out.nc"    ; input file name
>     fl_nm_out = diro+filo
>     system ("/bin/rm -f "+fl_nm_out) ; Remove any pre-existing file
>     ncdf    = addfile(fl_nm_out,"c")
>
> [snip]
>
>     temp2 = ESMF_regrid(temp2_36,Opt)      ; Do the regridding
>     printVarSummary(temp2)   ; this has attributes and possiblt time/level
>                                                ; coordinate information
>
>                                     ; not sure why you are doing this
>     temp2!0 = "time"
>     temp2!1 = "lay"
>     temp2!2  = "lat"
>     temp2!3  = "lon"
>     temp2 at long_name  = "TEMP2"
>     temp2 at units  = "k"
>     temp2 at var_desc = "temperature at 2 m"
>
>     printVarSummary (temp2)
>     printMinMax(temp2,True)
>
>     ncdf->TFLAG =TFLAG
>     ncdf->temp2 = temp2       ;***** NOT (/temp2/)
>
>                                             ; the (/.../) strip meta data
>
> On Fri, Aug 28, 2015 at 2:21 PM, Luo, Chao <chao.luo at eas.gatech.edu> wrote:
>> Thanks very much for your help and time. I still can't write out variable
>> attribute even I followed your sample, and I don't what caused the problem.
>> Appreciate any suggestions.
>>
>> Chao
>>
>> ________________________________
>> From: "Alan Brammer" <abrammer at albany.edu>
>> To: "Chao Luo" <chao.luo at eas.gatech.edu>
>> Cc: "ncl-talk Talk" <ncl-talk at ucar.edu>
>> Sent: Friday, August 28, 2015 12:39:28 PM
>>
>> Subject: Re: [ncl-talk] write variable attribute
>>
>>  The below example is how it should work.   A ncl_filedump of your output
>> netcdf might be useful for people to see what is missing compared to the
>> data in the script.
>> Not sure I follow what the problem is.  If the metadata is written I don't
>> see how the attributes weren't also.
>>
>>
>> $   ncl
>>
>>
>>  ncout = addfile("test.nc","c")
>>   x = 0
>>  x!0 = "test"
>>  x&test = 23
>>  x at Some_att = "First Att"
>>  x at Some_other_att = "Second Att"
>>  x at date = systemfunc("date")
>>  ncout->x = x
>>  exit
>>
>> $  ncl_filedump test.nc
>>  Copyright (C) 1995-2015 - All Rights Reserved
>>  University Corporation for Atmospheric Research
>>  NCAR Command Language Version 6.3.0
>>  The use of this software is governed by a License Agreement.
>>  See http://www.ncl.ucar.edu/ for more details.
>>
>> Variable: f
>> Type: file
>> filename: test
>> path: test.nc
>>    file global attributes:
>>    dimensions:
>>       test = 1
>>    variables:
>>       integer x ( test )
>>          date : Fri Aug 28 19:32:35 UTC 2015
>>          Some_other_att : Second Att
>>          Some_att : First Att
>>
>>       integer test ( test )
>>
>>
>> On Fri, Aug 28, 2015 at 3:13 PM, Luo, Chao <chao.luo at eas.gatech.edu> wrote:
>>>
>>> Yes, it write out metadata of TEMP2, but still no TEMP2 attribute write
>>> out when I use ncdf->tem2 = temp2. What I want to write out is TEMP2 values
>>> and attribute. Thanks!
>>>
>>> Chao
>>>
>>> ________________________________
>>> From: "Alan Brammer" <abrammer at albany.edu>
>>> To: "Chao Luo" <chao.luo at eas.gatech.edu>
>>> Cc: "ncl-talk Talk" <ncl-talk at ucar.edu>
>>> Sent: Friday, August 28, 2015 11:06:55 AM
>>> Subject: Re: [ncl-talk] write variable attribute
>>>
>>>
>>> You've commented out the line that explicitly writes the attributes to the
>>> file.  Likely because you haven't explicitly defined the variable in the
>>> file.
>>> But by also surrounding temp2 with (/ /) you're telling ncl to not copy
>>> any metadata.
>>> That notation is only useful when you have already defined the variable in
>>> the file.
>>> So
>>>     ncdf->temp2   =  temp2
>>> Will write out the contents and the metadata/attributes with the variable.
>>>
>>>
>>>
>>> ;   filevarattdef(ncdf,"temp2",temp2)
>>>
>>>     ncdf->TFLAG =TFLAG
>>>     ncdf->temp2   =  (/temp2/)
>>>
>>> On Fri, Aug 28, 2015 at 1:33 PM, Luo, Chao <chao.luo at eas.gatech.edu>
>>> wrote:
>>>>
>>>> Dear All,
>>>>
>>>> I am working on temperature interpolation from 36km to 4km by
>>>> ESMF_regrid, and want to write out the TEMP2 at new resolution,
>>>> unfortunately the temp2 attribute didn't write out in netcdf file, I have
>>>> tried to define TEMP2 attribute  by:
>>>>
>>>>     temp2!0 = "time"
>>>>     temp2!1 = "lay"
>>>>     temp2!2  = "lat"
>>>>     temp2!3  = "lon"
>>>>     temp2 at long_name  = "TEMP2"
>>>>     temp2 at units  = "k"
>>>>     temp2 at var_desc = "temperature at 2 m"
>>>>
>>>> and by function of filevarattdef. Both didn't work.
>>>>
>>>> Thanks very much for and help or suggestions!
>>>>
>>>> Here is the script I used:
>>>>
>>>>
>>>> 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/wrf/WRFUserARW.ncl"
>>>> load "$NCARG_ROOT/lib/ncarg/nclscripts/esmf/ESMF_regridding.ncl"
>>>>
>>>> begin
>>>>
>>>>  diri_36  = "/data11/cluo/CMAQv5.0.1/scripts/mcip4.1/36km/201107/data/"
>>>>  diri_4  = "/data12/cluo/smoke3.6.5/data/ge_dat/"
>>>>  namei_36 = "GRIDCRO2D_148x112_20110701"
>>>>  namei_4 = "geo_em.d01_1332x1008.nc"
>>>>
>>>>  f1 = addfile(diri_36+namei_36,"r")
>>>>
>>>> ;output lat/lon
>>>>   lat2d_36km     = f1->LAT(0,0,:,:)       ; (TSTEP, LAY, ROW, COL) , 36km
>>>>   lon2d_36km     = f1->LON(0,0,:,:)       ; (TSTEP, LAY, ROW, COL) , 36km
>>>>
>>>>   printVarSummary (lon2d_36km)
>>>>   printMinMax(lon2d_36km,True)
>>>>
>>>>
>>>>  f2 = addfile(diri_4+namei_4,"r")
>>>> ;input lat/lon
>>>>
>>>>   lat2d_4km = f2->XLAT_M(0,:,:)       ; 4km
>>>>   lon2d_4km = f2->XLONG_M(0,:,:)      ; 4km
>>>>
>>>>   printVarSummary (lon2d_4km)
>>>>   printMinMax(lon2d_4km,True)
>>>>
>>>>   name_in = "METCRO2D_148x112_20110701"
>>>>
>>>>   nlay = 1
>>>>   ntime = 25
>>>>
>>>>   time = new((/ntime/),integer)
>>>>   time = ispan(0,ntime-1,1)
>>>>
>>>>   f3 = addfile(diri_36+name_in,"r") temp2_36 = f3->TEMP2(:,:,:,:)
>>>> ;(TSTEP, LAY, ROW, COL)
>>>>  TFLAG  = f3->TFLAG(:,0,:)
>>>>  temp2_36 at _FillValue = 9.999e+36
>>>>
>>>>  printVarSummary (temp2_36)
>>>>  printMinMax(temp2_36,True)
>>>>
>>>>    diro    = "/data11/cluo/cmaq4.6/data/post/smk31_post/"
>>>>     filo    = "out.nc"    ; input file name
>>>>     fl_nm_out = diro+filo
>>>>     system ("/bin/rm -f "+fl_nm_out) ; Remove any pre-existing file
>>>>     ncdf      = addfile(fl_nm_out,"c")
>>>>
>>>> ;---Set up options for regridding
>>>>     Opt                = True
>>>>
>>>>     Opt at SrcGridLat     = lat2d_36km     ; source grid
>>>>     Opt at SrcGridLon     = lon2d_36km
>>>>     Opt at DstGridLat     = lat2d_4km     ; destination grid
>>>>     Opt at DstGridLon     = lon2d_4km
>>>>
>>>>     Opt at SrcRegional    = True          ; Necessary if grids
>>>>     Opt at DstRegional    = True          ; are regional
>>>>
>>>>     Opt at InterpMethod   = "bilinear"    ; "patch", "conserve"
>>>>
>>>>     Opt at ForceOverwrite = True          ; Optional, but recommended.
>>>>     Opt at PrintTimings   = True          ; Optional.
>>>>     Opt at Debug          = True          ; Optional
>>>>
>>>>     temp2 = ESMF_regrid(temp2_36,Opt)      ; Do the regridding
>>>>
>>>>     temp2!0 = "time"
>>>>     temp2!1 = "lay"
>>>>     temp2!2  = "lat"
>>>>     temp2!3  = "lon"
>>>>     temp2 at long_name  = "TEMP2"
>>>>     temp2 at units  = "k"
>>>>     temp2 at var_desc = "temperature at 2 m"
>>>>
>>>>     printVarSummary (temp2)
>>>>     printMinMax(temp2,True)
>>>>
>>>> ;   filevarattdef(ncdf,"temp2",temp2)
>>>>
>>>>     ncdf->TFLAG =TFLAG
>>>>     ncdf->temp2   =  (/temp2/)
>>>>
>>>>   end
>>>>
>>>>
>>>>
>>>>
>>>>
>>>>
>>>>
>>>>
>>>>
>>>> _______________________________________________
>>>> ncl-talk mailing list
>>>> ncl-talk at ucar.edu
>>>> List instructions, subscriber options, unsubscribe:
>>>> http://mailman.ucar.edu/mailman/listinfo/ncl-talk
>>>>
>>>
>>>
>>
>>
>>
>> _______________________________________________
>> ncl-talk mailing list
>> ncl-talk at ucar.edu
>> List instructions, subscriber options, unsubscribe:
>> http://mailman.ucar.edu/mailman/listinfo/ncl-talk
>>


More information about the ncl-talk mailing list