;*********************************************** ; csv_7.ncl ; ; Concepts illustrated: ; - Reading two CSV files via 'asciiread' ; - Using str_match_ic_regex to extract lines (rows) ; - Writing the selected information to an ascii file ; - Finding the user specified 'fldstr' and extracting the values ; - Plotting the random data ; - Drawing the counties of North Dakota and surrounding areas ;*********************************************** ; This script reads 2 CSV files, where one contains the data values ; and a year/month field, and the other contains the lat/lon locations. ; Data are extracted using a desired field string and a desired year and month. ;*********************************************** ; input data ;*********************************************** diri = "./" fkey = "479615" flnm = fkey+".NorthDakota.csv" strs = asciiread(diri+flnm,-1,"string") ;*********************************************** ; user specified YYYYMM and VARIABLE ;*********************************************** yyyymm = 197504 fldstr = "DPNT" ; case sensitive con = 1.00 ;*********************************************** ; match all data lines with specified YYYYYY ;*********************************************** seldata = str_match_ic_regex(strs, tostring(yyyymm)) ; 6.3.0 print(seldata) print("=====") ;*********************************************** ; write selected data to ascii file ;*********************************************** seldir = "./" selfil = fkey+"."+yyyymm+".csv" system("/bin/rm -f "+seldir+selfil) asciiwrite(seldir+selfil, seldata) ; all data for selected yyyymm ;*********************************************** ; Which field matches ;*********************************************** FIELDS = (/"STATION","STATION_NAME","DATE","CLDD","DPNP","DPNT","HTDD" \ ,"DT90","DX32","DT00","DT32","DP01","DP05","DP10","EMXP","MXSD" \ ,"DSNW","TPCP","TSNW","EMXT","EMNT","MMXT","MMNT","MNTM" /) nfield = ind(FIELDS.eq.fldstr) + 1 ; field to be extracted print("fldstr="+fldstr+" corresponds to field number "+nfield) print("=====") ;*********************************************** ; extract the values ;*********************************************** sdat = str_get_field(seldata , 2, ",") data = tofloat( str_get_field(seldata , nfield, ",") )*con data@_FillValue = -9999*con print(sdat+" : "+data) print("=====") ;*********************************************** ; Read station locations from 2nd ascii file and set wks params ;*********************************************** strll = asciiread(diri+fkey+".latlon.csv", -1, "string") ; print(strll) ; STATION","STATION_NAME", "LAT", "LON" print("=====") sloc = str_get_field(strll , 2, ",") lat = tofloat( str_get_field(strll , 3, ",") ) lon = tofloat( str_get_field(strll , 4, ",") ) print(sloc +" : "+lat+" "+lon) pltDir = "./" ; pltNam = fkey+"_NorthDakota_"+fldstr+"_"+yyyymm pltNam = "csv" ; match script name pltTyp = "png" ; send graphics to PNG file ;*********************************************** ; Plot ;*********************************************** wks = gsn_open_wks(pltTyp,pltDir+pltNam) res = True res@gsnFrame = False ; So we can draw markers res@gsnMaximize = True res@pmTickMarkDisplayMode = "Always" res@trGridType = "TriangularMesh" ; The default if you res@mpMinLatF = 45.8 ; range to zoom in on res@mpMaxLatF = 49.1 res@mpMinLonF = -104.2 res@mpMaxLonF = -96.5 res@mpOutlineBoundarySets = "AllBoundaries" res@mpDataBaseVersion = "MediumRes" res@mpDataSetName = "Earth..4" ; U.S. counties ;res@cnLevelSelectionMode = "ManualLevels" ;res@cnMinLevelValF = ;res@cnMaxLevelValF = ;res@cnLevelSpacingF = res@cnFillOn = True res@cnFillPalette = "WhViBlGrYeOrRe" ; set color map res@cnLinesOn = True res@cnLineLabelsOn = False res@sfXArray = lon res@sfYArray = lat res@tiMainString = fldstr+": "+yyyymm res@pmLabelBarOrthogonalPosF = 0.15 ; move labelbar down a smidge res@pmLabelBarHeightF = 0.07 ; make labelbar thinner map = gsn_csm_contour_map(wks,data,res) ; ; Draw markers on the plot in the lat/lon locations. ; mkres = True mkres@gsMarkerIndex = 16 ; Filled circle mkres@gsMarkerSizeF = 0.016 gsn_polymarker(wks,map,lon,lat,mkres) frame(wks)