U With Expected At Least One Variable Assignment

Reading Assignment
An Introduction to Statistical Methods and Data Analysis, (see Course Schedule).
Chisquare Test of Independence
How to test the independence of two categorical variables? It will be done using the Chisquare test of independence. As will all prior statistical tests we need to define null and alternative hypotheses. Also, as we have learned, the null hypothesis is what is assumed to be true until we have evidence to go against it. In this lesson, we are interested in researching if two categorical variables are related or associated (i.e. dependent). Therefore, until we have evidence to suggest that they are we must assume that they are not. This is the motivation behind the hypothesis for the Chisquare Test of Independence:
\(H_0\): In the population, the two categorical variables are independent.
\(H_a\): In the population, two categorical variables are dependent.
[NOTE: The are several ways to phrase these hypotheses. Instead of using the words "independent" and "dependent" one could say "there is no relationship between the two categorical variables" versus "there is a relationship between the two categorical variables". The important part is that the null hypothesis refers to the two categorical variables not being related while the alternative is trying to show that they are related.]
Once we have gathered our data we summarize the data in the twoway contingency table. This table represents the observed counts and is called the Observed Counts Table or simply the Observed Table. The contingency table on the introduction page to this lesson represented the observed counts of the party affiliation and opinion for those surveyed. The question becomes, "How would this table look if the two variables were not related?" That is, under the null hypothesis that the two variables are independent, what would we expect to find in our data if the two variables (e.g. Party Affiliation and Opinion) were not related? We need to find what is called the Expected Counts Table or simply the Expected Table. This table displays what the counts would be for our sample data if there were no association between the variables.
Finding Expected Counts from Observed Counts
Once we have the observed counts we need to compute the expected counts under the null hypothesis that the two categorical variables are independent. This is done using the marginal totals and overall total to compute expected counts for each cell of the table. In words, to find the expected count for each cell in the table we take multiply the marginal row and column totals for that cell and divide by the overall total. Formulaically for each cell this is:
\[E=\frac{row\ total \times column\ total}{sample\ size} \]
To demonstrate, we will use the Party Affiliation and Opinion on Tax Reform example.
Observed Table:
favor indifferent opposed total democrat 138 83 64 285 republican 64 67 84 215 total 202 150 148 500 Calculating Expected Counts from Observed Counts
favor indifferent opposed total democrat \[\frac{285\cdot 202}{500}=115.14\] \[\frac{285\cdot 150}{500}=85.5\] \[\frac{285\cdot 148}{500}=84.36\] 285 republican \[\frac{215\cdot 202}{500}=86.86\] \[\frac{215\cdot 150}{500}=64.5\] \[\frac{215\cdot 148}{500}=63.64\] 215 total 202 150 148 500 To better understand what these expected counts represent, first recall that the expected counts table is designed to reflect what the sample data counts would be if the two variables were independent. Taking what we know of independent events, we would be saying that the sample counts should show a similarity on opinions of tax reform between democrats and republicans. If you find the proportion of each cell by taking a cell's expected count divided by it's row total, you will discover that in the expected table each opinion proportion is the same for democrats and repulicans. That is, from the expected counts, 0.404 of the democrats and 0.404 of the republicans favor the bill; 0.3 of the democrats and 0.3 of the republicans are indifferent; and 0.296 of the democrats and 0.296 of the republicans are opposed.
The statistical question becomes, "Are the observed counts so different from the expected counts that we can conclude a relationship between the two variables?" To conduct this test we compute a Chisquare test statistic where we compare each cell's observed count to it's respective expected count. This Chisquare test statistic is calculated as follows:
\[\chi^{2*}=\sum (O_iE_i)^2/E_i \]
As we have done with other statistical tests, we make our decision by either comparing the value of the test statistic to a critical value (rejection region approach), or by finding the probability of getting this test statistic value or one more extreme (pvalue approach). The critical value for our Chisquare test is \(\chi^2_{\alpha}\) with degree of freedom = (r  1) (c  1), while the pvalue is found by \(P(\chi^2>\chi^{2*})\) with degrees of freedom = (r  1)(c  1)
Calculating the test statistic by hand:
\[\chi^{2*}=\frac{(138115.14)^2}{115.14}+\frac{(8385.50)^2}{85.50}+\frac{(6484.36)^2}{84.36}+\frac{(6486.86)^2}{86.86}+\frac{(6764.50)^2}{64.50}+\frac{(8463.64)^2}{63.64}=22.152\]
with degrees for freedom equal to (2  1)(3  1) = 2.
Example: Political Affiliation and Opinion and Tax Reform
Let's apply the Chisquare Test of Independence to our example where we have as random sample of 500 U.S. adults who are questioned regarding their political affiliation and opinion on a tax reform bill. We will test if the political affiliation and their opinion on a tax reform bill are dependent at a 5% level of significance. The observed contingency table (political_affiliation.txt) is given below. Also we often want to include each cell's expected count and contribution to the Chisquare test statistic which can be done by the software
favor  indifferent  opposed  total  
democrat  138  83  64  285 
republican  64  67  84  215 
total  202  150  148  500 
Minitab Steps for Chisquare Test for Independence
 The command in Minitab is: Stat > Tables > ChiSquare Test for Association
 If you have summarized (i.e. observed count) from the drop down box "Summarized data in twoway table". Select and enter the columns that contain the observed counts. Otherwise, if you have the raw data use "Raw data (categorical variables). Note that if using the raw data your data will need to consist of two columns: one with the explantory variable data (goes in the 'row' field) and the response variable data (goes in the 'column' field).
 Labeling (Optional) When using the summarized data you can label the rows and columns if you have the variable labels in columns of the worksheet. For example if we have a column with the two political party affiliations and a column with the three opinion choices we could use these columns to label the output.
 Click the Statistics tab. Keep checked the four boxes already checked, but also check the box for "Each cell's contribution to the chisquare. Click OK.
 Click OK.
SPECIAL NOTE: If you have the observed counts in a table you can copy/paste them into Minitab. For instance you can copy the entire observed counts table (excluding the totals!) for our example and paste these into Mintiab starting with the first empty cell of a column.
Cell Contents: Count, Expected count, Contribution to Chisquare
favor
indiffer
opposed
All
1
138
83
64
285
115.14
85.50
84.36
4.5836 0.0731 4.9138 2
64
67
84
215
86.86
64.50
63.64
6.0163 0.0969 6.5317 All
202
150
148
500
Pearson ChiSq = 4.539 + 0.073 + 4.914 + 6.016 + 0.097 + 6.514 = 22.152 DF = 2, PValue = 0.000
Likelihood Ratio ChiSquare (IGNORE THIS Ignore the Fisher's pvalue! The pvalue highlighted above is calculated using the methods we learned in this lesson. More specifically, the chisquare we learned is referred to as the Pearson Chisquare. The Fisher's test uses a different method than what we explained in this lesson to calculate a test statistic and pvalue. This method incorporates a log of the ratio of observed to expected values. Just a different technique that is more complicated to do byhand. Minitab automaticallly includes both results in its output.)
The Chisquare test statistic is 22.152 and calculated by summing all the individual cell's Chisquare contributions:
4.584 + 0.073 + 4.914 + 6.016 + 0.097 + 6.532 = 22.152
The pvalue is found by \(P(\chi^2>22.152)\) with degrees of freedom = (21)(31) = 2. Minitab calculates this pvalue to be less than 0.001 and reports it as 0.000. Given this pvalue of 0.000 is less than alpha of 0.05, we reject the null hypothesis that political affiliation and their opinion on a tax reform bill are independent. We conclude that they are dependent, that there is an association between the two variables.
Using Minitab
Click on this link to follow along with how to perform a ChiSquare test of independence from summarized data in Minitab.
Click on the 'Minitab Movie' icon to display a walk through of 'Using Minitab to Perform a ChiSquare Test of Independence from Summarized Data'.
Condition for Using the Chisquare Test
Exercise caution when there are small expected counts. Minitab will give a count of the number of cells that have expected frequencies less than five. Some statisticians hesitate to use the chisquare test if more than 20% of the cells have expected frequencies below five, especially if the pvalue is small and these cells give a large contribution to the total chisquare value.
Example: Tire Quality
The operations manager of a company that manufactures tires wants to determine whether there are any differences in the quality of workmanship among the three daily shifts. She randomly selects 496 tires and carefully inspects them. Each tire is either classified as perfect, satisfactory, or defective, and the shift that produced it is also recorded. The two categorical variables of interest are: shift and condition of the tire produced. The data (shift_quality.txt) can be summarized by the accompanying twoway table. Do these data provide sufficient evidence at the 5% significance level to infer that there are differences in quality among the three shifts?
Perfect Satisfactory Defective Total Shift 1 106 124 1 231 Shift 2 67 85 1 153 Shift 3 37 72 3 112 Total 210 281 5 496 Minitab output:
Chisquare Test
Expected counts are printed below observed counts
C1
C2
C3
Total
1
106
124
1
231
97.80
130.87
2.33
2
67
85
1
153
64.78
86.68
1.54
3
37
72
3
112
47.42
63.45
1.13
Total
210
281
5
496
ChiSq = 8.647 DF = 4, PValue = 0.071
Note: 3 cells with expected counts less than 5.0.
In the above example, we don't have a significant result at 5% significance level since the pvalue (0.071) is greater than 0.05. Even if we did have a significant result, we still cannot trust the result, because there are 3 (33.3% of) cells with expected counts < 5.0.
CAUTION: Sometimes researchers will classify quantitative data into categories (e.g. take height measurements and categorize as 'below average', 'average', and 'above average'. Doing so results in a loss of information  one cannot do the reverse of taking the categories and reproducing the raw quantitative measurements. Instead of categorizing, the data should be analyzed using quantitative methods.
The 2 × 2 Table: Chisquare Test Analagous to ZTest of Two Independent Proportions
Say we have study of two categorical variables each with only two levels. One of the response levels is considered the "success" response and the other the "failure" response. A general 2 × 2 table of the observed counts would be as follows:
Success  Failure  Total  
Group 1  A  B  A + B 
Group 2  C  D  C + D 
The observed counts in this table represent the following proportions:
Success  Failure  Total  
Group 1  \(\frac{A}{A+B}=\hat{p_1}\)  \(1\hat{p_1}\)  A + B 
Group 2  \(\frac{C}{C+D}=\hat{p_2}\)  \(1\hat{p_2}\)  C + D 
Recall from our Ztest of two proportions that our null hypothesis is that the two population proportions, \(p_1\) and \(p_2\), were assummed equal while our alternative hypothesis was that they were not equal. This null hypothesis would be analagous to the two groups being independent. Also, if the two success proportions are equal then the two failure proportions would also be equal. Note as well that with our Ztest the conditions were that the number of successes and failures for each group was at least 5. That equates to the Chisquare conditions that all expected cells in a 2 × 2 table be at least 5. (Remember at least 80% of all cells need an expected count of at least 5. With 80% of 4 equal to 3.2 this means all 4 cells must satisfy the condition).
When we run a Chisquare test of independence on a 2 × 2 table, the resulting Chsquare test statistic would be equal to the square of the Ztest statistic from the Ztest of two independent proportions. Consider the following example where we form a 2 × 2 for the Political Party and Opinion by only considering the Favor and Opposed responses:
favor  oppose  Total  
democrat  138  64  202 
republican  64  84  148 
Total  202  148  350 
The Chisquare test produces a test statistic of 22.00 with pvalue 0.000
The Ztest comparing the two sample proportions of \(\hat{p_d}=\frac{138}{202}=0.683\) minus \(\hat{p_r}=\frac{64}{148}=0.432\) results in a Ztest statistic of 4.69 with pvalue of 0.000. If we square the Ztest statistic we get 4.69^{2} = 21.99 or 22.00 with rounding error.
So which test is better when we have a 2 × 2 table? The are the same from statistical decision standpoint: A signficant Chisquare test would be similar to concluding a difference in the two proportions. The benefit of the twoproportion test is that we can calculate a confidence interval for this difference to generate an estimate of just how large the difference might be.
Common error messages in NCL
 Subscript out of range, error in subscript #1
 Number of subscripts on righthandside do not match number of dimensions of variable: (4), Subscripts used: (3)
 Assignment type mismatch, right hand side can't be coerced to type of left hand side
 syntax error: possibly an undefined procedure
 syntax error: function fspan expects 3 arguments, got 2
 syntax error: line 1
 Dimension sizes of left hand side and right hand side of assignment do not match
 Number of dimensions on right hand side do not match number of dimension in left hand side
 Dimension size mismatch, dimension (0) of left hand side reference does not have the same size as the right hand side reference after subscripting.
 The type of missing value could not be converted to type of variable.
 The result of the conditional expression yields a missing value. NCL can not determine branch, see the ismissing function.
 Variable (x1) is undefined
 Attempt to reference attribute (FillValue) which is undefined
 There are 1 floats larger than INT_MAX, which has been flagged missing
 Argument 0 of the current function or procedure was coerced to the appropriate type and thus will not change if the function or procedure modifies its value
 tofloat: A bad value was passed to (string) tofloat, input strings must contain numeric digits, replacing with missing value
 Minus: Dimension size, for dimension number 0, of operands does not match, can't continue
 Minus: Number of dimensions do not match, can't continue
 _NclBuildArray: each element of a literal array must have the same dimension sizes, at least one item doesn't
 scalar field is constant; no contour lines will appear
 ContourPlotDraw: Workspace reallocation would exceed maximum size
 _NhlCreateSplineCoordApprox: Attempt to create spline approximation for X axis failed: consider adjusting trXTensionF value
 Argument list too long
 Unable to load System Resource File
 ContourPlotSetValues: Data values out of range of levels set by EXPLICITLEVELS mode
 ARSCAM/ARPRAM ALGORITHM FAILURE
 NhlCvtStringToEnum: Unable to convert string
 NhlDraw: cannot draw Plot Member
 MDRGSF/MDRGOF  ERROR OPENING RANGS/GSHHS CAT FILE
 TransformPostDraw: tfPolyDrawList element 0 is invalid
 xxYYYYZZZZ is not a valid resource in nnnn at this time
 NclMalloc failed
Subscript out of range, error in subscript #1
Sample code that causes the error:
;Using bad index subscripting x = random_uniform(100,100,(/10,20,30/)) print(x(5,20,5)) ; index '20' is invalid ;Using bad coordinate variable subscripting ntim = 5 nlat = 10 nlon = 20 y = random_uniform(0,.100,(/ntim,nlat,nlon/)) time = ispan(1,ntim,1) lat = fspan(5,5,nlat) lon = fspan(10,30,nlon) y!0 = "time" y!1 = "lat" y!2 = "lon" y&time = time y&lat = lat y&lon = lon ys = y(:,{20:10},{10:20}) ; range {20:10} is invalid for "lat" coord varCause: There are two possible causes: 1) subscripting an array using an index that is outofrange of the size of your array, or 2) subscripting an array using coordinate values that are outofrange of the coordinate variables. Index subscript numbers start at 0 and go from left to right, so subscript "#1" refers to the second dimension from the left.
Fix: Check your subscript indexes to make sure they are in the range of your array size. Use print and printVarSummary.
Number of subscripts on righthandside do not match number of dimensions of variable: (4), Subscripts used: (3)
Sample code that causes the error:
Cause: Subscripting an array using the wrong dimensionality.
Fix: Check and fix your subscript syntax. Use print and printVarSummary.
Assignment type mismatch, right hand side can't be coerced to type of left hand side
Sample code that causes the error:
Cause: Reassigning a variable using a different type or dimensionality.
Fix: Use the reassignment operator, or delete the variable first.
x = 5 x := "Now I'm a string"or
x = 5 delete(x) x = "Now I'm a string"syntax error: possibly an undefined procedure
Sample code that causes the error:
Cause: Referencing a function or procedure that doesn't exist.
Fix: Check the spelling of function/procedure and whether you need to load another NCL script that defines it.
syntax error: function fspan expects 3 arguments, got 2
Sample code that causes the error:
Cause: Calling a function or procedure with the wrong number of arguments.
Fix: Check and correct your function or procedure arguments. Read the documentation for that particular function for help.
syntax error: line 1
Sample code that causes the error:
Cause: You have an unclosed code block, like a "begin" without an "end", an "if" without an "end if", or a "do" without an "end do".
Fix: Check for unclosed code blocks and close them.
Dimension sizes of left hand side and right hand side of assignment do not match
Sample code that causes the error:
Cause: Assigning a 1D array to another 1D array with a different number of elements.
Fix: Check and fix the array sizes on the left and/or right side of the "=", or use the reassignment operator.
x = (/2,5,9,3/) ; x has 4 elements y = (/8,7,0,2,3/) ; y has 5 elements x := y ; x now has 5 elementsNumber of dimensions on right hand side do not match number of dimension in left hand side
Sample code that causes the error:
Cause: Assigning one array to another when they don't have the same number of dimensions.
Fix: Use dimsizes and printVarSummary to check your array sizes, and then correct as necessary. You can also use the reassignment operator.
x = random_uniform(10,10,(/3,2,2/)) ; x is 3 x 2 x 2 y = random_uniform(20,20,(/4,5/)) ; y is 4 x 5 x := y ; x is now 4 x 5Dimension size mismatch, dimension (0) of left hand side reference does not have the same size as the right hand side reference after subscripting.
Sample code that causes the error:
Cause: Subsetting an array and trying to assign it a variable or another array subset that has a different number of elements.
Fix: Check and correct your array subscripts.
The type of missing value could not be converted to type of variable.
Sample code that causes the error:
x = (/1,2,3,4,5,999/) ; x is an array of integers x@_FillValue = 999. ; 999. is a float, which is a "higher" type than an integerCause: Setting a missing value attribute (_FillValue) using a value that is a higher type than the variable you are setting it for.
Fix: The value of the _FillValue attribute must be the same (or lower) type as the variable you are attaching it to. You can use one of the conversion functions, like toint, to force a lower type.
The result of the conditional expression yields a missing value. NCL can not determine branch, see the ismissing function.
Sample code that causes the error:
Cause: Using a missing value in an "if" statement or some other conditional statement.
Fix: If there's a chance your variable could be missing, then use ismissing to test for missing values.
x = new(1,float) if(.not.ismissing(x).and.x.gt.5) then print("x > 5") end ifVariable (x1) is undefined
Sample code that causes the error:
Cause: Referencing a variable that doesn't exist.
Fix: Doublecheck the spelling of the variable you are referencing.
Attempt to reference attribute (FillValue) which is undefined
Sample code that causes the error:
Cause: Referencing an attribute that doesn't exist.
Fix: Check and correct the spelling of the attribute you are trying to reference.
There are 1 floats larger than INT_MAX, which has been flagged missing
Sample code that causes the error:
Cause: Using "toint" to convert a float or double value that is larger than (2^311) to an integer.
Fix: Try using tolong, or use where to do something with these large values before converting them:
x = (/2^19,2^20,2^30,2^31/) i = toint(x) ; triggers the warning ;Solution 1: live with it; the last value will be set to the default int missing value print(i) ; last value is 2147483647 (integer missing) ;Solution 2: use "tolong" l = tolong(x) print(l) ; last value is 2147483648, the correct value ;Solution 3: convert large values to a smaller value, and perhaps set to missing x@_FillValue = 999 x = where(x.ge.(2^311),x@_FillValue,x) j = toint(x) print(j) ; This is effectively the same as solution #1, but ; no warnings are produced. Last value is 999 and ; is a missing value.Argument 0 of the current function or procedure was coerced to the appropriate type and thus will not change if the function or procedure modifies its value
Sample code that causes the error:
Cause: Calling a function or procedure with the wrong argument type. This generally happens if the function is expecting a string and you give it a numerical value.
Fix: Convert the argument to a string using the tostring function or concatenating it with an empty string ("") using the (+) operator.
x = (/8,1,5/) str = str_join("" + x,",")Tip: one way you can find out what line an NCL script is failing on is to comment out the "begin" and "end" statements (if any) of the main code, and then run the script with the x option:
ncl x 11.ncl This will cause every line to be echoed to the screen as it is executed.tofloat: A bad value was passed to (string) tofloat, input strings must contain numeric digits, replacing with missing value
str = "ab54cd" x = tofloat(str)Cause: Trying to convert a string that contains nonnumeric characters to a numerical value. This is common when reading in ASCII files that contain nonnumeric fields.
Fix: Print out the strings you are trying to convert, to verify that they actually have numbers in them. If they don't, then you either have an error in your code, or you'll need to fix these strings to contain only numerical values before converting them.
Minus: Dimension size, for dimension number 0, of operands does not match, can't continue
x = (/2,9,3/) ; x has 3 elements y = (/8,7,0,3/) ; y has 4 elements diff = xy ; errorCause: Trying to subtract two arrays of different lengths.
Fix: Check your arrays to make sure they are the same size. Use printVarSummary to help examine the arrays.
Minus: Number of dimensions do not match, can't continue
x = (/(/1,2,3,4/),(/5,6,7,8/),(/9,10,11,12/)/) ; 3 x 4 y = (/1,0,2,5/) ; 4 elements diff = x  yCause: Trying to subtract two arrays of different dimensionality.
Fix: If one array is a subset in size of another, then you can use the conform function to "conform" the smaller array to the size of the larger one.
x = (/(/1,2,3,4/),(/5,6,7,8/),(/9,10,11,12/)/) ; 3 x 4 y = (/1,0,2,5/) ; 4 elements diff = x  conform(x,y,1) ; y will be propagated to a 3 x 4 array_NclBuildArray: each element of a literal array must have the same dimension sizes, at least one item doesn't
This message comes from trying to use a "nested" array of dimension sizes inside something like a new statement.
xdims = (/10,20,30/) x = random_uniform(10,10,xdims) data = new((/2,xdims/),float) ; this will cause the errorThe third line of the above code expands to:
data = new((/2,(/10,20,30/)/),float) ; this will cause the error You cannot use "(/2,(/10,20,30/)/)" syntax in a new statement. It must be changed to "(/2,10,20,30/)": xdims = (/10,20,30/) xrank = dimsizes(xdims) x = random_uniform(10,10,xdims) data_dims = new(xrank+1,integer) data_dims(0) = 2 data_dims(1:) = xdims data = new(data_dims,float)scalar field is constant; no contour lines will appear
Sample code that causes the error:
Cause: You are trying to contour a data variable where every element is the same value. A plot will be created, but it will be a blank box with the text "CONSTANT FIELD  VALUE IS 2".
Fix: If you believe that your data variable should not be constant, then you need to doublecheck either the data that's being read in, or check all of your calculations. The printMinMax procedure is very useful in "debugging" your data to make sure it has the range of values that you expect.
Sometimes having a constant field is correct. We've seen this occasionally when users try to plot the first time step of simulated data. The first time step might be some an "initial condition" where the whole array is initialized to the same value.
If you simply want to skip the drawing of a plot that has a constant field, then use min and max:
x = new((/50,50/),float) x = 2. ; Setting the whole array to a single value, 2.0 wks = gsn_open_wks("x11","contour") res = True res@cnFillOn = True if(min(x).eq.max(x)) then print("Error: you have a constant field. Will not create a contour plot.") else plot = gsn_csm_contour(wks,x,res) end ifIf you want the plot to be drawn, but with a color, then use the cnConstFEnableFill resource as suggested:
x = new((/50,50/),float) x = 2. ; Setting the whole array to a single value, 2.0 wks = gsn_open_wks("x11","contour") res = True res@cnConstFEnableFill = True ; enable the whole field to be drawn in a single color res@cnFillOn = True plot = gsn_csm_contour(wks,x,res)ContourPlotDraw: Workspace reallocation would exceed maximum size
Cause: This is caused when trying to contour a very large data array.
Fix: There are two possible fixes.
A quickandeasy one to try is to switch to raster contouring:
res@cnFillMode = "RasterFill" res@cnRasterSmoothingOn = True ; optional, but may produce better resultsA second fix is more involved, and requires that you "bump up" your workspace size. Add the following code right after your "gsn_open_wks" call:
setvalues NhlGetWorkspaceObjectId() "wsMaximumSize" : 300000000 end setvaluesYou will likely need to adjust the "300000000" number. Start with something small and slowly increase it until the error message goes away.
You can permanently bump up the workspace size by setting this resource in your ~/.hluresfile:
*wsMaximumSize : 300000000_NhlCreateSplineCoordApprox: Attempt to create spline approximation for X axis failed: consider adjusting trXTensionF value
Cause: If you are plotting regional data over a map via one of the gsn_csm_xxxx_map routines, then the error might be caused by the code trying to add a longitude cyclic point. Try setting res@gsnAddCyclic = False.
If you are not plotting regional data over a map, this message can come from trying to plot data that has highly irregularlyspaced X and/or Y axis coordinate values.
Sample code that causes the error: The script below shows the problem and also the fix, which is to subscript the data over a region that is not so irregularlyspaced.
; ; Generate some dummy data with dummy 1D lat/lon coordinate arrays, ; with highly irregular spacing for the latitudes. ; lat = (/88,10,9,8,7,6,5,4,3,2,1,0,1,2,3,4,5,6,7,8,9,10,88/) lon = ispan(179,179,60) nlat = dimsizes(lat) nlon = dimsizes(lon) lat@units = "degrees_north" lon@units = "degrees_east" x = generate_2d_array(10, 10, 19.69, 15.82, 0, (/nlat,nlon/)) x!0 = "lat" x!1 = "lon" x&lat = lat x&lon = lon wks = gsn_open_wks("x11","spline_error") res = True res@cnFillOn = True res@tiMainString = "Contours are in wrong latitude location" plot = gsn_csm_contour_map(wks,x,res) ; Warning from "_NhlCreateSplineCoordApprox" res@tiMainString = "Contours are in correct latitude location" plot = gsn_csm_contour_map(wks,x({10:10},:),res) ; Using {10:10} subscripting for the ; latitudes removes the warning.Argument list too long
Sample code that causes the error:
diri = "/some/directory/path/" files = systemfunc("ls 1 " + diri + "sgp15swfcldgrid*")Cause: You are trying to list the contents of a directory that has hundreds of files, and reaching a UNIX limitation on the "ls" command.
Fix: Use one of these two methods:
files = systemfunc("cd "+diri+" ; echo sgp15swfcldgrid*  xargs ls") files = systemfunc("find " + diri + " name 'sgp15swfcldgrid*' print  xargs basename")Unable to load System Resource File
Sample code that causes the error:
This is an error message that can occur when you run any NCL script.
Cause: You either don't have your NCARG_ROOT environment variable set correctly or set at all. NCARG_ROOT needs to be set to the root location of where the NCL software is installed.
Fix: Check the setting of your NCARG_ROOT environment variable and other NCARG_XXXX variables.
On the UNIX command line, type:
which nclIf this returns (for example), "/usr/local/ncl/bin", then NCARG_ROOT should be set to /usr/local/ncl. See the section "Set the NCARG_ROOT environment variable" section on the NCL download page for more information.
ContourPlotSetValues: Data values out of range of levels set by EXPLICITLEVELS mode
Sample code that causes the error: a = addfile("$NCARG_ROOT/lib/ncarg/data/cdf/uv300.nc","r") u = a>U(1,:,:) ; read July zonal winds printMinMax(u,0) wks = gsn_open_wks("x11","contour") res = True res@cnLevelSelectionMode = "ExplicitLevels" ; set explicit contour levels res@cnLevels = (/ 50, 60, 70, 80, 90, 100, 110 /) plot = gsn_csm_contour_map(wks,u,res)Cause: You've chosen a set of contour levels that fall completely outside the range of your data. In the above case, "u" has a min/max of 15.27 and 41.07, but the levels specified are greater than the maximum value.
Fix: Fix the cnLevels setting so that at least some of the levels overlap with the actual range of the data you are trying to plot. Use the printMinMax procedure to help determine a good range of levels to use.
ARSCAM/ARPRAM ALGORITHM FAILURE
Sample code that causes the error:
None provided. This is an elusive problem that is hard to reproduce.
Cause: This error is usually caused by a precision error in our lowlevel contouring algorithm. It seems to occur when you have very "twisty" contours in small plot, like in a panel plot. It is more likely happen when you are drawing filled contours using the default "area fill" mode.
Fix: One thing you can try as a workaround is to fill the contours using "raster fill" mode instead:
res@cnFillMode = "RasterFill" ; Default is "AreaFill" In addition, you can smooth the raster contours, making them closer in appearance to areafilled contours: res@cnRasterSmoothingOn = TrueUnfortunately, if you need contours produced by the "AreaFill" mode, there's no instant fix for this. We found that if you tweak certain things, like the rotation of your plot (mpCenterLonF), the size of the plot (vpWidthF/vpHeightF), or the contour levels themselves, you might be able to get rid of this error message.
There's a more detailed and lowlevel description.
NhlCvtStringToEnum: Unable to convert string
NhlCvtStringToEnum: Unable to convert string
Sample code that causes the error:
This code shows three cases that will produce this error:
a = addfile("$NCARG_ROOT/lib/ncarg/data/cdf/uv300.nc","r") u = a>U(1,:,:) wks = gsn_open_wks("x11","contour") res = True res@cnFillOn = True res@mpProjection = "Ortographic" ; Should be "Orthographic" res@cnFillPalette = "Ranbow" ; Should be "Rainbow" res@cnLevelSelectionMode = "Explicit" ; Should be "ExplicitLevels" res@cnLevels = ispan(12,40,2) plot = gsn_csm_contour_map(wks,u,res)Cause: This fatal message occurs when you are setting a graphical resource that expects a predefined string value, and you give it something invalid. Here are the errors the above code will produce:
fatal:NhlCvtStringToEnum: Unable to convert string "Ortographic" to requested type warning:Error retrieving resource mpProjection from args  Ignoring Arg fatal:CvtStringToCmap:Unable to convert string "Ranbow" to ColorMap warning:Error retrieving resource cnFillPalette from args  Ignoring Arg fatal:NhlCvtStringToEnum: Unable to convert string "Explicit" to requested type warning:Error retrieving resource cnLevelSelectionMode from args  Ignoring ArgFix: Check the values you are using for the resource in question. Once you make the suggested fixes the code will work as expected.
res@mpProjection = "Orthographic" res@cnFillPalette = "Rainbow" res@cnLevelSelectionMode = "ExplicitLevels"NhlDraw: cannot draw Plot Member
Sample code that causes the error:
wks = gsn_open_wks("x11","test") res = True res@gsnDraw = False res@gsnFrame = False contour_line = gsn_csm_contour(wks,z1,res) res@cnFillOn = True contour_fill = gsn_csm_contour(wks,z2,res) overlay(contour_fill,contour_line) draw(contour_line) ; contour_line is now an "overlay" plot and can't be drawn individually. frame(wks) draw(contour_fill) frame(wks)Cause: This error occurs when you try to draw a plot that has been overlaid on another plot.
Fix: If you need to draw "contour_line" by itself, then you will need to do this before the overlay call:
wks = gsn_open_wks("x11","test") res = True res@gsnDraw = False res@gsnFrame = False contour_line = gsn_csm_contour(wks,z1,res) res@cnFillOn = True contour_fill = gsn_csm_contour(wks,z2,res) draw(contour_line) ; draw this before you overlay it frame(wks) overlay(contour_fill,contour_line) draw(contour_fill) frame(wks)MDRGSF/MDRGOF  ERROR OPENING RANGS/GSHHS CAT FILE
Full error:
fatal:MapRGDHDrawMapList: MDRGSF/MDRGOF  ERROR OPENING RANGS/GSHHS CAT FILE fatal:PlotManagerDraw: error in plot draw fatal:_NhlPlotManagerDraw: Draw errorSample code that causes the error:
wks = gsn_open_wks("x11","rangs_test") mpres = True mpres@gsnMaximize = True mpres@mpLimitMode = "LatLon" mpres@mpMinLonF = 15. mpres@mpMaxLonF = 15 mpres@mpMinLatF = 40. mpres@mpMaxLatF = 70. mpres@mpDataBaseVersion = "HighRes" mpres@pmTickMarkDisplayMode = "Always" map = gsn_csm_map(wks,res)Cause: You are trying to use the highresolution map database (res@mpDataBaseVersion = "HighRes"), but you haven't downloaded the necessary map database files.
Fix: you need to download a set of ten files that NCL reads to generate the highres coastlines. For information on how to get these files, go to the page on highresolution coastlines.
TransformPostDraw: tfPolyDrawList element 0 is invalid
Sample code that causes the error:
wks = gsn_open_wks("x11","polydraw_test") mpres = True mpres@gsnMaximize = True mpres@gsnDraw = False mpres@gsnFrame = False map = gsn_csm_map(wks,mpres) gsres = True gsres@gsMarkerIndex = 16 gsres@gsLineColor = "NavyBlue" gsres@gsLineThicknessF = 3 dum = gsn_add_polyline(wks,map,(/0,0/),(/90,90/),gsres) dum = gsn_add_polyline(wks,map,(/0,180/),(/30,30/),gsres) dum = gsn_add_polyline(wks,map,(/180,0/),(/30,30/),gsres) dum = gsn_add_polymarker(wks,map,(/60,30,30,60/),(/45,15,15,45/),gsres) draw(map) ; Only the last "dum" set of primitives will show up, frame(wks) ; which are the markers in this case.Cause: If you are using one of the gsn_add_polyxxx functions to add primitives to your plot and not using a unique variable name for the return value, you will one of these errors for every call.
Fix: Make sure each of the variables returned by the gsn_add_polyxxx routines is unique:
. . . dum1 = gsn_add_polyline(wks,map,(/0,0/),(/90,90/),gsres) dum2 = gsn_add_polyline(wks,map,(/0,180/),(/30,30/),gsres) dum3 = gsn_add_polyline(wks,map,(/180,0/),(/30,30/),gsres) dum4 = gsn_add_polymarker(wks,map,(/60,30,30,60/),(/45,15,15,45/),gsres) . . .xxYYYYZZZZ is not a valid resource in nnnn at this time
Sample code that causes the warning:
M = 29 ispn = conform_dims((/M,M/),ispan(M/2,M/2,1)^2,1) jspn = conform_dims((/M,M/),ispan(M/2,M/2,1)^2,0) T = sqrt(64*(jspn + ispn)) wks = gsn_open_wks("x11","plot") res = True res@gsnMaximize = True res@xyLineColor = "NavyBlue" res@cnLineLabelOn = False plot = gsn_csm_contour(wks,T,res)The above script should produce two warnings:
warning:xyLineColor is not a valid resource in plot_contour at this time warning:cnLineLabelOn is not a valid resource in plot_contour at this timeCause: This warning can be caused by two things:
 Misspelling a graphical resource name, for example, "xyLineColour" instead of "xyLineColor".
 Setting a resource that is not recognized by the type of plotting function you are calling, for example, setting "xyLineColor" when drawing a contour plot.
Fix: Doublecheck the spelling of your graphical resources and make sure you are setting resources that are recognized by the plotting function. In the example above, you want to use cnLineColor, and correct "cnLineLabelOn" to be cnLineLabelsOn.
Tip: If you use an editor enhancement while editing your NCL scripts, this will help you know if you are spelling resources correctly as the resource name will be highlighted. See the Editor enhancement examples page for more details.
NclMalloc Failed
Sample code that causes the error:
Say, for example, there is 8 GB of available memory on your system and that you are trying to read two variables off a NetCDF with the following dimensions:
f = addfile("some_file.nc","r") T = f>T ; ntim x nlat x mlon ( 1320 x 360 x 720) P = f>P ; ntim x klev x nlat x mlon ( 1320 x 6 x 360 x 720)The total memory requirement of these two arrays will be:
T: 1320*360*720 = 342144000 values = 342144000*4 = 1368576000 bytes (~1.37GB) P: 1320*6*360*720 = 2052864000 values = 2052864000*4 = 8211456000 bytes (~8.21GB)Together, these two arrays require 9,580,032,000 bytes (~9.58 GB), which would lead to the error:
fatal:NclMalloc FailedCause: This message likely means the memory requirements of the variable(s) have exceeded the available memory on your system. This can happen if you are trying to read or create very large arrays, or if you have lots of variables.
Fix: You need to reduce to reduce the amount of memory being requested by the NCL script:
 Use a "do" loop to only access part of the arrays at a time: f = addfile("some_file.nc","r") do nt=0,ntim1 T = f>T(nt,:,:) do kl=0,klev1 P = f>P(nt,kl,:,:) ...do some operation with T and P... end do end do
 Reduce the spatial dimensions (if possible). This could be useful if you are plotting T and P, plotting every other value may not make that much of a visual difference: T = f>T(:,::2,::2) P = f>T(:,:,::2,::2)
 If you don't have big arrays, but you have lots of variables in your script, then use delete to remove variables you no longer need: f = addfile("some_file.nc","r") T = f>T P = f>P QVAPOR = f>QVAPOR PH = f>PH T = T + 300. P = P + f>PB QVAPOR = QVAPOR > 0.000 PH = ( PH + f>PHB ) / 9.81 z = wrf_user_unstagger(PH,PH@stagger) tk = wrf_tk( P , T ) ; calculate TK delete([/PH,T/]) slp = wrf_slp( z, tk, P, QVAPOR ) delete([/z,tk,P,QVAPOR/])
Tip: Note that several delete commands are used, rather than one big one at the end. This is so you can free up memory as you go, in case new calculations cause you to exceed your machine's memory limit.
0 Thoughts to “U With Expected At Least One Variable Assignment”