;***************************************************************** ; convert_to_relhum converts mxrat (mixing ratio) ; to relhum (relative humidity) ;**************************************************************** pro convert_to_relhum,oldcdfname,time_dim_name,height_dim_name,mxrat,dataname ; ; Open the old netcdf file ; cdf_id=ncdf_open(oldcdfname) ; ; Get the dimension id's ; time_id=ncdf_dimid(cdf_id, time_dim_name) height_id=ncdf_dimid(cdf_id, height_dim_name) ; ; Get the dimensions ; ncdf_diminq, cdf_id, time_id, char_strng, num_times ncdf_diminq, cdf_id, height_id, char_string, num_heights ; ; Get the variable id's ; pressure_id=ncdf_varid(cdf_id, 'pressure') temperature_id=ncdf_varid(cdf_id, 'temp') ; ; Get the variables ; ncdf_varget, cdf_id, pressure_id, pressure ncdf_varget, cdf_id, temperature_id, temperature pressure=transpose(pressure) temperature=transpose(temperature) ; ; Convert mixing ratio to relative humidity ; svp =fltarr(num_times,num_heights) ;saturation vapor pressure smxrat=fltarr(num_times,num_heights) ;saturation mixing ratio rh=fltarr(num_times,num_heights) ;relative humidity for j=0,(num_times-1) do begin ;loop through time for k=0,(num_heights-1) do begin ;loop through height ;Relative humidity calculation courtesy of Erik Vernon if temperature[j,k] eq -9999 or pressure[j,k] eq -9999 or mxrat[j,k] eq -9999 then rh[j,k]=-9999 $ else begin svp[j,k]=6.112*exp((17.67*(temperature[j,k]-273.15))/((temperature[j,k]-273.15)+243.5)) smxrat[j,k]=1000.*(0.622*(svp[j,k]/((pressure[j,k]/100.)-svp[j,k]))) rh[j,k]=100.*(mxrat[j,k]/smxrat[j,k]) endelse endfor endfor ; ; Replace rh values greater than 100 with 100 ; result=where(rh gt 100.,count) if (count ne 0) then rh[result]=100. ; ; Replace the mixing ratio values with relative humidity ; mxrat=rh ; ; Change the data name to relative humidity ; dataname='relhum' ; ; Close the netcdf file ; ncdf_close,cdf_id return end