Stand With Ukraine
Showing posts with label python. Show all posts
Showing posts with label python. Show all posts

Friday, February 6, 2015

A python script: example for downloading GlobSnow data (snow extent in this case)

Recently a friend asked me if it is possible to selectively download folders from http servers in linux.. Probably it is possible by using wget or rsync, but I have never succeeded to make them work exactly the way I needed. So I wrote a small script for the task and passed it to him hoping that this might be his first step to learning Python. And below are 2 versions of the same script:
  1. The version I have actually given to my friend,
  2. The improved a bit more scary-looking version, which is a bit closer to the way I think it should be written.

First, below I show a quick and simple way of downloading files, with a minimal account for possible errors. The next step is to check if the sizes of existing files are the same as the sizes of the remote files and to replace the bad files, if required. Of course, it takes some time to download the data, especially if you need several years. If you work remotely I would suggest using tmux or screen, so your program would continue running even if the ssh session is closed for some reason. But if those are not installed, you still can get away by using nohup as follows:
nohup pyhton download.py >& log.txt &

Cheers and any comments are welcome

Tuesday, January 21, 2014

netcdf4-python copying variables from one netcdf file to another

This has arisen as a problem of converting NETCDF4 to NETCDF3_CLASSIC which is still used by some old software. I did try nc4tonc3 provided with netCDF4 but that one also wanted the input file in NETCDF4_CLASSIC format... That is why I wrote this little script to actually copy variables and dimensions. There were no attributes in the files, so I did not care to handle attributes.

From a comment by Nikolay Koldunov, there is a utility for this tasks nccopy ...

Wednesday, August 10, 2011

How I implemented 2d affine transform in python

Although there is nothing special about it, but, strangely, I could not find it in GDAL-python (though it is very good library for working with raster and shape files, and I like it), also I looked at another respected library - shapely, but unfortunately, there is no capability to move, rotate or translate the shapes (but still there are some very handy functions like union, intersection, distance, area, ..). Another thing is that this can be easily done in Java using JTS, like this:
first I construct a polygon at the origin of the coordinate system and then determine the transformation needed to obtain this unit rectangle from the initial polygon (It can be defined uniquely using height of the polygon and the coordinates of the centers of the parallel edges). Then using the inverse transform we obtain the geometry object corresponding to the specification above (height, coordinates of the centers of the parallel sides). I drew these polygons using their wkt representation in OpenJump.


And still I decided to use python, since there exists an easy way of displaying results in matplotlib. Some people use descartes library for this purpose, but I don't feel like installing another dependency, since the thing with python is that if you change the OS, you have to reinstall them all. For me it is: matplotlib, numpy, scipy, gdal, cdat, shapely, netcdf4-python.... Well, I am getting distracted from the main point. So briefly, I feed my geometries to matplotlib as follows:

So the transformation I did, can be initialized using 3 source and 3 corresponding destination points, that shoud not lie on the same line. Tested it, kind of works. Probably will optimize and improve it later, if necessary, but for now it looks like this:
Basically, I just solve a system of linear equations with respect to parameters of the transformation, and then use the parameters to transform other points.


Wednesday, May 19, 2010

Cleaning Timetable

So I moved to another apartment - multi 3, and we had to decide how to divide our cleaning responsibilities fair and square. For this purpose and for fun I have created a pdf file using python and reportlab(http://www.reportlab.com/) library. Here is the code:
So like this we will change the place and of course after the end of the week all the blanks should be checked)).And the screenshot of the result:

Wednesday, December 23, 2009

Permutations

It is time to write something about programming. Here I want to present a function implemented in python which returns the list of permutations of the given string.

#return the list of the permutations of s
def get_permutations(s):
result = []
if len(s) == 1:
result.append(s)
return result

for i in range(0,len(s)):
s_tmp = s.replace(s[i],s[0])[1:]
subset_permuts = get_permutations(s_tmp)
for perm in subset_permuts:
result.append( s[i] + perm )
return result

It takes 16.5364508629 seconds in order to find the permutations of the string '1234567890'.
For the code

print get_permutations('123');
print 'Execution time: ', time.time() - t0, ' s'

The output is following:
['123', '132', '213', '231', '321', '312']
Execution time: 5.88893890381e-05 s