;------------------------------------------------------------------------------ ; Script to read ASCII data and to output it to a netCDF file ; Nothing fancy column-by-column extraction and write to netCDF ;------------------------------------------------------------------------------ ;ID1 ID2 Lat LON DATE TIME DEPTH TEMP SALT SOURCE PRES FLOAT JULD CAL1 CAL2 FLAG1 FLAG2 ;11 25 15.22 59.97 04-JUN-2011 13:32 17.000 29.531 36.580 Argo 17.000 1901186 22434. 26.481888 1.688554 O 1 ;11 25 15.22 59.97 04-JUN-2011 13:32 18.000 29.532 36.580 Argo 18.000 1901186 22434. 26.455912 1.685137 O 1 ;------------------------------------------------------------------------------ load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.ncl" ; utility function undef("extract_cols") function extract_cols(array[*]:string, opt[1]:integer, nfield[1]:integer \ ,long_name[1]:string, units[1]:string) local q begin delim = " -:" + str_get_nl() ; the last is the new line string if (opt.eq.0) then q = tochar(str_get_field( array,nfield, " -:") ) ; ascii/string dimq = dimsizes(q) q!1 = "nchar"+dimq(1) else if (opt.eq.1) then q = toint( str_get_field( array,nfield, " -:") ) ; integer else if (opt.eq.2) then q = tofloat( str_get_field(array,nfield, " -:") ) ; float end if end if end if ; attach meta data q!0 = "row" q@long_name = long_name q@units = units return(q) end ;*********************************************************************** ; MAIN ;*********************************************************************** ; diri = "./" fili = "input.txt" diro = "./" filo = "DonDNA.nc" ; Read file as type 'string' dats = asciiread(diri+fili, -1, "string") ; read file as type string NROW = dimsizes(dats) ; includes header nrow = NROW-1 ; # rows with data ; skip 1st line (ie, line number 0) nfld = str_fields_count(dats(1)," -:") ; # columns (fields) print("nfld="+nfld) do nf=1,nfld fld = str_get_field(dats(1), nf, " -:") print("nf="+nf+" "+fld) end do ; Separate fields (columns) and assign meta data id1 = extract_cols( dats(1:), 1, 1, "ID1", "") id2 = extract_cols( dats(1:), 1, 2, "ID1", "") lat = extract_cols( dats(1:), 2, 3, "latitude" , "degrees_north") lon = extract_cols( dats(1:), 2, 4, "longitude", "degrees_east") day = extract_cols( dats(1:), 1, 5, "day" , "day_of_month") mons = extract_cols( dats(1:), 0, 6, "month" , "month_of_current_year") year = extract_cols( dats(1:), 1, 7, "year" , "gregorian calendar") hour = extract_cols( dats(1:), 1, 8, "hour" , "hour_of_current_day") minut = extract_cols( dats(1:), 1, 9, "minute" , "hour_of_current_hour") depth = extract_cols( dats(1:), 2, 10, "depth" , "????") temp = extract_cols( dats(1:), 2, 11, "temperature", "degC") salt = extract_cols( dats(1:), 2, 12, "salinity" , "???") src = extract_cols( dats(1:), 0, 13, "Data Source", "") pres = extract_cols( dats(1:), 2, 14, "pressure" , "???") fltid = extract_cols( dats(1:), 1, 15, "Float ID" , "") juld = extract_cols( dats(1:), 2, 16, "Julian Day" , "CNES STANDARD - 1950") cal1 = extract_cols( dats(1:), 2, 17, "CAL1" , "???") cal2 = extract_cols( dats(1:), 2, 18, "CAL2" , "???") flg1 = extract_cols( dats(1:), 0, 19, "Flag 1" , "") flg2 = extract_cols( dats(1:), 1, 20, "Flag 2" , "") ; write netCDF system("/bin/rm -f "+diro+filo) ; remove any pre-existing file ncdf = addfile(diro+filo ,"c") ; open output netCDF file ncdf@title = "ASCII columns-to-netCDF variables" ncdf@source_file = fili ncdf@creation_date = systemfunc("date") ; output variables directly ncdf->ID1 = id1 ncdf->ID2 = id2 ncdf->FLOAT = fltid ncdf->lat = lat ncdf->lon = lon ncdf->juld = juld ncdf->minute = minut ncdf->hour = hour ncdf->day = day ncdf->year = year ncdf->month_name = mons ncdf->TEMP = temp ncdf->SALT = salt ncdf->PRES = pres ncdf->CAL1 = cal1 ncdf->CAL2 = cal2 ncdf->flg1 = flg1 ncdf->flg2 = flg2