Changeset 3604 for nappy


Ignore:
Timestamp:
14/03/08 07:56:40 (11 years ago)
Author:
astephen
Message:

Added start of functions for API.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • nappy/trunk/nappy/nappy_api.py

    r3602 r3604  
    88What do we want in the API? 
    99 
    10 openNAFile  - done - read and/or write 
    11 convertNCToNA(na_file, nc_file, **kwargs) 
    12  - do same for this from command line/nc-INterface version 
    13 convertNAToNC(nc_file, na_file, **kwargs) 
     10 
     11 
     12def convertNCToNA(nc_file, na_file=None, var_ids=None, na_items_to_override={}, 
     13            only_return_file_names=False, exclude_vars=[], 
     14            requested_ffi=None, delimiter="    ", float_format="%g", size_limit=None): 
     15    """ 
     16    Takes a NetCDF and converts to one or more NASA Ames files. Options are: 
     17    <nc_file>                           - name of input file (NetCDF). 
     18    <na_file>                           - name of output file (NASA Ames or CSV) - will be used as base name if multiple files. 
     19    <var_list>                  - a comma-separated list of variables (i.e. var ids) to include in the output file(s). 
     20    <ffi>                                       - NASA Ames File Format Index (FFI) to write to (normally automatic). 
     21    <float_format>          - a python formatting string such as %s, %g or %5.2f 
     22    <delimiter>                         - the delimiter you wish to use between data items in the output file such as "   " or " 
     23\t" 
     24    <limit_ffi_1001_rows>       - if format FFI is 1001 then chop files up into <limitFFI1001Rows> rows of data. 
     25    <exclude_vars>          - a comma-separated list of variables (i.e. var ids) to exclude in the output file(s). 
     26    <key1>,<value1>[,<key2>,<value2>[...]] - list of comma-separated key,value pairs to overwrite in output files: 
     27                                                                * Typically the keys are in: 
     28                                   * "DATE", "RDATE", "ANAME", "MNAME","ONAME", "ORG", "SNAME", "VNAME". 
     29        --names-only                    - only display a list of file names that would be written (i.e. don't convert actual fil 
     30es). 
     31    """  
     32    convertor = apply(nappy.nc_convertor.nc_to_na.NCToNA, [], arg_dict) 
     33    convertor.convert() 
     34 
     35    # If user only wants files then only give them that 
     36    if arg_dict["only_return_file_names"] == True: 
     37        print convertor.constructNAFileNames(na_file) 
     38    else: 
     39        convertor.writeNAFiles(na_file, mode) 
     40        print convertor.output_message 
     41 
     42    print "Should NCToNA return a list of the na file names produced?" 
     43    return True     
     44     
     45def convertNCToCSV(nc_file, csv_file=None, **kwargs): 
     46    """ 
     47    Reads in a NetCDF file and writes the data out to a CSV file following the 
     48    NASA Ames standard. 
     49    """ 
     50    if not csv_file: 
     51        nc_base = nc_file 
     52        if nc_file[-3:] == ".nc": 
     53            nc_base = nc_file[:-3] 
     54         
     55        csv_file = nc_base + ".csv" 
     56        kwargs["na_file"] = csv_file 
     57        kwargs["delimiter"] = "," 
     58    
     59    return apply(convertNCToNA, [nc_file], kwargs) 
     60     
     61def convertNAToNC(na_file, nc_file=None, mode="w", variables=None, aux_variables=None, 
     62                 global_attributes={"Conventions":"CF-1.0"}, 
     63                 time_units=None, time_warning=True, 
     64                 rename_variables={}): 
     65    """ 
     66    Takes a NASA Ames file and converts to a NetCDF file. Options are: 
     67 
    1468na2nc.py [-m <mode>] [-v <var_list>] [-a <aux_var_list>] [-g <global_atts_list>] 
    1569            [-r <rename_vars_list>] [-t <time_units>] [-n] -i <na_file> [-o <nc_file>] 
     
    2882                 time_units=None, time_warning=True, 
    2983                 rename_variables={}): 
    30 convertNCToCSV 
    31 convertNAToCSV 
     84    """ 
     85    convertor = apply(nappy.nc_convertor.na_to_nc.NAToNC, [], arg_dict) 
     86    convertor.convert() 
     87    convertor.writeNCFile(nc_file, mode) 
     88    print "SHould this return nc file path?" 
     89    return True     
     90 
     91 
     92def convertNAToCSV(...): 
     93    """ 
     94    Reads in a NASA Ames file and writes it out again with commas as the delimiter. 
     95    """ 
     96    fin = openNAFile.... 
     97    na_dict = fin.na_dict... 
     98    fout = openNAFile(.... 
     99    fout.write(delimiter=",") 
     100    fout.close() 
     101    return True 
     102 
    32103 
    33104compareNAFiles - do it 
     
    37108convertCDMSObjectsToCSV 
    38109convertNADictToNC 
    39 readCDMSObjectsFromNA - returns cds 
    40 ims var list and g,o 
    41 lobal atts 
     110readCDMSObjectsFromNA - returns cdsms var list and global atts 
    42111 
    43112some things from utils... 
     
    49118 have a look at what is needed 
    50119 
    51 import nappy  (via nappy_api.py module) 
    52 nappy.convertNAToNC(na_file, nc_file) 
    53 nappy.convertNCToNA(nc_file, na_file) 
     120 
     121Some docs here on usage... 
     122>>> import nappy  (via nappy_api.py module) 
     123>>> nappy.convertNAToNC(na_file, nc_file) 
     124>>> nappy.convertNCToNA(nc_file, na_file) 
    54125 
    55126 
Note: See TracChangeset for help on using the changeset viewer.