; The csv file is rather 'odd' from a USA point of view. ; It uses the European' approach to represent numbers. ; Specifically, a comma is used to separate the integral part of a number ; from the decimal part. ; ; There are three issues: ; [1] A comma [ , ] is used as BOTH a value separator AND a decimal point. ; [2] There is a lack of consistency in the csv file. ; Most values are enclosed with double-quotes [ " ]. ; Unfortunately, some are not !!! ; [3] A string ["#VALOR!"] is used to denote a missing (_FillValue). ; ; Sample CSV: ; "Month","Latitude","Longitude","NPP" ; "3","38,78","-72,27","321,3" <=== 7 fields separated by a , and enclosed within " ; "3","38,78","-72,27","335,4372" ; "3","38,78","-72,27","698,908" ; "6","21,2","-158","#VALOR!" <=== ?missing value? ; "5","-9",-25,"-15,3542161160713" <=== 5 fields integer -25 has no " ; "10","41,17","-66,17",-318 <=== 6 fields integer -318 has no " ; "4","40","-71,96","-777,25" <=== 6 fields integer 40 has no " ; "7","39,84","-72","92,5764" <=== 6 fields ; "11",-11,-25,"23,7089369952225" <=== 5 fields integer -11 and -25 have no " ; ; The CSV file must be edited the address the above issues. ;=================================================================== ;---Data dirNPP = "./" filNPP = "npp_edited.csv" pthNPP = dirNPP+filNPP x1 = asciiread(pthNPP,-1,"string") nrow = dimsizes(x1) ;;print(x1) ; original file print("---") print("nrow="+nrow) print("---") ;---Reset csv _FillValue: NCL syntax := to overwrite current variable x1 := str_sub_str(x1,"#VALOR!","-9999.9") ;---Create variables to split (parse) and recreate the source file space = " " minus = "-" comma = "," decpt = "." dq = str_get_dq() ; " ;---Multiple passes/iterations to extract/parse values so NCL can handle [very fast] x1 := str_sub_str(x1, dq ,space ) ; replace " with 'space' x1 := str_sub_str(x1," , ",space ) ; replace 'space-comma-space' with 'space' x1 := str_sub_str(x1," ," ,space ) ; replace 'space-comma' with 'space' x1 := str_sub_str(x1,", " ,space ) ; replace 'comma-space' with 'space' x1 := str_sub_str(x1,",-" ,space ) ; replace 'comma-minus' with 'space' x1 := str_sub_str(x1,"," ,decpt ) ; replace " with . [decpt] ;---Number of fields nflds = str_fields_count(x1(1), space) print("nflds="+nflds) ; nflds=4 print("---") ;---Create integer and float numbers month = toint (str_get_field(x1(1:),1,space)) lat = tofloat(str_get_field(x1(1:),2,space)) lon = tofloat(str_get_field(x1(1:),3,space)) npp = tofloat(str_get_field(x1(1:),4,space)) npp@_FillValue = -9999.9 ;;print(month+" "+lat+" "+lon+" "+npp) printVarSummary(npp) ;---Create netCDF month!0= "N" lat!0 = "N" lon!0 = "N" npp!0 = "N" lat@units = "degrees_north" lon@units = "degrees_east" npp@units = "???" npp@long_name = "NPP" dirNC = "./" filNC = "NPP_EDITED.nc" pthNC = dirNC+filNC system("/bin/rm -f "+pthNC) ; remove any pre-existing file ncdf = addfile(pthNC ,"c") ; open output netCDF file fAtt = True ; assign file attributes fAtt@title = "NPP csv file converted to netCDF" fAtt@source_file = filNPP fAtt@ncltalk = "Melanie O Hanoly: 2/12/2002" fAtt@Conventions = "None" fAtt@creation_date = systemfunc ("date") fileattdef( ncdf, fAtt ) ; copy file attributes ncdf->MONTH = month ncdf->LAT = lat ncdf->LON = lon ncdf->NPP = npp