def addfile_micaps(fname, getfn=True): ''' Add a MICAPS data file (Data formats from CMA). :param fname: (*string*) The MICAPS file name. :param getfn: (*string*) If run ``__getfilename`` function or not. Default is ``True``. :returns: (*DimDataFile*) Opened file object. ''' if getfn: fname, isweb = __getfilename(fname) meteodata = MeteoDataInfo() meteodata.openMICAPSData(fname) datafile = DimDataFile(meteodata) return datafile
def addfile_surfer(fname, getfn=True): ''' Add a Surfer ASCII grid data file. :param fname: (*string*) The Surfer ASCII grid file name. :param getfn: (*string*) If run ``__getfilename`` function or not. Default is ``True``. :returns: (*DimDataFile*) Opened file object. ''' if getfn: fname, isweb = __getfilename(fname) meteodata = MeteoDataInfo() meteodata.openSurferGridData(fname) datafile = DimDataFile(meteodata) return datafile
def addfile_nc(fname, getfn=True): ''' Add a netCDF data file. :param fname: (*string*) The netCDF file name. :param getfn: (*string*) If run ``__getfilename`` function or not. Default is ``True``. :returns: (*DimDataFile*) Opened file object. ''' if getfn: fname, isweb = __getfilename(fname) meteodata = MeteoDataInfo() meteodata.openNetCDFData(fname) datafile = DimDataFile(meteodata) return datafile
def addfile_lonlat(fname, getfn=True, missingv=-9999.0): ''' Add a Lon/Lat ASCII data file. :param fname: (*string*) The Lon/Lat ASCII file name. :param getfn: (*string*) If run ``__getfilename`` function or not. Default is ``True``. :returns: (*DimDataFile*) Opened file object. ''' if getfn: fname, isweb = __getfilename(fname) meteodata = MeteoDataInfo() meteodata.openLonLatData(fname) meteodata.getDataInfo().setMissingValue(missingv) datafile = DimDataFile(meteodata) return datafile
def addfile_grads(fname, getfn=True): ''' Add a GrADS data file. use this function is GrADS control file has no ``.ctl`` suffix, otherwise use ``addfile`` function. :param fname: (*string*) GrADS control file name. :param getfn: (*string*) If run ``__getfilename`` function or not. Default is ``True``. :returns: (*DimDataFile*) Opened file object. ''' if getfn: fname, isweb = __getfilename(fname) meteodata = MeteoDataInfo() meteodata.openGrADSData(fname) datafile = DimDataFile(meteodata) return datafile
def addfile_hypart(fname, getfn=True): ''' Add a HYSPLIT concentration data file. :param fname: (*string*) The HYSPLIT concentration file name. :param getfn: (*string*) If run ``__getfilename`` function or not. Default is ``True``. :returns: (*DimDataFile*) Opened file object. ''' if getfn: fname, isweb = __getfilename(fname) if not os.path.exists(fname): raise IOError('No such file: ' + fname) meteodata = MeteoDataInfo() meteodata.openHYSPLITPartData(fname) datafile = DimDataFile(meteodata) return datafile
def addfile_ascii_grid(fname, getfn=True): ''' Add a ESRI ASCII grid data file. :param fname: (*string*) The ESRI ASCII grid file name. :param getfn: (*string*) If run ``__getfilename`` function or not. Default is ``True``. :returns: (*DimDataFile*) Opened file object. ''' if getfn: fname, isweb = __getfilename(fname) if not os.path.exists(fname): raise IOError('No such file: ' + fname) meteodata = MeteoDataInfo() meteodata.openASCIIGridData(fname) datafile = DimDataFile(meteodata) return datafile
def addfile_awx(fname, getfn=True): ''' Add a AWX data file (Satellite data file format from CMA). use this function is the file has no ``.awx`` suffix, otherwise use ``addfile`` function. :param fname: (*string*) The AWX file name. :param getfn: (*string*) If run ``__getfilename`` function or not. Default is ``True``. :returns: (*DimDataFile*) Opened file object. ''' if getfn: fname, isweb = __getfilename(fname) if not os.path.exists(fname): raise IOError('No such file: ' + fname) meteodata = MeteoDataInfo() meteodata.openAWXData(fname) datafile = DimDataFile(meteodata) return datafile
def addfile_hytraj(fname, getfn=True): ''' Add a HYSPLIT trajectory endpoint data file. :param fname: (*string*) The HYSPLIT trajectory endpoint file name. :param getfn: (*string*) If run ``__getfilename`` function or not. Default is ``True``. :returns: (*DimDataFile*) Opened file object. ''' if isinstance(fname, basestring): if getfn: fname, isweb = __getfilename(fname) if not os.path.exists(fname): raise IOError('No such file: ' + fname) meteodata = MeteoDataInfo() meteodata.openHYSPLITTrajData(fname) datafile = DimDataFile(meteodata) return datafile
def addfile_mm5(fname, getfn=True, reffile=None): ''' Add a MM5 data file. :param fname: (*string*) The MM5 file name. :param getfn: (*string*) If run ``__getfilename`` function or not. Default is ``True``. :returns: (*DimDataFile*) Opened file object. ''' if getfn: fname, isweb = __getfilename(fname) meteodata = MeteoDataInfo() if reffile is None: meteodata.openMM5Data(fname) else: meteodata.openMM5Data(fname, reffile) datafile = DimDataFile(meteodata) return datafile
def addfile_grib(fname, getfn=True, version=None): ''' Add a GRIB data file. :param fname: (*string*) The GRIB file name. :param getfn: (*string*) If run ``__getfilename`` function or not. Default is ``True``. :param version: (*int*) None, GRIB-1 or GRIB-2. Default is None, the version will be read from data. :returns: (*DimDataFile*) Opened file object. ''' if getfn: fname, isweb = __getfilename(fname) meteodata = MeteoDataInfo() if version is None: meteodata.openNetCDFData(fname) else: meteodata.openGRIBData(fname, version) datafile = DimDataFile(meteodata) return datafile
def addfile(fname, access='r', dtype='netcdf', keepopen=False, **kwargs): """ Opens a data file that is written in a supported file format. :param fname: (*string*) The full or relative path of the data file to load. :param access: (*string*) The access right setting to the data file. Default is ``r``. :param dtype: (*string*) The data type of the data file. Default is ``netcdf``. :param keepopen: (*boolean*) If the file keep open after this function. Default is ``False``. The file need to be closed later if ``keepopen`` is ``True``. :returns: (*DimDataFile*) Opened file object. """ if access == 'r': fname = fname.strip() fname, isweb = __getfilename(fname) if fname is None: raise IOError(fname) if isweb: return addfile_nc(fname, False) if not os.path.exists(fname): raise IOError(fname) fsufix = os.path.splitext(fname)[1].lower() if fsufix == '.ctl': return addfile_grads(fname, False) elif fsufix == '.tif': return addfile_geotiff(fname, False) elif fsufix == '.awx': return addfile_awx(fname, False) elif fsufix == '.bil': return addfile_bil(fname, False) meteodata = MeteoDataInfo() meteodata.openData(fname, keepopen) datafile = DimDataFile(meteodata, access=access) return datafile elif access == 'c': if dtype == 'arl': arldata = ARLDataInfo() arldata.createDataFile(fname) datafile = DimDataFile(arldata=arldata) elif dtype == 'bufr': bufrdata = BufrDataInfo() if os.path.exists(fname): try: os.remove(fname) except: info = sys.exc_info() print info[0], ":", info[1] bufrdata.createDataFile(fname) datafile = DimDataFile(bufrdata=bufrdata) else: version = kwargs.pop('version', 'netcdf3') if version == 'netcdf3': version = NetcdfFileWriter.Version.netcdf3 else: version = NetcdfFileWriter.Version.netcdf4 ncfile = NetcdfFileWriter.createNew(version, fname) largefile = kwargs.pop('largefile', None) if not largefile is None: ncfile.setLargeFile(largefile) datafile = DimDataFile(access=access, ncfile=ncfile) return datafile elif access == 'w': fname = fname.strip() fname, isweb = __getfilename(fname) if fname is None: raise IOError(fname) meteodata = MeteoDataInfo() ncfile = NetcdfFileWriter.openExisting(fname) meteodata = MeteoDataInfo() meteodata.openData(ncfile.getNetcdfFile(), True) datafile = DimDataFile(dataset=meteodata, access=access, ncfile=ncfile) return datafile else: return None