Re: ascii to netcdf

From: Wei Huang <huangwei_at_nyahnyahspammersnyahnyah>
Date: Thu Oct 03 2013 - 08:53:14 MDT

Here is a modified script to try:

Wei

; Read in the data as an nlat*nlon x 3 array (not nlat x nlon x 3)

  nlat = 129
  nlon = 126
  data = asciiread("total_bc.txt",(/nlat*nlon,3/),"float")

; Extract the nlat coordinates from the data array.
;
; Since the nlatitude values are repeated, we only need to
; grab every nlon-th value.

  lat = data(::nlon,0)

  printVarSummary(lat)

; Extract the nlon coordinates. Since the first "nlon" nlongitudes
; are unique, and then repeat themselves afterwards, we can just
; grab these first "nlon" values and ignore the rest.

  lon = data(0:nlon-1,1)

  printVarSummary(lon)

; Convert data to a 2D grid.
;
; The size of grid: (/89,240/) (Note 89 * 240 = 2130)

  temp2D = onedtond(data(:,2),(/nlat,nlon/)) ; convert 1D array to a 2D array

; Assign named dimensions
  lat!0 = "lat"
  lon!0 = "lon"

  temp2D!0 = "lat"
  temp2D!1 = "lon"

; Assign coordinate variables
  lat&lat = lat
  lon&lon = lon

  temp2D&lat = lat
  temp2D&lon = lon

  printVarSummary(temp2D)

;;;;;;;write netcdf

;===================================================================
; Assume variables T, PS and ORO exist and that they have
; associated meta data: (a) coordinate variables time, lev, nlat, nlon
; and (b) attributes
;===================================================================

  diro = "./" ; Output directory
  filo = "total_bc.nc" ; Output file
  system("/bin/rm -f " + diro + filo) ; remove if exists
  fout = addfile (diro + filo, "c") ; open output file

;===================================================================
; explicitly declare file definition mode. Improve efficiency.
;===================================================================
  setfileoption(fout,"DefineMode",True)

;===================================================================
; create global attributes of the file
;===================================================================
  fAtt = True ; assign file attributes
  fAtt@title = "NCL Efficient Approach to netCDF Creation"
  fAtt@source_file = "original-file.nc"
  fAtt@Conventions = "None"
  fAtt@creation_date = systemfunc ("date")
  fileattdef( fout, fAtt ) ; copy file attributes

;===================================================================
; predefine the coordinate variables and their dimensionality
; Note: to get an UNLIMITED record dimension, we set the dimensionality
; to -1 (or the actual size) and set the dimension name to True.
;===================================================================
  dimNames = (/"lat", "lon"/)
  dimSizes = (/ nlat, nlon/)
  dimUnlim = (/False, False/)
  filedimdef(fout,dimNames,dimSizes,dimUnlim)

;===================================================================
; predefine the the dimensionality of the variables to be written out
;===================================================================
; Here we are using NCL functions to facilitate defining
; each variable's dimension name(s) and type.
; The following could be replaced with explicit, user defined dimension
; names different from those associated with the variable in memory.
; Say, PS(time,nlat,nlon) in the NCL script. They could be redefined for the file via:
; filevardef(fout, "PS" ,typeof(PS) ,(/"TIME","nlatitude","nlongitude"/))
;===================================================================
  print("getvardims(lat)=" + getvardims(lat))
  print("getvardims(lon)=" + getvardims(lon))
  print("getvardims(temp2D)=" + getvardims(temp2D))

  filevardef(fout, "lat", typeof(lat), getvardims(lat))
  filevardef(fout, "lon", typeof(lon), getvardims(lon))
  filevardef(fout, "BC", typeof(temp2D), getvardims(temp2D))

;===================================================================
; Copy attributes associated with each variable to the file
; All attributes associated with each variable will be copied.
;====================================================================
  filevarattdef(fout,"lat", lat) ; copy lat attributes
  filevarattdef(fout,"lon", lon) ; copy lon attributes
  filevarattdef(fout,"BC", temp2D) ; copy BC attributes

;===================================================================
; explicitly exit file definition mode. **NOT REQUIRED**
;===================================================================
  setfileoption(fout,"DefineMode",False)

;===================================================================
; output only the data values since the dimensionality and such have
; been predefined. The "(/", "/)" syntax tells NCL to only output the
; data values to the predefined locations on the file.
;====================================================================
  fout->lat = (/lat/)
  fout->lon = (/lon/)
  fout->BC = (/temp2D/)

huangwei@ucar.edu
VETS/CISL
National Center for Atmospheric Research
P.O. Box 3000 (1850 Table Mesa Dr.)
Boulder, CO 80307-3000 USA
(303) 497-8924

On Oct 3, 2013, at 12:49 AM, Nitin Patil <nitinpatil85@gmail.com> wrote:

> Dear ncl users,
>
> I am currently new to ncl and I am trying to convert ascii to netcdf in it.
>
> I have a ascii file which includes 3 columns (lat, lon and 1 variable) and I have tried in ncl but giving some errors. If anyone knows where I am making mistake. Changes will be highly appreciated. Please find the attached ncl code and ascii file for reference.
>
> --
> With Best Wishes,
> Nitin Patil
> <total_bc.txt><ascii2nc.ncl>_______________________________________________
> 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 Thu Oct 3 08:53:25 2013

This archive was generated by hypermail 2.1.8 : Fri Oct 04 2013 - 16:45:17 MDT