;*********************************************** ; csv_8.ncl ; ; Concepts illustrated: ; - Reading CSV file via 'asciiread' ; - Using str_match_ic_regex to extract lines (rows) ; Here: all lines that contain the string matching 'keystr' ; - Writing the selected information to an ascii file ; - Find the user specified 'fldstr' and extract the time series values ; - Plot the random data. ;*********************************************** ; input data ;*********************************************** diri = "./" fkey = "479615" flnm = fkey+".NorthDakota.csv" strs = asciiread(diri+flnm,-1,"string") ;*********************************************** ; user specified YYYYMM and VARIABLE ;*********************************************** keystr = "WILLISTON" fldstr = "MMXT" ; case sensitive con = 0.10 ;*********************************************** ; match all data with specified YYYYYY ;*********************************************** seldata = str_match_ic_regex(strs, keystr) ; 6.3.0 ns = dimsizes(seldata) ;---Check length of string array print("--------------------------------------------------------------") if(ns.ne.784) print("The length of seldata should be 784, is " + ns) exit else print("The length of seldata looks correct.") end if ;---Check first and last strings str0 = "COOP:329425,WILLISTON SLOULIN FIELD INTERNATIONAL AIRPORT ND US,194904,-9999,-91,82,-9999,-9999,0,0,11,-9999,-9999,0,-9999,-9999,-9999,29,0,-9999,24,643,360,502" strn = "COOP:329425,WILLISTON SLOULIN FIELD INTERNATIONAL AIRPORT ND US,201407,164,-188,-7,17,7,0,0,0,2,0,0,49,0,0,66,0,99,43,847,542,694" print("--------------------------------------------------------------") if(seldata(0).ne.str0.or.seldata(ns-1).ne.strn) print("The first and last strings are not correct.") print("File was not read properly") exit else print("The first and last strings were read properly.") end if ;*********************************************** ; write selected data to ascii file ;*********************************************** seldir = "./" selfil = fkey+"."+keystr+".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("--------------------------------------------------------------") if(nfield.ne.22) print("fldstr="+fldstr+" should correspond to field number #22") else print("Field number is correct") end if ;*********************************************** ; extract the station name, yyyymm and values for the specified field ;*********************************************** sdat = str_get_field(seldata , 2, ",") ; station name ;---Test field #2. print("--------------------------------------------------------------") if(sdat(0).ne."WILLISTON SLOULIN FIELD INTERNATIONAL AIRPORT ND US") print("Field #2 didn't read properly") exit else print("Field #2 is correct") end if yyyymm = toint(str_get_field(seldata , 3, ",")) ; yyyymm ame data = tofloat( str_get_field(seldata , nfield, ",") )*con ; variable values data@_FillValue = -9999*con dmin = min(data) ; -3.1 dmax = max(data) ; 94 ;---Test min/max of data print("--------------------------------------------------------------") if((dmin-(-3.1)).gt.1e-6.or.(dmax-94).gt.1e-6) print("Min/max of data are incorrect: " + dmin + "/" + dmax) print("Should be -3.1 / 94") else print("Min/max of data are correct") end if ;*********************************************** ; plot ;*********************************************** ; convert yyyymm to fractions for plotting ;*********************************************** yrFrac = yyyymm_to_yyyyfrac(yyyymm, 0.0) print("--------------------------------------------------------------") if(min(yrFrac).ne.1949.25.or.max(yrFrac).ne.2014.5) print("Min/max of yrFrac are incorrect: " + dmin + "/" + dmax) print("Should be 1949.25 / 2014.5") else print("Min/max of yrFrac are correct") end if pltDir = "./" pltNam = fkey+"_NorthDakota_"+fldstr+"_"+keystr pltTyp = "png" wks = gsn_open_wks(pltTyp,pltDir+pltNam) ;gsn_define_colormap(wks,"WhViBlGrYeOrRe") res = True res@gsnMaximize = True res@vpHeightF = 0.4 ; change aspect ratio of plot res@vpWidthF = 0.8 res@trXMinF = min(yrFrac)-1 res@trXMaxF = max(yrFrac)+1 res@gsnCenterString = keystr+": "+fldstr map = gsn_csm_xy(wks,yrFrac,data,res)