Re: Replacing variables in netcdf file

From: Mary Haley <haley_at_nyahnyahspammersnyahnyah>
Date: Mon Mar 07 2011 - 15:18:37 MST

Siraj,

I believe you should just be able to overwrite your data directly, as long as its the same type and size as what's already on the file.

BE CAREFUL DOING THIS. You will, of course, overwrite your original data and will not be able to get it back. You also need to be careful with attributes, coordinate, arrays, etc.

You want to open the file with "w" as the second argument to "addfile". This will allow you to read and write the file.

To verify this, try the below script to see how "data" gets changed. Note how the attributes and coordinates stay the same, and I've added a new attribute.
begin
;---Create some dummy data
  nlat = 500
  nlon = 500
  dims = (/nlat,nlon/)
  data_orig = random_uniform(0,1000,dims)

;---Create coordinate arrays
  lat = fspan(10,90,nlat)
  lon = fspan(0,100,nlon)
  lat@units = "degrees_north"
  lat@units = "degrees_east"

;---Attach to data_orig.
  data_orig!0 = "lat"
  data_orig!1 = "lon"
  data_orig&lat = lat
  data_orig&lon = lon

;---Add dummy attributes
  data_orig@date_written = systemfunc("date")
  data_orig@units = "none"
  data_orig@long_name = "dummy_data"

  print("==================================================================")
  printVarSummary(data_orig)
  print("min/max data_orig = " + min(data_orig) + "/" + max(data_orig))
  print("(Should be between 0 and 1000.)")

;---Open file to create.
  filename = "rewrite.nc"

  if(isfilepresent(filename)) then
    system("/bin/rm " + filename)
  end if
  f = addfile(filename,"c")

;---Write data_orig to file
  f->data = data_orig

;---Close file.
  delete(f)

;---Reopen file to rewrite
  f = addfile(filename,"w")

;---Read data
  data_read = f->data
  print("==================================================================")
  printVarSummary(data_read)
  print("min/max data_read = " + min(data_read) + "/" + max(data_read))
  print("(Should be exactly same as before.)")

  data_new = random_uniform(-500,500,dims)
  data_new@date_rewritten = systemfunc("date")

;---Write out new data
  f->data = data_new

;---Close file.
  delete(f)

;---Reopen file to read only
  f = addfile(filename,"r")

;---Read data
  data_final = f->data
  print("==================================================================")
  printVarSummary(data_final)
  print("min/max data_final = " + min(data_final) + "/" + max(data_final))
  print("(Should be between -500 and 500.)")

end

On Mar 4, 2011, at 4:10 PM, Siraj ul Islam wrote:

> Hi,
>
> I am trying to replace some variables in CCSM4 model's initial condition (netcdf format). I am using NCEP daily data and want to replace variables like U, V etc in the initial conditions of CCSM4 with NCEP variables U, V etc.
> Can NCL work for this kind of rewriting netcdf file with replaced data?
>
> Cheer
> Siraj
>
>
> --
> Siraj Ul Islam
>
> PhD student / Research Assistant
> Environmental Science and Engineering,
> University of Northern British Columbia,
> Prince George, BC, Canada
>
> <image001.gif>
>
>
> _______________________________________________
> ncl-talk mailing list
> List instructions, subscriber options, unsubscribe:
> http://mailman.ucar.edu/mailman/listinfo/ncl-talk

_______________________________________________
ncl-talk mailing list
List instructions, subscriber options, unsubscribe:
http://mailman.ucar.edu/mailman/listinfo/ncl-talk
Received on Mon Mar 7 15:18:44 2011

This archive was generated by hypermail 2.1.8 : Mon Mar 07 2011 - 15:20:57 MST