; ; This program processes a one dimensional variable and writes it to the ; output netcdf file. Determines the nearest neighbor value for the ; time interval. ; Contains some additional operations depending on the input variable. ; pro one_dimensional_nearest_neighbor,dataname common ncdf_file, oldcdfname,twoD,time_dim_name,height_dim_name,$ height_var_name,avgfilename,curdate common old_grid, num_times,num_heights,base_time,time_offset,$ height,site,files,num_files,num_days common new_grid, numtimes, newtimes, numheights, newheights,$ timespacing, heightspacing common bracket, startbst common data, avgdata ; ; Loop through all the matching files in the directory ; m=-1 ;counts number of cdf files without the variable p=-1 ;counts number of cdf files with variable j=-1 ;counts number of cdf files for i=0,num_files-1 do begin ; ; Don't try to process any gif files that may be in the directory ; if strpos(files[i],'gif') eq -1 and $ strpos(files[i],'png') eq -1 then begin j=j+1 print,i,j,files[i] ; ; Open the netcdf file ; cdf_id=ncdf_open(files[i]) ; ; Get the dimension id's ; time_did=ncdf_dimid(cdf_id,time_dim_name) ; ; Get the dimensions ; ncdf_diminq,cdf_id,time_did,charstring,ntimes ; ; Create array for nodata value ; dummy=make_array(ntimes,/float,value=-9999) ; ; Get the variable id ; data_id=ncdf_varid(cdf_id,dataname) ; ; Get the variable if it exists ; if data_id ne -1 then begin print,'Processing 1-D ',dataname ncdf_varget, cdf_id, data_id, data p=p+1 endif else begin print, 'Variable not in this file' m=m+1 endelse ; ; If it is the first file and the variable exists, get attributes ; if p eq 0 and data_id ne -1 then begin print, 'first variable, getting the attributes' datainfo=ncdf_varinq(cdf_id, data_id) ;inquire about the variable numatts=datainfo.natts ;number of attributes assigned to the variable parts=strsplit(files[i],'/',/extract) part=strsplit(parts[5],'.',/extract) origfile=part[0]+'.'+part[1] ; ; Store the attribute names ; dataatr=strarr(numatts) ;string array to hold attribute names for k=0,numatts-1 do begin ; Returns the name of the attribute dataatr[k]=ncdf_attname(cdf_id, data_id, k) endfor endif ; ; Combine the data together ; if p eq 0 and m lt 0 and data_id ne -1 then begin print,'first file and variable exists' combined_data=data endif else if p ge 0 and data_id ne -1 then begin print,'adding on existing variable' combined_data=[combined_data,data] endif else if p lt 0 and m eq 0 and data_id eq -1 then begin print,'first file and variable doesnt exist' combined_data=dummy endif else if m ge 0 and data_id eq -1 then begin print,'adding on dummy variable' combined_data=[combined_data,dummy] endif ; ; Close the netcdf file ; ncdf_close,cdf_id endif ;end of not a gif file endfor ;end of looping through the matching files ; ; Continue processing data because some data exists ; if j gt -1 and p gt -1 then begin data=combined_data combined_data=0 ;release memory ; ; Remove very large bad values ; result=where (data ge 1e18,count) if count ne 0 then data[result]=-9999 ; ; Convert cloudtop and cloudbase from msl to agl ; if dataname eq 'cld_base' or dataname eq 'top_base' then begin if site eq 'sgp' then begin result=where(data ne -9999, count) if count ne 0 then data[result]=data[result]-2*318. endif if site eq 'nsa' then begin result=where(data ne -9999, count) if count ne 0 then data[result]=data[result]-7. endif if site eq 'twpc1' then begin result=where(data ne -9999, count) if count ne 0 then data[result]=data[result]-3. endif if site eq 'twpc2' then begin result=where(data ne -9999, count) if count ne 0 then data[result]=data[result]-6. endif endif ; ; Give all bad or missing data vales the same bad flag of -9999 ; if dataname eq 'CloudBaseBestEstimate' or dataname eq 'RadarFirstTop' then begin result=where(data le 0, count) if count ne 0 then data[result]=-9999 endif if dataname eq 'liq' or dataname eq 'liq2' then begin result=where(data le 0, count) if count ne 0 then data[result]=-9999 endif if dataname eq 'sfc_pressure' then begin ; two no data values, -9999, -999900 ; the last surface pressure in the file is 0 result=where(data le -9999 or data eq 0 or data eq 9.9999e7, count) if count ne 0 then data[result]=-9999 ;convert to just one no data values endif ; ; Find the data in timespacing intervals ; avg_data=make_array(numtimes,/float,value=-9999) for t=0,numtimes-2 do begin result=where(time_offset ge newtimes[t] and $ time_offset lt newtimes[t+1], count) if count ge 1 then begin if count ge 2 then stop times_int=time_offset[result] data_int=data[result] timespaces=ABS(times_int-newtimes[t]) closest=min(timespaces,closest_sub) avg_data[t]=data_int[closest_sub] ;print,t,newtimes[t],times_int,newtimes[t+1],avg_data[t] endif else avg_data[t]=-9999 endfor ; ; Find the data in the last timespacing interval ; result=where(time_offset ge newtimes[numtimes-1] and $ time_offset lt (newtimes[numtimes-1]+timespacing), count) if count ge 1 then begin if count ge 2 then stop times_int=time_offset[result] data_int=data[result] timespaces=ABS(times_int-newtimes[numtimes-1]) closest=min(timespaces,closest_sub) avg_data[t]=data_int[closest_sub] ;print,t,newtimes[t],time_offset[result],newtimes[t+1],avg_data[t] endif else avg_data[t]=-9999 ; ; Plot to check data ; plot1='no' ;flag this yes if you want plots to the screen, no if you don't if plot1 eq 'yes' then begin ; ; Find the minimum value for the data ; result=where(avg_data eq -9999, count) if count ne 0 then avg_data[result]=1e8 dminv=min(avg_data) result=where(avg_data eq 1e8, count) if count ne 0 then avg_data[result]=-9999 ;myadd=max(avg_data)*.10 ;so I can see the newdata above the old dmaxv=max(avg_data);+myadd window,2,title='avg '+dataname plot,newtimes,avg_data,psym=6,yrange=[dminv,dmaxv] window,3,title='raw '+dataname plot,time_offset,data,psym=6,$ yrange=[dminv,dmaxv],$ xrange=[newtimes[0],newtimes[numtimes-1]] stop endif ;plots to screen ; ; Open the netcdf file ; avg_id=ncdf_open(avgfilename, /write) ; ; Get the variable dimension ; time_id=ncdf_dimid(avg_id,'time') ; ; Put the netcdf file into define mode ; ncdf_control, avg_id, /redef ; ; Define variable ; avg_data_id=ncdf_vardef(avg_id, dataname, time_id) ; ; Go back to an old file and get the information ; for a variable. Necessary for attribute copying ; for i=0,num_files-1 do begin if strpos(files[i],'gif') eq -1 and $ strpos(files[i],'png') eq -1 then begin cdf_id=ncdf_open(files[i]) data_id=ncdf_varid(cdf_id,dataname) if data_id ne -1 then goto, found_var ncdf_close,cdf_id ;close file endif endfor found_var: ;skip out of loop because found a file containing the variable ; ; Define attributes ; for i=0,numatts-1 do begin nowatt=dataatr[i] mytry=ncdf_attcopy(cdf_id, data_id, nowatt, avg_id, avg_data_id) endfor if dataname eq 'cld_base' or dataname eq 'top_base' then begin ncdf_attdel,avg_id,avg_data_id,'units' ncdf_attput,avg_id,avg_data_id,'units ','meters AGL' endif ncdf_close,cdf_id ;close old file ncdf_attput, avg_id, avg_data_id, 'missing ','-9999' ncdf_attput, avg_id, avg_data_id, 'original data source file ',origfile ; ; Change from define mode to data mode ; ncdf_control, avg_id, /endef ; ; Write variable ; ncdf_varput, avg_id, avg_data_id, avg_data ; ; Close the netcdf file ; ncdf_close, avg_id endif ;variable does exist in one of the files return end