<div>Hi Matthias:</div>
<div> </div>
<div>Thank you very much for your help ! I've gotten it</div>
<div> </div>
<div>Best Regards:</div>
<div> </div>
<div>Javier Peña<br><br></div>
<div class="gmail_quote">2009/11/5 Matthias Fripp <span dir="ltr"><<a href="mailto:matthias.fripp@eci.ox.ac.uk">matthias.fripp@eci.ox.ac.uk</a>></span><br>
<blockquote class="gmail_quote" style="PADDING-LEFT: 1ex; MARGIN: 0px 0px 0px 0.8ex; BORDER-LEFT: #ccc 1px solid">
<div style="WORD-WRAP: break-word">
<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 class="im">
<div><br></div>
<div>To write out multiple columns of data, you would use something like this:</div>
<div><br></div></div>
<div>
<div class="im">
<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> say "Error writing to file " fname</div>
<div> return</div>
<div>endif</div>
<div><br></div></div>
<div>*find the time range set by the user</div></div>
<div>
<div class="im">
<div>*you may need to adjust this depending on your version of grads</div>
<div>'query dims'</div></div>
<div>
<div class="im">
<div>tmin = subwrd(result, 51)</div>
<div>tmax = subwrd(result, 53)</div>
<div><br></div></div>
<div>
<div class="im">
<div>*loop through all times, writing out data</div>
<div>timestep = tmin</div>
<div>while timestep <= tmax</div>
<div> 'set t ' timestep</div></div>
<div>
<div class="im">
<div> 'q time'</div>
<div> datetime = subwrd(result, 3)</div>
<div><br></div></div>
<div class="im">
<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> 'd u_var'</div>
<div> u = subwrd(result,4)</div>
<div><br></div></div>
<div class="im">
<div> 'd v_var'</div>
<div> v = subwrd(result,4)</div>
<div><br></div></div>
<div>
<div class="im">
<div> 'd temp_var'</div>
<div> temp = subwrd(result,4)</div>
<div><br></div></div>
<div class="im">
<div>
<div> 'd hgt_var'</div>
<div> hgt = subwrd(result,4)</div>
<div><br></div></div></div></div>
<div class="im">
<div>* compose a line of text and write it to the output file</div>
<div> outstr = datetime ',' u ',' v ',' temp ',' hgt</div>
<div> rc = write(fname,outstr,append)</div>
<div> if (rc != 0)</div>
<div> say "Error writing to file " fname</div>
<div> break</div>
<div> endif</div>
<div> 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>
<div class="im">
<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. </div>
<div><br></div></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 class="im">
<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>
<div><br>
<div>
<div class="im">
<div>On Nov 5, 2009, at 9:13 AM, Javier Peña wrote:</div><br></div>
<div>
<div></div>
<div class="h5">
<blockquote type="cite">
<div dir="ltr">Thank you very much Matthias: <br><br>It works! <br>I have another question . I wish if I 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"> </div>
<div dir="ltr"> Fecha u v temp hgt<br>00Z01JUN2009 -1.9065 0.225504 288.06 9385.06<br>12Z01JUN2009 1.02317 -1.33784 295.778 9385.06<br>
00Z02JUN2009 -1.15094 -3.90728 290.077 9385.06<br>12Z02JUN2009 -0.65753 -1.19813 295.153 9385.06<br></div>
<div>Thanks. Best Regards:</div>
<div> </div>
<div>Javier Peña<br><br></div>
<div class="gmail_quote">2009/11/4 Matthias Fripp <span dir="ltr"><<a href="mailto:matthias.fripp@eci.ox.ac.uk" target="_blank">matthias.fripp@eci.ox.ac.uk</a>></span><br>
<blockquote class="gmail_quote" style="PADDING-LEFT: 1ex; MARGIN: 0px 0px 0px 0.8ex; BORDER-LEFT: rgb(204,204,204) 1px solid">
<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". 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>
<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> <br>I have a netcdf file from which I can obtain any of the variables with the following script (for example to get u wind component): <br> <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 <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>
<br>Date U (m / s) <br>2009/12/06 00:00 2.3 <br>2009/12/06 06:00 3.4 <br>2009/12/06 18:00 5.6 <br>2009/12/07 00:00 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" target="_blank">javier.p@ereda.com</a><br>
EREDA<br><br> Tel: +34 91 5014755<br> Fax: +34 91 5014756<br> c/ Téllez 26, 28007 MADRID<br> <a href="http://www.ereda.com/" target="_blank">www.ereda.com</a><br>
</blockquote></div></div></div><br></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> Tel: +34 91 5014755<br>
Fax: +34 91 5014756<br> c/ Téllez 26, 28007 MADRID<br> <a href="http://www.ereda.com">www.ereda.com</a><br>