Some Questions

Post any questions, ideas, or topics related to Jython and Python scripting.
User avatar
Jon
Posts: 192
Joined: Fri Jan 09, 2009 8:44 pm
Location: Madison, WI

Re: Some Questions

Post by Jon »

This test only uses netcdf-java stuff, so it's probably "better". What's interesting is that URL encoding the path gets things working!

Code: Select all

import os

from java.io import StringWriter
from ucar.nc2 import NCdumpW

def simple(testcase, filepath):
    writer = StringWriter()
    status = NCdumpW.print(filepath, writer, None)
    print '%s: file_loaded=%s; file_exists=%s\n' % (testcase, status, os.path.exists(filepath))
    #print writer
   
simple("**UNDERSCORED**", "/Volumes/LaCie/mcidasv/hydra/IASI/IASI_xxx_1C_M02_20070115_1140.nc")
simple("**ESCAPED**", "/Volumes/LaCie/mcidasv/hydra/IASI/IASI_xxx_1C_M02_20070115\ 1140.nc")
simple("**URLENCODED**", "file:///Volumes/LaCie/mcidasv/hydra/IASI/IASI_xxx_1C_M02_20070115%201140.nc")
simple("**SIMPLE**", "/Volumes/LaCie/mcidasv/hydra/IASI/IASI_xxx_1C_M02_20070115 1140.nc")


And the results:

Code: Select all

**UNDERSCORED**: file_loaded=True; file_exists=True

**ESCAPED**: file_loaded=False; file_exists=False

**URLENCODED**: file_loaded=True; file_exists=False

**SIMPLE**: file_loaded=False; file_exists=True
User avatar
joleenf
Posts: 1123
Joined: Mon Jan 19, 2009 7:16 pm

Re: Some Questions

Post by joleenf »

I had hoped that the traceback exceptions would catch problems when reading the netCDF files, now I see that they did not. I have modified my code, updated the jar file linked above. This is basically what I have now. It still is not perfect, but hopefully raises an exception for failure to read the file or find the field.

Code: Select all

def getGridFlatField(fileName,fieldLongName,sourceType = 'netcdf.grid'):
   """
     Simplistic reading of netCDF file.  Extra functionality can be added as
     needed.

     myField, dataDescription = readHDFnetCDF(fileName, fieldLongName, sourceType='netcdf.grid')

     inputFileName - full user path and name of grid file
     fieldLongName - found using ncdump or hover over field in Field Selector
     dataChoice - the flat field to return from grid
     dataType - netcdf.grid works for files loaded via Grid File(netCDF/GRIB/OpenDAP/GEMPAK)
                data handler
     
     original code from McV forum: 
     http://mcidas.ssec.wisc.edu/forums/viewtopic.php?f=31&t=894&hilit=managedDataSource&sid=1cb1e033255bb28ae0afc9124a69871a
   """
   from os.path import exists

   if exists(fileName):
      try:
         dataSource1 = makeDataSource(fileName, sourceType)
      except:
         raise Exception(sys.exec_info[0])

      if not (dataSource1):
         errorMessage = "Read error loading file:  " + fileName
         raise Exception(errorMessage)
      else:
         dataChoice1 = dataSource1.findDataChoice(fieldLongName)
         if not (dataChoice1):
            errorMessage = "Field not found:  " + fieldLongName
            raise Exception(errorMessage)
         else:
            flatField1 = dataSource1.getData(fieldLongName)
            dataDescription1 = dataChoice1.getDescription()
   else:
      errorMessage = "File Not Found:  " + fileName
      raise Exception(errorMessage)

   return flatField1, dataDescription1
Post Reply