Ascii file to a regular lat/lon netcdf grid
Added by Richard Dixon almost 5 years ago
Hi,
I have .csv data in the format
Lat Lon Value
1.5 51.0 3
2.0 52.5 4
3.5 53.5 6
It's across the whole globe on regular lat/lon grid, but with values only reported on land areas.
I would like to import this into a regular global lat/long that effectively "fills in" the areas with 0 or Null where a lat/lon point isn't reported in my list of data points (essentially over most of the oceans).
I'd noticed a couple of queries on here sort of similar to this (e.g. https://code.mpimet.mpg.de/boards/1/topics/329) but I wondered if my request is achievable in cdo?
Many thanks
Richard
Replies (3)
RE: Ascii file to a regular lat/lon netcdf grid - Added by Karin Meier-Fleischer almost 5 years ago
Hi Richard,
you can convert the CSV data to an unstructured grid but it is not possible in my opinion to automatically remap and fill it with missing values.
I would recommend to use NCL to read the CSV data, generate a global grid of missing values and replace the missing values with the CSV data.
I've used the following CSV test data:
lon lat value 5.0 40.0 1000 5.0 41.0 1000 5.0 42.0 1200 5.0 44.0 1600 5.5 40.0 1000 5.5 41.0 1000 5.5 42.0 1200 5.5 44.0 1600 6.0 40.0 1300 6.0 42.0 1700 6.0 44.0 1900 6.0 45.0 2000 6.5 40.0 1300 6.5 42.0 1700 6.5 44.0 1900 6.5 45.0 2000 7.0 40.0 1500 7.0 41.0 1200 7.0 42.0 1300 7.0 43.0 1000 7.0 44.0 1000 7.5 40.0 1500 7.5 41.0 1200 7.5 42.0 1300 7.5 43.0 1000 7.5 44.0 1000 8.0 40.0 1000 8.0 41.0 1000 8.0 42.0 1200 8.0 43.0 1600 8.5 40.0 1000 8.5 41.0 1000 8.5 42.0 1200 8.5 43.0 1600
The NCL script to convert the CSV data to a global grid netCDF file and check it by plotting the CSV and netCDF data:
lines = asciiread("input.csv",-1,"string") vlon = tofloat(str_get_field(lines(1:),1," ")) vlat = tofloat(str_get_field(lines(1:),2," ")) var = tofloat(str_get_field(lines(1:),3," ")) nvalues = dimsizes(var) lat = fspan(-90.0,89.5,360) lon = fspan(-180.0,179.5,720) data = new((/dimsizes(lat),dimsizes(lon)/),float) lat!0 = "lat" lat&lat = lat lat@long_name = "latitude" lat@units = "degrees_north" lon!0 = "lon" lon&lon = lon lon@long_name = "longitude" lon@units = "degrees_east" data!0 = "lat" data!1 = "lon" data&lat = lat data&lon = lon data@units = "variable units" data@long_name = "variable from CSV file" do i=0,nvalues-1 ind_lat = ind(lat .eq. vlat(i)) ind_lon = ind(lon .eq. vlon(i)) data(ind_lat,ind_lon) = (/var(i)/) end do :-- write data to output file system("rm -rf global_data.nc") fout = addfile("global_data.nc","c") setfileoption(fout,"DefineMode",True) dimNames = (/"lat", "lon"/) dimSizes = (/dimsizes(lat), dimsizes(lon)/) dimUnlim = (/False, False/) filedimdef(fout,dimNames,dimSizes,dimUnlim) filevardef(fout, "lat", typeof(lat), getvardims(lat)) filevardef(fout, "lon", typeof(lon), getvardims(lon)) filevardef(fout, "data", typeof(data), getvardims(data)) filevarattdef(fout, "lat", lat) filevarattdef(fout, "lon", lon) filevarattdef(fout, "data", data) setfileoption(fout,"DefineMode",False) fout->lat = (/lat/) fout->lon = (/lon/) fout->data = (/data/) delete(fout) ;-- read the new data and create a plot fin = addfile("global_data.nc","r") data_in = fin->data wks = gsn_open_wks("x11", "plot_new") res = True res@gsnDraw = False res@gsnFrame = False res@cnFillOn = True res@cnLinesOn = False res@cnFillMode = "RasterFill" res@mpMinLatF = min(vlat) - 2.0 res@mpMaxLatF = max(vlat) + 2.0 res@mpMinLonF = min(vlon) - 2.0 res@mpMaxLonF = max(vlon) + 2.0 plot = gsn_csm_contour_map(wks, data, res) ;-- draw the vlat, vlon positions with circle marker pmres = True pmres@gsMarkerIndex = 4 id = gsn_add_polymarker(wks, plot, vlon, vlat, pmres) draw(plot) frame(wks)
-Karin
RE: Ascii file to a regular lat/lon netcdf grid - Added by Richard Dixon almost 5 years ago
Dear Karin
Thank you VERY much for taking the time to write all of this - I will definitely give this a go.
Best wishes
Richard
RE: Ascii file to a regular lat/lon netcdf grid - Added by Richard Dixon over 4 years ago
My data had an awkward grid spacing of 0.00833333 degrees lat/long so I had to do some truncating of decimal places in the reading in to ensure numbers matched, but I am happy to report this worked. Many thanks again for your help.
Richard