<html><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div>Sorry, I just noticed an error in how I formatted the numbers in my earlier reply. A corrected version of the script is below...</div><div><br></div><div>-Matthias</div><div><br></div><div>====== revised message ===========</div><div><br></div><div>To write out multiple columns of data, you would use something like this:</div><div><br></div><div><div>*write a header in the file</div><div>fname="output_file.csv"</div><div>rc = write(fname,"date,u,v,temp,hgt")</div><div><div>if (rc != 0)</div><div>&nbsp;&nbsp;say "Error writing to file " fname</div><div>&nbsp;&nbsp;return</div><div>endif</div><div><br></div></div><div>*find the time range set by the user</div><div><div>*you may need to adjust this depending on your version of grads</div><div>'query dims'</div><div><div>tmin = subwrd(result, 51)</div><div>tmax = subwrd(result, 53)</div><div><br></div><div><div>*loop through all times, writing out data</div><div>timestep = tmin</div><div>while timestep &lt;= tmax</div><div>&nbsp;&nbsp;'set t ' timestep</div><div><div>&nbsp;&nbsp;'q time'</div><div>&nbsp;&nbsp;datetime = subwrd(result, 3)</div><div><br></div><div>* get the data for this time step</div><div><div>* note: change all the "?_var" variables to match whatever is in your grib file</div><div><br></div></div><div>&nbsp;&nbsp;'d u_var'</div><div>&nbsp;&nbsp;u = subwrd(result,4)</div><div><br></div><div>&nbsp;&nbsp;'d v_var'</div><div>&nbsp;&nbsp;v = subwrd(result,4)</div><div><br></div><div><div>&nbsp;&nbsp;'d temp_var'</div><div>&nbsp;&nbsp;temp = subwrd(result,4)</div><div><br></div><div><div>&nbsp;&nbsp;'d hgt_var'</div><div>&nbsp;&nbsp;hgt = subwrd(result,4)</div><div><br></div></div></div><div>* compose a line of text and write it to the output file</div><div>&nbsp;&nbsp;outstr = datetime ',' u ',' v ',' temp ',' hgt</div><div>&nbsp;&nbsp;rc =&nbsp;write(fname,outstr,append)</div><div>&nbsp;&nbsp;if (rc != 0)</div><div>&nbsp;&nbsp; &nbsp;say "Error writing to file " fname</div><div>&nbsp;&nbsp; &nbsp;break</div><div>&nbsp;&nbsp;endif</div><div>&nbsp;&nbsp;timestep = timestep + 1</div><div><div>endwhile</div><div><div>close(fname)</div><div>'set t ' tmin ' ' tmax</div><div><br></div></div></div></div></div></div></div><div>This script assumes you have previously selected a range of times with "set time" or "set t", and a single point with set lat, set lon, set x, set y, set z, etc. It can be extended pretty easily to loop across a range of x-y-z values if you want to do that instead.&nbsp;</div><div><br></div><div>If you are working with remote data (e.g., in gradsdods), it will probably be much faster if you use the "define" statement to read in large chunks of data at once, and then loop through them locally. In that case, you may also want to use 'q defval' to read numeric values directly from the defined variable into your script variables, and then use math_format() to convert the numeric values to text before composing the output string.</div><div><br></div><div>This creates a text file with commas between the values. I'm sure there's some way to specify tabs instead, but I'm not sure how.</div><div><br></div><div>I hope that helps.</div><div><br></div><div>Matthias</div></div><div><br><div><div>On Nov 5, 2009, at 9:13 AM, Javier Peña wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><div id="result_box" dir="ltr">Thank you very much Matthias: <br><br>It works! <br>I have another question . I wish if I&nbsp;get several variables, these appear in columns in the text file. Is this possible?</div> <div dir="ltr">I mean, I'd like a file.txt in which appears something similar to:</div> <div dir="ltr">&nbsp;</div> <div dir="ltr">&nbsp;&nbsp; Fecha&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; u&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; v&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;temp&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hgt<br>00Z01JUN2009&nbsp;&nbsp; -1.9065&nbsp;&nbsp;&nbsp; &nbsp;0.225504&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;288.06&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 9385.06<br>12Z01JUN2009&nbsp;&nbsp; 1.02317&nbsp;&nbsp;&nbsp; -1.33784&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 295.778&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 9385.06<br> 00Z02JUN2009&nbsp;&nbsp; -1.15094&nbsp;&nbsp; -3.90728&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 290.077&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 9385.06<br>12Z02JUN2009&nbsp;&nbsp; -0.65753&nbsp; -1.19813&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 295.153&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 9385.06<br></div> <div>Thanks. Best&nbsp; Regards:</div> <div>&nbsp;</div> <div>Javier Peña<br><br></div> <div class="gmail_quote">2009/11/4 Matthias Fripp <span dir="ltr">&lt;<a href="mailto:matthias.fripp@eci.ox.ac.uk">matthias.fripp@eci.ox.ac.uk</a>&gt;</span><br> <blockquote class="gmail_quote" style="padding-left: 1ex; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0.8ex; border-left-color: rgb(204, 204, 204); border-left-width: 1px; border-left-style: solid; position: static; z-index: auto; "> <div style="WORD-WRAP: break-word">you could use something like this: <div><br></div> <div> <div>'q time'</div> <div>datetime = subwrd(result, 3)</div> <div><br></div> <div>That will give you the date and time in grads format, e.g., "00Z01JAN1979".&nbsp;If you want, you could go further, with something like this:</div> <div><br></div> <div> <div>formatted_time = substr(datetime, 9, 4) '/' substr(datetime, 6, 3) '/' substr(datetime, 4, 2) ' ' substr(datetime, 1, 2) ':00'</div> <div>say "formatted time: " formatted_time</div> <div><br></div> <div>Unfortunately, I don't know any way to convert "JAN" to "01" within grads. So you may need to use awk (or similar) to convert the dates after-the-fact, in which case you might as well just write the datetime directly to the file, and then convert that later.</div> <div><br></div><font color="#888888"> <div>Matthias</div> <div><br></div></font></div> <div> <div></div> <div class="h5"> <div> <div>On Nov 4, 2009, at 4:26 PM, Javier Peña wrote:</div><br> <blockquote type="cite"> <div dir="ltr">Hello everyone, <br>&nbsp; <br>I have a netcdf file from&nbsp;which I can obtain any of the variables with the following script (for example to get u wind component): <br>&nbsp; <br>* takes the values of the component or in a column <br> 'reinit' <br>'sdfopen <a href="http://prueba.nc/" target="_blank">prueba.nc</a>' <br>'set lat 41' <br>'set lon 359' <br>'set lev 1' <br>to = 1 <br>* Write the variable to a file <br> * 'set gxout vector' <br>* 'set fwrite z: \ dummy.dat' <br>while (to &lt;5) <br>'set t' to <br>tmp ='' <br>tmp = subwrd (rerult, 4) <br>'d u', tmp = tmp%''subwrd (result, 4) <br> rrc = write ( 'c: \ prueba_c \ pruebas_archivo_ecmwf \ new_u.txt', tmp) <br>to = to + 1 <br>endwhile <br><br>but my question is, can I get the date which belongs each variable value? That is, if I have values for the velocity component u: 2.3, 3.4, 5.6, 3.9, can I get a. txt what next? <br> &nbsp; <br>Date&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; U (m / s) <br>2009/12/06 00:00&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 2.3 <br>2009/12/06 06:00&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 3.4 <br>2009/12/06 18:00&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;5.6 <br>2009/12/07 00:00&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 3.9</div> <div><br clear="all">Thank you very much. Best regards.</div> <div><br>Javier Peña</div></blockquote></div><br></div></div></div></div></blockquote></div><br><br clear="all"><br>-- <br>Javier Peña Álvarez<br>Energías Renovables<br><a href="mailto:javier.p@ereda.com">javier.p@ereda.com</a><br> EREDA<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Tel: &nbsp;+34 91 5014755<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Fax: +34 91 5014756<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;c/ Téllez 26, 28007 MADRID<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<a href="http://www.ereda.com">www.ereda.com</a><br></blockquote></div><br></div></body></html>