Showing posts with label statistics. Show all posts
Showing posts with label statistics. Show all posts

Wednesday, February 4, 2015

Reference Table of Common Metadata and Statistical Queries

Sorry everyone!  No pretty pictures to go along with this post!

In using GrADS, you likely want to access metadata from the file, for example: The calendar date of your current time step, or your current latitude range.  This post will serve as a reference table that includes code segments for many common metadata queries (most of which can be used to add more data to your plot).  In addition, it will include code segments that use 'set gxout stat' to query basis statistics about your data.  The code within the table can just be copy pasted into your scripts, you will then just have to follow the naming convention that I came up with.

The focus will be on the following queries:
  • 'q dims'
  • 'q file'
  • 'q gxinfo'
  • 'q xinfo'
  • 'q time'
  • 'q ctlinfo'
If you are unfamiliar with these commands, learn more here.


Result
Code
Get time step of file (dt)'q ctlinfo'
tdef=sublin(result,7);dt=subwrd(tdef,5)
Get longitude resolution of file (dlon)'q ctlinfo'
dlon=sublin(result,4);dlon=subwrd(dlon,5)
Get latitude resolution of file (dlat)'q ctlinfo'
dlat=sublin(result,5);dlat=subwrd(dlat,5)
Get Number of time steps in file'q file'
t=sublin(result,5);t=subwrd(t,12)
Get Number of x-points in file'q file'
xpts=sublin(result,5);xpts=subwrd(xpts,3)
Get Number of y-points in file'q file'
ypts=sublin(result,5);ypts=subwrd(ypts,6)
Get Number of z-points in file'q file'
zpts=sublin(result,5);zpts=subwrd(zpts,9)
Get current longitude range
(if longitude is varying)
'q dims'
lons=sublin(result,2)
lon1=subwrd(lons,6)
lon2=subwrd(lons,8)
Get current latitude range
(if latitude is varying)
'q dims'
lats=sublin(result,3)
lat1=subwrd(lats,6)
lat2=subwrd(lats,8)
Get page coordinates of a specific lat/lon pair'q w2xy 'lon' 'lat
xpos=subwrd(result,3)
ypos=subwrd(result,6)
Get boundaries of plot in page units'q gxinfo'
xlims=sublin(result,3);ylims=sublin(result,4)
xmin=subwrd(xlims,4);xmax=subwrd(xlims,6)
ymin=subwrd(ylims,4);ymax=subwrd(ylims,6)
Get current time range
(Including day of the week)
'q time'
time1=subwrd(result,3);weekday1=subwrd(result,6)
time2=subwrd(result,5);weekday2=subwrd(result,7)
Get graphics window size in pixels'q xinfo'
xpx=sublin(result,4);xpx=subwrd(xpx,4)
ypx=sublin(result,5);ypx=subwrd(ypx,4)

Below is a table showing how to use the 'set gxout stat' option to get some basic statistical properties of your variables.  Before any of these are used, you need to first to change your graphics out option to stat: 'set gxout stat' and then display your variable.  Example below:

     'set gxout stat'
     'd rh2m'

This will print out a list of data onto to GrADS text window, then the following table will be useful for pulling out various statistics

Stat
Code
N (data points) N=sublin(result,11);N=subwrd(N,5)
Mean mean=sublin(result,12);mean=subwrd(mean,2)
Standard Deviationstd=sublin(result,13);std=subwrd(std,2)
Maxmax=sublin(result,9);max=subwrd(max,5)
Minmin=sublin(result,9);min=subwrd(min,4)

Saturday, October 5, 2013

Script: Statpack; Perform basic statistical analysis on user-input arrays.





This is another one of those scrips that extends the flexibility of GrADS beyond it's intended purpose.  As usual, these analyses might better be performed using higher level data analysis software (e.g., python or matlab).  However, this script might be useful for generating quick statistics on data sets that are in GrADS format without the intermediary step of saving data to a file to read into another program.


This script is not a script that you call, but instead a library of functions that you include in your script.  Note: you must include all functions as some of these functions rely on each other to operate.

In order to use this script, your data arrays must in in the format of strings with your array elements separated by spaces.  e.g., array='1 2 3 4 5 6 7 8 9 10'.

Each function requires different inputs.  A list of the included functions is below, as well is in a brief help page at the top of the script.

Function list
  • mean(arr): Returns mean of input array "arr"
  • stdev(arr): Returns Standard Deviation of input array "arr"
  • sort(arr): Returns sorted array (lowest to highest) of array "arr"
  • rank(arr): Returns an array size equal to array "arr" ranging from 1 to size(arr)
  • percentile(arr,p): Returns the data percentile (p) of array "arr"
  • size(arr): Returns the number of elements in array "arr"
  • max(arr): Returns the maximum value in array "arr"
  • min(arr): Returns the minimum value in array "arr"
  • correlation(arr1,arr2): Returns the correlation coefficient "r" between "arr1" and "arr2"
  • regression(arr1,arr2): Returns the regression coefficient between "arr1" and "arr2


Example Use: 
  •  Calculate median of array: data='9 7 8 6 5 4 3 2 1' 
           median=percentile(data,0.5)
           say median
 
           Outputs "5" to screen

  • Sort array: data='9 7 8 6 5 4 3 2 1' 
          sorted=sort(data)
          say sorted
  
          Outputs "1 2 3 4 5 6 7 8 9" to screen


Download statpack.gs