Codebase list netcdf4-python / run/b9523853-a5d3-49e1-a0a3-e97fa44c3af6/main setup.py
run/b9523853-a5d3-49e1-a0a3-e97fa44c3af6/main

Tree @run/b9523853-a5d3-49e1-a0a3-e97fa44c3af6/main (Download .tar.gz)

setup.py @run/b9523853-a5d3-49e1-a0a3-e97fa44c3af6/mainraw · history · blame

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
import os, sys, subprocess, glob
import os.path as osp
import shutil
import configparser
from setuptools import setup, Extension, find_namespace_packages
from setuptools.dist import Distribution

setuptools_extra_kwargs = {
    "install_requires": ["numpy>=1.9","cftime"],
    "entry_points": {
        'console_scripts': [
            'ncinfo = netCDF4.utils:ncinfo',
            'nc4tonc3 = netCDF4.utils:nc4tonc3',
            'nc3tonc4 = netCDF4.utils:nc3tonc4',
        ]
    },
}

open_kwargs = {'encoding': 'utf-8'}


def check_hdf5version(hdf5_includedir):
    try:
        f = open(os.path.join(hdf5_includedir, 'H5public.h'), **open_kwargs)
    except IOError:
        return None
    hdf5_version = None
    for line in f:
        if line.startswith('#define H5_VERS_INFO'):
            hdf5_version = line.split('"')[1]
    return hdf5_version

def get_hdf5_version(direc):
    # check to see if hdf5 headers in direc, return version number or None
    hdf5_version = None
    sys.stdout.write('checking %s ...\n' % direc)
    hdf5_version = check_hdf5version(direc)
    if hdf5_version is None:
        sys.stdout.write('hdf5 headers not found in %s\n' % direc)
        return None
    else:
        sys.stdout.write('%s headers found in %s\n' %
                        (hdf5_version,direc))
        return hdf5_version

def check_ifnetcdf4(netcdf4_includedir):
    try:
        f = open(os.path.join(netcdf4_includedir, 'netcdf.h'), **open_kwargs)
    except IOError:
        return False
    isnetcdf4 = False
    for line in f:
        if line.startswith('nc_inq_compound'):
            isnetcdf4 = True
    return isnetcdf4


def check_api(inc_dirs,netcdf_lib_version):
    has_rename_grp = False
    has_nc_inq_path = False
    has_nc_inq_format_extended = False
    has_cdf5_format = False
    has_nc_open_mem = False
    has_nc_create_mem = False
    has_parallel_support = False
    has_parallel4_support = False
    has_pnetcdf_support = False
    has_szip_support = False
    has_quantize = False
    has_zstandard = False
    has_bzip2 = False
    has_blosc = False
    has_ncfilter = False
    has_set_alignment = False

    for d in inc_dirs:
        try:
            f = open(os.path.join(d, 'netcdf.h'), **open_kwargs)
        except IOError:
            continue

        has_nc_open_mem = os.path.exists(os.path.join(d, 'netcdf_mem.h'))
        has_nc_filter = os.path.exists(os.path.join(d, 'netcdf_filter.h'))

        for line in f:
            if line.startswith('nc_rename_grp'):
                has_rename_grp = True
            if line.startswith('nc_inq_path'):
                has_nc_inq_path = True
            if line.startswith('nc_inq_format_extended'):
                has_nc_inq_format_extended = True
            if line.startswith('#define NC_FORMAT_64BIT_DATA'):
                has_cdf5_format = True
            if line.startswith('nc_def_var_quantize'):
                has_quantize = True
            if line.startswith('nc_set_alignment'):
                has_set_alignment = True

        if has_nc_open_mem:
            try:
                f = open(os.path.join(d, 'netcdf_mem.h'), **open_kwargs)
            except IOError:
                continue
            for line in f:
                if line.startswith('EXTERNL int nc_create_mem'):
                    has_nc_create_mem = True

        if has_nc_filter:
            try:
                f = open(os.path.join(d, 'netcdf_filter.h'), **open_kwargs)
            except IOError:
                continue
            for line in f:
                if line.startswith('EXTERNL int nc_def_var_zstandard'):
                    has_zstandard = True
                if line.startswith('EXTERNL int nc_def_var_bzip2'):
                    has_bzip2 = True
                if line.startswith('EXTERNL int nc_def_var_blosc'):
                    has_blosc = True
                if line.startswith('EXTERNL int nc_inq_filter_avail'):
                    has_ncfilter = True

        ncmetapath = os.path.join(d,'netcdf_meta.h')
        if os.path.exists(ncmetapath):
            for line in open(ncmetapath):
                if line.startswith('#define NC_HAS_CDF5'):
                    has_cdf5_format = bool(int(line.split()[2]))
                if line.startswith('#define NC_HAS_PARALLEL'):
                    has_parallel_support = bool(int(line.split()[2]))
                if line.startswith('#define NC_HAS_PARALLEL4'):
                    has_parallel4_support = bool(int(line.split()[2]))
                if line.startswith('#define NC_HAS_PNETCDF'):
                    has_pnetcdf_support = bool(int(line.split()[2]))
                if line.startswith('#define NC_HAS_SZIP_WRITE'):
                    has_szip_support = bool(int(line.split()[2]))
        # NC_HAS_PARALLEL4 missing in 4.6.1 (issue #964)
        if not has_parallel4_support and has_parallel_support and not has_pnetcdf_support:
            has_parallel4_support = True
        # for 4.6.1, if NC_HAS_PARALLEL=NC_HAS_PNETCDF=1, guess that
        # parallel HDF5 is enabled (must guess since there is no
        # NC_HAS_PARALLEL4)
        elif netcdf_lib_version == "4.6.1" and not has_parallel4_support and has_parallel_support:
            has_parallel4_support = True
        break

    return has_rename_grp, has_nc_inq_path, has_nc_inq_format_extended, \
           has_cdf5_format, has_nc_open_mem, has_nc_create_mem, \
           has_parallel4_support, has_pnetcdf_support, has_szip_support, has_quantize, \
           has_zstandard, has_bzip2, has_blosc, has_set_alignment, has_ncfilter


def getnetcdfvers(libdirs):
    """
    Get the version string for the first netcdf lib found in libdirs.
    (major.minor.release). If nothing found, return None.
    """

    import os, re, sys, ctypes

    if sys.platform.startswith('win'):
        regexp = re.compile('^netcdf.dll$')
    elif sys.platform.startswith('cygwin'):
        bindirs = []
        for d in libdirs:
            bindirs.append(os.path.dirname(d) + '/bin')
        regexp = re.compile(r'^cygnetcdf-\d.dll')
    elif sys.platform.startswith('darwin'):
        regexp = re.compile(r'^libnetcdf.dylib')
    else:
        regexp = re.compile(r'^libnetcdf.so')

    if sys.platform.startswith('cygwin'):
        dirs = bindirs
    else:
        dirs = libdirs
    for d in dirs:
        try:
            candidates = [x for x in os.listdir(d) if regexp.match(x)]
            if len(candidates) != 0:
                candidates.sort(
                    key=lambda x: len(x))  # Prefer libfoo.so to libfoo.so.X.Y.Z
                path = os.path.abspath(os.path.join(d, candidates[0]))
            lib = ctypes.cdll.LoadLibrary(path)
            inq_libvers = lib.nc_inq_libvers
            inq_libvers.restype = ctypes.c_char_p
            vers = lib.nc_inq_libvers()
            return vers.split()[0]
        except Exception:
            pass  # We skip invalid entries, because that's what the C compiler does

    return None

def extract_version(CYTHON_FNAME):
    version = None
    with open(CYTHON_FNAME) as fi:
        for line in fi:
            if (line.startswith('__version__')):
                _, version = line.split('=')
                version = version.strip()[1:-1]  # Remove quotation characters.
                break
    return version


HDF5_dir = os.environ.get('HDF5_DIR')
HDF5_incdir = os.environ.get('HDF5_INCDIR')
HDF5_libdir = os.environ.get('HDF5_LIBDIR')
netCDF4_dir = os.environ.get('NETCDF4_DIR')
netCDF4_incdir = os.environ.get('NETCDF4_INCDIR')
netCDF4_libdir = os.environ.get('NETCDF4_LIBDIR')
szip_dir = os.environ.get('SZIP_DIR')
szip_libdir = os.environ.get('SZIP_LIBDIR')
szip_incdir = os.environ.get('SZIP_INCDIR')
hdf4_dir = os.environ.get('HDF4_DIR')
hdf4_libdir = os.environ.get('HDF4_LIBDIR')
hdf4_incdir = os.environ.get('HDF4_INCDIR')
jpeg_dir = os.environ.get('JPEG_DIR')
jpeg_libdir = os.environ.get('JPEG_LIBDIR')
jpeg_incdir = os.environ.get('JPEG_INCDIR')
curl_dir = os.environ.get('CURL_DIR')
curl_libdir = os.environ.get('CURL_LIBDIR')
curl_incdir = os.environ.get('CURL_INCDIR')
mpi_incdir = os.environ.get('MPI_INCDIR')

USE_NCCONFIG = os.environ.get('USE_NCCONFIG')
if USE_NCCONFIG is not None:
    USE_NCCONFIG = bool(int(USE_NCCONFIG))
USE_SETUPCFG = os.environ.get('USE_SETUPCFG')
# override use of setup.cfg with env var.
if USE_SETUPCFG is not None:
    USE_SETUPCFG = bool(int(USE_SETUPCFG))
else:
    USE_SETUPCFG = True

setup_cfg = 'setup.cfg'
# contents of setup.cfg will override env vars, unless
# USE_SETUPCFG evaluates to False.
ncconfig = None
use_ncconfig = None
if USE_SETUPCFG and os.path.exists(setup_cfg):
    sys.stdout.write('reading from setup.cfg...\n')
    config = configparser.ConfigParser()
    config.read(setup_cfg)
    try:
        HDF5_dir = config.get("directories", "HDF5_dir")
    except:
        pass
    try:
        HDF5_libdir = config.get("directories", "HDF5_libdir")
    except:
        pass
    try:
        HDF5_incdir = config.get("directories", "HDF5_incdir")
    except:
        pass
    try:
        netCDF4_dir = config.get("directories", "netCDF4_dir")
    except:
        pass
    try:
        netCDF4_libdir = config.get("directories", "netCDF4_libdir")
    except:
        pass
    try:
        netCDF4_incdir = config.get("directories", "netCDF4_incdir")
    except:
        pass
    try:
        szip_dir = config.get("directories", "szip_dir")
    except:
        pass
    try:
        szip_libdir = config.get("directories", "szip_libdir")
    except:
        pass
    try:
        szip_incdir = config.get("directories", "szip_incdir")
    except:
        pass
    try:
        hdf4_dir = config.get("directories", "hdf4_dir")
    except:
        pass
    try:
        hdf4_libdir = config.get("directories", "hdf4_libdir")
    except:
        pass
    try:
        hdf4_incdir = config.get("directories", "hdf4_incdir")
    except:
        pass
    try:
        jpeg_dir = config.get("directories", "jpeg_dir")
    except:
        pass
    try:
        jpeg_libdir = config.get("directories", "jpeg_libdir")
    except:
        pass
    try:
        jpeg_incdir = config.get("directories", "jpeg_incdir")
    except:
        pass
    try:
        curl_dir = config.get("directories", "curl_dir")
    except:
        pass
    try:
        curl_libdir = config.get("directories", "curl_libdir")
    except:
        pass
    try:
        curl_incdir = config.get("directories", "curl_incdir")
    except:
        pass
    try:
        mpi_incdir = config.get("directories","mpi_incdir")
    except:
        pass
    try:
        use_ncconfig = config.getboolean("options", "use_ncconfig")
    except:
        pass
    try:
        ncconfig = config.get("options", "ncconfig")
    except:
        pass

try:
    if ncconfig is None:
        if netCDF4_dir is not None:
            ncconfig = os.path.join(netCDF4_dir, 'bin/nc-config')
        else:  # otherwise, just hope it's in the users PATH.
            ncconfig = 'nc-config'
    HAS_NCCONFIG = subprocess.call([ncconfig, '--libs'],
                                     stdout=subprocess.PIPE) == 0
except OSError:
    HAS_NCCONFIG = False

# make sure USE_NCCONFIG from environment takes
# precendence over use_ncconfig from setup.cfg (issue #341).
if USE_NCCONFIG is None and use_ncconfig is not None:
    USE_NCCONFIG = use_ncconfig
elif USE_NCCONFIG is None:
    # if nc-config exists, and USE_NCCONFIG not set, try to use it.
    if HAS_NCCONFIG: USE_NCCONFIG=True
#elif USE_NCCONFIG is None:
#    USE_NCCONFIG = False # don't try to use nc-config if USE_NCCONFIG not set

try:
    HAS_PKG_CONFIG = subprocess.call(['pkg-config', '--libs', 'hdf5'],
                                     stdout=subprocess.PIPE) == 0
except OSError:
    HAS_PKG_CONFIG = False

def _populate_hdf5_info(dirstosearch, inc_dirs, libs, lib_dirs):
    global HDF5_incdir, HDF5_dir, HDF5_libdir

    nohdf5dirs = HDF5_incdir is None and HDF5_libdir is None and  HDF5_dir is None
    if HAS_PKG_CONFIG and nohdf5dirs:
        # if HDF5 dirs not specified, and pkg-config available, use it
        dep = subprocess.Popen(['pkg-config', '--cflags', 'hdf5'],
                               stdout=subprocess.PIPE).communicate()[0]
        inc_dirs.extend([str(i[2:].decode()) for i in dep.split() if
                         i[0:2].decode() == '-I'])
        dep = subprocess.Popen(['pkg-config', '--libs', 'hdf5'],
                               stdout=subprocess.PIPE).communicate()[0]
        libs.extend(
            [str(l[2:].decode()) for l in dep.split() if l[0:2].decode() == '-l'])
        lib_dirs.extend(
            [str(l[2:].decode()) for l in dep.split() if l[0:2].decode() == '-L'])
        dep = subprocess.Popen(['pkg-config', '--cflags', 'hdf5'],
                               stdout=subprocess.PIPE).communicate()[0]
        inc_dirs.extend(
            [str(i[2:].decode()) for i in dep.split() if i[0:2].decode() == '-I'])
    else:
        if HDF5_incdir is None and HDF5_dir is None:
            sys.stdout.write("""
    HDF5_DIR environment variable not set, checking some standard locations ..\n""")
            for direc in dirstosearch:
                hdf5_version = get_hdf5_version(os.path.join(direc, 'include'))
                if hdf5_version is None:
                    continue
                else:
                    HDF5_dir = direc
                    HDF5_incdir = os.path.join(direc, 'include')
                    sys.stdout.write('%s found in %s\n' %
                                    (hdf5_version,HDF5_dir))
                    break
            if HDF5_dir is None:
                raise ValueError('did not find HDF5 headers')
        else:
            if HDF5_incdir is None:
                HDF5_incdir = os.path.join(HDF5_dir, 'include')
            hdf5_version = get_hdf5_version(HDF5_incdir)
            if hdf5_version is None:
                raise ValueError('did not find HDF5 headers in %s' % HDF5_incdir)
            else:
                sys.stdout.write('%s found in %s\n' %
                                (hdf5_version,HDF5_dir))

        if HDF5_libdir is None and HDF5_dir is not None:
            HDF5_libdir = os.path.join(HDF5_dir, 'lib')

        if HDF5_libdir is not None: lib_dirs.append(HDF5_libdir)
        if HDF5_incdir is not None: inc_dirs.append(HDF5_incdir)

        libs.extend(['hdf5_hl', 'hdf5'])


dirstosearch = []
if os.environ.get("CONDA_PREFIX"):
    dirstosearch.append(os.environ["CONDA_PREFIX"]) # linux,macosx
    dirstosearch.append(os.path.join(os.environ["CONDA_PREFIX"],'Library')) # windows
dirstosearch += [os.path.expanduser('~'), '/usr/local', '/sw', '/opt',
                '/opt/local', '/opt/homebrew', '/usr']

# try nc-config first
if USE_NCCONFIG and HAS_NCCONFIG:  # Try nc-config.
    sys.stdout.write('using %s...\n' % ncconfig)
    dep = subprocess.Popen([ncconfig, '--libs'],
                           stdout=subprocess.PIPE).communicate()[0]
    libs = [str(l[2:].decode()) for l in dep.split() if l[0:2].decode() == '-l']
    lib_dirs = [str(l[2:].decode()) for l in dep.split() if
                l[0:2].decode() == '-L']
    dep = subprocess.Popen([ncconfig, '--cflags'],
                           stdout=subprocess.PIPE).communicate()[0]
    inc_dirs = [str(i[2:].decode()) for i in dep.split() if
                i[0:2].decode() == '-I']

    # check to see if hdf5 found in directories returned by nc-config
    hdf5_version = None
    for direc in inc_dirs:
        hdf5_version = get_hdf5_version(direc)
        if hdf5_version is not None:
            break
    # if hdf5 not found, search other standard locations (including those specified in env vars).
    if hdf5_version is None:
        sys.stdout.write('nc-config did provide path to HDF5 headers, search standard locations...')
        _populate_hdf5_info(dirstosearch, inc_dirs, libs, lib_dirs)

# If nc-config doesn't work, fall back on brute force method.
else:
    lib_dirs = []
    inc_dirs = []
    libs = []

    # _populate_hdf5_info will use HDF5_dir, HDF5_libdir and HDF5_incdir if they are set.
    # otherwise pkg-config will be tried, and if that fails, dirstosearch will be searched.
    _populate_hdf5_info(dirstosearch, inc_dirs, libs, lib_dirs)

    if netCDF4_incdir is None and netCDF4_dir is None:
        sys.stdout.write("""
NETCDF4_DIR environment variable not set, checking standard locations.. \n""")
        for direc in dirstosearch:
            sys.stdout.write('checking %s ...\n' % direc)
            isnetcdf4 = check_ifnetcdf4(os.path.join(direc, 'include'))
            if not isnetcdf4:
                continue
            else:
                netCDF4_dir = direc
                netCDF4_incdir = os.path.join(direc, 'include')
                sys.stdout.write('netCDF4 found in %s\n' % netCDF4_dir)
                break
        if netCDF4_dir is None:
            raise ValueError('did not find netCDF version 4 headers')
    else:
        if netCDF4_incdir is None:
            netCDF4_incdir = os.path.join(netCDF4_dir, 'include')
        isnetcdf4 = check_ifnetcdf4(netCDF4_incdir)
        if not isnetcdf4:
            raise ValueError(
                'did not find netCDF version 4 headers %s' % netCDF4_incdir)

    if netCDF4_libdir is None and netCDF4_dir is not None:
        netCDF4_libdir = os.path.join(netCDF4_dir, 'lib')

    if sys.platform == 'win32':
        libs.extend(['netcdf', 'zlib'])
    else:
        libs.extend(['netcdf', 'z'])

    if netCDF4_libdir is not None: lib_dirs.append(netCDF4_libdir)
    if netCDF4_incdir is not None: inc_dirs.append(netCDF4_incdir)

    # add szip to link if desired.
    if szip_libdir is None and szip_dir is not None:
        szip_libdir = os.path.join(szip_dir, 'lib')
    if szip_incdir is None and szip_dir is not None:
        szip_incdir = os.path.join(szip_dir, 'include')
    if szip_incdir is not None and szip_libdir is not None:
        libs.append('sz')
        lib_dirs.append(szip_libdir)
        inc_dirs.append(szip_incdir)
    # add hdf4 to link if desired.
    if hdf4_libdir is None and hdf4_dir is not None:
        hdf4_libdir = os.path.join(hdf4_dir, 'lib')
    if hdf4_incdir is None and hdf4_dir is not None:
        hdf4_incdir = os.path.join(hdf4_dir, 'include')
    if hdf4_incdir is not None and hdf4_libdir is not None:
        libs.append('mfhdf')
        libs.append('df')
        lib_dirs.append(hdf4_libdir)
        inc_dirs.append(hdf4_incdir)
    # add jpeg to link if desired.
    if jpeg_libdir is None and jpeg_dir is not None:
        jpeg_libdir = os.path.join(jpeg_dir, 'lib')
    if jpeg_incdir is None and jpeg_dir is not None:
        jpeg_incdir = os.path.join(jpeg_dir, 'include')
    if jpeg_incdir is not None and jpeg_libdir is not None:
        libs.append('jpeg')
        lib_dirs.append(jpeg_libdir)
        inc_dirs.append(jpeg_incdir)
    # add curl to link if desired.
    if curl_libdir is None and curl_dir is not None:
        curl_libdir = os.path.join(curl_dir, 'lib')
    if curl_incdir is None and curl_dir is not None:
        curl_incdir = os.path.join(curl_dir, 'include')
    if curl_incdir is not None and curl_libdir is not None:
        libs.append('curl')
        lib_dirs.append(curl_libdir)
        inc_dirs.append(curl_incdir)

if sys.platform == 'win32':
    runtime_lib_dirs = []
else:
    runtime_lib_dirs = lib_dirs

# Do not require numpy for just querying the package
# Taken from the h5py setup file.
if any('--' + opt in sys.argv for opt in Distribution.display_option_names +
        ['help-commands', 'help']) or sys.argv[1] == 'egg_info':
    pass
else:
    # append numpy include dir.
    import numpy
    inc_dirs.append(numpy.get_include())

# get netcdf library version.
netcdf_lib_version = getnetcdfvers(lib_dirs)
if netcdf_lib_version is None:
    sys.stdout.write('unable to detect netcdf library version\n')
else:
    netcdf_lib_version = str(netcdf_lib_version)
    sys.stdout.write('using netcdf library version %s\n' % netcdf_lib_version)

cmdclass = {}
DEFINE_MACROS = [("NPY_NO_DEPRECATED_API", "NPY_1_7_API_VERSION")]
netcdf4_src_root = osp.join(osp.join('src','netCDF4'), '_netCDF4')
netcdf4_src_c = netcdf4_src_root + '.c'
netcdf4_src_pyx = netcdf4_src_root + '.pyx'
if 'sdist' not in sys.argv[1:] and 'clean' not in sys.argv[1:] and '--version' not in sys.argv[1:]:
    sys.stdout.write('using Cython to compile netCDF4.pyx...\n')
    # remove _netCDF4.c file if it exists, so cython will recompile _netCDF4.pyx.
    # run for build *and* install (issue #263). Otherwise 'pip install' will
    # not regenerate _netCDF4.c, even if the C lib supports the new features.
    if len(sys.argv) >= 2:
        if os.path.exists(netcdf4_src_c):
            os.remove(netcdf4_src_c)
    # this determines whether renameGroup and filepath methods will work.
    has_rename_grp, has_nc_inq_path, has_nc_inq_format_extended, \
    has_cdf5_format, has_nc_open_mem, has_nc_create_mem, \
    has_parallel4_support, has_pnetcdf_support, has_szip_support, has_quantize, \
    has_zstandard, has_bzip2, has_blosc, has_set_alignment, has_ncfilter = \
    check_api(inc_dirs,netcdf_lib_version)
    # for netcdf 4.4.x CDF5 format is always enabled.
    if netcdf_lib_version is not None and\
       (netcdf_lib_version > "4.4" and netcdf_lib_version < "4.5"):
        has_cdf5_format = True

    # disable parallel support if mpi4py not available.
    #try:
    #    import mpi4py
    #except ImportError:
    #    f.write('disabling mpi parallel support because mpi4py not found\n')
    #    has_parallel4_support = False
    #    has_pnetcdf_support = False

    f = open(osp.join('include', 'constants.pyx'), 'w')
    if has_rename_grp:
        sys.stdout.write('netcdf lib has group rename capability\n')
        f.write('DEF HAS_RENAME_GRP = 1\n')
    else:
        sys.stdout.write('netcdf lib does not have group rename capability\n')
        f.write('DEF HAS_RENAME_GRP = 0\n')

    if has_nc_inq_path:
        sys.stdout.write('netcdf lib has nc_inq_path function\n')
        f.write('DEF HAS_NC_INQ_PATH = 1\n')
    else:
        sys.stdout.write('netcdf lib does not have nc_inq_path function\n')
        f.write('DEF HAS_NC_INQ_PATH = 0\n')

    if has_nc_inq_format_extended:
        sys.stdout.write('netcdf lib has nc_inq_format_extended function\n')
        f.write('DEF HAS_NC_INQ_FORMAT_EXTENDED = 1\n')
    else:
        sys.stdout.write(
            'netcdf lib does not have nc_inq_format_extended function\n')
        f.write('DEF HAS_NC_INQ_FORMAT_EXTENDED = 0\n')

    if has_nc_open_mem:
        sys.stdout.write('netcdf lib has nc_open_mem function\n')
        f.write('DEF HAS_NC_OPEN_MEM = 1\n')
    else:
        sys.stdout.write('netcdf lib does not have nc_open_mem function\n')
        f.write('DEF HAS_NC_OPEN_MEM = 0\n')

    if has_nc_create_mem:
        sys.stdout.write('netcdf lib has nc_create_mem function\n')
        f.write('DEF HAS_NC_CREATE_MEM = 1\n')
    else:
        sys.stdout.write('netcdf lib does not have nc_create_mem function\n')
        f.write('DEF HAS_NC_CREATE_MEM = 0\n')

    if has_cdf5_format:
        sys.stdout.write('netcdf lib has cdf-5 format capability\n')
        f.write('DEF HAS_CDF5_FORMAT = 1\n')
    else:
        sys.stdout.write('netcdf lib does not have cdf-5 format capability\n')
        f.write('DEF HAS_CDF5_FORMAT = 0\n')

    if has_parallel4_support:
        sys.stdout.write('netcdf lib has netcdf4 parallel functions\n')
        f.write('DEF HAS_PARALLEL4_SUPPORT = 1\n')
    else:
        sys.stdout.write('netcdf lib does not have netcdf4 parallel functions\n')
        f.write('DEF HAS_PARALLEL4_SUPPORT = 0\n')

    if has_pnetcdf_support:
        sys.stdout.write('netcdf lib has pnetcdf parallel functions\n')
        f.write('DEF HAS_PNETCDF_SUPPORT = 1\n')
    else:
        sys.stdout.write('netcdf lib does not have pnetcdf parallel functions\n')
        f.write('DEF HAS_PNETCDF_SUPPORT = 0\n')

    if has_quantize:
        sys.stdout.write('netcdf lib has bit-grooming/quantization functions\n')
        f.write('DEF HAS_QUANTIZATION_SUPPORT = 1\n')
    else:
        sys.stdout.write('netcdf lib does not have bit-grooming/quantization functions\n')
        f.write('DEF HAS_QUANTIZATION_SUPPORT = 0\n')

    if has_zstandard:
        sys.stdout.write('netcdf lib has zstandard compression functions\n')
        f.write('DEF HAS_ZSTANDARD_SUPPORT = 1\n')
    else:
        sys.stdout.write('netcdf lib does not have zstandard compression functions\n')
        f.write('DEF HAS_ZSTANDARD_SUPPORT = 0\n')

    if has_bzip2:
        sys.stdout.write('netcdf lib has bzip2 compression functions\n')
        f.write('DEF HAS_BZIP2_SUPPORT = 1\n')
    else:
        sys.stdout.write('netcdf lib does not have bzip2 compression functions\n')
        f.write('DEF HAS_BZIP2_SUPPORT = 0\n')

    if has_blosc:
        sys.stdout.write('netcdf lib has blosc compression functions\n')
        f.write('DEF HAS_BLOSC_SUPPORT = 1\n')
    else:
        sys.stdout.write('netcdf lib does not have blosc compression functions\n')
        f.write('DEF HAS_BLOSC_SUPPORT = 0\n')

    if has_szip_support:
        sys.stdout.write('netcdf lib has szip compression functions\n')
        f.write('DEF HAS_SZIP_SUPPORT = 1\n')
    else:
        sys.stdout.write('netcdf lib does not have szip compression functions\n')
        f.write('DEF HAS_SZIP_SUPPORT = 0\n')

    if has_set_alignment:
        sys.stdout.write('netcdf lib has nc_set_alignment function\n')
        f.write('DEF HAS_SET_ALIGNMENT = 1\n')
    else:
        sys.stdout.write('netcdf lib does not have nc_set_alignment function\n')
        f.write('DEF HAS_SET_ALIGNMENT = 0\n')

    if has_ncfilter:
        sys.stdout.write('netcdf lib has nc_inq_filter_avail function\n')
        f.write('DEF HAS_NCFILTER = 1\n')
    else:
        sys.stdout.write('netcdf lib does not have nc_inq_filter_avail function\n')
        f.write('DEF HAS_NCFILTER = 0\n')

    f.close()

    if has_parallel4_support or has_pnetcdf_support:
        import mpi4py
        inc_dirs.append(mpi4py.get_include())
        # mpi_incdir should not be needed if using nc-config
        # (should be included in nc-config --cflags)
        if mpi_incdir is not None: inc_dirs.append(mpi_incdir)

    ext_modules = [Extension("netCDF4._netCDF4",
                             [netcdf4_src_pyx],
                             define_macros=DEFINE_MACROS,
                             libraries=libs,
                             library_dirs=lib_dirs,
                             include_dirs=inc_dirs + ['include'],
                             runtime_library_dirs=runtime_lib_dirs)]
    # set language_level directive to 3
    for e in ext_modules:
        e.cython_directives = {'language_level': "3"} #
else:
    ext_modules = None

# if NETCDF_PLUGIN_DIR set, install netcdf-c compression plugins inside package
# (should point to location of lib__nc* files built by netcdf-c)
copied_plugins=False
if os.environ.get("NETCDF_PLUGIN_DIR"):
    plugin_dir = os.environ.get("NETCDF_PLUGIN_DIR")
    plugins = glob.glob(os.path.join(plugin_dir, "lib__nc*"))
    if not plugins:
        sys.stdout.write('no plugin files in NETCDF_PLUGIN_DIR, not installing..\n')
        data_files = []
    else:
        data_files = plugins
        sys.stdout.write('installing netcdf compression plugins from %s ...\n' % plugin_dir)
        sofiles = [os.path.basename(sofilepath) for sofilepath in data_files]
        sys.stdout.write(repr(sofiles)+'\n')
        if 'sdist' not in sys.argv[1:] and 'clean' not in sys.argv[1:] and '--version' not in sys.argv[1:]:
            for f in data_files:
                shutil.copy(f, osp.join(os.getcwd(),osp.join(osp.join('src','netCDF4'),'plugins')))
            copied_plugins=True
else:
    sys.stdout.write('NETCDF_PLUGIN_DIR not set, no netcdf compression plugins installed\n')
    data_files = []

setup(name="netCDF4",
      cmdclass=cmdclass,
      version=extract_version(netcdf4_src_pyx),
      long_description="netCDF version 4 has many features not found in earlier versions of the library, such as hierarchical groups, zlib compression, multiple unlimited dimensions, and new data types.  It is implemented on top of HDF5.  This module implements most of the new features, and can read and write netCDF files compatible with older versions of the library.  The API is modelled after Scientific.IO.NetCDF, and should be familiar to users of that module.\n\nThis project is hosted on a `GitHub repository <https://github.com/Unidata/netcdf4-python>`_ where you may access the most up-to-date source.",
      author="Jeff Whitaker",
      author_email="jeffrey.s.whitaker@noaa.gov",
      url="http://github.com/Unidata/netcdf4-python",
      download_url="http://python.org/pypi/netCDF4",
      platforms=["any"],
      license='License :: OSI Approved :: MIT License',
      description="Provides an object-oriented python interface to the netCDF version 4 library.",
      keywords=['numpy', 'netcdf', 'data', 'science', 'network', 'oceanography',
                'meteorology', 'climate'],
      classifiers=["Development Status :: 3 - Alpha",
                   "Programming Language :: Python :: 3",
                   "Programming Language :: Python :: 3.6",
                   "Programming Language :: Python :: 3.7",
                   "Programming Language :: Python :: 3.8",
                   "Intended Audience :: Science/Research",
                   "License :: OSI Approved :: MIT License",
                   "Topic :: Software Development :: Libraries :: Python Modules",
                   "Topic :: System :: Archiving :: Compression",
                   "Operating System :: OS Independent"],
      packages=find_namespace_packages(where="src"),
      package_dir={'':'src'},
      package_data={"netCDF4.plugins": ["lib__nc*"]},
      ext_modules=ext_modules,
      python_requires=">=3.6",
      **setuptools_extra_kwargs)

# remove plugin files copied from outside source tree
if copied_plugins:
    for f in sofiles:
        filepath = osp.join(osp.join(osp.join('src','netCDF4'),'plugins'),f)
        if os.path.exists(filepath):
            os.remove(filepath)