[ncl-talk] Convert ASCII file to netcdf file

Guido Cioni guidocioni at gmail.com
Sun Mar 12 01:28:56 MST 2017


Hey,
I assume you already gave a look at the specific page on the NCL online guide.

First, what does it mean that you “cannot use cd_calendar”? Does it return error? What kind of error? 

Second, given the other error that you are reporting, I believe that you are trying to make a contour plot out of the NETCDF file. If your goal is to make a contour plot out of the data contained in the ASCII file I would suggest you to do that directly, given that NCL has this capability. tTrying to convert everything into a NETCDF file before could be tricky. 

Now, let’s get to the file. Have you tried to look into that before plotting? 

[1] Look at the time variable: 

[snippet…]
Time coordinate :  804 steps
     RefTime =  1950-01-01 00:00:00  Units = months  Calendar = standard
  YYYY-MM-DD hh:mm:ss  YYYY-MM-DD hh:mm:ss  YYYY-MM-DD hh:mm:ss  YYYY-MM-DD hh:mm:ss
 18200-02-01 00:00:00 18200-03-01 00:00:00 18200-04-01 00:00:00 18200-05-01 00:00:00
 18200-06-01 00:00:00 18200-07-01 00:00:00 18200-08-01 00:00:00 18200-09-01 00:00:00
 18200-10-01 00:00:00 18200-11-01 00:00:00 18200-12-01 00:00:00 18201-01-01 00:00:00
 18208-06-01 00:00:00 18208-07-01 00:00:00 18208-08-01 00:00:00 18208-09-01 00:00:00
[…]

How is NCL supposed to interpret that? If you read from the ASCII file directly 195001 NCL will interpret as a number. You need to convert that into a time variable using YYYY and MM separate data. Look at the NCL documentation (http://www.ncl.ucar.edu/Document/Functions/date.shtml <http://www.ncl.ucar.edu/Document/Functions/date.shtml>). 

[2] Look at the lat and lon variables. They are NOT ordered in the ASCII file, so they will not be ordered in NETCDF file as well. 

 lat = 9.68, 8.35, 8.75, 8.98, 8.12, 8.58, 7.72, 7.47, 7.33, 7.17, 6.9, 6.82,
    6.97, 6.98, 6.98, 6.03, 6.12, 8.03 ;

NCL can not handle non-monotonic coordinate array, so you would need to reorder every row in the ASCII file before even plotting or converting to a NETCDF file. 

Good luck

> Il giorno 12 mar 2017, alle ore 02:58, Priyanka Silva <priyankakeraminiya6 at gmail.com> ha scritto:
> 
> Dear Sir/Madam,
> 
> 
> 
> I'm a beginner of the NCL. I wanted to convert ASCII file  to netcdf file. I created netcdf file but I cannot use it. The ASCII file has Monthly precipitation data of 18 stations (from 195001 to 201612). It has time, latitude , longitude and precipitation data. Missing value for precipitation is -999.9. The ASCII file is attached. When I use my netcdf file I cannot use "cd_calendar" and also I get the following error messages.
> 
> 
> 
> 
> 
> warning:ScalarFieldSetValues: irregular coordinate array sfYArray non-monotonic:
>  defaulting sfYArray
> warning:ContourPlotSetValues: cnLevelSpacingF value exceeds or equals data range
> : defaulting
> 
> 
> 
> 
> Thank you for any help.
> 
> 
> 
> Best regards,
> 
> Priyanka.
> 
> 
> 
> 
> 
> 
> 
> 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/csm/diagnostics_cam.ncl"
> 
> begin
> ;---------------------------------------------------------------------------
> ncol = 4
> nrow = numAsciiRow ("Mon_precip.txt")
> 
> ntime =804
> nlat = 804
> nlon = 804
> 
> data = asciiread ("Mon_precip.txt",(/nrow,ncol/), "float")
> 
> data at FillValue = -999.9
> printVarSummary(data)
> 
> time = data(:ntime-1,0)
> mtime = dimsizes(time)
> lat = data (::nlat,1)
> mlat = dimsizes(lat)
> lon = data (::nlon,2)
> mlon = dimsizes(lon)
> 
> precip = data (:,3)
> 
> precip at units = "mm/month"
> lat at long_name = "latitude"
> lat at units     = "degrees_north"
> lon at long_name = "longitude"
> lon at units     = "degrees_east"
> time at units   = "months since 1950-01"
> time at calendar = "standard"
> 
> printVarSummary(time)
> printVarSummary(lat)
> printVarSummary(lon)
> printVarSummary(precip)
> 
> ;---------------------------------------------------------------------------
> pre = onedtond(precip,(/mtime,mlat,mlon/))
> printVarSummary(pre)
> ;------------------------------------------------------------------------
>   ; Assign named dimension
> ;----------------------------------------------------------------------
> lat!0 = "lat"
> lon!0 = "lon"
> time!0 = "time"
> 
> pre!0 = "time"
> pre!1 = "lat"
> pre!2 = "lon"
> ;-----------------------------------------------------------------------
>   ; Assign coordinate variables
> ;-----------------------------------------------------------------------
> time&time = time
> lat&lat =  lat
> lon&lon =  lon
> 
> pre&time = time
> pre&lat  = lat
> pre&lon  = lon
> ;---------------------------------------------------------------------
>   ; write netcdf
> ;--------------------------------------------------------------------
> system("rm -rf precip_monSL.nc")
> fout = addfile("precip_monSL.nc","c")
> fout->precip = pre
> ;--------------------------------------------------------------------
>      ;create global attributes of the file
> ;------------------------------------------------------------------
> fAtt  = True
> fAtt at title = "Monthly average precipitation in Sri Lanka(195001-201612)"
> fileattdef( fout, fAtt ) ; copy file attributes
> ;-----------------------------------------------------------------------------------------------------
> 
> end
> 
> 
> 
> 
> 
> <Mon_precip.txt>_______________________________________________
> 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/20170312/6c0bf59d/attachment.html 


More information about the ncl-talk mailing list