473,387 Members | 1,942 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Using Numeric 24.0b2 with Scientific.IO.NetCDF

I am having a problem using Numeric-24.0b2 in conjunction with
the NetCDF module from ScientificPython (version 2.4.9).
This problem does not surface using Numeric-23.8. The problem
arises in using the "min" function on a NetCDF floating array.
In 23.8, the "min" function returns a floating scalar, while in
24.0b2 it returns an *array* of length "1". Below I list a
simple NetCDF file and a Python script that illustrate the
problem. When I run the script using 23.8, I get the result:

1.0 <type 'float'>

whereas using 24.0b2 I get:

1.0 <type 'array'>

This creates a backward incompatibility that breaks several of
my codes.

NetCDF file simple.cdl (used to create simple.nc with "ncgen")
--------------------------------------------------------------

netcdf simple {
dimensions:
num = 3 ;
variables:
float temp(num) ;
data:

temp = 1, 2, 3 ;
}
Python script
-------------

import Numeric
from Scientific.IO.NetCDF import NetCDFFile

cdf_file1 = NetCDFFile("simple.nc","r")
temp = cdf_file1.variables["temp"][:]

print min(temp), type(min(temp))

Jul 19 '05 #1
5 2338
bandw wrote:
I am having a problem using Numeric-24.0b2 in conjunction with
the NetCDF module from ScientificPython (version 2.4.9).
This problem does not surface using Numeric-23.8. The problem
arises in using the "min" function on a NetCDF floating array.
In 23.8, the "min" function returns a floating scalar, while in
24.0b2 it returns an *array* of length "1". Below I list a
simple NetCDF file and a Python script that illustrate the
problem. When I run the script using 23.8, I get the result:

1.0 <type 'float'>

whereas using 24.0b2 I get:

1.0 <type 'array'>

This creates a backward incompatibility that breaks several of
my codes.


Call float(temp) if you really need a Python float. The change was
intentional such that A[i] would always be an array regardless of the
shape of A. This greatly simplifies certain types of code although the
change does have its transition costs for some specific pieces of older
code like yours.

BTW, you don't want to use the builtin min(). That iterates over the
array as if it were a Python list. Use minimum.reduce().

--
Robert Kern
rk***@ucsd.edu

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter

Jul 19 '05 #2
Robert,

Thanks for your reply. However, I am still having problems. Sometimes
I get a scalar return
and sometimes I get an array. For example, using the netCDF file:

netcdf simple {
dimensions:
num = 3 ;
variables:
float temp0(num) ;
int temp1(num) ;
data:

temp0 = 1., 2., 3. ;
temp1 = 1, 2, 3 ;
}

and running:

#
import Numeric
print Numeric.__version__
from Scientific.IO.NetCDF import NetCDFFile

cdf_file1 = NetCDFFile("simple.nc","r")

var1 = cdf_file1.variables["temp0"][:]
var2 = cdf_file1.variables["temp1"][:]
min1 = reduce(Numeric.minimum,var1)
min2 = reduce(Numeric.minimum,var2)

print "Types of var1, min(var1), min1:",type(var1), type(min(var1)),
type(min1)
print "Types of var2, min(var2), min2:",type(var2), type(min(var2)),
type(min2)

I get:

24.0b2
Types of var1, min(var1), min1: <type 'array'> <type 'array'> <type
'array'>
Types of var2, min(var2), min2: <type 'array'> <type 'int'> <type
'int'>

Even something like:

import Numeric
a = Numeric.array([1.,2.])
print type(a),type(min(a))

<type 'array'> <type 'float'>

does not produce an array.

Any comments woud be appreciated.

Fred Clare

Jul 21 '05 #3
bandw wrote:
Robert,

Thanks for your reply. However, I am still having problems. Sometimes
I get a scalar return
and sometimes I get an array. For example, using the netCDF file:

netcdf simple {
dimensions:
num = 3 ;
variables:
float temp0(num) ;
int temp1(num) ;
data:

temp0 = 1., 2., 3. ;
temp1 = 1, 2, 3 ;
}

and running:

#
import Numeric
print Numeric.__version__
from Scientific.IO.NetCDF import NetCDFFile

cdf_file1 = NetCDFFile("simple.nc","r")

var1 = cdf_file1.variables["temp0"][:]
var2 = cdf_file1.variables["temp1"][:]
min1 = reduce(Numeric.minimum,var1)
min2 = reduce(Numeric.minimum,var2)

print "Types of var1, min(var1), min1:",type(var1), type(min(var1)),
type(min1)
print "Types of var2, min(var2), min2:",type(var2), type(min(var2)),
type(min2)

I get:

24.0b2
Types of var1, min(var1), min1: <type 'array'> <type 'array'> <type
'array'>
Types of var2, min(var2), min2: <type 'array'> <type 'int'> <type
'int'>

Even something like:
import Numeric
a = Numeric.array([1.,2.])
print type(a),type(min(a))


<type 'array'> <type 'float'>

does not produce an array.


Hmm, odd. Anyways, follow my advice: use minimum.reduce() and wrap
results in float() or array() if you really need floats or rank-0 arrays.

--
Robert Kern
rk***@ucsd.edu

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter

Jul 21 '05 #4
Thanks again. I will take your advice. My concern is in not knowing
where in all
my python code I am assuming a scalar return in certain circumstances.
But I
guess I can take care of the errors as they come up.

Fred

Jul 21 '05 #5
I am having more problems with 24.0b2. Consider the NetCDF file:

netcdf very_simple {
dimensions:
num = 2 ;
variables:
float T(num) ;
T:mv = 5.0f ;
data:
T = 1., 2. ;
}

and the python script:

import Numeric
from Scientific.IO.NetCDF import NetCDFFile
file = NetCDFFile("simple.nc","r")
T = file.variables["T"]

a = T.mv
print "T.mv = ", a
print "type(T.mv) = ", type(a)
print "len(T.mv) = ", len(a)
print "T.mv[0] = ", a[0]
print "len(T.mv[0]) = ", len(a[0])
print "type(T.mv[0]) = ", type(a[0])

which produces the output:

T.mv = [ 5.]
type(T.mv) = <type 'array'>
len(T.mv) = 1
T.mv[0] = 5.0
len(T.mv[0]) = 1
type(T.mv[0]) = <type 'array'>

I can see no reason why T.mv[0] should be typed as an array.

Jul 21 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Kyler Laird | last post by:
Python 2.3.3 (#2, Jan 4 2004, 12:24:16) on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import Numeric >>> import MLab >>> import Scientific.Statistics...
6
by: Gaubitzer Erwin | last post by:
Hi there I wrote a short program which reads scientific data from a file and stores its values in a Numeric array. At the same time it reads the names of its dimensions which are then in the...
9
by: skilpat | last post by:
I am going to be doing a lot of work with large data sets stored in various netCDF files, and after checking out the alternatives, I would really like to go with SciPy. The problem is that SciPy...
1
by: jelle | last post by:
#No rant intended I'm not at all confused wether I should learn an one of the advanced array modules, I'm slightly confused over which I should pick up. I'm impressed with the efforts of SciPy...
11
by: Pieter | last post by:
Hi, I'm having some troubles with my numeric-types in my VB.NET 2005 application, together with a SQL Server 2000. - I first used Single in my application, and Decimal in my database. But a...
5
by: lars.uffmann | last post by:
Hi - since my Suse 10.1 didn't have netcdf support enabled by default, I downloaded the sources from http://www.unidata.ucar.edu/software/netcdf/ then did ../configure --prefix=/usr make make...
2
by: Jerry | last post by:
I am writing text out to a text file and I want to LEFT justify the text. I wave found examples for the following: // Format a negative integer or floating-point number in various ways....
0
by: vorticitywolfe | last post by:
Hello, I am working with a netcdf file and trying to remove a long string from it e.g. "KAST BLAH BLAH BLAH BLAH DATA BLAH DATA BLAH BLAH BLAH BLAH BLAH DATA BLAH DATA BLAH" Right now what I...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.