<div dir="ltr"><br><br><div class="gmail_quote">---------- Forwarded message ----------<br>From: <b class="gmail_sendername">Sara Fenech</b> <span dir="ltr"><<a href="mailto:sarafenech@gmail.com">sarafenech@gmail.com</a>></span><br>
Date: Wed, Jul 2, 2014 at 12:44 PM<br>Subject: Re: [Wrf-users] Data analysis using NCL<br>To: Li Xianxiang <<a href="mailto:lixx@smart.mit.edu">lixx@smart.mit.edu</a>><br>Cc: "<a href="mailto:wrf-users@ucar.edu">wrf-users@ucar.edu</a>" <<a href="mailto:wrf-users@ucar.edu">wrf-users@ucar.edu</a>><br>
<br><br><div dir="ltr">Hi Li <div><br></div><div>First of all thanks for your reply. I have altered my script (shown below) and it seems to work fine when opening the wrfout file for domain 1 (48km). When I tried running the script for wrfout file of domain 2 (9,6km nest) I got very unrealistic huge values (billions m/s).</div>
<div><br></div><div>Do you have any idea what the problem could be?</div><div><br></div><div>Kind Regards</div><div>Sara</div><div><br></div><div><div>;---------------LOAD FUNCTIONS AND PROCEDURES ----------------</div><div class="">
<div>
<br></div><div>load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl"</div><div>load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_csm.ncl"</div><div>load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.ncl"</div>
<div>load "$NCARG_ROOT/lib/ncarg/nclscripts/wrf/WRFUserARW.ncl"</div><div><br></div><div>; -------------- BEGINING OF NCL SCRIPT ----------------</div><div><br></div><div><br></div><div>begin</div><div>;********************************************************</div>
<div>; read in netCDF file and make a loop for all time steps</div><div>;********************************************************</div></div><div> in = addfile("/home/wrf/OUTPUT/run_PBL2winter/<a href="http://wrfout_d01_2010-12-01.nc" target="_blank">wrfout_d01_2010-12-01.nc</a>","r")</div>
<div><br></div><div><br></div><div><br></div><div><br></div><div>;********************************************************</div><div>; Process all the time steps</div><div>;********************************************************</div>
<div><br></div><div><br></div><div><br></div><div>times = wrf_user_list_times(in) ; get times in the file </div><div class=""><div>ntimes = dimsizes(times) ; number of times in the file </div><div><br>
</div></div><div>;T100 = new(ntimes,float) ; creation of a T vector at each time step</div>
<div>;P98 = new(ntimes,float) ; creation of a P vector at each time step</div><div>;Q = new(ntimes,float) ; creation of a Q vector at each time step</div><div>;rh100 = new(ntimes,float) ; creation of a RH vector at each time step</div>
<div>windspd100 = new(ntimes,float) ; creation of a Windspeed vector at each time step</div><div class=""><div>;print(ntimes)</div><div><br></div><div>do it = 0,ntimes-1 ;Loop for the time: it= starting time</div>
<div>
<br></div></div><div class=""><div>time = it</div><div><br></div><div><br></div><div>;************************************************</div><div>; - Select lon & lat of the point of interest - </div><div>;************************************************</div>
</div><div>; - The function wrf_user_ll_to_ij find the nearest grid point for a specific lat and lon</div><div><br></div><div>Latitude = 35.99725</div><div>Longitude = 14.36775</div><div class=""><div><br></div><div>res = True </div>
<div>
res@returnInt = True ; False : return real values, True: return interger values</div></div><div>point = wrf_user_ll_to_ij(in,Longitude,Latitude,res) ; wrf_user_ll_to_ij(nc_file,lon,lat,opt)</div>
<div>
</div><div> x = point(0)</div><div> y = point(1)</div><div><br></div><div> ;print("X location is: " + x) ; print the value of X at the screen</div><div class=""><div> ;print("Y location is: " + y) ; print the value of Y at the screen</div>
<div><br></div><div><br></div><div><br></div></div><div>;*************************************************************************************</div><div>; - extract wind, Temperature, Pressure, relative humidity and height coordinates- *</div>
<div>;*************************************************************************************</div><div><br></div><div>; Wind and Height</div><div> u = wrf_user_getvar(in,"ua",time) ; u averaged to mass points</div>
<div> v = wrf_user_getvar(in,"va",time) ; v averaged to mass points</div><div> height = wrf_user_getvar(in, "z",time) ; height is our vertical coordinate</div><div> ;ter = wrf_user_getvar(in, "ter",time) ; model terrain height (HGT_M, HGT_U, HGT_V)</div>
<div> </div><div>; Conform data to Terrain Height </div><div> ;nheight = conform(height,ter,(/1,2/)) ; assuming height is a 3d array and ter is a 2d array</div><div> ;height = height - nheight</div><div><br></div><div>
; Temperature</div><div><br></div><div> ;T_in = wrf_user_getvar(in, "tc", time) ; Extract sea level pressure (°C)</div><div><br></div><div>; Surface Pressure</div><div><br></div><div> ;P_in = wrf_user_getvar(in, "pressure", time) ; Extract sea level pressure (Pa)</div>
<div><br></div><div><br></div><div><br></div><div>; Relative humidity</div><div><br></div><div> ;rh_in = wrf_user_getvar(in, "rh", time) ; Extract relative humidity (%)</div><div><br></div><div>;print(rh_in) </div>
<div><br></div><div><br></div><div>;*******************************************************************************</div><div>; - Interpolate wind speed and wind direction at 100m height - * </div><div>;*******************************************************************************</div>
<div><br></div><div><br></div><div> </div><div> ; Interpolate U,V to 100 Meters</div><div> u_plane = wrf_user_intrp3d( u,height,"h", 100,0.,False)</div><div> v_plane = wrf_user_intrp3d( v,height,"h", 100,0.,False)</div>
<div> </div><div> ; Calculate Wind Speed from Vectors</div><div> spd = (u_plane*u_plane + v_plane*v_plane)^(0.5)</div><div> windspd100(it)=spd(x,y)</div><div><br></div><div> ; Wind direction at 100 Meters</div>
<div> ; r2d = 45.0/atan(1.0) ; conversion factor (radians to degrees)</div><div> ; dir = atan2(u_plane,v_plane) * r2d + 180</div><div> ; dir100 = dir(x,y)</div><div><br></div><div> ; Wind Speed </div><div> spd@description = "Wind Speed"</div>
<div> spd@units = "m/s"</div><div> u_plane@units = "m/s"</div><div><br></div><div>;************************************************************</div><div>; - Interpolate temperature at 100m height - * </div>
<div>;************************************************************</div><div> ; T_plane = wrf_user_intrp3d( T_in,height,"h", 98,0.,False)</div><div> ; T100(it)=T_plane(x,y)</div><div>;************************************************************</div>
<div>; - Interpolate P at 98m height - * </div><div>;************************************************************</div><div> ; P_plane = wrf_user_intrp3d( P_in,height,"h", 98,0.,False)</div>
<div> ; P98(it)=P_plane(x,y)</div><div>;************************************************************</div><div>; - Interpolate rh at 98m height - * </div><div>;************************************************************</div>
<div><br></div><div> ; rh_plane = wrf_user_intrp3d( rh_in,height,"h", 98,0.,False)</div><div> ; rh100(it)=rh_plane(x,y)</div><div> </div><div><br></div><div><br></div><div><br></div><div><br></div><div>end do</div>
<div><br></div><div><br></div><div><br></div><div>;************************************************************</div><div>; - Print the variables at the screen - * </div><div>;************************************************************</div>
<div><br></div><div><br></div><div>print(" Time Wind_speed_100m ") </div><div> </div><div>do it = 0,ntimes-1</div><div><br></div><div><br></div><div> print (sprintf("%5.0f",it) +" " \</div>
<div> +sprintf("%23.2f", windspd100(it)) +" " )</div><div class=""><div><br></div><div> </div><div><br></div><div><br></div><div><br></div><div>end do ; end of time loop</div>
<div>
<br></div></div><div>;************************************************************</div><div>; - Print the variables at the screen - *</div><div>;************************************************************</div>
<div><br></div><div><br></div><div> ;print(windspd100(it))</div><div> </div><div> line = new(721,string) ; new(ntimes+1,string) ; declare a string array</div><div> </div><div> ; print headers</div><div>
line(0) = " Time Wind_speed "</div><div> </div><div> do it = 0,ntimes-1</div><div><br></div><div> line(it+1) = sprintf("%5.0f",it) +" " \</div><div> +sprintf("%23.2f", windspd100(it)) +" " </div>
<div class="">
<div><br></div><div> end do ; end of time loop</div><div><br></div></div><div> print(""+line) ; print output to console</div><div> asciiwrite("PBL2AhraxDec.txt",line) ; write output to file</div>
<div><br></div><div><br></div><div> </div><div>end</div></div><div><br></div><div><br></div></div><div class="HOEnZb"><div class="h5"><div class="gmail_extra"><br><br><div class="gmail_quote">On Mon, Jun 30, 2014 at 5:59 PM, Li Xianxiang <span dir="ltr"><<a href="mailto:lixx@smart.mit.edu" target="_blank">lixx@smart.mit.edu</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="auto"><div>Hi Sara,</div><div><br></div><div>Basically your error is to interpolate with a single value (rather than an array). Here</div>
<div><br></div><div><span>ua = ua_in(0:0,y,x)</span></div><div><span><br></span></div><div><span>is just a scalar rather than an array. It should be</span></div><div><span><br></span></div><div><span>ua = us_in(:, y,x)</span></div>
<div><span><br></span></div><div><span>The same applies to other variables. You can use printVarSummary to check the dimension of a variable.</span></div><div><span><br></span></div><div><span>Other thoughts:</span></div>
<div><span>1. wrf_ll_to_ij returns the index of the point in the domain, starting from 1. But in ncl index of an array starts from 0. So usually we should subtract 1 from the i and j. However, I may be wrong here. You'd better check.</span></div>
<div><span><br></span></div><div><span>2. I remember wrf_user_getvar(nc_file, "ua", time) returns the unstaggered u. You can check.</span></div><div><span><br></span></div><div><span>Xianxiang</span></div><div>
<div>
<div><br></div><div><br>On 30 Jun, 2014, at 11:38 PM, "Sara Fenech" <<a href="mailto:sarafenech@gmail.com" target="_blank">sarafenech@gmail.com</a>> wrote:<br><br></div><blockquote type="cite"><div><div dir="ltr">
Hi all<div><br></div><div>I am very new to NCL and I've been trying to write a simple script but I'm having some problems. Well I have done 7 runs using wrf having hourly data for 3 months. Now I would like to extract the wind speed for each time step:</div>
<div>1) at a particular Lat and Lon (I managed this)</div><div>2) at a predefined height (in m)</div><div><br></div><div>Now I have managed to plot the wind speed for all my domain at a particular and I have managed to get wind speeds for a particular lat and lon but I cannot manage to obtain both at once. </div>
<div><br></div><div>Thus to sum it up I would like one value for wind speed for each time step (where lat lon and lev are predefined.)</div><div><br></div><div>The last thing I would like to ask is how I can save the extracted data into a file. </div>
<div><br></div><div>Thanks a lot for all you help. I'm attaching the script which is not working maybe you can spot what's wrong (Something to do with dimensions) together with the error message at the bottom. </div>
<div><br></div><div>Thanks Again</div><div><br></div><div>Sara </div><div><br></div><div><div><div>;******************************************************************************</div><div>;*NCL script to generate netCDF file containing wind speed at specific location *</div>
<div>;* Example for <a href="http://wrfout_d01_2010-12-01.nc" target="_blank">wrfout_d01_2010-12-01.nc</a> *</div><div>;* *</div><div>;************************************************************************************</div>
<div><br></div><div><br></div><div>; -------------- LOAD FUNCTIONS AND PROCEDURES ----------------</div><div><br></div><div>load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl"</div><div>load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_csm.ncl"</div>
<div>load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.ncl"</div><div>load "$NCARG_ROOT/lib/ncarg/nclscripts/wrf/WRFUserARW.ncl"</div><div><br></div><div>; -------------- BEGINING OF NCL SCRIPT ----------------</div>
<div><br></div><div><br></div><div>begin</div><div>;********************************************************</div><div>; read in netCDF file and make a loop for all time steps</div><div>;********************************************************</div>
<div><br></div><div> a = addfile("/home/wrf/OUTPUT/run_PBL2winter/<a href="http://wrfout_d01_2010-12-01.nc" target="_blank">wrfout_d01_2010-12-01.nc</a>","r")</div><div><br></div><div> </div><div>times = wrf_user_list_times(a) ; get times in the file </div>
<div>ntimes = dimsizes(times) ; number of times in the file </div><div>wind_speed = new(ntimes,float) ; creation of a windspeed vector at each time step</div><div>;print(ntimes)</div><div><br></div><div>do it = 0,ntimes-1 ;Loop for the time: it= starting time</div>
<div>print("Working on time " + it )</div><div>time = it</div><div><br></div><div><br></div><div>;************************************************</div><div>; - Select lon & lat of the point of interest - </div>
<div>;************************************************</div><div><br></div><div><br></div><div> res = True </div><div> res@returnInt = True ; False : return real values, True: return interger values</div><div> lat = 42.47 ; Latitude of the point of interest</div>
<div> lon = 12.98 ; Longitude of the point of interest</div><div> point = wrf_user_ll_to_ij(a,lon,lat,res) ; wrf_user_ll_to_ij(nc_file,lon,lat,opt)</div><div> </div><div> x = point(1)</div><div> y = point(0)</div>
<div><br></div><div> print("X location is: " + x) ; print the value of X at the screen</div><div> print("Y location is: " + y) ; print the value of Y at the screen</div><div>;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;</div>
<div>; The specific height levels that we want the data interpolated to.</div><div> height_levels = (/ 1039./) ; height levels to plot - in meter</div><div> nlevels = 1 ; number of height levels</div><div><br>
</div><div>;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;</div><div><br></div><div><br></div><div>;************************************************</div><div>; - extract wind components and unstagger them - </div>
<div>;************************************************</div><div><br></div><div><br></div><div>; ----- Hence U and V are staggered, we need to average them to get the point on the mass grid using wrf_user_unstagger</div>
<div><br></div><div> U = wrf_user_getvar(a, "U", time)</div><div> ua_in = wrf_user_unstagger(U,U@stagger)</div><div> ua = ua_in(0:0,y,x) ;ua(bottom_up,grid_lon,grid_lat)</div><div> V = wrf_user_getvar(a, "V", time)</div>
<div> va_in = wrf_user_unstagger(V,V@stagger) </div><div> va = va_in(0:0,y,x) ;va(bottom_up,grid_lon,grid_lat)</div><div> W = wrf_user_getvar(a, "W", time)</div><div> z_in = wrf_user_unstagger(W,W@stagger)</div>
<div> z = z_in(0:0,x,y) ;z(bottom_up,grid_lon,grid_lat)</div><div><br></div><div> wind_speed(it) = sqrt(ua^2+va^2)</div><div> ;copy_VarCoords(ua,wind_speed(it)) ; copy coord vars to speed</div><div>
;print(wind_speed(it))</div>
<div><br></div><div>;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;</div><div>;Interpolating to the requested height</div><div>;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;</div><div> u_plane = wrf_user_intrp4d( ua,z,"h",height_levels,0.,False)</div>
<div> v_plane = wrf_user_intrp3d( va,z,"h",height_Levels,0.,False)</div><div> spd_plane =wrf_user_intrp3d (wind_speed(it),z,"h",height_Levels,0.,False) ; speed in m/sec</div><div><br></div>
<div>
print(spd_plane(it))</div><div><br></div><div>end do ; end of time loop</div><div>;************************************************</div><div>; - Write wind speed in ascii file - </div><div>;************************************************</div>
<div><br></div><div><br></div><div><br></div><div><br></div><div> ; XLAT = lat ; (nlat)</div><div> ; YLON = lon ; (mlon)</div><div> ;windspeed = wind_speed ; (ntim,nlat,mlon)</div>
<div><br></div><div> ;dimwindspeed = dimsizes(windspeed)</div><div> ;ntim = dimwindspeed(0)</div><div> ;nXLAT = dimwindspeed(1)</div><div> ;mYLON = dimwindspeed(2)</div><div><br></div><div> ;npts = nXLAT*mYLON ; total number of grid points</div>
<div><br></div><div> ;fName = "foo.txt"</div><div> ;data = new( npts, "string")</div><div><br></div><div> ;npt = -1</div><div><br></div><div> ;do nl=0,nXLAT-1 </div><div> ; do ml=0,mYLON-1</div>
<div><br></div><div> ; npt = npt + 1 </div><div> ; data(npt) = sprinti("%0.5i", (npt+1) ) </div><div> ; data(npt) = data(npt) + sprintf("%7.1f ",XLAT(nl))</div><div> ;data(npt) = data(npt) + sprintf("%7.1f ",YLON(ml))</div>
<div><br></div><div> ;do nt=0,ntim-1</div><div> ; data(npt) = data(npt) + sprintf("%10.3f ", windspeed(nt,nl,ml))</div><div> ;end do</div><div> ;end do</div><div> ;end do</div><div><br></div>
<div> ;asciiwrite (fName , data)</div><div><br></div><div><br></div><div>; -------------- END OF NCL SCRIPT ----------------</div><div><br></div><div>end</div><div><br></div><div><b><u>Error Message</u></b></div><div><br>
</div><div><div>fatal:Subscript out of range, error in subscript #0</div><div>fatal:An error occurred reading dims</div><div>fatal:["Execute.c":8567]:Execute: Error occurred at or near line 223 in file $NCARG_ROOT/lib/ncarg/nclscripts/wrf/WRFUserARW.ncl</div>
<div><br></div><div>fatal:["Execute.c":8567]:Execute: Error occurred at or near line 84 in file SpLev.ncl</div></div><div><br></div><div><br></div></div></div></div>
</div></blockquote></div></div><blockquote type="cite"><div><span>_______________________________________________</span><br><span>Wrf-users mailing list</span><br><span><a href="mailto:Wrf-users@ucar.edu" target="_blank">Wrf-users@ucar.edu</a></span><br>
<span><a href="http://mailman.ucar.edu/mailman/listinfo/wrf-users" target="_blank">http://mailman.ucar.edu/mailman/listinfo/wrf-users</a></span><br></div></blockquote></div></blockquote></div><br></div>
</div></div></div><br></div>