[ncl-talk] writing netCDF file

Mary Haley haley at ucar.edu
Wed Aug 2 15:51:45 MDT 2017


The reason all your metadata is stripped from the file is because you are
writing the variables to the file with:

ncdf->time= (/time/)
ncdf->lev=  (/lev/)
ncdf->data2=(/dataf/)
ncdf->data3=(/pv/)
ncdf->data6=(/hm/)

When you use (/.../) around a variable, this strips off all the metadata.
Remove the (/.../)

ncdf->time  = time
ncdf->lev   = lev
ncdf->data2 = dataf
ncdf->data3 = pv
ncdf->data6 = hm

I should point out, however, that this is what we call the "inefficient"
method of writing data to a NetCDF file. This method is just fine as long
as you don't have a lot of data to write to the file. If you need to write
a lot of variables to a file, or really large variables to a file, then you
may benefit from using the "efficient" method.  Please see:

http://www.ncl.ucar.edu/Applications/o-netcdf.shtml

where it discusses "method 1" and "method 2".

--Mary

On Mon, Jul 31, 2017 at 8:06 PM, Dipti Sharma <sdipti596 at gmail.com> wrote:

> Hi Mary,
>
> Thank you for your response. Sorry I could not respond you back earlier
> but I was away for couple of weeks. I ran the code by implementing your
> suggestion but it still did not solve the issue. I made the changes as you
> suggested as.given below (attached) and created netcdf file has very weird
> metadata structure. ncl11, ncl12, ncl13, ....(attached at the end).
>
> load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl"
> load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_csm.ncl"
>
> ; setfileoption("nc", "FileStructure", "Advanced")
>
> fn = "C:\Users\Downloads\Newfolder\MERRA2_200.inst3_3d_asm_Nv.
> 19950203.nc4"
> fn1 = "C:\Users\Downloads\Newfolder\MERRA2_200.inst3_3d_gas_Nv.
> 19950203.nc4"
>
>  fi = addfile(fn, "r")
> fi1= addfile(fn1, "r")
>
> ; setfileoption("nc", "Format",  "NetCDF4Classic")
>
>  printVarSummary(fi)
> ;print(fi)
>  time = fi->time
>  lev = fi->lev
>  lat = fi->lat
>  lon = fi->lon
>  h = fi->H
>  ;u = fi->U;(:,:,264,108)
> ; v = fi->V;(:,:,264,108)
>  w=fi->OMEGA;(:,:,264,108)
>  p=fi->PL;(:,:,264,108)
> den=fi1->AIRDENS;(:,:,264,108)
> hm=h(:,:,264,108)
> ;uh=u(:,:,264,108)
> ;uv=v(:,:,264,108)
> wv=w(:,:,264,108)
> pv=p(:,:,264,108)
> denv=den(:,:,264,108)
> ;printVarSummary(u)
> printVarSummary(denv)
> printVarSummary(wv)
> printMinMax(wv,True)
> wv!0="time"
> wv!1="lev"
> wv&time=time
> wv&lev=lev
> denv!0="time"
> denv!1="lev"
> denv&time=time
> denv&lev=lev
> n=dimsizes(wv)
> n1=dimsizes(time)
> n2=dimsizes(lev)
> ;wc=new((/n1,n2/),"float")
> ;do i=0,n1-1
> ;wc(i,:)=wv(i,:)/(-9.8*denv(i,:))
> ;end do
> ;wc=wv
> ;wc=(wv(time|:,lev|:)/(-9.8*denv(time|:,lev|:)))
> wc=(wv/(-9.8*denv))
> copy_VarMeta(wv,wc)
> printMinMax(wc,True)
> ;newlat=lat(108)
> ;newlon=lon(264)
> wc!0="time"
> wc!1="lev"
> wc&time=time
> wc&lev=lev
> printVarSummary(wc)
> dataf = wc
> ;dataf = newdatan(lon|:,lev|:,time|:,lat|:)
> printVarSummary(dataf)
> ;print(time)
> ;return
> ;Writeout new netCDF file
> wc&time = time
> wc&lev = lev
> ;dataf!0="time"
> ;dataf!1="lev"
> ;dataf&time=time
> ;dataf&lev=lev
> ;wc&lat = newlat
> ;wc&lon = newlon
> ncdf=addfile("C:/Users/Downloads/Newfolder/MERRA_19950203nwww.nc","c")
> dataf!0="time"
> dataf!1="lev"
> dataf&time=time
> dataf&lev=lev
> ncdf->time= (/time/)
> ncdf->lev=  (/lev/)
> ncdf->data2=(/dataf/)
> ncdf->data3=(/pv/)
> ;ncdf->data4=(/uv/)
> ;ncdf->data5=(/uh/)
> ncdf->data6=(/hm/)
> return
> ;return
> ;printVarSummary(v)
> return
> ;print("t(0,0,0,0) = " + t(0,0,0,0))
> ;print("u(0,1,1,1) = " + u(0,1,1,1))
> ;print("v(0,2,2,2) = " + v(0,2,2,2))
> ;print("t&lat(0) = " + t&lat(0))
> ;print("t at units = " + t at units)
> ;print("u at units = " + u at units)
> ;print("v at units = " + v at units)
> ;u1 = fi->U(::2)
> ;v1 = fi->V(3:121:3)
>  ;;g1 = fi=>/grp1
>  ;;print(g1)
>  ;;printVarSummary(g1)
> ;exit
>  ;;uf = fi->V
>  ;;wks  = gsn_open_wks("x11", "xy")
>
> ;; res              = True
>  ;;res at tiMainString = "Basic plot"
>
>  ;;plot1 = gsn_csm_contour(wks, uf(0,0,:,:), False)
>
>  ;;ug = g1->V
>
>  ;;plot2 = gsn_csm_contour(wks, ug(0,0,:,:), False)
> ;;return
> ;end
>
> The NetCDF file I created has the metadata as below,
>
> netcdf file:/C:/Users/Downloads/Newfolder/MERRA_19950203nwww.nc {
>   dimensions:
>     ncl0 = 8;
>     ncl1 = 72;
>     ncl2 = 8;
>     ncl3 = 72;
>     ncl4 = 8;
>     ncl5 = 72;
>     ncl6 = 8;
>     ncl7 = 72;
>   variables:
>     int time(ncl0=8);
>
>     double lev(ncl1=72);
>
>     float data2(ncl2=8, ncl3=72);
>
>     float data3(ncl4=8, ncl5=72);
>
>     float data6(ncl6=8, ncl7=72);
>
> }
>
> Thanks again.
>
> Dipti
>
>
> On Mon, Jul 17, 2017 at 12:34 AM, Mary Haley <haley at ucar.edu> wrote:
>
>> Dear Dipti,
>>
>> I'm sorry, but I missed that you had a second question imbedded in this
>> email.
>>
>> Were you able to get the metadata added to "wc"?
>>
>> The reason it doesn't have metadata like "var" is because when you do a
>> calculation and assign it to a variable that doesn't exist:
>>
>> wc=(wv/(-9.8*denv))
>>
>> then "wc" will not inherit any metadata from wv or denv except the
>> _FillValue attribute.
>>
>> In order to get metadata, you either need to do a direct variable copy
>> first, so that "wc" gets metadata before doing the calculation:
>>
>> wc=wv   ; Trick to copy metadata from wv to wc (values also get copied)
>> wc=(wc/(-9.8*denv))
>>
>> or you can use copy_VarMeta:
>>
>> wc=(wv/(-9.8*denv))
>> copy_VarMeta(wv,wc)   ; copy wv's metadata to wc
>>
>> In either case, you should update the long_name/standard_name and units
>> attributes of wc to correctly reflect what they are.
>>
>> --Mary
>>
>>
>>
>> On Thu, Jun 29, 2017 at 5:12 PM, Dipti Sharma <sdipti596 at gmail.com>
>> wrote:
>>
>>> Hi Mary,
>>>
>>> Thank you. It worked now. I also made file name little shorter then that
>>> worked.
>>>
>>> ncdf=addfile("/Users/Downloads/New\ folder/MERRA_19950830.nc","c")
>>> but one thing, when I do printVarSummay(var) I get the var info with
>>> 2dimensions
>>>
>>> Number of Dimensions: 2
>>> Dimensions and sizes:   [time | 8] x [lev | 72]
>>> Coordinates:
>>>             time: [0..1260]
>>>             lev: [   1..  72]
>>> Number Of Attributes: 13
>>>   lon : -112.5
>>>   lat :   42
>>>   valid_range : ( -1e+15, 1e+15 )
>>>   vmin :        -1e+15
>>>   vmax :        1e+15
>>>   standard_name :       air_density
>>>   add_offset :   0
>>>   scale_factor :         1
>>>   fmissing_value :      1e+15
>>>   missing_value :       1e+15
>>>   _FillValue :  1e+15
>>>   units :       kg m-3
>>>
>>> But for wc, when I do printVarSummay(wc)
>>> Variable: wc
>>> Type: float
>>> Total Size: 2304 bytes
>>>             576 values
>>> Number of Dimensions: 2
>>> Dimensions and sizes:   [8] x [72]; There is different format so how to
>>> get this like ' [time | 8] x [lev | 72]'?
>>>
>>> Coordinates:
>>> Number Of Attributes: 1
>>>   _FillValue :  1e+15
>>> (0)
>>> (0)     min=-0.0658635   max=0.0909946
>>>
>>> ALSO, my necdf file is not looking with correct attributes,
>>>
>>> netcdf file:/C:/Users/Downloads/New%20folder/MERRA2_19950830.nc {
>>>   dimensions:
>>>     ncl0 = 8; should be time=8;
>>>     ncl1 = 72;lev=72; etc.
>>>     ncl2 = 8;
>>>     ncl3 = 72;
>>>   variables:
>>>     int time(ncl0=8);
>>>
>>>     double lev(ncl1=72);
>>>
>>>     float data2(ncl2=8, ncl3=72);
>>>
>>> }
>>>
>>>
>>> Thank you so much again.
>>>
>>> Dipti
>>>
>>>
>>>
>>> On Thu, Jun 29, 2017 at 11:49 AM, Mary Haley <haley at ucar.edu> wrote:
>>>
>>>> Hi Dipti,
>>>>
>>>> I'm not sure that addfile can recognize Windows type of directory paths
>>>> with "C:\....".
>>>>
>>>> Try using UNIX-style paths:
>>>>
>>>> ncdf=addfile("/Users/Downloads/New\ folder/MERRA2_200.inst3_3d_asm
>>>> _Nv.19950830.nc <http://merra2_200.inst3_3d_asm_nv.19950830.nc/>","c")
>>>>
>>>> Note that I used forward slashes ('/') instead of backward slashes
>>>> ('\').  However, since you have a space in one of your directory
>>>> names----"New folder"---I put a backward slash in front of the space, just
>>>> in case.
>>>>
>>>> --Mary
>>>>
>>>>
>>>> On Wed, Jun 28, 2017 at 5:55 PM, Dipti Sharma <sdipti596 at gmail.com>
>>>> wrote:
>>>>
>>>>> Dear NCL,
>>>>>
>>>>> I am trying to write netCDF file for selected variable (wc, the
>>>>> vertical velocity here in this example) from MERRA-2 wind data. However, I
>>>>> always get an error message saying that
>>>>>  " fatal:Could not create (C:\Users\Downloads\New folder\
>>>>> MERRA2_200.inst3_3d_asm_Nv.19950830.nc)
>>>>>
>>>>> Could someone please help me how to write the output file in netCDF. I
>>>>> want to have wc (time,lev) 2D data from 4D (time, lev,lat,lon). I just want
>>>>> the data at 1 location (1 lat and 1 lon).
>>>>>
>>>>> Thank you very much in advance for your help.
>>>>>
>>>>> Regards,
>>>>>
>>>>> Dipti
>>>>>
>>>>> Attached is the script that I used to read the data.
>>>>>
>>>>>
>>>>> load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl"
>>>>> load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_csm.ncl"
>>>>>
>>>>> ; setfileoption("nc", "FileStructure", "Advanced")
>>>>>
>>>>> fn = "C:\Users\Downloads\New folder\MERRA2_200.inst3_3d_asm
>>>>> _Nv.19950830.nc4"
>>>>> fn1 = "C:\Users\Downloads\New folder\MERRA2_200.inst3_3d_gas
>>>>> _Nv.19950830.nc4"
>>>>>
>>>>>  fi = addfile(fn, "r")
>>>>> fi1= addfile(fn1, "r")
>>>>>
>>>>> ; setfileoption("nc", "Format",  "NetCDF4Classic")
>>>>>
>>>>>  printVarSummary(fi)
>>>>> ;print(fi)
>>>>>
>>>>>  time = fi->time
>>>>>  lev = fi->lev
>>>>>  lat = fi->lat
>>>>>  lon = fi->lon
>>>>>  u = fi->U;(:,:,269,101)
>>>>>  v = fi->V;(:,:,269,101)
>>>>>  w=fi->OMEGA;(:,:,269,101)
>>>>>  p=fi->PL;(:,:,269,101)
>>>>> den=fi1->AIRDENS;(:,:,269,101)
>>>>> uh=u(:,:,269,101)
>>>>> uv=v(:,:,269,101)
>>>>> wv=w(:,:,269,101)
>>>>> pv=PL(:,:,269,101)
>>>>> denv=den(:,:,269,101)
>>>>> ;printVarSummary(u)
>>>>> printVarSummary(denv)
>>>>> printVarSummary(wv)
>>>>> printMinMax(wv,True)
>>>>> ;calculate vertical velocity
>>>>> wc=(wv/(-9.8*denv))
>>>>> printVarSummary(wc)
>>>>> printMinMax(wc,True)
>>>>> wc!0="time"
>>>>> wc!1="lev"
>>>>> ;wc!2="lat"
>>>>> ;wc!3="lon"
>>>>> datad = wc;(time|:,lev|:)
>>>>> printVarSummary(datad)
>>>>>
>>>>> ;Writeout new netCDF file
>>>>>
>>>>> wc&time = time
>>>>> wc&lev = lev
>>>>> ncdf=addfile("C:\Users\Downloads\New folder\MERRA2_200.inst3_3d_asm
>>>>> _Nv.19950830.nc","c")
>>>>> ncdf->time= (/time/)
>>>>> ncdf->lev=  (/lev/)
>>>>> ncdf->data=(/datad/)
>>>>> return
>>>>> end
>>>>>
>>>>>
>>>>>
>>>>> _______________________________________________
>>>>> ncl-talk mailing list
>>>>> ncl-talk at ucar.edu
>>>>> 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/20170802/feaea801/attachment.html 


More information about the ncl-talk mailing list